-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakeSuttaHtml.js
158 lines (135 loc) · 6.73 KB
/
makeSuttaHtml.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
export default function makeSuttaHtml(bookAbbreviation, paliData, transData, htmlData, referenceData, article, bookLength) {
let paliVerse = "<span class='pli-verse segment'>";
let englishVerse = "<span class='eng-verse segment'>";
let inAVerse = false;
let html = "";
let suttaNumber = article.match(/(\d*\.*\d*-*\d+)$/g)[0].replace("-", "–");
const includePali = JSON.parse(localStorage.pali);
function isHeading(htmlWrapper) {
if (/<h1 class='sutta-title'>/.test(htmlWrapper) || /<h1 class='range-title sutta-title'>/.test(htmlWrapper) || /<h1 class='range-title'>/.test(htmlWrapper)) {
return true;
} else {
return false;
}
}
function orderThePairs(englishFirst, englishSegment, paliSegment) {
let html = "";
if (englishFirst === "true") {
html += englishSegment + paliSegment;
} else {
html += paliSegment + englishSegment;
}
return html;
}
Object.keys(htmlData).forEach(section => {
let htmlWrapper = htmlData[section];
let reference = "";
console.log(article)
if (localStorage.includeRefrence==true) {
if (referenceData[section]) {
const referenceArray = referenceData[section].split(", ");
for (let i = 0; i < referenceArray.length; i++) {
if (/ms\d/.test(referenceArray[i])) {
reference = referenceArray[i];
}
}
}
}
// if Pali title and English title are identical, delete English
if (/<h/.test(htmlWrapper) && paliData[section] === transData[section]) {
if (includePali) {
transData[section] = "";
}
}
// If <j> is being converted to a class
if (localStorage.enjambment && /\<j\>/.test(transData[section])) {
console.log("found enjambment");
transData[section] = transData[section].replace(/\<j\>/, '<span class="enjambment">');
}
// OK, so the bit below is hard coded to always include the Pali in the headings and always remove the number from the English segment. The code was originally written with the assumption that when pali was not included in the text, then it should not be included in the headings.
//
// remove sutta number in heading within the Pali segment
if (isHeading(htmlWrapper)) {
if (includePali || isHeading(htmlWrapper)) {
// Pali is being included
//remove the number from inside paliData
paliData[section] = paliData[section].replace(/^\d*\.*\d*–*\d+\.* ([A-Za-zĀāīūñÑ])/, "$1");
// put the correct complete number on the front of the paliData
paliData[section] = `<span class="sutta-number">${suttaNumber}</span> ${paliData[section].trim()}: `;
} else {
// Pali is not being included so the sutta number must be added to English title
transData[section] = `<span class="sutta-number">${suttaNumber}</span> ${transData[section]}`;
}
}
// step down h1 to h2 etc
if (JSON.parse(localStorage.stepDown) === true) {
htmlWrapper = htmlWrapper.replace("<h5", "<h6").replace("</h5", "</h6").replace("<h4", "<h5").replace("</h4", "</h5").replace("<h3", "<h4").replace("</h3", "</h4").replace("<h2", "<h3").replace("</h2", "</h3").replace("<h1", "<h2").replace("</h1", "</h2");
}
// flatten headings to classes
if (JSON.parse(localStorage.flatten) === true) {
htmlWrapper = htmlWrapper.replace("<h6", "<p class='heading-6'").replace("</h6", "</p").replace("<h5", "<p class='heading-5'").replace("</h5", "</p").replace("<h4", "<p class='heading-4'").replace("</h4", "</p").replace("<h3", "<p class='heading-3'").replace("</h3", "</p");
if (JSON.parse(localStorage.stepDown) === false) {
htmlWrapper = htmlWrapper.replace("<h2", "<p class='heading-2'").replace("</h2", "</p");
}
}
let [openHtml, closeHtml] = htmlWrapper.split(/{}/);
if ("kn/dhp" == bookAbbreviation) {
// this is needed because Dhp chapters have multiple articles within them
openHtml = openHtml.replace(/<article .+?>/, "");
closeHtml = closeHtml.replace(/<\/article>/, "");
}
if (/<p><span class='verse-line'>/.test(openHtml)) {
inAVerse = true;
}
let verseNumberHtml = "";
if (inAVerse) {
if (includePali) {
if (/dhp\d+?:1$/.test(section)) {
const verseNumber = section.match(/dhp(\d+?):1$/)[1];
verseNumberHtml = `<span class="verse-number">${verseNumber}. </span>`;
}
paliVerse += `<span class="pli-lang" id="${section}">${verseNumberHtml}${paliData[section]}</span>`;
} else {
paliVerse = "";
}
englishVerse += `${transData[section] ? `<span class="eng-lang">${reference ? `<span class="reference">${reference}</span>` : ""}${transData[section]}</span>` : ""}`;
if (/<span class='verse-line'>{}<\/span><\/p>/.test(htmlData[section])) {
// now we have finished the verse
inAVerse = false; // reset the flag
html += `<p class="verse"><span class="segment-pair">`;
const paliSegment = includePali ? `${paliVerse}</span>` : "";
const englishSegment = `${englishVerse}</span>`;
html += orderThePairs(localStorage.englishFirst, englishSegment, paliSegment);
html += `</span></p>`;
// reset the verse containers
paliVerse = "<span class='pli-verse segment'>";
englishVerse = "<span class='eng-verse segment'>";
}
} else {
// not in a verse
let translationPart = "";
if (includePali || isHeading(htmlWrapper)) {
translationPart = `<span class="eng-lang segment">${reference ? `<span class="reference">${reference}</span>` : ""}${transData[section]}</span>`;
} else {
// there is no need to put the translation in language spans if there is no Pali
translationPart = `${transData[section]}`;
}
const paliSegment = `${includePali || isHeading(htmlWrapper) ? `<span class="pli-lang segment">${reference ? `<span class="reference">${reference}</span>` : ""}${paliData[section]}</span>` : ""}`;
const englishSegment = `${!transData[section] ? "" : translationPart}`;
html += `${openHtml}<span class="segment-pair">`;
html += orderThePairs(localStorage.englishFirst, englishSegment, paliSegment);
html += `</span>${closeHtml}`;
}
});
const articleElement = document.getElementById(article);
// html = html.replace("</article>", "").replace(/<article id=".+?">/, "");
articleElement.outerHTML = html;
localStorage.completionCounter++;
const progressBar = document.getElementById("progress-bar");
const width = (localStorage.completionCounter / bookLength) * 100;
progressBar.style.width = width + "%";
if (localStorage.completionCounter == bookLength) {
const progressLabel = document.getElementById("progress-label");
progressLabel.innerHTML = "Finished";
}
}