forked from source-academy/sicp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateTocHtml.js
280 lines (257 loc) · 9.04 KB
/
generateTocHtml.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
import { recursiveProcessTextHtml } from "./parseXmlHtml.js";
import { tableOfContent, allFilepath } from "./index.js";
import {
html_links_part1,
html_links_part2,
html_licences,
indexPage
} from "./htmlContent";
const generateChapterIndex = filename => {
let chapterIndex = "";
if (filename.match(/chapter/)) {
// match the number after string "chapter"
chapterIndex += filename.match(/(?<=chapter)\d+/g)[0];
}
if (filename.match(/section/)) {
// "section"
chapterIndex += "." + filename.match(/(?<=section)\d+/g)[0];
}
if (filename.match(/subsection/)) {
// "subsection"
chapterIndex += "." + filename.match(/(?<=subsection)\d+/g)[0];
}
if (filename.match(/foreword/)) {
chapterIndex = filename.match(/foreword\d*/g)[0];
} else if (filename.match(/prefaces/)) {
chapterIndex = filename.match(/prefaces\d*/g)[0];
} else if (filename.match(/acknowledgements/)) {
chapterIndex = "acknowledgements";
} else if (filename.match(/references/)) {
chapterIndex = "references";
} else if (filename.match(/see/)) {
chapterIndex = "see";
} else if (filename.match(/indexpreface/)) {
chapterIndex = "index";
} else if (filename.match(/making/)) {
chapterIndex = "making-of";
}
//console.log(chapterNumber);
return chapterIndex;
};
const truncateTitle = chapterTitle => {
let truncatedTitle = "";
chapterTitle.forEach(item => (truncatedTitle += item));
return truncatedTitle;
};
const recursiveFindTitle = (node, chapterTitle) => {
if (!node) return;
if (node.nodeName == "NAME") {
// using recursiveProcessText function from parseXML.js
recursiveProcessTextHtml(node.firstChild, chapterTitle);
} else {
recursiveFindTitle(node.firstChild, chapterTitle);
}
if (!chapterTitle[0])
return recursiveFindTitle(node.nextSibling, chapterTitle);
return;
};
export const generateTOC = (doc, tableOfContent, filename) => {
const index = generateChapterIndex(filename);
const chapterTitle = [];
recursiveFindTitle(doc.documentElement, chapterTitle);
const title = truncateTitle(chapterTitle);
let relativePathToMain = "";
for (let i = 1; i < filename.split("/").length; i++) {
relativePathToMain += "../";
}
tableOfContent[filename] = { index, title, relativePathToMain };
};
export const sortTOC = allFilepath => {
allFilepath = allFilepath.sort();
let head = [];
let mid = [];
let tail = [];
for (const filename of allFilepath) {
if (
filename.match(/foreword/) ||
filename.match(/prefaces/) ||
filename.match(/acknowledgements/) ||
filename.match(/see/)
) {
head.push(filename);
} else if (filename.match(/references/) || filename.match(/making/)) {
tail.push(filename);
} else {
mid.push(filename);
}
}
return head.concat(mid, tail);
};
export const recursiveProcessTOC = (index, writeTo, option, toIndexFolder) => {
if (index >= allFilepath.length) {
return;
}
let next = index + 1;
const filename = allFilepath[index];
const chapterIndex = tableOfContent[filename].index;
const chapterTitle = tableOfContent[filename].title;
const displayTitle = chapterIndex.match(/[a-z]+/)
? chapterTitle
: `${chapterIndex} ${chapterTitle}`;
const nextOption = option;
if (index == 0 && option == "sidebar") {
writeTo.push(`
<div class="collapse" id="nav-sidebar" role="tablist" aria-multiselectable="true">
<!-- insert a dummy entry, to give one extra line of space -->
<a class="navbar-brand" href="index.html"> </a>
`);
}
if (filename.match(/others/) || filename.match(/subsection/)) {
if (option == "index") {
writeTo.push(`
<div class="card card-inverse">
<div class="card-header" role="tab" id="index-${index + 1}">
<h5 class="mb-0">
<span class="collapsed" data-toggle="collapse" href="#index-collapse-${
index + 1
}" aria-expanded="false" aria-controls="index-collapse-${
index + 1
}">
<a href="${toIndexFolder}${chapterIndex}.html"> ${displayTitle}</a>
</span>
</h5>
</div>
</div>
`);
} else if (option == "sidebar") {
writeTo.push(`
<div class="card card-inverse">
<div class="card-header" role="tab" id="sidebar-${index + 1}">
<h5 class="mb-0">
<span class="collapsed" data-toggle="collapse" href="#sidebar-collapse-${
index + 1
}" aria-expanded="false" aria-controls="sidebar-collapse-${
index + 1
}">
<a href="${toIndexFolder}${chapterIndex}.html"> ${displayTitle}</a>
</span>
</h5>
</div>
</div>
`);
}
if (filename.match(/others/)) {
return recursiveProcessTOC(next, writeTo, nextOption, toIndexFolder);
} else if (allFilepath[next].match(/subsection/)) {
return recursiveProcessTOC(next, writeTo, nextOption, toIndexFolder);
} else {
return;
}
} else {
if (option == "index") {
writeTo.push(`
<div class="card card-inverse">
<div class="card-header" role="tab" id="index-${index + 1}">
<h5 class="mb-0">
<a class="index-show collapsed" data-toggle="collapse" href="#index-collapse-${
index + 1
}" aria-expanded="true" aria-controls="index-collapse-${
index + 1
}">
➤ <!-- ➤ (because this one is rendered blue on mobile: ▶ -->
</a>
<a class="index-hide collapsed" data-toggle="collapse" href="#index-collapse-${
index + 1
}" aria-expanded="true" aria-controls="index-collapse-${
index + 1
}">
▼ <!-- ▼ (because the corresponding one is not rendered) -->
</a>
<a href="${toIndexFolder}${chapterIndex}.html">${displayTitle}</a>
</h5>
</div>
<div id="index-collapse-${
index + 1
}" class="collapse" role="tabpanel" aria-labelledby="headingOne">
<div class="card-block">
`);
} else if (option == "sidebar") {
writeTo.push(`
<div class="card card-inverse">
<div class="card-header" role="tab" id="sidebar-${index + 1}">
<h5 class="mb-0">
<a class="sidebar-show collapsed" data-toggle="collapse" href="#sidebar-collapse-${
index + 1
}" aria-expanded="true" aria-controls="sidebar-collapse-${
index + 1
}">
➤ <!-- ➤ (because this one is rendered blue on mobile: ▶ -->
</a>
<a class="sidebar-hide collapsed" data-toggle="collapse" href="#sidebar-collapse-${
index + 1
}" aria-expanded="true" aria-controls="sidebar-collapse-${
index + 1
}">
▼ <!-- ▼ (because the corresponding one is not rendered) -->
</a>
<a href="${toIndexFolder}${chapterIndex}.html">${displayTitle}</a>
</h5>
</div>
<div id="sidebar-collapse-${
index + 1
}" class="collapse" role="tabpanel" aria-labelledby="headingOne">
<div class="card-block">
`);
}
recursiveProcessTOC(next, writeTo, nextOption, toIndexFolder);
writeTo.push(`
</div>
</div>
</div>
`);
if (filename.match(/section/)) {
while (allFilepath[next].match(/subsection/)) {
next++;
}
if (allFilepath[next].match(/section/)) {
return recursiveProcessTOC(next, writeTo, nextOption, toIndexFolder);
} else {
return;
}
} else {
while (allFilepath[next].match(/section/)) {
next++;
}
return recursiveProcessTOC(next, writeTo, nextOption, toIndexFolder);
}
}
};
export const indexHtml = writeToIndex => {
//let chapArrIndex = 0;
//console.log(tableOfContent);
writeToIndex.push(html_links_part1);
writeToIndex.push(`
<meta name="description" content="" />
<title>
Structure and Interpretation of Computer Programs, Comparison Edition
</title>
`);
html_links_part2(writeToIndex, "", "js");
// TOC at the sidebar
recursiveProcessTOC(0, writeToIndex, "sidebar", "./chapters/");
writeToIndex.push("</div>\n"); // <div class='collapse'>
// index page content
writeToIndex.push(`
<div class="chapter-content">
<div class="chapter-text" >`);
indexPage(writeToIndex);
// TOC at index page
writeToIndex.push("<h2>Content</h2>");
writeToIndex.push("\n<div class='nav-index'>");
recursiveProcessTOC(0, writeToIndex, "index", "./chapters/");
writeToIndex.push("</div>\n"); // <div class='nav-index'>
writeToIndex.push("</div>\n"); // <div class="chapter-content">
writeToIndex.push(html_licences);
writeToIndex.push("</div>\n"); // <div class="container scroll">
writeToIndex.push("</body></html>");
};