forked from thesunshade/sutta-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
606 lines (547 loc) · 18.2 KB
/
build.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
const { match } = require("assert");
const fs = require("fs");
let locatorFirstArray = [];
let rawIndexArray = [];
let alphabetKeys;
let xrefArray = [];
const newObject = {
A: {},
B: {},
C: {},
D: {},
E: {},
F: {},
G: {},
H: {},
I: {},
J: {},
K: {},
L: {},
M: {},
N: {},
O: {},
P: {},
Q: {},
R: {},
S: {},
T: {},
U: {},
V: {},
W: {},
Y: {},
Z: {},
};
function natsort(options) {
if (options === void 0) {
options = {};
}
var ore = /^0/;
var sre = /\s+/g;
var tre = /^\s+|\s+$/g;
// unicode
var ure = /[^\x00-\x80]/;
// hex
var hre = /^0x[0-9a-f]+$/i;
// numeric
var nre = /(0x[\da-fA-F]+|(^[\+\-]?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?(?=\D|\s|$))|\d+)/g;
// datetime
var dre = /(^([\w ]+,?[\w ]+)?[\w ]+,?[\w ]+\d+:\d+(:\d+)?[\w ]?|^\d{1,4}[\/\-]\d{1,4}[\/\-]\d{1,4}|^\w+, \w+ \d+, \d{4})/; // tslint:disable-line
var toLowerCase = String.prototype.toLocaleLowerCase || String.prototype.toLowerCase;
var GREATER = options.desc ? -1 : 1;
var SMALLER = -GREATER;
var normalize = options.insensitive
? function (s) {
return toLowerCase.call("".concat(s)).replace(tre, "");
}
: function (s) {
return "".concat(s).replace(tre, "");
};
function tokenize(s) {
return s.replace(nre, "\0$1\0").replace(/\0$/, "").replace(/^\0/, "").split("\0");
}
function parse(s, l) {
// normalize spaces; find floats not starting with '0',
// string or 0 if not defined (Clint Priest)
return ((!s.match(ore) || l === 1) && parseFloat(s)) || s.replace(sre, " ").replace(tre, "") || 0;
}
return function (a, b) {
// trim pre-post whitespace
var aa = normalize(a);
var bb = normalize(b);
// return immediately if at least one of the values is empty.
// empty string < any others
if (!aa && !bb) {
return 0;
}
if (!aa && bb) {
return SMALLER;
}
if (aa && !bb) {
return GREATER;
}
// tokenize: split numeric strings and default strings
var aArr = tokenize(aa);
var bArr = tokenize(bb);
// hex or date detection
var aHex = aa.match(hre);
var bHex = bb.match(hre);
var av = aHex && bHex ? parseInt(aHex[0], 16) : aArr.length !== 1 && Date.parse(aa);
var bv = aHex && bHex ? parseInt(bHex[0], 16) : (av && bb.match(dre) && Date.parse(bb)) || null;
// try and sort Hex codes or Dates
if (bv) {
if (av === bv) {
return 0;
}
if (av < bv) {
return SMALLER;
}
if (av > bv) {
return GREATER;
}
}
var al = aArr.length;
var bl = bArr.length;
// handle numeric strings and default strings
for (var i = 0, l = Math.max(al, bl); i < l; i += 1) {
var af = parse(aArr[i] || "", al);
var bf = parse(bArr[i] || "", bl);
// handle numeric vs string comparison.
// numeric < string
if (isNaN(af) !== isNaN(bf)) {
return isNaN(af) ? GREATER : SMALLER;
}
// if unicode use locale comparison
if (ure.test(af + bf) && af.localeCompare) {
var comp = af.localeCompare(bf);
if (comp > 0) {
return GREATER;
}
if (comp < 0) {
return SMALLER;
}
if (i === l - 1) {
return 0;
}
}
if (af < bf) {
return SMALLER;
}
if (af > bf) {
return GREATER;
}
if ("".concat(af) < "".concat(bf)) {
return SMALLER;
}
if ("".concat(af) > "".concat(bf)) {
return GREATER;
}
}
return 0;
};
}
function sortCitationsList(citations) {
const orderedBooks = ["DN", "MN", "SN", "AN", "Kp", "Dhp", "Ud", "Iti", "Snp", "Vv", "Pv", "Thag", "Thig"];
const citationsObject = {
DN: [],
MN: [],
SN: [],
AN: [],
Kp: [],
Dhp: [],
Ud: [],
Iti: [],
Snp: [],
Vv: [],
Pv: [],
Thag: [],
Thig: [],
};
for (let i = 0; i < citations.length; i++) {
for (let x = 0; x < orderedBooks.length; x++) {
if (citations[i].match(RegExp(orderedBooks[x]))) {
citationsObject[orderedBooks[x]].push(citations[i]);
}
}
}
let bookSubList = [];
for (let i = 0; i < orderedBooks.length; i++) {
const book = orderedBooks[i];
const sortedCitations = citationsObject[book].sort(natsort());
bookSubList.push(sortedCitations);
}
bookSubList = bookSubList.flat();
if (citations.length > bookSubList.length) {
console.warn(`❌ There is an invalid ciation: ${citations[0]}`);
}
return bookSubList;
}
let alphabetGroupedObject = {
A: {},
B: {},
C: {},
D: {},
E: {},
F: {},
G: {},
H: {},
I: {},
J: {},
K: {},
L: {},
M: {},
N: {},
O: {},
P: {},
Q: {},
R: {},
S: {},
T: {},
U: {},
V: {},
W: {},
Y: {},
Z: {},
};
let csvData;
// read csv file
try {
const tsvFileContents = fs.readFileSync("./src/data/general-index.csv", "utf8");
csvData = tsvFileContents;
console.log("✅ successfully read");
} catch (err) {
console.log("❌There was an error reading");
console.error(err);
}
function makeArrayOfXrefs(rawIndexArray) {
for (let i = 0; i < rawIndexArray.length; i++) {
if (/xref/.test(rawIndexArray[i][2])) xrefArray.push(rawIndexArray[i][2].replace("xref ", "").replace("\r", ""));
}
}
// build the index object
function createIndexObject() {
let lines = csvData.split("\n");
for (let i = 0; i < lines.length; i++) {
rawIndexArray[i] = lines[i].split("\t");
}
// count total unique locators
const allLocatorsArray = [];
for (let i = 0; i < rawIndexArray.length - 1; i++) {
const locator = rawIndexArray[i][2].trim();
if (!locator.match(/xref/)) {
allLocatorsArray.push(locator);
}
}
const totalUniqueLocatorsLength = [...new Set(allLocatorsArray.flat())].length;
console.log(totalUniqueLocatorsLength);
try {
fs.writeFileSync("./src/data/uniqueLocators.js", `export const uniqueLocators =${totalUniqueLocatorsLength}`);
} catch (err) {
console.log("❌There was an error writing updateDate");
console.error(err);
}
function normalizeDiacriticString(string) {
return string
.normalize("NFD") /*separates diacritics from letter */
.replace(/[\u0300-\u036f]/g, ""); /*removes diacritic characters */
}
for (let i = 0; i < rawIndexArray.length - 1; i++) {
const head = rawIndexArray[i][0].trim();
const sub = rawIndexArray[i][1].trim();
const locator = rawIndexArray[i][2].trim();
const headStartingWithLetter = head.replace("“", "");
const firstRealLetter = normalizeDiacriticString(headStartingWithLetter.charAt(0)).toUpperCase();
if (head === "") {
console.error(`❌ @${i + 1} there is a blank headword! Sub: ${sub}, Locator: ${locator}`);
}
if (/xref/.test(head)) {
console.warn(`❌ The headword @${i + 1} "${head}" contains 'xref'`);
}
if (/["']/.test(sub + head)) console.warn(`❌ The sub/headword @${i + 1} ${head}/${sub} contains straight quotes
This may indicate that the csv file format was incorrect`);
if (!alphabetGroupedObject[firstRealLetter].hasOwnProperty(head)) {
// the key of the headword does not exist in the object yet, so create the key and add the locator-xref object
alphabetGroupedObject[firstRealLetter][head] = { [sub]: { locators: [], xrefs: [] } };
if (/xref/.test(locator)) {
alphabetGroupedObject[firstRealLetter][head][sub].xrefs.push(locator);
} else {
alphabetGroupedObject[firstRealLetter][head][sub].locators.push(locator);
}
} else {
if (!alphabetGroupedObject[firstRealLetter][head].hasOwnProperty(sub)) {
// the key for the headword exists, but the sub does not exist as a key
alphabetGroupedObject[firstRealLetter][head][sub] = { locators: [], xrefs: [] };
if (/xref/.test(locator)) {
alphabetGroupedObject[firstRealLetter][head][sub].xrefs.push(locator);
} else {
alphabetGroupedObject[firstRealLetter][head][sub].locators.push(locator);
}
} else {
// the head and sub already exist, so the locator must be pushed into the array
if (/xref/.test(locator)) {
alphabetGroupedObject[firstRealLetter][head][sub].xrefs.push(locator);
} else {
alphabetGroupedObject[firstRealLetter][head][sub].locators.push(locator);
}
}
}
}
// sort locators
const alphabetArray = Object.keys(alphabetGroupedObject);
for (let a = 0; a < alphabetArray.length; a++) {
const headwords = Object.keys(alphabetGroupedObject[alphabetArray[a]]);
const alphabetObject = alphabetGroupedObject[alphabetArray[a]];
for (let i = 0; i < headwords.length; i++) {
const subs = Object.keys(alphabetObject[headwords[i]]);
const headWordObject = alphabetObject[headwords[i]];
for (let x = 0; x < subs.length; x++) {
headWordObject[subs[x]].locators = sortCitationsList(headWordObject[subs[x]].locators);
}
for (let x = 0; x < subs.length; x++) {
headWordObject[subs[x]].xrefs.sort();
}
}
}
function sortedKeys(object) {
return Object.keys(object).sort((a, b) => {
a = a.replace("“", "");
b = b.replace("“", "");
return a.localeCompare(b, undefined, { sensitivity: "base" });
});
}
alphabetKeys = Object.keys(newObject);
for (let i = 0; i < alphabetKeys.length; i++) {
const unsortHeadwObj = alphabetGroupedObject[alphabetKeys[i]];
const sortedHeadwObjArr = sortedKeys(unsortHeadwObj);
for (let x = 0; x < sortedHeadwObjArr.length; x++) {
newObject[alphabetKeys[i]][sortedHeadwObjArr[x]] = alphabetGroupedObject[alphabetKeys[i]][sortedHeadwObjArr[x]];
}
}
function getRootHeadword(headword) {
return headword.replace(/ \(.+?\)/, "");
}
for (let i = 0; i < alphabetKeys.length; i++) {
const letter = alphabetKeys[i];
const headwords = Object.keys(newObject[letter]);
let counter = 1;
for (let x = 0; x < headwords.length - 1; x++) {
const currentHeadword = headwords[x];
const currentRootHeadword = getRootHeadword(headwords[x]);
const nextRootHeadword = getRootHeadword(headwords[x + 1]);
if (currentRootHeadword === nextRootHeadword) {
newObject[letter][currentHeadword].counter_value = counter;
counter++;
} else if (x > 0) {
const previousRootHeadword = getRootHeadword(headwords[x - 1]);
if (currentRootHeadword === previousRootHeadword) {
newObject[letter][currentHeadword].counter_value = counter;
counter = 1;
}
}
}
}
// count number of unique locators per headword
let locatorCountHeadwordsList = [];
const alphabetList = Object.keys(newObject);
for (let i = 0; i < alphabetList.length; i++) {
const letter = alphabetList[i];
const letterObject = newObject[letter];
const headwordArray = Object.keys(letterObject);
// console.log(letterObject[headwordArray[0]]);
for (let x = 0; x < headwordArray.length; x++) {
const headword = headwordArray[x];
// console.log("HEADWORD: " + headword);
let allLocators = [];
const headwordObject = letterObject[headword];
const subheadArray = Object.keys(headwordObject);
for (let y = 0; y < subheadArray.length; y++) {
const subhead = subheadArray[y];
// console.log("SUBHEAD: " + subhead);
const subheadObject = headwordObject[subhead];
allLocators.push(subheadObject.locators);
}
const uniqueLocatorsLength = [...new Set(allLocators.flat())].length;
// console.log(headword + ": " + uniqueLocatorsLength);
if (uniqueLocatorsLength > 0) {
locatorCountHeadwordsList.push([headword, uniqueLocatorsLength]);
}
}
}
locatorCountHeadwordsList.sort(function (a, b) {
return a[1] - b[1];
});
locatorCountHeadwordsList.reverse();
const object = `export const indexObject =${JSON.stringify(newObject, null, 5)}`;
try {
fs.writeFileSync("./src/data/index-object.js", object);
console.log("✅ indexObject written");
} catch (err) {
console.log("❌There was an error writing indexObject");
console.error(err);
}
const headwordLocatorCount = `export const indexObject =${JSON.stringify(locatorCountHeadwordsList, null, 5)}`;
try {
fs.writeFileSync("./src/data/headwordLocatorCount.js", headwordLocatorCount);
console.log("✅ headwordLocatorCount written");
} catch (err) {
console.log("❌There was an error writing headwordLocatorCount");
console.error(err);
}
let headwordLocatorCountHTML = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" type="image/png" sizes="32x32" href="favicon-table.png">
<title>Headwords sorted by number of locators</title>
<style>
.table{
display:flex;
flex-direction:column;
max-width:20rem;
border-top:solid 1px black;
font-family:Arial, Helvetica, sans-serif
}
.row{
display:flex;
flex-direction:row;
border-bottom:solid 1px black;
justify-content: space-between;
padding:.2rem .5rem;
border-left:solid 1px black;
border-right:solid 1px black;
}
.row:nth-child(even) {background: #ee7121}
.row:nth-child(odd) {background: #fff9f1}
</style>
</head>
<body>
<div>The following is the number of unique locators (i.e. citations) that each headword has (across all subheads). Headwords without actual locators are not included.</div>`;
headwordLocatorCountHTML += `<div class="table">`;
for (let i = 0; i < locatorCountHeadwordsList.length; i++) {
headwordLocatorCountHTML += `
<div class="row">
<div class="headword">${locatorCountHeadwordsList[i][0]}</div>
<div class="count">${locatorCountHeadwordsList[i][1]}</div>
</div>`;
}
headwordLocatorCountHTML += `
<div>
</body>`;
try {
fs.writeFileSync("./public/locatorCountTable.html", headwordLocatorCountHTML);
console.log("✅ headwordLocatorCountHTML written");
} catch (err) {
console.log("❌There was an error writing headwordLocatorCountHTML");
console.error(err);
}
}
function createHeadingsArray() {
let listOfHeadwords = [];
for (let i = 0; i < alphabetKeys.length; i++) {
const headwords = Object.keys(newObject[alphabetKeys[i]]);
listOfHeadwords.push(headwords);
}
listOfHeadwords = listOfHeadwords.flat();
const headwordsArray = `export const headwordsArray =${JSON.stringify(listOfHeadwords, null, 5)}`;
try {
fs.writeFileSync("./src/data/headwords-array.js", headwordsArray);
console.log("✅ headwordsArray written");
} catch (err) {
console.log("❌There was an error writing headwordsArray");
console.error(err);
}
makeArrayOfXrefs(rawIndexArray);
//go through xrefArray and make sure that each one appears in the list of headwords
for (let i = 0; i < xrefArray.length; i++) {
if (!headwordsArray.includes(xrefArray[i].trim())) {
console.log(`❌ ${xrefArray[i]} is not a valid xref`);
}
}
}
function createLocatorSortedArray() {
for (let i = 0; i < rawIndexArray.length - 1; i++) {
if (!/xref/.test(rawIndexArray[i][2])) {
locatorFirstArray.push([rawIndexArray[i][2].replace(/\r/, ""), rawIndexArray[i][0], rawIndexArray[i][1]]);
}
}
locatorFirstArray = locatorFirstArray.sort(natsort());
// test for blank locator field
for (let i = 0; i < locatorFirstArray.length; i++) {
if (locatorFirstArray[i][0] === "") {
console.error(`❌ Missing Locator, Head: ${locatorFirstArray[i][1]}; Sub: ${locatorFirstArray[i][2] ? locatorFirstArray[i][2] : "blank"}`);
}
if (!/(DN|MN|SN|AN|Kp|Dhp|Ud|Iti|Snp|Vv|Pv|Thag|Thig|xref)/.test(locatorFirstArray[i][0])) {
console.error(`❌ Bad citation or xref:${locatorFirstArray[i][0] ? locatorFirstArray[i][0] : "blank"}; Head: ${locatorFirstArray[i][1]}; Sub: ${locatorFirstArray[i][2] ? locatorFirstArray[i][2] : "blank"}`);
}
}
// test for blank subheads
let blankSubheads = 0;
for (let i = 0; i < locatorFirstArray.length; i++) {
if (locatorFirstArray[i][2] === "") blankSubheads++;
}
const array = `export const indexArray =${JSON.stringify(locatorFirstArray, null, 5)}`;
try {
fs.writeFileSync("./src/data/index-array.js", array);
console.log(`✅ indexArray written with ⫷ ${blankSubheads}⫸ blank subheads`);
} catch (err) {
console.log("❌There was an error writing indexArray");
console.error(err);
}
}
function createLocatorSortedObject() {
const locatorObject = {};
for (let i = 0; i < locatorFirstArray.length; i++) {
if (locatorObject.hasOwnProperty(locatorFirstArray[i][0])) {
locatorObject[locatorFirstArray[i][0]].push(locatorFirstArray[i][1] + ", " + locatorFirstArray[i][2]);
} else {
locatorObject[locatorFirstArray[i][0]] = [locatorFirstArray[i][1] + ", " + locatorFirstArray[i][2]];
}
}
const locatorBookObject = {
DN: [],
MN: [],
SN: [],
AN: [],
Kp: [],
Dhp: [],
Ud: [],
Iti: [],
Snp: [],
Vv: [],
Pv: [],
Thag: [],
Thig: [],
};
function findBook(citation) {
return citation.match(/(DN|MN|SN|AN|Kp|Dhp|Ud|Iti|Snp|Vv|Pv|Thag|Thig|xref)/)[0];
}
for (let i = 0; i < locatorFirstArray.length; i++) {
const book = findBook(locatorFirstArray[i][0]);
locatorBookObject[book].push(locatorFirstArray[i]);
}
const locatorBookObjectString = `export const locatorBookObject =${JSON.stringify(locatorBookObject, null, 2)}`;
try {
fs.writeFileSync("./src/data/locator-book-object.js", locatorBookObjectString);
console.log("✅ locatorBookObject written");
} catch (err) {
console.log("❌There was an error writing locatorBookObject");
console.error(err);
}
}
createIndexObject();
createLocatorSortedArray();
createHeadingsArray();
createLocatorSortedObject();
const currentDate = new Date().toLocaleString("en-gb", {
year: "numeric",
month: "long",
day: "numeric",
});
const currentTime = new Date().toLocaleTimeString([], { hour: "numeric", minute: "2-digit", hour12: true });
try {
fs.writeFileSync("./src/data/updateDate.js", `export const updateDate ="${currentDate + ", " + currentTime}"`);
} catch (err) {
console.log("❌There was an error writing updateDate");
console.error(err);
}