@helloao/tools 0.0.6 → 0.0.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.
Files changed (104) hide show
  1. package/dist/cjs/generation/api.cjs +240 -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 +158 -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 +176 -0
  16. package/dist/cjs/parser/codex-parser.cjs.map +7 -0
  17. package/dist/cjs/parser/index.cjs +48 -0
  18. package/dist/cjs/parser/index.cjs.map +7 -0
  19. package/dist/cjs/parser/iterators.cjs +204 -0
  20. package/dist/cjs/parser/iterators.cjs.map +7 -0
  21. package/dist/cjs/parser/types.cjs +17 -0
  22. package/dist/cjs/parser/types.cjs.map +7 -0
  23. package/dist/cjs/parser/usfm-parser.cjs +791 -0
  24. package/dist/cjs/parser/usfm-parser.cjs.map +7 -0
  25. package/dist/cjs/parser/usx-parser.cjs +471 -0
  26. package/dist/cjs/parser/usx-parser.cjs.map +7 -0
  27. package/dist/cjs/utils.cjs +85 -0
  28. package/dist/cjs/utils.cjs.map +7 -0
  29. package/dist/esm/generation/api.js +198 -0
  30. package/dist/esm/generation/api.js.map +7 -0
  31. package/dist/esm/generation/audio.js +53 -0
  32. package/dist/esm/generation/audio.js.map +7 -0
  33. package/dist/esm/generation/book-order.js +478 -0
  34. package/dist/esm/generation/book-order.js.map +7 -0
  35. package/dist/esm/generation/common-types.js +2 -0
  36. package/dist/esm/generation/common-types.js.map +7 -0
  37. package/dist/esm/generation/dataset.js +132 -0
  38. package/dist/esm/generation/dataset.js.map +7 -0
  39. package/dist/esm/generation/index.js +8 -0
  40. package/dist/esm/generation/index.js.map +7 -0
  41. package/dist/esm/index.js +6 -0
  42. package/dist/esm/index.js.map +7 -0
  43. package/dist/esm/parser/codex-parser.js +144 -0
  44. package/dist/esm/parser/codex-parser.js.map +7 -0
  45. package/dist/esm/parser/index.js +8 -0
  46. package/dist/esm/parser/index.js.map +7 -0
  47. package/dist/esm/parser/iterators.js +152 -0
  48. package/dist/esm/parser/iterators.js.map +7 -0
  49. package/dist/esm/parser/types.js +2 -0
  50. package/dist/esm/parser/types.js.map +7 -0
  51. package/dist/esm/parser/usfm-parser.js +749 -0
  52. package/dist/esm/parser/usfm-parser.js.map +7 -0
  53. package/dist/esm/parser/usx-parser.js +447 -0
  54. package/dist/esm/parser/usx-parser.js.map +7 -0
  55. package/dist/esm/utils.js +51 -0
  56. package/dist/esm/utils.js.map +7 -0
  57. package/{generation → dist/types/generation}/api.d.ts +2 -2
  58. package/{generation → dist/types/generation}/audio.d.ts +1 -1
  59. package/{generation → dist/types/generation}/dataset.d.ts +1 -1
  60. package/dist/types/generation/index.d.ts +7 -0
  61. package/dist/types/index.d.ts +5 -0
  62. package/{parser → dist/types/parser}/codex-parser.d.ts +1 -1
  63. package/dist/types/parser/index.d.ts +7 -0
  64. package/{parser → dist/types/parser}/usfm-parser.d.ts +1 -1
  65. package/{parser → dist/types/parser}/usx-parser.d.ts +2 -2
  66. package/package.json +25 -3
  67. package/generation/api.js +0 -187
  68. package/generation/api.spec.d.ts +0 -2
  69. package/generation/api.spec.js +0 -620
  70. package/generation/audio.js +0 -60
  71. package/generation/audio.spec.d.ts +0 -2
  72. package/generation/audio.spec.js +0 -36
  73. package/generation/book-order.js +0 -494
  74. package/generation/book-order.spec.d.ts +0 -2
  75. package/generation/book-order.spec.js +0 -8
  76. package/generation/common-types.js +0 -2
  77. package/generation/dataset.js +0 -106
  78. package/generation/index.d.ts +0 -7
  79. package/generation/index.js +0 -38
  80. package/index.d.ts +0 -5
  81. package/index.js +0 -32
  82. package/parser/codex-parser.js +0 -160
  83. package/parser/codex-parser.spec.d.ts +0 -2
  84. package/parser/codex-parser.spec.js +0 -645
  85. package/parser/index.d.ts +0 -7
  86. package/parser/index.js +0 -35
  87. package/parser/iterators.js +0 -222
  88. package/parser/iterators.spec.d.ts +0 -2
  89. package/parser/iterators.spec.js +0 -174
  90. package/parser/types.js +0 -2
  91. package/parser/usfm-parser.js +0 -840
  92. package/parser/usfm-parser.spec.d.ts +0 -2
  93. package/parser/usfm-parser.spec.js +0 -1593
  94. package/parser/usx-parser.js +0 -486
  95. package/parser/usx-parser.spec.d.ts +0 -2
  96. package/parser/usx-parser.spec.js +0 -1260
  97. package/utils.js +0 -73
  98. package/utils.spec.d.ts +0 -2
  99. package/utils.spec.js +0 -42
  100. /package/{generation → dist/types/generation}/book-order.d.ts +0 -0
  101. /package/{generation → dist/types/generation}/common-types.d.ts +0 -0
  102. /package/{parser → dist/types/parser}/iterators.d.ts +0 -0
  103. /package/{parser → dist/types/parser}/types.d.ts +0 -0
  104. /package/{utils.d.ts → dist/types/utils.d.ts} +0 -0
@@ -1,486 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.USXParser = exports.PARSER_VERSION = void 0;
4
- const iterators_1 = require("./iterators");
5
- var NodeType;
6
- (function (NodeType) {
7
- NodeType[NodeType["Text"] = 3] = "Text";
8
- })(NodeType || (NodeType = {}));
9
- /**
10
- * The version of the parser.
11
- * Used to determine whether input files need to be re-parsed.
12
- */
13
- exports.PARSER_VERSION = '1';
14
- /**
15
- * Defines a class that is able to parse USX content.
16
- */
17
- class USXParser {
18
- _domParser;
19
- _noteCounter = 0;
20
- constructor(domParser) {
21
- this._domParser = domParser;
22
- }
23
- /**
24
- * Parses the specified USX content.
25
- *
26
- * @param usx The USX content to parse.
27
- * @returns The parse tree that was generated.
28
- */
29
- parse(usx) {
30
- const parser = this._domParser;
31
- const doc = parser.parseFromString(usx, 'application/xml');
32
- const usxElement = doc.documentElement;
33
- let root = {
34
- type: 'root',
35
- content: [],
36
- };
37
- const bookElement = usxElement.querySelector('book[code]');
38
- if (!bookElement) {
39
- throw new Error('The USX content does not contain a book element.');
40
- }
41
- const bookCode = bookElement.getAttribute('code') || '';
42
- if (!bookCode) {
43
- throw new Error('The book element does not contain a code attribute.');
44
- }
45
- root.id = bookCode;
46
- const header = usxElement.querySelector('para[style="h"]');
47
- if (header) {
48
- root.header = header.textContent || '';
49
- }
50
- const titles = usxElement.querySelectorAll('para[style="mt1"], para[style="mt2"], para[style="mt3"]');
51
- // const title2 = usxElement.querySelector('para[style="mt2"]');
52
- // const title3 = usxElement.querySelector('para[style="mt3"]');
53
- if (titles.length > 0) {
54
- root.title = [...titles]
55
- .map((t) => t.textContent)
56
- .filter((t) => t)
57
- .join(' ');
58
- }
59
- for (let content of this.iterateRootContent(usxElement)) {
60
- root.content.push(content);
61
- }
62
- return root;
63
- }
64
- *iterateRootContent(usxElement) {
65
- const iterator = (0, iterators_1.iterateAll)(usxElement);
66
- while (true) {
67
- const { done, value: child } = iterator.next();
68
- if (done) {
69
- break;
70
- }
71
- if (!(child instanceof Element)) {
72
- continue;
73
- }
74
- if (child.nodeName === 'chapter') {
75
- if (child.hasAttribute('eid')) {
76
- continue;
77
- }
78
- const chapter = {
79
- type: 'chapter',
80
- number: parseInt(child.getAttribute('number') || '0', 10),
81
- content: [],
82
- footnotes: [],
83
- };
84
- for (let content of this.iterateChapterContent(chapter, iterator)) {
85
- chapter.content.push(content);
86
- }
87
- yield chapter;
88
- }
89
- else if (child.nodeName === 'para') {
90
- const style = child.getAttribute('style');
91
- if (style === 's1' ||
92
- style === 's2' ||
93
- style === 's3' ||
94
- style === 's4') {
95
- yield {
96
- type: 'heading',
97
- content: child.textContent ? [child.textContent] : [],
98
- };
99
- }
100
- }
101
- }
102
- }
103
- *iterateChapterContent(chapter, nodes) {
104
- while (true) {
105
- const { done, value: element } = nodes.next();
106
- if (done) {
107
- break;
108
- }
109
- if (!(element instanceof Element)) {
110
- continue;
111
- }
112
- if (element.nodeName === 'chapter') {
113
- break;
114
- }
115
- else if (element.nodeName === 'para') {
116
- const style = element.getAttribute('style');
117
- if (style === 's1' ||
118
- style === 's2' ||
119
- style === 's3' ||
120
- style === 's4') {
121
- yield {
122
- type: 'heading',
123
- content: element.textContent
124
- ? [element.textContent]
125
- : [],
126
- };
127
- }
128
- else if (style === 'b') {
129
- yield {
130
- type: 'line_break',
131
- };
132
- }
133
- else if (style === 'd') {
134
- yield* this.parseHebrewSubtitle(element, chapter, nodes);
135
- }
136
- }
137
- else if (element.nodeName === 'verse') {
138
- if (element.hasAttribute('eid')) {
139
- continue;
140
- }
141
- yield this.parseVerse(element, chapter, nodes);
142
- }
143
- }
144
- }
145
- *iterateVerseContent(chapter, verse, nodes) {
146
- while (true) {
147
- const { done, value: node } = nodes.next();
148
- if (done) {
149
- break;
150
- }
151
- if (node.nodeName === 'verse') {
152
- break;
153
- }
154
- const parent = node.parentElement;
155
- let poem = null;
156
- let descriptive = null;
157
- if (parent.nodeName === 'para') {
158
- const style = parent.getAttribute('style');
159
- if (style === 'q1' ||
160
- style === 'q2' ||
161
- style === 'q3' ||
162
- style === 'q4') {
163
- poem =
164
- style === 'q1'
165
- ? 1
166
- : style === 'q2'
167
- ? 2
168
- : style === 'q3'
169
- ? 3
170
- : 4;
171
- }
172
- else if (style === 'd') {
173
- descriptive = true;
174
- }
175
- }
176
- for (let content of this.iterateNodeTextContent(nodes, node, chapter, verse)) {
177
- if (poem !== null || descriptive !== null) {
178
- if (typeof content === 'string') {
179
- let text = {
180
- text: content,
181
- };
182
- if (poem !== null) {
183
- text.poem = poem;
184
- }
185
- if (descriptive !== null) {
186
- text.descriptive = true;
187
- }
188
- yield text;
189
- }
190
- else {
191
- let text = {
192
- ...content,
193
- };
194
- if ('text' in text) {
195
- if (poem !== null) {
196
- text.poem = poem;
197
- }
198
- if (descriptive !== null) {
199
- text.descriptive = true;
200
- }
201
- }
202
- yield text;
203
- }
204
- }
205
- else {
206
- yield content;
207
- }
208
- }
209
- }
210
- }
211
- parseVerse(element, chapter, nodes) {
212
- const verse = {
213
- type: 'verse',
214
- number: parseInt(element.getAttribute('number') || '0', 10),
215
- content: [],
216
- };
217
- for (let content of this.iterateVerseContent(chapter, verse, nodes)) {
218
- addOrJoin(verse.content, content);
219
- }
220
- trimContent(verse.content);
221
- return verse;
222
- }
223
- *parseHebrewSubtitle(para, chapter, nodes) {
224
- const subtitle = {
225
- type: 'hebrew_subtitle',
226
- content: [],
227
- };
228
- for (let content of this.iterateHebrewSubtitleContent(para, chapter, nodes)) {
229
- if (typeof content === 'object' && 'number' in content) {
230
- yield content;
231
- continue;
232
- }
233
- addOrJoin(subtitle.content, content);
234
- }
235
- trimContent(subtitle.content);
236
- if (subtitle.content.length > 0) {
237
- yield subtitle;
238
- }
239
- }
240
- *iterateHebrewSubtitleContent(element, chapter, nodes) {
241
- while (true) {
242
- const { done, value: node } = nodes.next();
243
- if (done) {
244
- break;
245
- }
246
- if (!(0, iterators_1.isParent)(node, element)) {
247
- nodes.rewind(1);
248
- break;
249
- }
250
- if (node instanceof Element && node.nodeName === 'verse') {
251
- yield this.parseVerse(node, chapter, nodes);
252
- }
253
- else {
254
- yield* this.iterateNodeTextContent(nodes, node, chapter);
255
- }
256
- }
257
- }
258
- *iterateNodeTextContent(nodes, node, chapter, verse) {
259
- if (node instanceof Element && node.nodeName === 'note') {
260
- yield* this.iterateNote(nodes, node, chapter, verse);
261
- }
262
- else if (node instanceof Element && node.nodeName === 'char') {
263
- yield* this.iterateChar(nodes, node);
264
- }
265
- else if (node instanceof Element &&
266
- node.nodeName === 'para' &&
267
- node.getAttribute('style') === 'b') {
268
- for (let _ of (0, iterators_1.children)(nodes, node)) {
269
- // iterate through all the children to prevent iterating over them multiple times
270
- }
271
- yield {
272
- lineBreak: true,
273
- };
274
- }
275
- else if (node.nodeType === NodeType.Text) {
276
- yield node.textContent || '';
277
- }
278
- }
279
- *iterateCharContent(char) {
280
- const style = char.getAttribute('style');
281
- const text = trimText(char.textContent || '');
282
- if (style === 'wj') {
283
- yield {
284
- text,
285
- wordsOfJesus: true,
286
- };
287
- }
288
- else {
289
- yield text;
290
- }
291
- }
292
- *iterateNote(nodes, node, chapter, verse) {
293
- const style = node.getAttribute('style');
294
- if (style === 'f') {
295
- const verseReferenceRegex = /^[0-9]{1,3}:[0-9]{1,3}/;
296
- let text = '';
297
- for (let child of (0, iterators_1.children)(nodes, node)) {
298
- if (child.nodeType === NodeType.Text) {
299
- text += child.textContent || '';
300
- }
301
- }
302
- text = text.trim();
303
- if (verseReferenceRegex.test(text)) {
304
- text = text.replace(verseReferenceRegex, '').trim();
305
- }
306
- const note = {
307
- noteId: this._noteCounter++,
308
- caller: node.getAttribute('caller') || null,
309
- text,
310
- reference: {
311
- chapter: chapter.number,
312
- verse: verse?.number ?? 0,
313
- },
314
- };
315
- chapter.footnotes.push(note);
316
- yield {
317
- noteId: note.noteId,
318
- };
319
- }
320
- else {
321
- for (let _ of (0, iterators_1.children)(nodes, node)) {
322
- // iterate through all the children
323
- // so that we don't end up with duplicates
324
- }
325
- }
326
- }
327
- *iterateChar(nodes, node) {
328
- const style = node.getAttribute('style');
329
- let text = '';
330
- for (let char of (0, iterators_1.children)(nodes, node)) {
331
- if (char.nodeType === NodeType.Text) {
332
- text += char.textContent || '';
333
- }
334
- }
335
- if (style === 'wj') {
336
- yield {
337
- text,
338
- wordsOfJesus: true,
339
- };
340
- }
341
- else {
342
- yield text;
343
- }
344
- // if (!parentChar(node) && !parentNote(node)) {
345
- // yield *iterateCharContent(node);
346
- // }
347
- }
348
- }
349
- exports.USXParser = USXParser;
350
- // Taken from https://github.com/gracious-tech/fetch/blob/1576cc4eafb32bf347a09332094cf17c2231c90c/converters/usx-to-json/src/elements.ts#L16
351
- const ignoredParaStyles = new Set([
352
- // <para> Identification [exclude all] - Running headings & table of contents
353
- 'ide', // See https://github.com/schierlm/BibleMultiConverter/issues/67
354
- 'rem', // Remarks (valid in schema though missed in docs)
355
- 'h',
356
- 'h1',
357
- 'h2',
358
- 'h3',
359
- 'h4',
360
- 'toc1',
361
- 'toc2',
362
- 'toc3',
363
- 'toca1',
364
- 'toca2',
365
- 'toca3',
366
- /* <para> Introductions [exclude all] - Introductionary (non-biblical) content
367
- Which might be helpful in a printed book, but intro material in apps is usually bad UX,
368
- and users that really care can research a translations methodology themselves
369
- */
370
- 'imt',
371
- 'imt1',
372
- 'imt2',
373
- 'imt3',
374
- 'imt4',
375
- 'is',
376
- 'is1',
377
- 'is2',
378
- 'is3',
379
- 'is4',
380
- 'ip',
381
- 'ipi',
382
- 'im',
383
- 'imi',
384
- 'ipq',
385
- 'imq',
386
- 'ipr',
387
- 'iq',
388
- 'iq1',
389
- 'iq2',
390
- 'iq3',
391
- 'iq4',
392
- 'ib',
393
- 'ili',
394
- 'ili1',
395
- 'ili2',
396
- 'ili3',
397
- 'ili4',
398
- 'iot',
399
- 'io',
400
- 'io1',
401
- 'io2',
402
- 'io3',
403
- 'io4',
404
- 'iex',
405
- 'imte',
406
- 'ie',
407
- /* <para> Headings [exclude some] - Exclude book & chapter headings but keep section headings
408
- Not excluded: ms# | mr | s# | sr | d | sp | sd#
409
- */
410
- 'mt',
411
- 'mt1',
412
- 'mt2',
413
- 'mt3',
414
- 'mt4',
415
- 'mte',
416
- 'mte1',
417
- 'mte2',
418
- 'mte3',
419
- 'mte4',
420
- 'cl',
421
- 'cd', // Non-biblical chapter summary, more than heading
422
- 'r', // Parallels to be provided by external data
423
- ]);
424
- function* iterateCharContent(char) {
425
- const style = char.getAttribute('style');
426
- const text = trimText(char.textContent || '');
427
- if (style === 'wj') {
428
- yield {
429
- text,
430
- wordsOfJesus: true,
431
- };
432
- }
433
- else {
434
- yield text;
435
- }
436
- }
437
- function trimText(text) {
438
- return text.replace(/\s+/g, ' ');
439
- }
440
- function trimContent(content) {
441
- for (let i = 0; i < content.length; i++) {
442
- const value = content[i];
443
- if (typeof value === 'string') {
444
- content[i] = trimText(value).trim();
445
- if (content[i] === '') {
446
- content.splice(i, 1);
447
- i--;
448
- continue;
449
- }
450
- }
451
- else if (isVerseText(value)) {
452
- value.text = trimText(value.text).trim();
453
- if (value.text === '') {
454
- content.splice(i, 1);
455
- i--;
456
- continue;
457
- }
458
- }
459
- }
460
- return content;
461
- }
462
- function addOrJoin(array, value) {
463
- if (array.length === 0) {
464
- array.push(value);
465
- }
466
- else {
467
- const last = array[array.length - 1];
468
- if (typeof last === 'string' && typeof value === 'string') {
469
- array[array.length - 1] = last + value;
470
- }
471
- else if (isVerseText(last) &&
472
- isVerseText(value) &&
473
- hasSameFormatting(last, value)) {
474
- last.text += value.text;
475
- }
476
- else {
477
- array.push(value);
478
- }
479
- }
480
- }
481
- function isVerseText(value) {
482
- return typeof value === 'object' && value !== null && 'text' in value;
483
- }
484
- function hasSameFormatting(a, b) {
485
- return a.poem === b.poem && a.wordsOfJesus === b.wordsOfJesus;
486
- }
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=usx-parser.spec.d.ts.map