forked from mgmeyers/obsidian-pandoc-reference-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-locator-list.mjs
134 lines (120 loc) · 3.07 KB
/
make-locator-list.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import escape from 'escape-string-regexp';
import fs from 'fs';
import convert from 'xml-js';
function readFiles(dirname, onFileContent, onError, done) {
fs.readdir(dirname, function (err, filenames) {
if (err) {
onError(err);
return;
}
for (let filename of filenames) {
if (!filename.endsWith('.xml')) continue;
const content = fs.readFileSync(dirname + filename);
onFileContent(filename, convert.xml2js(content.toString()));
}
done();
});
}
const locatorTerms = [
'act',
'appendix',
'article-locator',
'canon',
'elocation',
'equation',
'rule',
'scene',
'table',
'timestamp',
'title-locator',
'book',
'chapter',
'column',
'figure',
'folio',
'issue',
'line',
'note',
'opus',
'page',
'number-of-pages',
'paragraph',
'part',
'section',
'sub-verbo',
'verse',
'volume',
];
const locatorMap = {};
locatorTerms.forEach((k) => {
locatorMap[k] = true;
});
const locators = new Set();
const locatorToTerm = {};
readFiles(
'../locales/',
function (filename, content) {
if (content && content.elements) {
const locale = content.elements.find((el) => el.name === 'locale');
const lang = locale.attributes['xml:lang'];
const langTerms = (locatorToTerm[lang] = {});
if (locale) {
const terms = locale.elements.find((el) => el.name === 'terms');
if (terms) {
terms.elements.forEach((term) => {
if (!term.attributes) return;
const name = term.attributes.name;
if (locatorMap[name]) {
term.elements.forEach((el) => {
if (el.type === 'text') {
locators.add(el.text);
if (!langTerms[el.text]) langTerms[el.text] = name;
} else {
el.elements?.forEach((el) => {
locators.add(el.text);
if (!langTerms[el.text]) langTerms[el.text] = name;
});
}
});
}
});
}
}
}
},
function (err) {
throw err;
},
function () {
let outStr = ['/* eslint-disable no-irregular-whitespace */'];
const p1 = Array.from(locators);
const optionalPeriods = new Set();
for (const l of p1) {
if (!/\./.test(l)) continue;
const stripped = l.replaceAll('.', '');
if (locators.has(stripped)) {
optionalPeriods.add(l);
locators.delete(stripped);
}
}
const sorted = Array.from(locators);
sorted.sort((a, b) => b.length - a.length);
outStr.push(
`export const locators = /^([ \\t]*)(${sorted
.map((v) => {
const escaped = escape(v);
if (optionalPeriods.has(v)) {
return escaped.replaceAll('\\.', '\\.?');
}
return escaped;
})
.join('|')})([ \\t]*)/;`
);
outStr.push(
`export const locatorToTerm: Record<string, Record<string, string>> = ${JSON.stringify(
locatorToTerm
)}`
);
fs.writeFileSync('./src/parser/locators.ts', outStr.join('\n'));
}
);