usfm-grammar-web 3.0.0-alpha.1 → 3.0.0-alpha.10

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 CHANGED
@@ -1,58 +1,211 @@
1
1
  # USFM Grammar
2
2
 
3
3
  ## Description
4
- USFM Grammar is a JavaScript library for parsing and converting USFM (Unified Standard Format Markers) to/from USJ (Unified Standard JSON) format. This library provides functionalities to parse USFM strings into a syntax tree and convert them into a JSON-like structure (USJ), and vice versa.
4
+ This is the web alternative to the (USFM-Grammar 3.x)[https://www.npmjs.com/package/usfm-grammar] library to be used from HTML, react etc. USFM Grammar is a JavaScript library for parsing and converting USFM (Unified Standard Format Markers) to/from USJ (Unified Standard JSON) format. This library provides functionalities to parse USFM strings into a syntax tree and convert them into a JSON-like structure (USJ), and vice versa.
5
5
 
6
6
  ## Installation
7
7
  You can install USFM Grammar via npm:
8
8
 
9
- ```bash
10
- npm install usfm-grammar
11
- ```
12
9
 
13
10
  ## Usage
14
- Here's how you can use USFM Grammar in your JavaScript/TypeScript projects:
11
+ Here's how you can use USFM Grammar in your react projects:
12
+ ```bash
13
+ npm install usfm-grammar-web
14
+ ```
15
15
 
16
16
  ```javascript
17
- const { USFMParser} = require('usfm-grammar');
17
+ import React, { useState, useEffect } from 'react';
18
+ import { USFMParser, Filter, Validator } from 'usfm-grammar-web';
19
+
20
+ function App() {
21
+ const [result, setResult] = useState(null);
22
+ const [result2, setResult2] = useState(null);
23
+
24
+ useEffect(() => {
25
+ const initParser = async () => {
26
+ await USFMParser.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-alpha.1/tree-sitter-usfm.wasm",
27
+ "https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-alpha.1/tree-sitter.wasm");
28
+ await Validator.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-alpha.10/tree-sitter-usfm.wasm",
29
+ "https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-alpha.10/tree-sitter.wasm");
30
+
31
+ };
32
+ initParser();
33
+ }, []);
34
+
35
+ const calculateValue = async () => {
36
+ const usfmParser = new USFMParser('\\id GEN\n\\c 1\n\\p\n\\v 1 In the begining..\\v 2 more text');
37
+ const output = usfmParser.toUSJ();
38
+ setResult(JSON.stringify(output));
39
+
40
+ const usfmParser2 = new USFMParser(null, output) //initialse from USJ
41
+ const usfm = usfmParser2.usfm;
42
+ setResult2(usfm);
43
+ };
44
+
45
+ return (
46
+ <div className="App">
47
+ <div>
48
+ <button onClick={calculateValue}>Test</button>
49
+ <p>USJ: {result}</p>
50
+ <p>USFM: {result2}</p>
51
+ </div>
52
+ </div>
53
+ );
54
+ }
55
+
56
+ export default App;
57
+ ```
18
58
 
19
- (async () => {
20
- await USFMParser.init();
21
- const usfmParser = new USFMParser()
22
- const output = usfmParser.usfmToUsj('\\id GEN\n\\c 1\n\\p\n\\v 1 In the begining..\\v 2 more text')
59
+ It can be used directly in the HTML script tag too. Please ensure its dependencies are also included(xmldom, ajv, xpath).
60
+
61
+ ```html
62
+ <script type="module">
63
+ import { USFMParser, Filter, Validator } from 'https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-alpha.2/dist/bundle.mjs';
64
+ console.log('Hello world');
65
+ (async () => {
66
+ await USFMParser.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-alpha.10/tree-sitter-usfm.wasm",
67
+ "https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-alpha.10/tree-sitter.wasm");
68
+ await Validator.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-alpha.10/tree-sitter-usfm.wasm",
69
+ "https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-alpha.10/tree-sitter.wasm");
70
+ const usfmParser = new USFMParser('\\id GEN\n\\c 1\n\\p\n\\v 1 In the begining..\\v 2 more text')
71
+ const output = usfmParser.toUSJ()
23
72
  console.log({ output })
24
- const usfm = usfmParser.usjToUsfm(output)
73
+
74
+ const usfmParser2 = new USFMParser(null, output);
75
+ const usfm = usfmParser2.usfm;
25
76
  console.log({ usfm })
26
- })();
77
+ })();
78
+ </script>
27
79
  ```
28
80
 
29
- If the above import cause issue in an esm project use the below format:
81
+ ## API Documentation
82
+
83
+
84
+ ### Parsing USFM, checking errors
85
+
30
86
  ```javascript
31
- import pkg from 'usfm-grammar';
32
- const USFMParser = pkg.USFMParser;
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));
33
101
 
102
+ const usfmParser2 = new USFMParser(usfmString=null, fromUsj=USJ) // USJ to USFM
103
+ const usfmGen = usfmParser2.usfm;
104
+ console.log(usfmGen);
34
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)
35
107
 
36
- If using from react, please refer the instructions for it [here](../docs/react-usage.md).
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`.
37
174
 
38
- ## API Documentation
39
175
 
40
- ### `USFMParser.init()`
41
- 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*
42
177
 
43
- ### `USFMParser.usfmToUsj(usfmString: string): Object`
44
- 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`.
45
179
 
46
- - `usfmString`: The input USFM string.
180
+ - *usfm_grammar.Filter*
47
181
 
48
- Returns: A JSON-like object representing the USJ.
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
49
201
 
50
- ### `USFMParser.usjToUsfm(usjObject: Object): string`
51
- Converts a USJ object to a USFM string.
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.
52
207
 
53
- - `usjObject`: The input USJ object.
54
208
 
55
- Returns: The converted USFM string.
56
209
 
57
210
  ## Contributing
58
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/your-username/usfm-grammar).