@helloao/tools 0.0.1

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 (47) hide show
  1. package/LICENSE +21 -0
  2. package/generation/api.d.ts +203 -0
  3. package/generation/api.js +153 -0
  4. package/generation/api.spec.d.ts +2 -0
  5. package/generation/api.spec.js +463 -0
  6. package/generation/audio.d.ts +20 -0
  7. package/generation/audio.js +60 -0
  8. package/generation/audio.spec.d.ts +2 -0
  9. package/generation/audio.spec.js +36 -0
  10. package/generation/book-order.d.ts +15 -0
  11. package/generation/book-order.js +494 -0
  12. package/generation/book-order.spec.d.ts +2 -0
  13. package/generation/book-order.spec.js +8 -0
  14. package/generation/common-types.d.ts +327 -0
  15. package/generation/common-types.js +2 -0
  16. package/generation/dataset.d.ts +34 -0
  17. package/generation/dataset.js +106 -0
  18. package/generation/index.d.ts +7 -0
  19. package/generation/index.js +38 -0
  20. package/index.d.ts +5 -0
  21. package/index.js +32 -0
  22. package/package.json +27 -0
  23. package/parser/codex-parser.d.ts +97 -0
  24. package/parser/codex-parser.js +160 -0
  25. package/parser/codex-parser.spec.d.ts +2 -0
  26. package/parser/codex-parser.spec.js +645 -0
  27. package/parser/index.d.ts +7 -0
  28. package/parser/index.js +35 -0
  29. package/parser/iterators.d.ts +89 -0
  30. package/parser/iterators.js +222 -0
  31. package/parser/iterators.spec.d.ts +2 -0
  32. package/parser/iterators.spec.js +174 -0
  33. package/parser/types.d.ts +144 -0
  34. package/parser/types.js +2 -0
  35. package/parser/usfm-parser.d.ts +135 -0
  36. package/parser/usfm-parser.js +840 -0
  37. package/parser/usfm-parser.spec.d.ts +2 -0
  38. package/parser/usfm-parser.spec.js +1593 -0
  39. package/parser/usx-parser.d.ts +33 -0
  40. package/parser/usx-parser.js +425 -0
  41. package/parser/usx-parser.spec.d.ts +2 -0
  42. package/parser/usx-parser.spec.js +1260 -0
  43. package/typings/types.d.ts +14 -0
  44. package/utils.d.ts +29 -0
  45. package/utils.js +73 -0
  46. package/utils.spec.d.ts +2 -0
  47. package/utils.spec.js +42 -0
@@ -0,0 +1,160 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CodexParser = exports.codexSchema = exports.metadataSchema = exports.chapterHeadingSchema = void 0;
4
+ const zod_1 = require("zod");
5
+ const utils_1 = require("../utils");
6
+ exports.chapterHeadingSchema = zod_1.z.object({
7
+ type: zod_1.z.literal('chapter-heading'),
8
+ data: zod_1.z.object({
9
+ chapter: zod_1.z.coerce.number().int(),
10
+ }),
11
+ });
12
+ exports.metadataSchema = zod_1.z.discriminatedUnion('type', [
13
+ exports.chapterHeadingSchema
14
+ ]);
15
+ exports.codexSchema = zod_1.z.object({
16
+ cells: zod_1.z.array(zod_1.z.object({
17
+ // kind: z.number().int(),
18
+ language: zod_1.z.string(),
19
+ value: zod_1.z.string(),
20
+ metadata: zod_1.z.object({
21
+ type: zod_1.z.string()
22
+ }).passthrough().nullable().optional()
23
+ })),
24
+ });
25
+ /**
26
+ * Defines a parser for codex files.
27
+ */
28
+ class CodexParser {
29
+ // TODO:
30
+ // /**
31
+ // * Whether to preserve markdown in the parsed content.
32
+ // */
33
+ // preserveMarkdown: boolean = true;
34
+ _noteCounter = 0;
35
+ /**
36
+ * Parses the specified USX content.
37
+ *
38
+ * @param usx The USX content to parse.
39
+ * @returns The parse tree that was generated.
40
+ */
41
+ parse(codex) {
42
+ const json = JSON.parse(codex);
43
+ const data = exports.codexSchema.parse(json);
44
+ let root = {
45
+ type: 'root',
46
+ content: []
47
+ };
48
+ let chapters = new Map();
49
+ let lastChapter = null;
50
+ function addChapterContent(chapterNumber, content) {
51
+ let chapter = chapters.get(chapterNumber);
52
+ if (!chapter) {
53
+ chapter = {
54
+ type: 'chapter',
55
+ number: chapterNumber,
56
+ content: [],
57
+ footnotes: []
58
+ };
59
+ lastChapter = chapter;
60
+ chapters.set(chapterNumber, chapter);
61
+ }
62
+ chapter.content.push(...content);
63
+ }
64
+ function addReference(ref, content) {
65
+ addChapterContent(ref.chapter, [
66
+ {
67
+ type: 'verse',
68
+ number: ref.verse,
69
+ content: content
70
+ }
71
+ ]);
72
+ }
73
+ for (let cell of data.cells) {
74
+ if (cell.language === 'scripture') {
75
+ const lines = cell.value.split('\n');
76
+ const references = lines.map(l => (0, utils_1.parseVerseReference)(l) ?? l);
77
+ let currentRef = null;
78
+ let content = [];
79
+ for (let i = 0; i < references.length; i++) {
80
+ const ref = references[i];
81
+ const nextRef = references[i + 1];
82
+ if (typeof ref === 'string') {
83
+ // continuation on existing verse
84
+ // or line break between verses
85
+ if (ref === '') {
86
+ // line break
87
+ if (typeof nextRef === 'object') {
88
+ // between verses
89
+ if (currentRef && currentRef.chapter === nextRef.chapter) {
90
+ // in same chapter
91
+ addChapterContent(currentRef.chapter, [
92
+ {
93
+ type: 'line_break',
94
+ }
95
+ ]);
96
+ }
97
+ else {
98
+ // do nothing since line breaks between chapters are ignored
99
+ }
100
+ }
101
+ else {
102
+ // inside verse
103
+ content.push({
104
+ lineBreak: true
105
+ });
106
+ }
107
+ }
108
+ else if (ref) {
109
+ content.push(ref);
110
+ }
111
+ }
112
+ else {
113
+ if (!root.id) {
114
+ root.id = ref.book;
115
+ }
116
+ // new verse
117
+ if (currentRef) {
118
+ // finish previous verse
119
+ currentRef = null;
120
+ content = [];
121
+ }
122
+ currentRef = ref;
123
+ if (ref.content) {
124
+ content.push(ref.content);
125
+ }
126
+ addReference(ref, content);
127
+ }
128
+ }
129
+ }
130
+ else if (cell.language === 'markdown') {
131
+ if (cell.metadata) {
132
+ if (cell.metadata.type === 'chapter-heading') {
133
+ const metadata = exports.metadataSchema.parse(cell.metadata);
134
+ const chapter = metadata.data.chapter;
135
+ addChapterContent(chapter, [
136
+ {
137
+ type: 'heading',
138
+ content: [cell.value]
139
+ }
140
+ ]);
141
+ }
142
+ }
143
+ else {
144
+ if (lastChapter) {
145
+ lastChapter.footnotes.push({
146
+ noteId: this._noteCounter++,
147
+ text: cell.value,
148
+ caller: null,
149
+ });
150
+ }
151
+ }
152
+ }
153
+ }
154
+ for (let chapter of chapters.values()) {
155
+ root.content.push(chapter);
156
+ }
157
+ return root;
158
+ }
159
+ }
160
+ exports.CodexParser = CodexParser;
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=codex-parser.spec.d.ts.map