forked from thesunshade/sutta-index
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnBuild.js
224 lines (206 loc) · 6.25 KB
/
mnBuild.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
const fs = require("fs");
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;
};
}
let data;
try {
const tsvFileContents = fs.readFileSync("./src/private/MN-index-wisdom-cleaned.txt", "utf8");
data = tsvFileContents;
console.log("successfully read");
} catch (err) {
console.log("There was an error");
console.error(err);
}
// build the index object
let lines = data.split("\n");
let array = [];
for (let i = 0; i < lines.length; i++) {
const line = lines[i].replace(/\r/, "");
array[i] = [line.substring(0, line.indexOf(",")), line.substring(line.indexOf(",") + 1).trim()];
}
// console.log(array);
const output = `export const mnArray =${JSON.stringify(array, null, 5)}`;
try {
fs.writeFileSync("./src/private/mn-array.js", output);
console.log("array written");
} catch (err) {
console.error(err);
}
// create "see also" table
let seeAlsoString = "";
for (let i = 0; i < array.length; i++) {
const headword = array[i][0];
const theRest = array[i][1];
if (theRest.match(/See also /)) {
const seeAlso = theRest.split("See also ");
seeAlsoString += headword + "\t" + seeAlso[1] + "\n";
}
}
try {
fs.writeFileSync("./src/private/mn-seeAlso.csv", seeAlsoString);
console.log("see also array written");
} catch (err) {
console.error(err);
}
//create without "see also"
let newArray = [];
for (let i = 0; i < array.length; i++) {
let headword = array[i][0];
// if (headword.match(/:/)) {
// console.log(headword);
// }
let theRest = array[i][1];
if (theRest.match(/See also /)) {
const seeAlso = theRest.split("See also ");
theRest = seeAlso[0];
}
const subheads = theRest.split(";");
for (let x = 0; x < subheads.length; x++) {
if (subheads[x].charAt(0) === " ") {
const subheadWord = subheads[x].substring(0, subheads[x].indexOf(","));
const subheadCitations = subheads[x].substring(subheads[x].indexOf(",") + 1).trim();
headword = headword.replace(/:.+$/, "");
newArray.push([headword + subheadWord, subheadCitations]);
} else newArray.push([headword, subheads[x]]);
}
}
const newArrayOutput = `export const newArray =${JSON.stringify(newArray, null, 5)}`;
try {
fs.writeFileSync("./src/private/mn-newArray.js", newArrayOutput);
console.log("new array written");
} catch (err) {
console.error(err);
}
// turn newArray into location array
let locationArray = [];
for (let i = 0; i < newArray.length; i++) {
const head = newArray[i][0];
const locations = newArray[i][1].split(", ");
for (let x = 0; x < locations.length; x++) {
locationArray.push(["MN" + locations[x].trim().replace(/\.$/, ""), head]);
}
}
const locationArrayString = `export const newArray =${JSON.stringify(locationArray.sort(natsort()), null, 5)}`;
console.log(locationArray.sort(natsort()));
try {
fs.writeFileSync("./src/private/mn-locationArray.js", locationArrayString);
console.log("new location array written");
} catch (err) {
console.error(err);
}
let locationsCsv = "";
for (let i = 0; i < locationArray.length; i++) {
locationsCsv += locationArray[i][0] + "\t" + locationArray[i][1] + "\r";
}
try {
fs.writeFileSync("./src/private/mn-locationArray.csv", locationsCsv);
console.log("new location array written");
} catch (err) {
console.error(err);
}