usfm-grammar-web 3.0.0-alpha.9 → 3.0.0-beta.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +132 -20
- package/dist/bundle.mjs +1154 -73
- package/dist/bundle.mjs.map +1 -1
- package/package.json +14 -5
- package/tree-sitter.wasm +0 -0
package/README.md
CHANGED
|
@@ -15,7 +15,7 @@ npm install usfm-grammar-web
|
|
|
15
15
|
|
|
16
16
|
```javascript
|
|
17
17
|
import React, { useState, useEffect } from 'react';
|
|
18
|
-
import { USFMParser } from 'usfm-grammar-web';
|
|
18
|
+
import { USFMParser, Filter, Validator } from 'usfm-grammar-web';
|
|
19
19
|
|
|
20
20
|
function App() {
|
|
21
21
|
const [result, setResult] = useState(null);
|
|
@@ -23,8 +23,11 @@ function App() {
|
|
|
23
23
|
|
|
24
24
|
useEffect(() => {
|
|
25
25
|
const initParser = async () => {
|
|
26
|
-
await USFMParser.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-
|
|
27
|
-
"https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-
|
|
26
|
+
await USFMParser.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.16/tree-sitter-usfm.wasm",
|
|
27
|
+
"https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.16/tree-sitter.wasm");
|
|
28
|
+
await Validator.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.16/tree-sitter-usfm.wasm",
|
|
29
|
+
"https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.16/tree-sitter.wasm");
|
|
30
|
+
|
|
28
31
|
};
|
|
29
32
|
initParser();
|
|
30
33
|
}, []);
|
|
@@ -53,15 +56,17 @@ function App() {
|
|
|
53
56
|
export default App;
|
|
54
57
|
```
|
|
55
58
|
|
|
56
|
-
It can be used directly in the HTML script tag too.
|
|
59
|
+
It can be used directly in the HTML script tag too. Please ensure its dependencies are also included(xmldom, ajv, xpath).
|
|
57
60
|
|
|
58
61
|
```html
|
|
59
62
|
<script type="module">
|
|
60
|
-
import { USFMParser } from 'https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-
|
|
63
|
+
import { USFMParser, Filter, Validator } from 'https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.16/dist/bundle.mjs';
|
|
61
64
|
console.log('Hello world');
|
|
62
65
|
(async () => {
|
|
63
|
-
await USFMParser.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-
|
|
64
|
-
"https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-
|
|
66
|
+
await USFMParser.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.16/tree-sitter-usfm.wasm",
|
|
67
|
+
"https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.16/tree-sitter.wasm");
|
|
68
|
+
await Validator.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.16/tree-sitter-usfm.wasm",
|
|
69
|
+
"https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.16/tree-sitter.wasm");
|
|
65
70
|
const usfmParser = new USFMParser('\\id GEN\n\\c 1\n\\p\n\\v 1 In the begining..\\v 2 more text')
|
|
66
71
|
const output = usfmParser.toUSJ()
|
|
67
72
|
console.log({ output })
|
|
@@ -73,30 +78,137 @@ It can be used directly in the HTML script tag too.
|
|
|
73
78
|
</script>
|
|
74
79
|
```
|
|
75
80
|
|
|
81
|
+
## API Documentation
|
|
76
82
|
|
|
77
|
-
If using from react, please refer the instructions for it [here](../docs/react-usage.md).
|
|
78
83
|
|
|
79
|
-
|
|
84
|
+
### Parsing USFM, checking errors
|
|
85
|
+
|
|
86
|
+
```javascript
|
|
87
|
+
const USFM = '\\id GEN\n\\c 1\n\\p\n\\v 1 In the begining..\\v 2 some more text'
|
|
88
|
+
const usfmParser = new USFMParser(USFM);
|
|
89
|
+
console.log(usfmParser.errors)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### USJ
|
|
93
|
+
Here's how you can use USFM Grammar in your JavaScript projects to work with the JSON format, USJ:
|
|
94
|
+
|
|
95
|
+
```javascript
|
|
96
|
+
const USFM = '\\id GEN\n\\c 1\n\\p\n\\v 1 In the begining..\\v 2 some more text'
|
|
97
|
+
const usfmParser = new USFMParser(USFM);
|
|
98
|
+
|
|
99
|
+
const USJ = usfmParser.toUSJ() // USFM to USJ
|
|
100
|
+
console.log(JSON.stringify(USJ, null, 2));
|
|
101
|
+
|
|
102
|
+
const usfmParser2 = new USFMParser(usfmString=null, fromUsj=USJ) // USJ to USFM
|
|
103
|
+
const usfmGen = usfmParser2.usfm;
|
|
104
|
+
console.log(usfmGen);
|
|
105
|
+
```
|
|
106
|
+
Working with USJ, also gives options like filtering selected markers, to edit the original USFM content. To understand more about how `exclude_markers`, `include_markers`, `combine_texts` and `Filter` works refer the section on [filtering on USJ](#filtering-on-usj)
|
|
107
|
+
|
|
108
|
+
### USX
|
|
109
|
+
|
|
110
|
+
To work with the XML format, USX:
|
|
111
|
+
```javascript
|
|
112
|
+
import { DOMImplementation, XMLSerializer } from 'xmldom';
|
|
113
|
+
|
|
114
|
+
const USFM = '\\id GEN\n\\c 1\n\\p\n\\v 1 In the begining..\\v 2 some more text'
|
|
115
|
+
const usfmParser = new USFMParser(USFM);
|
|
116
|
+
|
|
117
|
+
const usxElem = usfmParser.toUSX() // USFM to USX
|
|
118
|
+
const usxSerializer = new XMLSerializer();
|
|
119
|
+
const usx = usxSerializer.serializeToString(usxElem);
|
|
120
|
+
|
|
121
|
+
console.log(usx);
|
|
122
|
+
|
|
123
|
+
const usfmParser2 = new USFMParser(usfmString=null, fromUsj=null, fromUsx=usxElem) // USX to USFM
|
|
124
|
+
const usfmGen = usfmParser2.usfm;
|
|
125
|
+
console.log(usfmGen);
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Autofix and Validation
|
|
129
|
+
Experimental Validation and Autofix feature for USFM:
|
|
130
|
+
```javascript
|
|
131
|
+
import {Validator} from "usfm-grammar";
|
|
132
|
+
|
|
133
|
+
const wrongUSFM="\\id GEN\n\\c 1\n\\v 1 test verse"
|
|
134
|
+
const checker = new Validator();
|
|
135
|
+
const resp = checker.isValidUSFM(wrongUSFM); // true or false
|
|
136
|
+
console.log(checker.message) // List of errors if present
|
|
137
|
+
|
|
138
|
+
const editedUSFM = checker.autoFixUSFM(wrongUSFM);
|
|
139
|
+
console.log(checker.message); // Report on autofix attempt
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
Validation of USJ format:
|
|
144
|
+
```javascript
|
|
145
|
+
const {Validator} = require("usfm-grammar");
|
|
146
|
+
const simpleUSJ = {
|
|
147
|
+
type: 'USJ',
|
|
148
|
+
version: '0.3.0',
|
|
149
|
+
content: [
|
|
150
|
+
{ type: 'book', marker: 'id', code: 'GEN', content: [] },
|
|
151
|
+
{ type: 'chapter', marker: 'c', number: '1', sid: 'GEN 1' },
|
|
152
|
+
{ type: 'para', marker: 'p', content: [
|
|
153
|
+
{type: 'verse', marker: 'v', number: 1 },
|
|
154
|
+
"In the begining..",
|
|
155
|
+
{type: 'verse', marker: 'v', number: 2 }
|
|
156
|
+
] }
|
|
157
|
+
]
|
|
158
|
+
}
|
|
159
|
+
const checker = new Validator();
|
|
160
|
+
console.log(checker.isValidUSJ(simpleUSJ));
|
|
161
|
+
console.log(checker.message);
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Filtering on USJ
|
|
165
|
+
The filtering on USJ, the JSON output, is a feature incorporated to allow data extraction, markup cleaning etc. The arguments `exclude_markers` and `include_markers` in the methods `USFMParser.toUSJ()` makes this possible. Also the `USFMParser.toList()`, can accept these inputs and perform similar operations. There is CLI versions also for these arguments to replicate the filtering feature there.
|
|
166
|
+
|
|
167
|
+
- *excludeMarkers*
|
|
168
|
+
|
|
169
|
+
The first input parameter to `toUSJ()` and `toList` of `USFMParser` class. Defaults to `null`. When proivded, all markers except those listed will be included in the output.
|
|
170
|
+
|
|
171
|
+
- *includeMarkers*
|
|
172
|
+
|
|
173
|
+
The second input parameter to `toUSJ()` and `toList` of `USFMParser` class. Defaults to `null`. When proivded, only those markers listed will be included in the output. `includeMarkers` is applied before applying `excludeMarkers`.
|
|
174
|
+
|
|
80
175
|
|
|
81
|
-
|
|
82
|
-
Initializes the USFMParser. This function must be called before creating instances of `USFMParser`. And can take the grammar and the tree-sitter files (in wasm format) as arguments, that is included in the package.
|
|
176
|
+
- *combineTexts*
|
|
83
177
|
|
|
84
|
-
|
|
85
|
-
Converts a USFM string to a USJ object.
|
|
178
|
+
Fourth input parameter to `toUSJ()` and `toList` of `USFMParser` class. Defaults to `true`. After filtering out makers like paragraphs and characters, we are left with texts from within them, if 'text-in-excluded-parent' is also not excluded. These text snippets may come as separate components in the contents list. When this option is `True`, the consequetive text snippets will be concatinated together. The text concatination is done in a puctuation and space aware manner. If users need more control over the space handling or for any other reason, would prefer the texts snippets as different components in the output, this can be set to `False`.
|
|
86
179
|
|
|
87
|
-
-
|
|
180
|
+
- *usfm_grammar.Filter*
|
|
88
181
|
|
|
89
|
-
|
|
182
|
+
This Class provides a set of enums that would be useful in providing in the `excludeMarkers` and `includeMarkers` inputs rather than users listing out individual markers. The class has following options
|
|
183
|
+
```
|
|
184
|
+
BOOK_HEADERS : identification and introduction markers
|
|
185
|
+
TITLES : section headings and associated markers
|
|
186
|
+
COMMENTS : comment markers like \rem
|
|
187
|
+
PARAGRAPHS : paragraph markers like \p, poetry markers, list table markers
|
|
188
|
+
CHARACTERS : all character level markups like \em, \w, \wj etc and their nested versions with +
|
|
189
|
+
NOTES : foot note, cross-reference and their content markers
|
|
190
|
+
STUDY_BIBLE : \esb and \cat
|
|
191
|
+
BCV : \id, \c and \v
|
|
192
|
+
TEXT : 'text-in-excluded-parent'
|
|
193
|
+
```
|
|
194
|
+
To inspect which are the markers in each of these options, it could be just printed out, `print(Filter.TITLES)`. These could be used individually or concatinated to get the desired filtering of markers and data:
|
|
195
|
+
```javascript
|
|
196
|
+
output = usfmParser.toUSJ(null, include_markers=Filter.BCV)
|
|
197
|
+
output = usfmParser.toUSJ(null, include_markers=Filter.BCV+Filter.TEXT)
|
|
198
|
+
output = usfmParser.toUSJ(exclude_markers=Filter.PARAGRAPHS+Filter.CHARACTERS)
|
|
199
|
+
```
|
|
200
|
+
- Inner contents of excluded markers
|
|
90
201
|
|
|
91
|
-
|
|
92
|
-
|
|
202
|
+
For markers like `\p` `\q` etc, by excluding them, we only remove them from the heirachy and retain the inner contents like `\v`, text etc that would be coming inside it. But for certain other markers like `\f`, `\x`, `\esb` etc, if they are excluded their inner contents are also excluded. Following is the set of all markers, who inner contents are discarded if they are mentioned in `excludeMarkers` or not included in `includeMarkers`.
|
|
203
|
+
```
|
|
204
|
+
BOOK_HEADERS, TITLES, COMMENTS, NOTES, STUDY_BIBLE
|
|
205
|
+
```
|
|
206
|
+
:warning: Generally, it is recommended to NOT use both `exclude_markers` and `includeMarkers` together as it could lead to unexpected behavours and data loss. For instance if `include_makers` has `\fk` and `excludeMarkers` has `\f`, the output will not contain `\fk` as all inner contents of `\f` will be discarded.
|
|
93
207
|
|
|
94
|
-
- `usjObject`: The input USJ object.
|
|
95
208
|
|
|
96
|
-
Returns: The parser object. To obtain the USFM generated from USJ use `parserObject.usfm`.
|
|
97
209
|
|
|
98
210
|
## Contributing
|
|
99
|
-
Contributions are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or create a pull request on [GitHub](https://github.com/
|
|
211
|
+
Contributions are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or create a pull request on [GitHub](https://github.com/Bridgeconn/usfm-grammar).
|
|
100
212
|
|
|
101
213
|
## License
|
|
102
214
|
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|