@helloao/tools 0.0.5 → 0.0.6
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/LICENSE +21 -21
- package/README.md +98 -98
- package/generation/api.d.ts +14 -2
- package/generation/api.js +17 -9
- package/generation/api.spec.js +81 -117
- package/generation/dataset.d.ts +1 -1
- package/generation/dataset.js +5 -5
- package/package.json +1 -2
- package/parser/iterators.spec.js +45 -45
- package/parser/usfm-parser.spec.js +171 -171
- package/parser/usx-parser.d.ts +2 -2
- package/parser/usx-parser.js +92 -31
- package/parser/usx-parser.spec.js +487 -487
- package/typings/types.d.ts +13 -13
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2022 AO Lab
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 AO Lab
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,98 +1,98 @@
|
|
|
1
|
-
## Hello AO Tools
|
|
2
|
-
|
|
3
|
-
Tools for the [Free Use Bible API](https://bible.helloao.org).
|
|
4
|
-
|
|
5
|
-
### Features
|
|
6
|
-
|
|
7
|
-
- Parse [USFM](https://ubsicap.github.io/usfm/), [USX](https://ubsicap.github.io/usx/), and Codex (JSON) files and understand some basic structure.
|
|
8
|
-
- Generate JSON from USFM, USX, and other formats.
|
|
9
|
-
|
|
10
|
-
### Installation
|
|
11
|
-
|
|
12
|
-
```
|
|
13
|
-
$ npm install @helloao/tools
|
|
14
|
-
```
|
|
15
|
-
|
|
16
|
-
### Usage
|
|
17
|
-
|
|
18
|
-
#### Parse a USX File
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
const
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
// The ID of the translation
|
|
41
|
-
// this should be unique for the translation
|
|
42
|
-
id: 'my translation id',
|
|
43
|
-
|
|
44
|
-
// The name of the translation in the translation's language
|
|
45
|
-
name: 'my translation name',
|
|
46
|
-
|
|
47
|
-
// The name of the translation in English
|
|
48
|
-
englishName: 'my translation name',
|
|
49
|
-
|
|
50
|
-
// The website that hosts information about the translation
|
|
51
|
-
website: 'translation website',
|
|
52
|
-
|
|
53
|
-
// The URL that hosts information about the license that the
|
|
54
|
-
// translation is shared under
|
|
55
|
-
licenseUrl: 'translation license',
|
|
56
|
-
|
|
57
|
-
// The ISO 639 letter language tag that the translation is primarily in.
|
|
58
|
-
language: 'eng',
|
|
59
|
-
|
|
60
|
-
// The direction that the text is written in.
|
|
61
|
-
// "ltr" means "left to right" and "rtl" means "right to left"
|
|
62
|
-
direction: 'ltr',
|
|
63
|
-
}
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
// The list of files that should be processed.
|
|
67
|
-
const files: generation.InputFile[] = [
|
|
68
|
-
{
|
|
69
|
-
// the metadata about the translation for this file
|
|
70
|
-
metadata: translation,
|
|
71
|
-
|
|
72
|
-
// The content contained in the file
|
|
73
|
-
content: 'YOUR USX',
|
|
74
|
-
|
|
75
|
-
// The type of the file.
|
|
76
|
-
// One of "usx", "usfm", and "json"
|
|
77
|
-
fileType: 'usx'
|
|
78
|
-
}
|
|
79
|
-
];
|
|
80
|
-
|
|
81
|
-
// Generate a dataset from the files
|
|
82
|
-
// Datasets organize all the files and their content
|
|
83
|
-
// by translation, book, chapter, and verse
|
|
84
|
-
const dataset = generation.dataset.generateDataset(files, domParser);
|
|
85
|
-
|
|
86
|
-
// Generate an API representation from the files
|
|
87
|
-
// This adds links between chapters and additional metadata.
|
|
88
|
-
const api = generation.api.generateApiForDataset(dataset);
|
|
89
|
-
|
|
90
|
-
// Generate output files from the API representation.
|
|
91
|
-
// This will give us a list of files and file paths that represent
|
|
92
|
-
// the entire API.
|
|
93
|
-
const outputFiles = generation.api.generateFilesForApi(api);
|
|
94
|
-
|
|
95
|
-
for (let file of outputFiles) {
|
|
96
|
-
console.log(file.path, file.content);
|
|
97
|
-
}
|
|
98
|
-
```
|
|
1
|
+
## Hello AO Tools
|
|
2
|
+
|
|
3
|
+
Tools for the [Free Use Bible API](https://bible.helloao.org).
|
|
4
|
+
|
|
5
|
+
### Features
|
|
6
|
+
|
|
7
|
+
- Parse [USFM](https://ubsicap.github.io/usfm/), [USX](https://ubsicap.github.io/usx/), and Codex (JSON) files and understand some basic structure.
|
|
8
|
+
- Generate JSON from USFM, USX, and other formats.
|
|
9
|
+
|
|
10
|
+
### Installation
|
|
11
|
+
|
|
12
|
+
```
|
|
13
|
+
$ npm install @helloao/tools
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
### Usage
|
|
17
|
+
|
|
18
|
+
#### Parse a USX File
|
|
19
|
+
|
|
20
|
+
```typescript
|
|
21
|
+
import { parser } from '@helloao/tools';
|
|
22
|
+
// Used to parse XML
|
|
23
|
+
const parser = new DOMParser();
|
|
24
|
+
const usx = new parser.USXParser(parser);
|
|
25
|
+
const parseTree = usx.parse('YOUR USX');
|
|
26
|
+
console.log(parseTree);
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
#### Generate the API Files for a translation
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { parser, generation } from '@helloao/tools';
|
|
33
|
+
|
|
34
|
+
// Used to parse XML
|
|
35
|
+
const domParser = new DOMParser();
|
|
36
|
+
|
|
37
|
+
// Each input file needs some metadata about the translation that it is associated with
|
|
38
|
+
const translation: generation.ParseTreeMetadata = {
|
|
39
|
+
translation: {
|
|
40
|
+
// The ID of the translation
|
|
41
|
+
// this should be unique for the translation
|
|
42
|
+
id: 'my translation id',
|
|
43
|
+
|
|
44
|
+
// The name of the translation in the translation's language
|
|
45
|
+
name: 'my translation name',
|
|
46
|
+
|
|
47
|
+
// The name of the translation in English
|
|
48
|
+
englishName: 'my translation name',
|
|
49
|
+
|
|
50
|
+
// The website that hosts information about the translation
|
|
51
|
+
website: 'translation website',
|
|
52
|
+
|
|
53
|
+
// The URL that hosts information about the license that the
|
|
54
|
+
// translation is shared under
|
|
55
|
+
licenseUrl: 'translation license',
|
|
56
|
+
|
|
57
|
+
// The ISO 639 letter language tag that the translation is primarily in.
|
|
58
|
+
language: 'eng',
|
|
59
|
+
|
|
60
|
+
// The direction that the text is written in.
|
|
61
|
+
// "ltr" means "left to right" and "rtl" means "right to left"
|
|
62
|
+
direction: 'ltr',
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// The list of files that should be processed.
|
|
67
|
+
const files: generation.InputFile[] = [
|
|
68
|
+
{
|
|
69
|
+
// the metadata about the translation for this file
|
|
70
|
+
metadata: translation,
|
|
71
|
+
|
|
72
|
+
// The content contained in the file
|
|
73
|
+
content: 'YOUR USX',
|
|
74
|
+
|
|
75
|
+
// The type of the file.
|
|
76
|
+
// One of "usx", "usfm", and "json"
|
|
77
|
+
fileType: 'usx',
|
|
78
|
+
},
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
// Generate a dataset from the files
|
|
82
|
+
// Datasets organize all the files and their content
|
|
83
|
+
// by translation, book, chapter, and verse
|
|
84
|
+
const dataset = generation.dataset.generateDataset(files, domParser);
|
|
85
|
+
|
|
86
|
+
// Generate an API representation from the files
|
|
87
|
+
// This adds links between chapters and additional metadata.
|
|
88
|
+
const api = generation.api.generateApiForDataset(dataset);
|
|
89
|
+
|
|
90
|
+
// Generate output files from the API representation.
|
|
91
|
+
// This will give us a list of files and file paths that represent
|
|
92
|
+
// the entire API.
|
|
93
|
+
const outputFiles = generation.api.generateFilesForApi(api);
|
|
94
|
+
|
|
95
|
+
for (let file of outputFiles) {
|
|
96
|
+
console.log(file.path, file.content);
|
|
97
|
+
}
|
|
98
|
+
```
|
package/generation/api.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { OutputFile, Translation, TranslationBook, TranslationBookChapter, TranslationBookChapterAudioLinks } from
|
|
2
|
-
import { DatasetOutput } from
|
|
1
|
+
import { OutputFile, Translation, TranslationBook, TranslationBookChapter, TranslationBookChapterAudioLinks } from './common-types';
|
|
2
|
+
import { DatasetOutput } from './dataset';
|
|
3
3
|
/**
|
|
4
4
|
* Defines the output of the API generation.
|
|
5
5
|
*/
|
|
@@ -189,6 +189,18 @@ export interface GenerateApiOptions {
|
|
|
189
189
|
* Defaults to false.
|
|
190
190
|
*/
|
|
191
191
|
generateAudioFiles?: boolean;
|
|
192
|
+
/**
|
|
193
|
+
* Gets the english name of the given language.
|
|
194
|
+
* If not provided, then the english name for the language will be unknown and omitted.
|
|
195
|
+
* @param language The language to get the english name for.
|
|
196
|
+
*/
|
|
197
|
+
getEnglishName?: (language: string) => string | null | undefined;
|
|
198
|
+
/**
|
|
199
|
+
* Gets the native name of the given language.
|
|
200
|
+
* If not provided, then the native name for the language will be unknown and omitted.
|
|
201
|
+
* @param language The language to get the native name for.
|
|
202
|
+
*/
|
|
203
|
+
getNativeName?: (language: string) => string | null | undefined;
|
|
192
204
|
/**
|
|
193
205
|
* The prefix that should be added to paths that are generated.
|
|
194
206
|
*/
|
package/generation/api.js
CHANGED
|
@@ -9,7 +9,6 @@ exports.bookChapterAudioApiLink = bookChapterAudioApiLink;
|
|
|
9
9
|
exports.jsonFile = jsonFile;
|
|
10
10
|
exports.downloadedFile = downloadedFile;
|
|
11
11
|
exports.replaceSpacesWithUnderscores = replaceSpacesWithUnderscores;
|
|
12
|
-
const all_iso_language_codes_1 = require("all-iso-language-codes");
|
|
13
12
|
/**
|
|
14
13
|
* Generates the API output for the given dataset.
|
|
15
14
|
* @param dataset The dataset to generate the API for.
|
|
@@ -25,8 +24,10 @@ function generateApiForDataset(dataset, options = {}) {
|
|
|
25
24
|
translationBooks: [],
|
|
26
25
|
translationBookChapters: [],
|
|
27
26
|
translationBookChapterAudio: [],
|
|
28
|
-
pathPrefix: apiPathPrefix
|
|
27
|
+
pathPrefix: apiPathPrefix,
|
|
29
28
|
};
|
|
29
|
+
const getNativeName = options.getNativeName;
|
|
30
|
+
const getEnglishName = options.getEnglishName;
|
|
30
31
|
for (let { books, ...translation } of dataset.translations) {
|
|
31
32
|
const apiTranslation = {
|
|
32
33
|
...translation,
|
|
@@ -35,8 +36,12 @@ function generateApiForDataset(dataset, options = {}) {
|
|
|
35
36
|
numberOfBooks: books.length,
|
|
36
37
|
totalNumberOfChapters: 0,
|
|
37
38
|
totalNumberOfVerses: 0,
|
|
38
|
-
languageName:
|
|
39
|
-
|
|
39
|
+
languageName: getNativeName
|
|
40
|
+
? getNativeName(translation.language) ?? undefined
|
|
41
|
+
: undefined,
|
|
42
|
+
languageEnglishName: getEnglishName
|
|
43
|
+
? getEnglishName(translation.language) ?? undefined
|
|
44
|
+
: undefined,
|
|
40
45
|
};
|
|
41
46
|
const translationBooks = {
|
|
42
47
|
translation: apiTranslation,
|
|
@@ -94,12 +99,15 @@ function generateApiForDataset(dataset, options = {}) {
|
|
|
94
99
|
}
|
|
95
100
|
for (let i = 0; i < translationChapters.length; i++) {
|
|
96
101
|
if (i > 0) {
|
|
97
|
-
translationChapters[i].previousChapterApiLink =
|
|
98
|
-
|
|
102
|
+
translationChapters[i].previousChapterApiLink =
|
|
103
|
+
bookChapterApiLink(translation.id, getBookLink(translationChapters[i - 1].book), translationChapters[i - 1].chapter.number, 'json', apiPathPrefix);
|
|
104
|
+
translationChapters[i].previousChapterAudioLinks =
|
|
105
|
+
translationChapters[i - 1].thisChapterAudioLinks;
|
|
99
106
|
}
|
|
100
107
|
if (i < translationChapters.length - 1) {
|
|
101
108
|
translationChapters[i].nextChapterApiLink = bookChapterApiLink(translation.id, getBookLink(translationChapters[i + 1].book), translationChapters[i + 1].chapter.number, 'json', apiPathPrefix);
|
|
102
|
-
translationChapters[i].nextChapterAudioLinks =
|
|
109
|
+
translationChapters[i].nextChapterAudioLinks =
|
|
110
|
+
translationChapters[i + 1].thisChapterAudioLinks;
|
|
103
111
|
}
|
|
104
112
|
}
|
|
105
113
|
api.availableTranslations.translations.push(apiTranslation);
|
|
@@ -165,13 +173,13 @@ function jsonFile(path, content, mergable) {
|
|
|
165
173
|
return {
|
|
166
174
|
path,
|
|
167
175
|
content,
|
|
168
|
-
mergable
|
|
176
|
+
mergable,
|
|
169
177
|
};
|
|
170
178
|
}
|
|
171
179
|
function downloadedFile(path, url) {
|
|
172
180
|
return {
|
|
173
181
|
path,
|
|
174
|
-
content: () => fetch(url).then(response => response.body),
|
|
182
|
+
content: () => fetch(url).then((response) => response.body),
|
|
175
183
|
};
|
|
176
184
|
}
|
|
177
185
|
function replaceSpacesWithUnderscores(str) {
|