-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.mjs
435 lines (377 loc) · 13.6 KB
/
compile.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
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
/*
Step 1
On this page https://berean.bible/downloads.htm download "Berean Standard Bible - eBible (.epub)" as bsb.epub
wget https://bereanbible.com/bsb.epub
Step 2
An epub is the same as a zip file, so unzip bsb.epub to the bsb directory
unzip bsb.epub -d bsb
Step 3
There should be many files in bsb/bsb - final - 7-18-21/OEBPS/Text
Parse those files, renaming them to be their book and chapter name like "1-chronicles-12.html" and put them into public/chapters
*/
import { readdir, writeFile, open } from "node:fs/promises";
import { parse } from "node-html-parser"; // https://www.npmjs.com/package/node-html-parser
import { words } from "./words.mjs";
import { isPlural } from "./yours.mjs";
//import { isPlural } from './pluralYous.mjs'
import { info } from "node:console";
parseChapters();
function upperFirst(word) {
return word[0].toUpperCase() + word.substring(1);
}
function upperCaseWords(body) {
const heavenHtml =
"<span class='nocap'>Heaven</span>" +
"<span class='cap'>heaven</span>" +
"<span class='bsb'>heaven</span>";
body = body.replace(/\bheaven\b/g, heavenHtml);
const earthHtml =
"<span class='nocap'>Earth</span>" +
"<span class='cap'>Earth</span>" +
"<span class='bsb'>earth</span>";
body = body.replace(/(?<!\bthe\s)\bearth\b/g, earthHtml);
const hellHtml =
"<span class='nocap'>Hell</span>" +
"<span class='cap'>Hell</span>" +
"<span class='bsb'>hell</span>";
body = body.replace(/\bhell\b/g, hellHtml);
return body;
}
async function parseChapters() {
console.log("Parsing BSB...");
const fromDir = "bsb/bsb - final - 7-18-21/OEBPS/Text";
const toDir = "public/chapters";
const filenames = await readdir(fromDir);
var foundWords = {};
for (const filename of filenames) {
const fromFile = await open(fromDir + "/" + filename);
var html = await fromFile.readFile();
fromFile.close();
var doc = parse(html);
const title = doc.querySelector("title").text;
if (!title.endsWith("BSB")) continue;
// Convert title to file name
var newFilename = title.substring(0, title.length - 4);
newFilename = newFilename.toLocaleLowerCase();
newFilename = newFilename.replaceAll(" ", "-");
var book = newFilename.substring(0, newFilename.lastIndexOf("-"));
var chapter = newFilename.substring(newFilename.lastIndexOf("-") + 1);
// Remove certain things
doc.querySelector("#topheading")?.remove();
doc.querySelector(".bsbheading")?.remove();
doc.querySelector(".calibre8")?.remove(); // The "Home" link on the bottom
// Remove tags
doc.querySelectorAll("a").forEach((el) => {
if (el.getAttribute("id") == "fn") return;
el.replaceWith(el.innerHTML);
});
// Save the file
var body = doc.querySelector("body").innerHTML;
// Footnotes begins
var footNotesAt = body.indexOf(
'<a class="pcalibre2 pcalibre1 pcalibre pcalibre3" id="fn">'
);
if (footNotesAt >= 0) {
body =
body.substring(0, footNotesAt) +
'<div class="fn">' +
body.substring(footNotesAt) +
"</div>";
}
// Spelling
for (var word in words) {
var regex = new RegExp("\\b(" + word + ")\\b", "gm");
var replacement = "";
replacement += "<span class='spell ca'>" + words[word].ca + "</span>";
replacement += "<span class='spell gb'>" + words[word].gb + "</span>";
replacement += "<span class='spell us'>" + words[word].us + "</span>";
body = body.replace(regex, replacement);
var wordCa = upperFirst(words[word].ca);
var wordGb = upperFirst(words[word].gb);
var wordUs = upperFirst(words[word].us);
regex = new RegExp("\\b(" + upperFirst(word) + ")\\b", "gm");
replacement = "";
replacement += "<span class='spell ca'>" + wordCa + "</span>";
replacement += "<span class='spell gb'>" + wordGb + "</span>";
replacement += "<span class='spell us'>" + wordUs + "</span>";
body = body.replace(regex, replacement);
}
// God's Name
body = body.replaceAll(
"The LORD",
'<span class="lord" data-name="The LORD">The LORD</span>'
);
body = body.replaceAll(
"the LORD",
'<span class="lord" data-name="the LORD">the LORD</span>'
);
body = body.replaceAll(
"O LORD",
'O <span class="lord" data-name="LORD">LORD</span>'
);
body = body.replaceAll(
", LORD",
', <span class="lord" data-name="LORD">LORD</span>'
);
body = body.replaceAll(
">LORD",
'><span class="lord" data-name="LORD">LORD</span>'
);
body = body.replaceAll(
"One LORD",
'One <span class="lord" data-name="LORD">LORD</span>'
);
body = body.replaceAll(
"mighty LORD",
'mighty <span class="lord" data-name="LORD">LORD</span>'
);
body = body.replaceAll(
"our LORD",
'our <span class="lord" data-name="LORD">LORD</span>'
);
body = body.replaceAll(
"THE LORD",
'<span class="caplord" data-name="THE LORD">THE LORD</span>'
);
// Capitalized Divine Names
// console.log(newFilename, `'${book}', '${chapter}'`)
// if (newFilename.startsWith("1-timothy")) {
// body = parseDivineNamesAndYalls(body, book, chapter)
// }
body = parseDivineNamesAndYalls(body, book, chapter);
// Make upper case words that should be upper case
body = upperCaseWords(body);
writeFile(toDir + "/" + newFilename + ".html", body);
}
// console.log("Cap words: " + Object.keys(reportedAllows).sort().map(w=>w+":"+reportedAllows[w]).join(", "))
// console.log("Cap words: " + Object.keys(reportedAllows).sort())
}
function isWordCharacter(char) {
if (char.toLowerCase() !== char.toUpperCase()) return true;
const chars = "0123456789-'";
if (chars.indexOf(char) >= 0) return true;
return false;
}
var reportedAllows = {};
var reportedBads = {};
function parseDivineNamesAndYalls(body, book, chapter) {
// console.log(book, chapter)
var beginning = true; // Expecting the beginning of a sentence? We expect a capital letter.
var inTag = false; // In a tag? We'll ignore everything here.
var inWord = false; // In a word?
// var inHeading = false
// var inRefText = false
var closingTag = false;
var verse = 0;
var tags = []; // a stack of tags. Each item is an array of words. Push and pop.
var index = 0;
var word = "";
var youCount = 0;
// prettier-ignore
var badCapitalWords = ["Spirit", "He", "His", "Us", "Our", "Most", "High", "Creator", "Oak", "You", "Me", "Him", "Almighty", "My", "Your", "Garden", "One", "Judge", "Wilderness", "Himself", "The", "Will", "Provide", "Myself", "Bring", "Book", "Feast", "Unleavened", "Bread", "Ten", "Commandments", "Covenant", "Ark", "Desert", "Feast", "Most", "Holy", "Place", "Is", "My", "Banner", "Law", "Meeting", "Mine", "Name", "Place", "Presence", "Tent", "Testimony", "Weeks", "Baby", "Baptist", "Beginning", "Being", "Beloved", "Branch", "Blessed", "Blood", "Breach", "Broad", "Brook", "Brothers", "Canal", "City", "Chosen", "Corner", "Days", "Day", "Dawn", "Daughter", "Destiny", "Destroy", "Eastern", "Dwelling", "Dung", "Dove", "Diviners", "Divine", "Distant", "Elevin", "Everlasting", "Excellency", "Fair", "Faithful", "Fast", "Father", "Favor", "Fear", "Field", "First", "Freedmen", "Fountain", "Forum", "Fortune", "Forsaken", "Forest", "Fool", "Folly", "Fish", "Gate", "Glory", "Goats", "Greater", "Great", "Inspection", "Land", "Light", "Life", "Magesty", "Lower", "Lawgiver", "Launderer", "Last", "Lion", "Lily", "Lilies", "Majestic", "Majesty", "Maker", "Messenger", "Messiah", "Mighty", "Middle", "Moon", "Moons", "Monument", "Morning", "Mountain", "Mysteries", "New", "Oaks", "Not", "Ovens", "Out", "Prophets", "Province", "Pool", "Prophet", "Prophets", "Protector", "Rabbi", "Righteous", "Righteousness", "Rock", "Rocks", "Root", "Salvation", "Salt", "Savior", "Saviour", "Scripture", "Scriptures", "Sea", "Second", "Seer", "Seers", "Serpent", "Servant", "Seven", "Sheep", "Shepherd", "Shepherds", "Son", "Song", "Songs", "Slaughter", "Skull", "Sought", "Sovereign", "Spirits", "Spring", "Star", "Still", "Stoic", "Stone", "Street", "Streets", "Strength", "Supper", "Teacher", "Taverns", "Thunder", "Three", "Thirty", "Their", "Tower", "Travelers", "Treatise", "Tower", "Twelve", "Twin", "True", "Truth", "Union", "Valley", "Word", "Yours", "Yourself"]
var replacements = []; // each item in array is an object with keys:
// "at" (index in string to start replacing),
// "length" (length of word to remove),
// "replacement" (the string to replace those characters with)
function processWord(thisWord, index) {
// console.log(beginning, thisWord, JSON.stringify(tags))
word = "";
if (tags.length > 0 && tags[tags.length - 1][2] == "reftext") {
// console.log("found verse ", thisWord)
verse = thisWord;
youCount = 0;
return;
}
if (Number.parseInt(thisWord) == thisWord) return;
if (
tags.length > 0 &&
(tags[tags.length - 1][2] == "fn" || tags[0][2] == "fn")
) {
return;
}
if (
tags.length > 0 &&
(tags[tags.length - 1][2] == "hdg" ||
tags[tags.length - 1][2] == "subhdg")
) {
beginning = true;
return;
}
if (tags.length > 0 && tags[tags.length - 1][2] == "cross1") {
beginning = true;
return;
}
var thisWordLower = thisWord.toLowerCase();
if (
thisWordLower == "you" ||
thisWordLower == "your" ||
thisWordLower == "yours"
) {
if (isPlural(book, chapter, verse, youCount)) {
replacements.push({
at: index - thisWord.length,
length: thisWord.length,
replacement:
"<span class='youpl' data-word='" +
thisWord +
"'>" +
thisWord +
"</span>",
});
}
youCount++;
}
if (thisWord.substring(0, 1) == thisWord.substring(0, 1).toUpperCase()) {
if (beginning) {
// All is good. It's a capital at the beginning of a sentence.
// console.log("Good capital word at beginning of sentence. beginning is now false.")
beginning = false;
} else {
// Capital not at beginning of sentence. Alert!
if (badCapitalWords.includes(thisWord)) {
replacements.push({
at: index - thisWord.length,
length: thisWord.length,
replacement:
"<span class='cap'>" +
thisWord +
"</span><span class='nocap'>" +
thisWord.toLowerCase() +
"</span><span class='bsb'>" +
thisWord +
"</span>",
});
if (reportedBads[thisWord] != undefined) {
reportedBads[thisWord] = reportedBads[thisWord] + 1;
} else {
// console.log(`Bad capital word found!!!!!!!!!!!!!!!!!!!!!! '${thisWord}'`)
reportedBads[thisWord] = 0;
}
} else {
if (reportedAllows[thisWord] != undefined) {
reportedAllows[thisWord] = reportedAllows[thisWord] + 1;
} else {
// console.log(`Capital, but we'll allow it: '${thisWord}'`)
reportedAllows[thisWord] = 0;
}
}
}
} else {
if (beginning) {
// Lower case at start of sentence. :(
// console.log("Lower case word at beginning of sentence. beginning is now false.")
beginning = false;
} else {
// Lower case in middle of sentence
}
}
}
for (index = 0; index < body.length; index++) {
var ch = body.substring(index, index + 1);
// console.log(`beginning is ${beginning}, inTag is ${inTag}, inWord is ${inWord} and we're looking at a '${ch}'`)
if (inTag) {
if (ch == ">") {
// console.log("end tag word: " + word)
if (closingTag && word == "p") {
// inHeading = false
beginning = true; // we just came across a <p> or </p> tag
}
// if (closingTag && word == "span") {
// inRefText = false
// }
if (closingTag) {
// console.log("should pop tag ", JSON.stringify(tags[tags.length-1]))
tags.pop();
}
// console.log(JSON.stringify(tags))
word = "";
inTag = false;
if (tags.length >= 1 && tags[tags.length - 1][0] == "br") {
// This was a <br> tag, so let's not store it.
// console.log("popping br")
tags.pop();
}
continue;
}
if (isWordCharacter(ch)) {
word = word + ch;
} else {
if (ch == "/") {
closingTag = true;
tags.pop(); // We just pushed a new empty tag, so we'll undo that here
}
if (word != "") {
// console.log("tag word: '" + word + "'")
// if (word == "reftext") {
// inRefText = true
// }
// if (word == "hdg" || word == "subhdg") {
// inHeading = true
// }
}
// console.log("Adding word '" + word + "'")
if (word != "") tags[tags.length - 1].push(word);
word = "";
}
continue;
}
if (ch == "<") {
if (word != "") processWord(word, index);
tags.push([]); // Yes, but maybe it's a closing tag, so we'll pop it if it is a closing tag
closingTag = false;
inTag = true;
continue;
}
if (ch == "“") {
// opening quotation mark
// inQuote = true
beginning = true;
}
if (ch == "‘") {
var previousCh = body.substring(index - 1, index);
if (previousCh == " " || previousCh == ">") {
// previous char was a space, so this must be the beginning of a quote
// console.log("in quote")
// inQuote = true
beginning = true;
} else {
// The previous char was a letter, so this is either an apostrophe, like in << haven't >>, or it is the end of quotation, like << yes', she said >>
// Either way we are not at the beginning
beginning = false;
}
}
/*
if (ch == "”") { // closing quotation mark
inQuote = false
beginning = false
}
*/
if (ch == "." || ch == "?" || ch == "!" || ch == ":") {
if (word != "") processWord(word, index);
beginning = true;
continue;
}
if (!inWord && isWordCharacter(ch)) {
word = ch;
inWord = true;
continue;
}
if (inWord && isWordCharacter(ch)) {
word = word + ch;
continue;
}
if (inWord && !isWordCharacter(ch)) {
if (word != "") processWord(word, index);
inWord = false;
continue;
}
}
for (var i = replacements.length - 1; i >= 0; i--) {
var rep = replacements[i];
body =
body.substring(0, rep.at) +
rep.replacement +
body.substring(rep.at + rep.length);
}
return body;
}