usfm-grammar-web 3.0.0-alpha.1 → 3.0.0-alpha.9
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 +63 -22
- package/dist/{esm/index.mjs → bundle.mjs} +53 -28
- package/dist/bundle.mjs.map +1 -0
- package/package.json +2 -3
- package/tree-sitter-usfm.wasm +0 -0
- package/dist/cjs/index.cjs +0 -2518
- package/dist/cjs/index.cjs.map +0 -1
- package/dist/esm/index.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -1,37 +1,78 @@
|
|
|
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
|
|
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
|
-
|
|
17
|
+
import React, { useState, useEffect } from 'react';
|
|
18
|
+
import { USFMParser } 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
|
+
};
|
|
29
|
+
initParser();
|
|
30
|
+
}, []);
|
|
31
|
+
|
|
32
|
+
const calculateValue = async () => {
|
|
33
|
+
const usfmParser = new USFMParser('\\id GEN\n\\c 1\n\\p\n\\v 1 In the begining..\\v 2 more text');
|
|
34
|
+
const output = usfmParser.toUSJ();
|
|
35
|
+
setResult(JSON.stringify(output));
|
|
36
|
+
|
|
37
|
+
const usfmParser2 = new USFMParser(null, output) //initialse from USJ
|
|
38
|
+
const usfm = usfmParser2.usfm;
|
|
39
|
+
setResult2(usfm);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
return (
|
|
43
|
+
<div className="App">
|
|
44
|
+
<div>
|
|
45
|
+
<button onClick={calculateValue}>Test</button>
|
|
46
|
+
<p>USJ: {result}</p>
|
|
47
|
+
<p>USFM: {result2}</p>
|
|
48
|
+
</div>
|
|
49
|
+
</div>
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export default App;
|
|
54
|
+
```
|
|
18
55
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
56
|
+
It can be used directly in the HTML script tag too.
|
|
57
|
+
|
|
58
|
+
```html
|
|
59
|
+
<script type="module">
|
|
60
|
+
import { USFMParser } from 'https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-alpha.2/dist/bundle.mjs';
|
|
61
|
+
console.log('Hello world');
|
|
62
|
+
(async () => {
|
|
63
|
+
await USFMParser.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-alpha.2/tree-sitter-usfm.wasm",
|
|
64
|
+
"https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-alpha.2/tree-sitter.wasm");
|
|
65
|
+
const usfmParser = new USFMParser('\\id GEN\n\\c 1\n\\p\n\\v 1 In the begining..\\v 2 more text')
|
|
66
|
+
const output = usfmParser.toUSJ()
|
|
23
67
|
console.log({ output })
|
|
24
|
-
|
|
68
|
+
|
|
69
|
+
const usfmParser2 = new USFMParser(null, output);
|
|
70
|
+
const usfm = usfmParser2.usfm;
|
|
25
71
|
console.log({ usfm })
|
|
26
|
-
})();
|
|
72
|
+
})();
|
|
73
|
+
</script>
|
|
27
74
|
```
|
|
28
75
|
|
|
29
|
-
If the above import cause issue in an esm project use the below format:
|
|
30
|
-
```javascript
|
|
31
|
-
import pkg from 'usfm-grammar';
|
|
32
|
-
const USFMParser = pkg.USFMParser;
|
|
33
|
-
|
|
34
|
-
```
|
|
35
76
|
|
|
36
77
|
If using from react, please refer the instructions for it [here](../docs/react-usage.md).
|
|
37
78
|
|
|
@@ -40,19 +81,19 @@ If using from react, please refer the instructions for it [here](../docs/react-u
|
|
|
40
81
|
### `USFMParser.init()`
|
|
41
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.
|
|
42
83
|
|
|
43
|
-
### `USFMParser.
|
|
84
|
+
### `USFMParser.toUSJ(usfmString: string): Object`
|
|
44
85
|
Converts a USFM string to a USJ object.
|
|
45
86
|
|
|
46
87
|
- `usfmString`: The input USFM string.
|
|
47
88
|
|
|
48
89
|
Returns: A JSON-like object representing the USJ.
|
|
49
90
|
|
|
50
|
-
### `USFMParser
|
|
51
|
-
|
|
91
|
+
### `new USFMParser(null, usjObject: Object)`
|
|
92
|
+
Initialize a parser object from USJ and also converts it to USFM.
|
|
52
93
|
|
|
53
94
|
- `usjObject`: The input USJ object.
|
|
54
95
|
|
|
55
|
-
Returns: The
|
|
96
|
+
Returns: The parser object. To obtain the USFM generated from USJ use `parserObject.usfm`.
|
|
56
97
|
|
|
57
98
|
## Contributing
|
|
58
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/your-username/usfm-grammar).
|
|
@@ -2422,23 +2422,41 @@ class $236d01ba6f80be5e$var$USFMParser {
|
|
|
2422
2422
|
});
|
|
2423
2423
|
$236d01ba6f80be5e$var$USFMParser.language = await (0, (/*@__PURE__*/$parcel$interopDefault($dc2P8))).Language.load(grammarPath);
|
|
2424
2424
|
}
|
|
2425
|
-
constructor(){
|
|
2425
|
+
constructor(usfmString = null, fromUsj = null, fromUsx = null){
|
|
2426
|
+
let inputsGiven = 0;
|
|
2427
|
+
if (usfmString !== null) inputsGiven += 1;
|
|
2428
|
+
if (fromUsj !== null) inputsGiven += 1;
|
|
2429
|
+
if (fromUsx !== null) inputsGiven += 1;
|
|
2430
|
+
if (inputsGiven > 1) throw new Error(`Found more than one input!
|
|
2431
|
+
Only one of USFM, USJ or USX is supported in one object.`);
|
|
2432
|
+
if (inputsGiven === 0) throw Error("Missing input! Either USFM, USJ or USX is to be provided.");
|
|
2433
|
+
if (usfmString !== null) {
|
|
2434
|
+
if (typeof usfmString !== "string" || usfmString === null) throw new Error("Invalid input for USFM. Expected a string.");
|
|
2435
|
+
this.usfm = usfmString;
|
|
2436
|
+
} else if (fromUsj !== null) {
|
|
2437
|
+
this.usj = fromUsj;
|
|
2438
|
+
this.usfm = this.convertUSJToUSFM();
|
|
2439
|
+
} else if (fromUsx !== null) this.usx = fromUsx;
|
|
2426
2440
|
this.parser = null;
|
|
2427
|
-
this.
|
|
2428
|
-
this.usj = null;
|
|
2441
|
+
this.initializeParser();
|
|
2429
2442
|
this.syntaxTree = null;
|
|
2443
|
+
this.errors = [];
|
|
2444
|
+
this.warnings = [];
|
|
2445
|
+
this.parseUSFM();
|
|
2430
2446
|
}
|
|
2431
2447
|
initializeParser() {
|
|
2432
2448
|
if (!$236d01ba6f80be5e$var$USFMParser.language) throw new Error("USFMParser not initialized. Call USFMParser.init() before creating instances.");
|
|
2433
2449
|
this.parser = new (0, (/*@__PURE__*/$parcel$interopDefault($dc2P8)))();
|
|
2434
2450
|
this.parser.setLanguage($236d01ba6f80be5e$var$USFMParser.language);
|
|
2451
|
+
this.parserOptions = (0, (/*@__PURE__*/$parcel$interopDefault($dc2P8))).Options = {
|
|
2452
|
+
bufferSize: 1048576
|
|
2453
|
+
};
|
|
2435
2454
|
}
|
|
2436
|
-
|
|
2437
|
-
|
|
2438
|
-
|
|
2439
|
-
|
|
2440
|
-
this.
|
|
2441
|
-
this.usj = this.convertUSFMToUSJ();
|
|
2455
|
+
toSyntaxTree() {
|
|
2456
|
+
return this.syntaxTree.toString();
|
|
2457
|
+
}
|
|
2458
|
+
toUSJ(excludeMarkers = null, includeMarkers = null, ignoreErrors = false, combineTexts = true) {
|
|
2459
|
+
this.usj = this.convertUSFMToUSJ(excludeMarkers, includeMarkers, ignoreErrors, combineTexts);
|
|
2442
2460
|
return this.usj;
|
|
2443
2461
|
}
|
|
2444
2462
|
usjToUsfm(usjObject) {
|
|
@@ -2451,12 +2469,17 @@ class $236d01ba6f80be5e$var$USFMParser {
|
|
|
2451
2469
|
parseUSFM() {
|
|
2452
2470
|
let tree = null;
|
|
2453
2471
|
try {
|
|
2454
|
-
tree = this.parser.parse(this.usfm);
|
|
2472
|
+
if (this.usfm.length > 25000) tree = this.parser.parse(this.usfm, null, this.parserOptions);
|
|
2473
|
+
else tree = this.parser.parse(this.usfm);
|
|
2455
2474
|
} catch (err) {
|
|
2456
|
-
|
|
2475
|
+
throw err;
|
|
2476
|
+
// console.log("Error in parser.parse()");
|
|
2477
|
+
// console.log(err.toString());
|
|
2478
|
+
// console.log(this.usfm);
|
|
2457
2479
|
}
|
|
2458
|
-
|
|
2459
|
-
|
|
2480
|
+
this.checkForErrors(tree);
|
|
2481
|
+
this.checkforMissing(tree.rootNode);
|
|
2482
|
+
// if (error) throw error;
|
|
2460
2483
|
this.syntaxTree = tree.rootNode;
|
|
2461
2484
|
}
|
|
2462
2485
|
checkForErrors(tree) {
|
|
@@ -2468,32 +2491,35 @@ class $236d01ba6f80be5e$var$USFMParser {
|
|
|
2468
2491
|
}
|
|
2469
2492
|
return null;
|
|
2470
2493
|
}
|
|
2494
|
+
checkforMissing(node) {
|
|
2495
|
+
for (let n of node.children){
|
|
2496
|
+
if (n.isMissing) this.errors.push(`At ${n.startPosition.row + 1}:${n.startPosition.column}, Error: Missing ${n.type}`);
|
|
2497
|
+
this.checkforMissing(n);
|
|
2498
|
+
}
|
|
2499
|
+
}
|
|
2471
2500
|
convertUSJToUSFM() {
|
|
2472
2501
|
const outputUSFM = new (0, $ce44dcc4919407eb$export$2e2bcd8739ae039)().usjToUsfm(this.usj); // Simulated conversion
|
|
2473
2502
|
return outputUSFM;
|
|
2474
2503
|
}
|
|
2475
|
-
convertUSFMToUSJ(
|
|
2476
|
-
if (!ignoreErrors && this.errors) {
|
|
2477
|
-
let errorString = this.errors.
|
|
2504
|
+
convertUSFMToUSJ(excludeMarkers = null, includeMarkers = null, ignoreErrors = false, combineTexts = true) {
|
|
2505
|
+
if (!ignoreErrors && this.errors.length > 0) {
|
|
2506
|
+
let errorString = this.errors.join("\n ");
|
|
2478
2507
|
throw new Error(`Errors present:\n\t${errorString}\nUse ignoreErrors = true to generate output despite errors.`);
|
|
2479
2508
|
}
|
|
2480
|
-
let jsonRootObj = {
|
|
2481
|
-
type: "USJ",
|
|
2482
|
-
version: "0.2.0",
|
|
2483
|
-
content: []
|
|
2484
|
-
};
|
|
2485
2509
|
let outputUSJ;
|
|
2486
2510
|
try {
|
|
2487
|
-
let usjGenerator = new (0, $1bdca1468aa54399$export$2e2bcd8739ae039)($236d01ba6f80be5e$var$USFMParser.language, this.usfm
|
|
2488
|
-
usjGenerator.nodeToUSJ(this.syntaxTree, jsonRootObj);
|
|
2511
|
+
let usjGenerator = new (0, $1bdca1468aa54399$export$2e2bcd8739ae039)($236d01ba6f80be5e$var$USFMParser.language, this.usfm);
|
|
2512
|
+
usjGenerator.nodeToUSJ(this.syntaxTree, usjGenerator.jsonRootObj);
|
|
2489
2513
|
outputUSJ = usjGenerator.jsonRootObj;
|
|
2490
2514
|
} catch (err) {
|
|
2491
2515
|
let message = "Unable to do the conversion. ";
|
|
2492
2516
|
if (this.errors) {
|
|
2493
|
-
let errorString = this.errors.
|
|
2517
|
+
let errorString = this.errors.join("\n ");
|
|
2494
2518
|
message += `Could be due to an error in the USFM\n\t${errorString}`;
|
|
2495
|
-
}
|
|
2496
|
-
return
|
|
2519
|
+
} else message = err.message;
|
|
2520
|
+
return {
|
|
2521
|
+
error: message
|
|
2522
|
+
};
|
|
2497
2523
|
}
|
|
2498
2524
|
if (includeMarkers) outputUSJ = (0, $ab5c81e737a0c6a6$export$88c58c54200c5b2)(outputUSJ, [
|
|
2499
2525
|
...includeMarkers,
|
|
@@ -2506,8 +2532,7 @@ class $236d01ba6f80be5e$var$USFMParser {
|
|
|
2506
2532
|
var $236d01ba6f80be5e$export$2e2bcd8739ae039 = $236d01ba6f80be5e$var$USFMParser;
|
|
2507
2533
|
|
|
2508
2534
|
|
|
2509
|
-
console.log("COmes in index.js");
|
|
2510
2535
|
|
|
2511
2536
|
|
|
2512
2537
|
export {$236d01ba6f80be5e$export$2e2bcd8739ae039 as USFMParser};
|
|
2513
|
-
//# sourceMappingURL=
|
|
2538
|
+
//# sourceMappingURL=bundle.mjs.map
|