usfm-grammar-web 3.0.0-beta.15 → 3.0.0-beta.17

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
@@ -23,10 +23,10 @@ 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-beta.15/tree-sitter-usfm.wasm",
27
- "https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.15/tree-sitter.wasm");
28
- await Validator.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.15/tree-sitter-usfm.wasm",
29
- "https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.15/tree-sitter.wasm");
26
+ await USFMParser.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.17/tree-sitter-usfm.wasm",
27
+ "https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.17/tree-sitter.wasm");
28
+ await Validator.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.17/tree-sitter-usfm.wasm",
29
+ "https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.17/tree-sitter.wasm");
30
30
 
31
31
  };
32
32
  initParser();
@@ -60,13 +60,13 @@ It can be used directly in the HTML script tag too. Please ensure its dependenci
60
60
 
61
61
  ```html
62
62
  <script type="module">
63
- import { USFMParser, Filter, Validator } from 'https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.15/dist/bundle.mjs';
63
+ import { USFMParser, Filter, Validator } from 'https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.17/dist/bundle.mjs';
64
64
  console.log('Hello world');
65
65
  (async () => {
66
- await USFMParser.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.15/tree-sitter-usfm.wasm",
67
- "https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.15/tree-sitter.wasm");
68
- await Validator.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.15/tree-sitter-usfm.wasm",
69
- "https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.15/tree-sitter.wasm");
66
+ await USFMParser.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.17/tree-sitter-usfm.wasm",
67
+ "https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.17/tree-sitter.wasm");
68
+ await Validator.init("https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.17/tree-sitter-usfm.wasm",
69
+ "https://cdn.jsdelivr.net/npm/usfm-grammar-web@3.0.0-beta.17/tree-sitter.wasm");
70
70
  const usfmParser = new USFMParser('\\id GEN\n\\c 1\n\\p\n\\v 1 In the begining..\\v 2 more text')
71
71
  const output = usfmParser.toUSJ()
72
72
  console.log({ output })
@@ -124,6 +124,34 @@ const usfmParser2 = new USFMParser(usfmString=null, fromUsj=null, fromUsx=usxEle
124
124
  const usfmGen = usfmParser2.usfm;
125
125
  console.log(usfmGen);
126
126
  ```
127
+ ### BibleNLP format
128
+ Bible NLP format consists of two `txt` files: the first, with verse texts, one per line and the second, with corresponding references. The API generates a JSON with two fields, `text` and `vref`, each containing an array of strings.
129
+
130
+ ```javascript
131
+
132
+ const output = usfmParser.toBibleNlpFormat()
133
+ //const output = my_parser.toBibleNlpFormat(true) //ignore_errors
134
+
135
+ const textLines = output.text.join('\n');
136
+ fs.writeFileSync('bibleNLP.txt', textLines, { encoding: 'utf-8' });
137
+
138
+ const refLines = output.vref.join('\n');
139
+ fs.writeFileSync('vref.txt', refLines, { encoding: 'utf-8' });
140
+ ```
141
+
142
+ ### Table/List format
143
+
144
+ ```javascript
145
+ const listOutput = usfmParser.toList();
146
+ /* const listOutput = usfmParser.toList(
147
+ Filter.NOTES, //exclude
148
+ ["id", "c", "v"] //include
149
+ true, //ignore errors
150
+ true //combine texts
151
+ )*/
152
+ const tableOutput = listOutput.map(row => row.join('\t')).join('\n');
153
+ console.log(tableOutput);
154
+ ```
127
155
 
128
156
  ### Autofix and Validation
129
157
  Experimental Validation and Autofix feature for USFM:
@@ -193,9 +221,9 @@ The filtering on USJ, the JSON output, is a feature incorporated to allow data e
193
221
  ```
194
222
  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
223
  ```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)
224
+ output = usfmParser.toUSJ(null, Filter.BCV) //to include
225
+ output = usfmParser.toUSJ(null, [...Filter.BCV, ...Filter.TEXT]) //to include
226
+ output = usfmParser.toUSJ([...Filter.PARAGRAPHS, ...Filter.CHARACTERS]) //to exclude
199
227
  ```
200
228
  - Inner contents of excluded markers
201
229
 
package/dist/bundle.mjs CHANGED
@@ -1660,7 +1660,8 @@ const $142605215cbb4f88$export$b32929182cc4fefe = [
1660
1660
  const $142605215cbb4f88$export$75b2f422ff60acaf = [
1661
1661
  "char",
1662
1662
  "note",
1663
- "figure"
1663
+ "figure",
1664
+ "ref"
1664
1665
  ];
1665
1666
  const $142605215cbb4f88$export$aa24f570494ce8d0 = [
1666
1667
  "type",
@@ -1707,6 +1708,16 @@ class $ce44dcc4919407eb$var$USFMGenerator {
1707
1708
  this.usfmString = "";
1708
1709
  }
1709
1710
  usjToUsfm(usjObj, nested = false) {
1711
+ if (usjObj.type === 'optbreak') {
1712
+ if (this.usfmString !== '' && ![
1713
+ '\n',
1714
+ '\r',
1715
+ ' ',
1716
+ '\t'
1717
+ ].includes(this.usfmString.slice(-1))) this.usfmString += ' ';
1718
+ this.usfmString += '// ';
1719
+ return;
1720
+ }
1710
1721
  if (usjObj.type === "ref") usjObj.marker = "ref";
1711
1722
  if (!(0, $142605215cbb4f88$export$b32929182cc4fefe).includes(usjObj.type)) {
1712
1723
  this.usfmString += "\\";
@@ -1731,11 +1742,15 @@ class $ce44dcc4919407eb$var$USFMGenerator {
1731
1742
  }
1732
1743
  if (Array.isArray(usjObj.content)) usjObj.content.forEach((item)=>{
1733
1744
  if (typeof item === "string") this.usfmString += item;
1734
- else this.usjToUsfm(item, usjObj.type === "char");
1745
+ else this.usjToUsfm(item, usjObj.type === "char" && item.marker !== "fv");
1735
1746
  });
1736
1747
  let attributes = [];
1737
1748
  Object.keys(usjObj).forEach((key)=>{
1738
- if (!(0, $142605215cbb4f88$export$aa24f570494ce8d0).includes(key)) attributes.push(`${key}="${usjObj[key]}"`);
1749
+ if (!(0, $142605215cbb4f88$export$aa24f570494ce8d0).includes(key)) {
1750
+ let lhs = key;
1751
+ if (key === "file") lhs = "src";
1752
+ attributes.push(`${lhs}="${usjObj[key]}"`);
1753
+ }
1739
1754
  });
1740
1755
  if (attributes.length > 0) this.usfmString += `|${attributes.join(" ")}`;
1741
1756
  if ((0, $142605215cbb4f88$export$75b2f422ff60acaf).includes(usjObj.type)) {
@@ -1743,6 +1758,14 @@ class $ce44dcc4919407eb$var$USFMGenerator {
1743
1758
  if (nested && usjObj.type === "char") this.usfmString += "+";
1744
1759
  this.usfmString += `${usjObj.marker}* `;
1745
1760
  }
1761
+ if (usjObj.type === "ms") {
1762
+ if ("sid" in usjObj) {
1763
+ if (attributes.length == 0) this.usfmString += '|';
1764
+ this.usfmString += `sid="${usjObj.sid}" `;
1765
+ }
1766
+ this.usfmString = this.usfmString.trim() + "\\*";
1767
+ }
1768
+ if (usjObj.type === "sidebar") this.usfmString += "\\esbe";
1746
1769
  if (!(0, $142605215cbb4f88$export$c63c0b6e358f0774).includes(usjObj.type) && this.usfmString[this.usfmString.length - 1] !== "\n") this.usfmString += "\n";
1747
1770
  return this.usfmString;
1748
1771
  }
@@ -2395,6 +2418,12 @@ class $0143e94dfd2d689d$var$ListGenerator {
2395
2418
  "Marker"
2396
2419
  ]
2397
2420
  ];
2421
+ this.bibleNlpFormat = {
2422
+ "text": [],
2423
+ "vref": []
2424
+ };
2425
+ this.prevChapter = "";
2426
+ this.prevVerse = "";
2398
2427
  }
2399
2428
  usjToListId(obj) {
2400
2429
  /* Update book code */ this.book = obj.code;
@@ -2425,6 +2454,24 @@ class $0143e94dfd2d689d$var$ListGenerator {
2425
2454
  else this.usjToList(item);
2426
2455
  }
2427
2456
  }
2457
+ usjToBibleNlpFormat(obj) {
2458
+ // Traverse the USJ object and build a dictionary for Bible NLP format
2459
+ if (obj.type === "book") this.usjToListId(obj);
2460
+ else if (obj.type === "chapter") this.usjToListC(obj);
2461
+ else if (obj.type === "verse") this.usjToListV(obj);
2462
+ else if (obj.content) {
2463
+ for (const item of obj.content)if (typeof item === "string") {
2464
+ if (this.currentChapter === this.prevChapter && this.currentVerse === this.prevVerse) this.bibleNlpFormat.text[this.bibleNlpFormat.text.length - 1] += " " + item.replace(/[\n\r]/g, " ").trim();
2465
+ else {
2466
+ const vref = `${this.book} ${this.currentChapter}:${this.currentVerse}`;
2467
+ this.bibleNlpFormat.text.push(item.replace(/[\n\r]/g, " ").trim());
2468
+ this.bibleNlpFormat.vref.push(vref);
2469
+ this.prevChapter = this.currentChapter;
2470
+ this.prevVerse = this.currentVerse;
2471
+ }
2472
+ } else this.usjToBibleNlpFormat(item);
2473
+ }
2474
+ }
2428
2475
  }
2429
2476
  var $0143e94dfd2d689d$export$2e2bcd8739ae039 = $0143e94dfd2d689d$var$ListGenerator;
2430
2477
 
@@ -3320,6 +3367,31 @@ Only one of USFM, USJ or USX is supported in one object.`);
3320
3367
  });
3321
3368
  }
3322
3369
  }
3370
+ toBibleNlpFormat(ignoreErrors = false) {
3371
+ /* Uses the toUSJ function with only BVC and text.
3372
+ Then the JSOn is converted to list of verse texts and vrefs*/ if (!ignoreErrors && this.errors.length > 0) {
3373
+ let errorString = this.errors.join("\n\t");
3374
+ throw new Error(`Errors present:\n\t${errorString}\nUse ignoreErrors=true to generate output despite errors`);
3375
+ }
3376
+ try {
3377
+ const usjDict = this.toUSJ(null, [
3378
+ ...(0, $ab5c81e737a0c6a6$export$ec91da630f36d5ea).BCV,
3379
+ ...(0, $ab5c81e737a0c6a6$export$ec91da630f36d5ea).TEXT
3380
+ ], ignoreErrors, true);
3381
+ const listGenerator = new (0, $0143e94dfd2d689d$export$2e2bcd8739ae039)();
3382
+ listGenerator.usjToBibleNlpFormat(usjDict);
3383
+ return listGenerator.bibleNlpFormat;
3384
+ } catch (exe) {
3385
+ let message = "Unable to do the conversion. ";
3386
+ if (this.errors.length > 0) {
3387
+ let errorString = this.errors.join("\n\t");
3388
+ message += `Could be due to an error in the USFM\n\t${errorString}`;
3389
+ }
3390
+ throw new Error(message, {
3391
+ cause: exe
3392
+ });
3393
+ }
3394
+ }
3323
3395
  toUSX(ignoreErrors = false) {
3324
3396
  /* Convert the syntax_tree to the XML format (USX) */ if (!ignoreErrors && this.errors.length > 0) {
3325
3397
  let errorString = this.errors.join("\n\t");
@@ -3572,6 +3644,13 @@ class $882b1daec90c4038$export$9eeb22c0bba4ed5e {
3572
3644
  const toReplace = modifiedUSFM.slice(start, end);
3573
3645
  modifiedUSFM = modifiedUSFM.replace(toReplace, `${toReplace}\\p\n`);
3574
3646
  changed = true;
3647
+ } else if (error.isError && !errorText.startsWith("\\") && error.previousSibling.type === "chapter" && !error.children.some((ch)=>ch.type === "paragraph")) {
3648
+ // console.log("Match 7.1");
3649
+ const start = error.previousSibling.startIndex;
3650
+ const end = error.startIndex;
3651
+ const toReplace = modifiedUSFM.slice(start, end);
3652
+ modifiedUSFM = modifiedUSFM.replace(toReplace, `${toReplace}\\p\n`);
3653
+ changed = true;
3575
3654
  } else if (errorText.startsWith("\\") && !$882b1daec90c4038$var$validMarkersPattern.test(errorText)) {
3576
3655
  // console.log("Match 8");
3577
3656
  modifiedUSFM = modifiedUSFM.replace(errorText, errorText.slice(1));