@helloao/tools 0.0.5 → 0.0.6-alpha

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