@helloao/tools 0.0.6 → 0.0.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.
Files changed (109) hide show
  1. package/dist/cjs/generation/api.cjs +366 -0
  2. package/dist/cjs/generation/api.cjs.map +7 -0
  3. package/dist/cjs/generation/audio.cjs +83 -0
  4. package/dist/cjs/generation/audio.cjs.map +7 -0
  5. package/dist/cjs/generation/book-order.cjs +508 -0
  6. package/dist/cjs/generation/book-order.cjs.map +7 -0
  7. package/dist/cjs/generation/common-types.cjs +17 -0
  8. package/dist/cjs/generation/common-types.cjs.map +7 -0
  9. package/dist/cjs/generation/dataset.cjs +238 -0
  10. package/dist/cjs/generation/dataset.cjs.map +7 -0
  11. package/dist/cjs/generation/index.cjs +51 -0
  12. package/dist/cjs/generation/index.cjs.map +7 -0
  13. package/dist/cjs/index.cjs +45 -0
  14. package/dist/cjs/index.cjs.map +7 -0
  15. package/dist/cjs/parser/codex-parser.cjs +187 -0
  16. package/dist/cjs/parser/codex-parser.cjs.map +7 -0
  17. package/dist/cjs/parser/commentary-csv-parser.cjs +133 -0
  18. package/dist/cjs/parser/commentary-csv-parser.cjs.map +7 -0
  19. package/dist/cjs/parser/index.cjs +50 -0
  20. package/dist/cjs/parser/index.cjs.map +7 -0
  21. package/dist/cjs/parser/iterators.cjs +204 -0
  22. package/dist/cjs/parser/iterators.cjs.map +7 -0
  23. package/dist/cjs/parser/types.cjs +17 -0
  24. package/dist/cjs/parser/types.cjs.map +7 -0
  25. package/dist/cjs/parser/usfm-parser.cjs +791 -0
  26. package/dist/cjs/parser/usfm-parser.cjs.map +7 -0
  27. package/dist/cjs/parser/usx-parser.cjs +483 -0
  28. package/dist/cjs/parser/usx-parser.cjs.map +7 -0
  29. package/dist/cjs/utils.cjs +237 -0
  30. package/dist/cjs/utils.cjs.map +7 -0
  31. package/dist/esm/generation/api.js +320 -0
  32. package/dist/esm/generation/api.js.map +7 -0
  33. package/dist/esm/generation/audio.js +53 -0
  34. package/dist/esm/generation/audio.js.map +7 -0
  35. package/dist/esm/generation/book-order.js +478 -0
  36. package/dist/esm/generation/book-order.js.map +7 -0
  37. package/dist/esm/generation/common-types.js +2 -0
  38. package/dist/esm/generation/common-types.js.map +7 -0
  39. package/dist/esm/generation/dataset.js +212 -0
  40. package/dist/esm/generation/dataset.js.map +7 -0
  41. package/dist/esm/generation/index.js +8 -0
  42. package/dist/esm/generation/index.js.map +7 -0
  43. package/dist/esm/index.js +6 -0
  44. package/dist/esm/index.js.map +7 -0
  45. package/dist/esm/parser/codex-parser.js +151 -0
  46. package/dist/esm/parser/codex-parser.js.map +7 -0
  47. package/dist/esm/parser/commentary-csv-parser.js +107 -0
  48. package/dist/esm/parser/commentary-csv-parser.js.map +7 -0
  49. package/dist/esm/parser/index.js +9 -0
  50. package/dist/esm/parser/index.js.map +7 -0
  51. package/dist/esm/parser/iterators.js +152 -0
  52. package/dist/esm/parser/iterators.js.map +7 -0
  53. package/dist/esm/parser/types.js +2 -0
  54. package/dist/esm/parser/types.js.map +7 -0
  55. package/dist/esm/parser/usfm-parser.js +749 -0
  56. package/dist/esm/parser/usfm-parser.js.map +7 -0
  57. package/dist/esm/parser/usx-parser.js +459 -0
  58. package/dist/esm/parser/usx-parser.js.map +7 -0
  59. package/dist/esm/utils.js +201 -0
  60. package/dist/esm/utils.js.map +7 -0
  61. package/{generation → dist/types/generation}/api.d.ts +148 -2
  62. package/{generation → dist/types/generation}/audio.d.ts +1 -1
  63. package/{generation → dist/types/generation}/common-types.d.ts +121 -24
  64. package/{generation → dist/types/generation}/dataset.d.ts +23 -1
  65. package/dist/types/generation/index.d.ts +7 -0
  66. package/dist/types/index.d.ts +5 -0
  67. package/{parser → dist/types/parser}/codex-parser.d.ts +51 -8
  68. package/dist/types/parser/commentary-csv-parser.d.ts +12 -0
  69. package/dist/types/parser/index.d.ts +8 -0
  70. package/{parser → dist/types/parser}/types.d.ts +28 -1
  71. package/{parser → dist/types/parser}/usfm-parser.d.ts +1 -1
  72. package/{parser → dist/types/parser}/usx-parser.d.ts +3 -3
  73. package/{utils.d.ts → dist/types/utils.d.ts} +6 -0
  74. package/package.json +27 -4
  75. package/generation/api.js +0 -187
  76. package/generation/api.spec.d.ts +0 -2
  77. package/generation/api.spec.js +0 -620
  78. package/generation/audio.js +0 -60
  79. package/generation/audio.spec.d.ts +0 -2
  80. package/generation/audio.spec.js +0 -36
  81. package/generation/book-order.js +0 -494
  82. package/generation/book-order.spec.d.ts +0 -2
  83. package/generation/book-order.spec.js +0 -8
  84. package/generation/common-types.js +0 -2
  85. package/generation/dataset.js +0 -106
  86. package/generation/index.d.ts +0 -7
  87. package/generation/index.js +0 -38
  88. package/index.d.ts +0 -5
  89. package/index.js +0 -32
  90. package/parser/codex-parser.js +0 -160
  91. package/parser/codex-parser.spec.d.ts +0 -2
  92. package/parser/codex-parser.spec.js +0 -645
  93. package/parser/index.d.ts +0 -7
  94. package/parser/index.js +0 -35
  95. package/parser/iterators.js +0 -222
  96. package/parser/iterators.spec.d.ts +0 -2
  97. package/parser/iterators.spec.js +0 -174
  98. package/parser/types.js +0 -2
  99. package/parser/usfm-parser.js +0 -840
  100. package/parser/usfm-parser.spec.d.ts +0 -2
  101. package/parser/usfm-parser.spec.js +0 -1593
  102. package/parser/usx-parser.js +0 -486
  103. package/parser/usx-parser.spec.d.ts +0 -2
  104. package/parser/usx-parser.spec.js +0 -1260
  105. package/utils.js +0 -73
  106. package/utils.spec.d.ts +0 -2
  107. package/utils.spec.js +0 -42
  108. /package/{generation → dist/types/generation}/book-order.d.ts +0 -0
  109. /package/{parser → dist/types/parser}/iterators.d.ts +0 -0
@@ -0,0 +1,791 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var usfm_parser_exports = {};
20
+ __export(usfm_parser_exports, {
21
+ UsfmParser: () => UsfmParser,
22
+ UsfmTokenizer: () => UsfmTokenizer,
23
+ isDigit: () => isDigit,
24
+ isWhitespace: () => isWhitespace,
25
+ loc: () => loc,
26
+ marker: () => marker,
27
+ t: () => t,
28
+ whitespace: () => whitespace,
29
+ word: () => word
30
+ });
31
+ module.exports = __toCommonJS(usfm_parser_exports);
32
+ var import_lodash = require("lodash");
33
+ class UsfmTokenizer {
34
+ _input = "";
35
+ _index = 0;
36
+ _start = 0;
37
+ get _tokenLength() {
38
+ return this._index - this._start;
39
+ }
40
+ /**
41
+ * Converts the given input into a list of tokens.
42
+ * @param input The input that should be tokenized.
43
+ */
44
+ tokenize(input) {
45
+ this._input = input;
46
+ this._index = 0;
47
+ return this._parseTokens();
48
+ }
49
+ _parseTokens() {
50
+ let tokens = [];
51
+ let token = this._parseToken();
52
+ while (token) {
53
+ tokens.push(token);
54
+ token = this._parseToken();
55
+ }
56
+ return tokens;
57
+ }
58
+ _parseToken() {
59
+ let state = "none";
60
+ let kind = null;
61
+ this._start = this._index;
62
+ while (this._index < this._input.length) {
63
+ const codePointNumber = this._input.codePointAt(this._index);
64
+ if (typeof codePointNumber === "undefined") {
65
+ throw new Error("Unable to get code point!");
66
+ }
67
+ const codePoint = String.fromCodePoint(codePointNumber);
68
+ if (state === "none") {
69
+ if (codePoint === "\\") {
70
+ state = "marker_start";
71
+ } else if (isWhitespace(codePoint)) {
72
+ state = "whitespace";
73
+ } else {
74
+ state = "word";
75
+ }
76
+ } else if (state === "marker_start") {
77
+ if (isDigit(codePoint)) {
78
+ if (this._tokenLength === 0) {
79
+ throw new Error(
80
+ "Invalid Marker: Markers must not contain only digits."
81
+ );
82
+ }
83
+ state = "marker_number";
84
+ } else if (codePoint === "*") {
85
+ this._index += codePoint.length;
86
+ kind = "marker";
87
+ break;
88
+ } else if (isWhitespace(codePoint)) {
89
+ kind = "marker";
90
+ break;
91
+ }
92
+ } else if (state === "marker_number") {
93
+ if (codePoint === "*") {
94
+ this._index += codePoint.length;
95
+ kind = "marker";
96
+ break;
97
+ } else if (!isDigit(codePoint)) {
98
+ kind = "marker";
99
+ break;
100
+ }
101
+ } else if (state === "whitespace") {
102
+ if (!isWhitespace(codePoint)) {
103
+ kind = "whitespace";
104
+ break;
105
+ }
106
+ } else if (state === "word") {
107
+ if (isWhitespace(codePoint) || codePoint === "\\") {
108
+ kind = "word";
109
+ break;
110
+ }
111
+ }
112
+ this._index += codePoint.length;
113
+ }
114
+ if (!kind) {
115
+ if (this._index >= this._input.length) {
116
+ if (state == "marker_start" || state === "marker_number") {
117
+ kind = "marker";
118
+ } else if (state === "word") {
119
+ kind = "word";
120
+ } else if (state === "whitespace") {
121
+ kind = "whitespace";
122
+ }
123
+ }
124
+ }
125
+ if (kind) {
126
+ return t(loc(this._start, this._index), kind);
127
+ }
128
+ return null;
129
+ }
130
+ }
131
+ class UsfmParser {
132
+ _poem = null;
133
+ _wordsOfJesus = false;
134
+ tokenize(input) {
135
+ const simpleTokens = new UsfmTokenizer().tokenize(input);
136
+ let tokens = [];
137
+ for (let t2 of simpleTokens) {
138
+ if (t2.kind === "marker") {
139
+ let source = input.substring(t2.loc.start, t2.loc.end);
140
+ const isEnd = source.endsWith("*");
141
+ if (isEnd) {
142
+ source = source.substring(0, source.length - 1);
143
+ }
144
+ let numberIndex = -1;
145
+ for (let i = 0; i < source.length; i++) {
146
+ if (isDigit(source[i])) {
147
+ numberIndex = i;
148
+ break;
149
+ }
150
+ }
151
+ let number = null;
152
+ if (numberIndex === 1) {
153
+ throw new Error(
154
+ "Markers must not be made only of numbers!"
155
+ );
156
+ }
157
+ if (numberIndex > 0) {
158
+ number = parseInt(source.substring(numberIndex));
159
+ source = source.substring(0, numberIndex);
160
+ }
161
+ if (source.length === 1) {
162
+ if (isEnd) {
163
+ const startMarker = (0, import_lodash.findLast)(
164
+ tokens,
165
+ (t3) => t3.kind === "marker" && t3.type === "start"
166
+ );
167
+ if (startMarker) {
168
+ source = startMarker.command;
169
+ }
170
+ }
171
+ if (source.length === 1) {
172
+ throw new Error(
173
+ `Markers must have a command! Token: ${t2.loc.start}-${t2.loc.end}`
174
+ );
175
+ }
176
+ }
177
+ tokens.push(
178
+ marker(t2.loc, source, number, isEnd ? "end" : "start")
179
+ );
180
+ } else if (t2.kind === "whitespace") {
181
+ let source = input.substring(t2.loc.start, t2.loc.end);
182
+ tokens.push(whitespace(t2.loc, source));
183
+ } else if (t2.kind === "word") {
184
+ let source = input.substring(t2.loc.start, t2.loc.end);
185
+ tokens.push(word(t2.loc, source));
186
+ }
187
+ }
188
+ return tokens;
189
+ }
190
+ parse(input) {
191
+ let root = {
192
+ type: "root",
193
+ content: []
194
+ };
195
+ const tokens = this.tokenize(input);
196
+ let expectingId = 0;
197
+ let expectingName = 0;
198
+ let expectingTitle = 0;
199
+ let expectingSectionHeading = 0;
200
+ let expectingFootnote = 0;
201
+ let expectingFootnoteReference = 0;
202
+ let expectingFootnoteText = 0;
203
+ let expectingReferenceText = 0;
204
+ let expectingWordAttribute = 0;
205
+ let expectingNestedWordAttribute = 0;
206
+ let expectingWordsOfJesus = 0;
207
+ let expectingIntroParagraph = 0;
208
+ let expectingCrossReference = 0;
209
+ let expectingUnknownCommand = 0;
210
+ let canParseFootnotes = true;
211
+ let chapter = null;
212
+ let lastVerse = null;
213
+ let verse = null;
214
+ let subtitle = null;
215
+ let words = [];
216
+ let verseContent = [];
217
+ let sectionContent = "";
218
+ let currentFootnoteId = 0;
219
+ let footnote = null;
220
+ this._poem = null;
221
+ const addWordsToVerseOrSubtitle = () => {
222
+ if (words.length > 0) {
223
+ const text = this._text(words.join("").trimEnd());
224
+ if (verse) {
225
+ verse.content.push(text);
226
+ } else if (subtitle) {
227
+ subtitle.content.push(text);
228
+ } else {
229
+ verseContent.push(text);
230
+ }
231
+ words = [];
232
+ }
233
+ };
234
+ const addVerseContentToChapter = (token) => {
235
+ if (!chapter) {
236
+ return;
237
+ }
238
+ if (verseContent.length > 0) {
239
+ if (chapter.content.some((c) => c.type === "verse")) {
240
+ this._throwError(
241
+ input,
242
+ token,
243
+ "Cannot infer first verse after other verses have been added to the chapter!"
244
+ );
245
+ }
246
+ verse = {
247
+ type: "verse",
248
+ number: 1,
249
+ content: verseContent
250
+ };
251
+ chapter.content.push(verse);
252
+ verseContent = [];
253
+ }
254
+ };
255
+ const cleanupVerse = () => {
256
+ if (!verse || !chapter) {
257
+ return;
258
+ }
259
+ let chapterContent = [];
260
+ for (let i = verse.content.length - 1; i >= 0; i--) {
261
+ let content = verse.content[i];
262
+ if (typeof content === "object" && "heading" in content) {
263
+ chapterContent.unshift({
264
+ type: "heading",
265
+ content: [content.heading]
266
+ });
267
+ verse.content.splice(i, 1);
268
+ } else if (typeof content === "object" && "lineBreak" in content && content.lineBreak) {
269
+ chapterContent.unshift({
270
+ type: "line_break"
271
+ });
272
+ verse.content.splice(i, 1);
273
+ } else {
274
+ break;
275
+ }
276
+ }
277
+ for (let content of chapterContent) {
278
+ chapter.content.push(content);
279
+ }
280
+ };
281
+ const completeVerseOrSubtitle = (token) => {
282
+ if (verse && isNaN(verse.number)) {
283
+ const index = chapter.content.indexOf(verse);
284
+ if (index >= 0) {
285
+ chapter.content.splice(index, 1);
286
+ }
287
+ verse = null;
288
+ }
289
+ if (verse || subtitle) {
290
+ addWordsToVerseOrSubtitle();
291
+ }
292
+ addVerseContentToChapter(token);
293
+ cleanupVerse();
294
+ };
295
+ const completeSection = () => {
296
+ if (expectingSectionHeading > 0) {
297
+ if (verse) {
298
+ addWordsToVerseOrSubtitle();
299
+ verse.content.push({
300
+ heading: sectionContent
301
+ });
302
+ } else if (chapter) {
303
+ chapter.content.push({
304
+ type: "heading",
305
+ content: [sectionContent]
306
+ });
307
+ } else {
308
+ root.content.push({
309
+ type: "heading",
310
+ content: [sectionContent]
311
+ });
312
+ }
313
+ sectionContent = "";
314
+ expectingSectionHeading = 0;
315
+ }
316
+ };
317
+ const addWordsToFootnote = () => {
318
+ if (footnote && words.length > 0) {
319
+ footnote.text += words.join(" ");
320
+ words = [];
321
+ }
322
+ };
323
+ for (let token of tokens) {
324
+ if (token.kind === "marker") {
325
+ if (token.command === "\\c") {
326
+ addWordsToVerseOrSubtitle();
327
+ cleanupVerse();
328
+ chapter = {
329
+ type: "chapter",
330
+ number: NaN,
331
+ content: [],
332
+ footnotes: []
333
+ };
334
+ verse = null;
335
+ verseContent = [];
336
+ root.content.push(chapter);
337
+ } else if (token.command === "\\v") {
338
+ if (!chapter) {
339
+ this._throwError(
340
+ input,
341
+ token,
342
+ "Cannot parse a verse without chapter information!"
343
+ );
344
+ } else {
345
+ completeSection();
346
+ completeVerseOrSubtitle(token);
347
+ lastVerse = verse;
348
+ verse = {
349
+ type: "verse",
350
+ number: NaN,
351
+ content: []
352
+ };
353
+ chapter.content.push(verse);
354
+ }
355
+ } else if (token.command === "\\d") {
356
+ if (!chapter) {
357
+ this._throwError(
358
+ input,
359
+ token,
360
+ "Cannot parse a hebrew subtitle without chapter information!"
361
+ );
362
+ } else {
363
+ completeVerseOrSubtitle(token);
364
+ subtitle = {
365
+ type: "hebrew_subtitle",
366
+ content: []
367
+ };
368
+ chapter.content.push(subtitle);
369
+ }
370
+ } else if (token.command === "\\b" || token.command === "\\p") {
371
+ if (!chapter) {
372
+ this._throwError(
373
+ input,
374
+ token,
375
+ "Cannot parse a line break without chapter information!"
376
+ );
377
+ } else {
378
+ if (verse) {
379
+ addWordsToVerseOrSubtitle();
380
+ verse.content.push({
381
+ lineBreak: true
382
+ });
383
+ } else {
384
+ completeVerseOrSubtitle(token);
385
+ chapter.content.push({
386
+ type: "line_break"
387
+ });
388
+ }
389
+ }
390
+ } else if (token.command === "\\q") {
391
+ addWordsToVerseOrSubtitle();
392
+ this._poem = token.number;
393
+ } else if (token.command === "\\p") {
394
+ addWordsToVerseOrSubtitle();
395
+ this._poem = null;
396
+ } else if (token.command === "\\id") {
397
+ expectingId = 1;
398
+ } else if (token.command === "\\h") {
399
+ expectingName = 1;
400
+ root.header = void 0;
401
+ } else if (token.command === "\\mt" || token.command === "\\+mt") {
402
+ expectingTitle = 1;
403
+ } else if (token.command === "\\s") {
404
+ expectingSectionHeading = 1;
405
+ } else if (token.command === "\\r") {
406
+ expectingReferenceText = 1;
407
+ } else if (token.command === "\\f" && canParseFootnotes) {
408
+ if (token.type === "start") {
409
+ if (!chapter) {
410
+ this._throwError(
411
+ input,
412
+ token,
413
+ "Cannot start a footnote outside of a chapter!",
414
+ true
415
+ );
416
+ } else {
417
+ addWordsToVerseOrSubtitle();
418
+ footnote = {
419
+ noteId: currentFootnoteId,
420
+ text: "",
421
+ caller: null
422
+ };
423
+ const ref = {
424
+ noteId: footnote.noteId
425
+ };
426
+ expectingFootnote = 1;
427
+ chapter.footnotes.push(footnote);
428
+ if (verse) {
429
+ verse.content.push(ref);
430
+ } else if (subtitle) {
431
+ subtitle.content.push(ref);
432
+ } else {
433
+ verseContent.push(ref);
434
+ }
435
+ currentFootnoteId += 1;
436
+ }
437
+ } else {
438
+ addWordsToFootnote();
439
+ expectingFootnote = 0;
440
+ expectingFootnoteText = 0;
441
+ expectingFootnoteReference = 0;
442
+ footnote = null;
443
+ }
444
+ } else if (token.command === "\\fr" && canParseFootnotes) {
445
+ if (!footnote) {
446
+ this._throwError(
447
+ input,
448
+ token,
449
+ "Cannot start a footnote reference outside of a footnote!",
450
+ true
451
+ );
452
+ } else {
453
+ expectingFootnoteReference = 1;
454
+ }
455
+ } else if (token.command === "\\ft" && canParseFootnotes) {
456
+ if (!footnote) {
457
+ this._throwError(
458
+ input,
459
+ token,
460
+ "Cannot start footnote text outside of a footnote!",
461
+ true
462
+ );
463
+ } else {
464
+ expectingFootnoteText = 1;
465
+ }
466
+ } else if (token.command === "\\w") {
467
+ if (token.type === "start") {
468
+ expectingWordAttribute = 1;
469
+ } else {
470
+ expectingWordAttribute = 0;
471
+ }
472
+ } else if (token.command === "\\+w") {
473
+ if (token.type === "start") {
474
+ expectingNestedWordAttribute = 1;
475
+ } else {
476
+ expectingNestedWordAttribute = 0;
477
+ }
478
+ } else if (token.command === "\\wj" || token.command === "\\+wj") {
479
+ if (token.type === "start") {
480
+ addWordsToVerseOrSubtitle();
481
+ this._wordsOfJesus = true;
482
+ } else {
483
+ addWordsToVerseOrSubtitle();
484
+ this._wordsOfJesus = false;
485
+ }
486
+ } else if (token.command === "\\ip") {
487
+ expectingIntroParagraph = 1;
488
+ canParseFootnotes = false;
489
+ } else if (token.command === "\\x") {
490
+ if (token.type === "start") {
491
+ expectingCrossReference = 1;
492
+ } else {
493
+ expectingCrossReference = 0;
494
+ }
495
+ } else if (token.command.indexOf("-") >= 0) {
496
+ if (token.type === "start") {
497
+ expectingUnknownCommand = 1;
498
+ }
499
+ } else if (token.type === "end") {
500
+ expectingUnknownCommand = 0;
501
+ }
502
+ } else if (token.kind === "word") {
503
+ if (expectingId > 0) {
504
+ if (expectingId === 1) {
505
+ root.id = token.word;
506
+ expectingId = 2;
507
+ }
508
+ } else if (expectingName > 0) {
509
+ if (root.header) {
510
+ root.header += " " + token.word;
511
+ } else {
512
+ root.header = token.word;
513
+ }
514
+ } else if (expectingTitle > 0) {
515
+ if (root.title) {
516
+ root.title += " " + token.word;
517
+ } else {
518
+ root.title = token.word;
519
+ }
520
+ } else if (expectingSectionHeading > 0) {
521
+ if (sectionContent) {
522
+ sectionContent += " " + token.word;
523
+ } else {
524
+ sectionContent = token.word;
525
+ }
526
+ } else if (expectingFootnoteReference > 0) {
527
+ if (expectingFootnoteReference = 1) {
528
+ const [chapter2, verse2] = token.word.split(/[\.\:]/);
529
+ if (footnote) {
530
+ footnote.reference = {
531
+ chapter: parseInt(chapter2),
532
+ verse: parseInt(verse2)
533
+ };
534
+ }
535
+ expectingFootnoteReference = 0;
536
+ }
537
+ } else if (expectingFootnoteText > 0) {
538
+ words.push(token.word);
539
+ } else if (expectingFootnote > 0) {
540
+ if (expectingFootnote === 1) {
541
+ if (token.word) {
542
+ if (footnote) {
543
+ if (token.word === "+" || token.word !== "-") {
544
+ footnote.caller = token.word;
545
+ } else {
546
+ footnote.caller = null;
547
+ }
548
+ }
549
+ }
550
+ expectingFootnote = 2;
551
+ } else {
552
+ words.push(token.word);
553
+ }
554
+ } else if (expectingReferenceText > 0) {
555
+ } else if (expectingCrossReference > 0) {
556
+ } else if (chapter && isNaN(chapter.number)) {
557
+ chapter.number = parseInt(token.word);
558
+ if (isNaN(chapter.number)) {
559
+ this._throwError(
560
+ input,
561
+ token,
562
+ "The first word token after a chapter marker must be parsable to an integer!"
563
+ );
564
+ }
565
+ } else if (verse && isNaN(verse.number)) {
566
+ verse.number = parseInt(token.word);
567
+ if (isNaN(verse.number)) {
568
+ this._throwError(
569
+ input,
570
+ token,
571
+ "The first word token after a verse marker must be parsable to an integer!"
572
+ );
573
+ }
574
+ } else if (expectingWordAttribute > 0) {
575
+ if (expectingWordAttribute === 1) {
576
+ const firstVerticalBarIndex = token.word.indexOf("|");
577
+ if (firstVerticalBarIndex >= 0) {
578
+ const name = token.word.slice(
579
+ 0,
580
+ firstVerticalBarIndex
581
+ );
582
+ words.push(name);
583
+ expectingWordAttribute = 2;
584
+ } else {
585
+ words.push(token.word);
586
+ }
587
+ }
588
+ } else if (expectingNestedWordAttribute > 0) {
589
+ if (expectingNestedWordAttribute === 1) {
590
+ const firstVerticalBarIndex = token.word.indexOf("|");
591
+ if (firstVerticalBarIndex >= 0) {
592
+ const name = token.word.slice(
593
+ 0,
594
+ firstVerticalBarIndex
595
+ );
596
+ words.push(name);
597
+ expectingNestedWordAttribute = 2;
598
+ } else {
599
+ words.push(token.word);
600
+ }
601
+ }
602
+ } else if (expectingUnknownCommand > 0) {
603
+ } else if (expectingIntroParagraph > 0) {
604
+ } else {
605
+ words.push(token.word);
606
+ }
607
+ } else if (token.kind === "whitespace") {
608
+ if (expectingId > 0) {
609
+ if (token.whitespace.includes("\n")) {
610
+ expectingId = 0;
611
+ }
612
+ } else if (expectingName > 0) {
613
+ if (token.whitespace.includes("\n")) {
614
+ expectingName = 0;
615
+ }
616
+ } else if (expectingTitle > 0) {
617
+ if (token.whitespace.includes("\n")) {
618
+ expectingTitle = 0;
619
+ }
620
+ } else if (expectingSectionHeading > 0) {
621
+ if (token.whitespace.includes("\n")) {
622
+ completeSection();
623
+ }
624
+ } else if (expectingReferenceText > 0) {
625
+ if (token.whitespace.includes("\n")) {
626
+ expectingReferenceText = 0;
627
+ }
628
+ } else if (expectingIntroParagraph > 0) {
629
+ if (token.whitespace.includes("\n")) {
630
+ expectingIntroParagraph = 0;
631
+ canParseFootnotes = true;
632
+ }
633
+ } else if (expectingId > 0 || expectingName > 0 || expectingTitle > 0 || expectingSectionHeading > 0 || expectingFootnote > 0 || expectingFootnoteReference > 0 || expectingFootnoteText > 0 || expectingReferenceText > 0 || expectingCrossReference > 0 || expectingWordAttribute > 0 || expectingNestedWordAttribute > 0) {
634
+ } else if (expectingUnknownCommand > 0) {
635
+ if (token.whitespace.includes("\n")) {
636
+ expectingUnknownCommand = 0;
637
+ }
638
+ } else if (words.length > 0) {
639
+ let lastWord = words[words.length - 1];
640
+ if (lastWord !== " ") {
641
+ words.push(" ");
642
+ }
643
+ }
644
+ }
645
+ }
646
+ completeVerseOrSubtitle(null);
647
+ return root;
648
+ }
649
+ renderMarkdown(tree) {
650
+ let md = "";
651
+ if (tree.header) {
652
+ md += `# ${tree.header}
653
+ `;
654
+ }
655
+ for (let c of tree.content) {
656
+ if (c.type === "heading") {
657
+ md += `## ${c.content.join(" ")}
658
+ `;
659
+ } else if (c.type === "chapter") {
660
+ md += `### ${c.number}
661
+ `;
662
+ for (let content of c.content) {
663
+ if (content.type === "heading") {
664
+ md += `#### ${content.content.join(" ")}
665
+ `;
666
+ } else if (content.type === "line_break") {
667
+ md += "\n\n";
668
+ } else if (content.type === "verse") {
669
+ md += `<em>${content.number}</em>`;
670
+ for (let v of content.content) {
671
+ if (typeof v === "string") {
672
+ md += v + " ";
673
+ } else if ("text" in v) {
674
+ md += v.text + " ";
675
+ }
676
+ }
677
+ md += "\n";
678
+ }
679
+ }
680
+ }
681
+ }
682
+ return md;
683
+ }
684
+ _hasAttribute() {
685
+ return this._poem !== null || this._wordsOfJesus;
686
+ }
687
+ _text(text) {
688
+ if (!this._hasAttribute()) {
689
+ return text;
690
+ }
691
+ const t2 = {
692
+ text
693
+ };
694
+ if (this._poem !== null) {
695
+ t2.poem = this._poem;
696
+ }
697
+ if (this._wordsOfJesus) {
698
+ t2.wordsOfJesus = true;
699
+ }
700
+ return t2;
701
+ }
702
+ _throwError(source, token, message, warn = false) {
703
+ if (token) {
704
+ let line = 1;
705
+ let column = 1;
706
+ let start = token.loc.start;
707
+ for (let i = 0; i < start; i++) {
708
+ let char = source[i];
709
+ if (char === "\n") {
710
+ line += 1;
711
+ column = 1;
712
+ } else {
713
+ column += 1;
714
+ }
715
+ }
716
+ let tokenDebug = "";
717
+ if (token.kind === "word") {
718
+ tokenDebug = ", word";
719
+ } else if (token.kind === "marker") {
720
+ tokenDebug = ", " + token.command;
721
+ } else {
722
+ tokenDebug = "";
723
+ }
724
+ if (warn) {
725
+ console.warn(`(${line}, ${column}${tokenDebug}) ${message}`);
726
+ } else {
727
+ throw new Error(`(${line}, ${column}${tokenDebug}) ${message}`);
728
+ }
729
+ } else {
730
+ if (warn) {
731
+ console.warn(message);
732
+ } else {
733
+ throw new Error(message);
734
+ }
735
+ }
736
+ }
737
+ }
738
+ function isDigit(char) {
739
+ return char.length === 1 && char >= "0" && char <= "9";
740
+ }
741
+ function isWhitespace(char) {
742
+ return char === " " || char === " " || char === "\n" || char === "\r";
743
+ }
744
+ function t(loc2, kind) {
745
+ return {
746
+ loc: loc2,
747
+ kind
748
+ };
749
+ }
750
+ function loc(start, end) {
751
+ return {
752
+ start,
753
+ end
754
+ };
755
+ }
756
+ function marker(loc2, command, number = null, type = "start") {
757
+ return {
758
+ kind: "marker",
759
+ loc: loc2,
760
+ command,
761
+ number,
762
+ type
763
+ };
764
+ }
765
+ function word(loc2, word2) {
766
+ return {
767
+ kind: "word",
768
+ loc: loc2,
769
+ word: word2
770
+ };
771
+ }
772
+ function whitespace(loc2, whitespace2) {
773
+ return {
774
+ kind: "whitespace",
775
+ loc: loc2,
776
+ whitespace: whitespace2
777
+ };
778
+ }
779
+ // Annotate the CommonJS export names for ESM import in node:
780
+ 0 && (module.exports = {
781
+ UsfmParser,
782
+ UsfmTokenizer,
783
+ isDigit,
784
+ isWhitespace,
785
+ loc,
786
+ marker,
787
+ t,
788
+ whitespace,
789
+ word
790
+ });
791
+ //# sourceMappingURL=usfm-parser.cjs.map