-
Notifications
You must be signed in to change notification settings - Fork 6
/
multipage.js
227 lines (188 loc) · 5.99 KB
/
multipage.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
"use strict";
var fs = require('fs');
var whacko = require('whacko');
var baseOutputPath = "./out/";
var specFile = 'single-page.html';
if(process.argv[2] !== undefined) {
if(process.argv[3] !== undefined) {
baseOutputPath = process.argv[3];
}
specFile = process.argv[2];
}
console.log('This platform is', process.platform, 'node', process.version);
console.log('Loading single-page.html');
var $ = whacko.load(fs.readFileSync(specFile));
var sections = [
{ id: "introduction" }
, { id: "infrastructure" }
, { id: "dom" }
, { id: "semantics" }
, { id: "document-metadata" }
, { id: "sections" }
, { id: "grouping-content" }
, { id: "textlevel-semantics" }
, { id: "edits" }
, { id: "semantics-embedded-content" }
, { id: "links" }
, { id: "tabular-data" }
, { id: "sec-forms" }
, { id: "interactive-elements" }
, { id: "semantics-scripting" }
, { id: "common-idioms-without-dedicated-elements" }
, { id: "disabled-elements" }
, { id: "matching-html-elements-using-selectors" }
, { id: "editing" }
, { id: "browsers" }
, { id: "webappapis" }
, { id: "syntax" }
, { id: "xhtml" }
, { id: "rendering" }
, { id: "obsolete" }
, { id: "iana" }
, { id: "index" }
, { id: "property-index" }
, { id: "idl-index" }
, { id: "references" }
, { id: "changes" }
, { id: "acknowledgements" }
];
console.log("Removing unused sections");
var keepsections = [];
for(var i=0; i<sections.length; i++) {
var hasNode = $("#" + sections[i].id).length;
if (hasNode === 1) {
keepsections.push(sections[i]);
} else {
console.log(" Removed " + sections[i].id);
}
}
sections = keepsections;
console.log("Creating ID->file mapping");
// first, create a mapping between the ids and their files
var idMap = [];
for(var i=0; i<sections.length; i++) {
var section = sections[i];
if(section.id==="index") {
section.filename = "fullindex";
} else {
section.filename = section.id;
}
var sNode = $('#'+section.id).parents('section');
if(!sNode) throw 'section not found';
if (sNode.length > 1) {
// if we are in a subsection, just take the first
sNode = sNode.first();
}
if (id === "semantics") {
// we only take the first subsection for semantics
// others will be handled by ids
idMap['#' + section.id] = section.filename;
sNode = sNode.find("section").first();
}
sNode.find('*[id]').each(function(i,element) {
idMap['#'+$(this).attr('id')] = section.filename;
});
}
// remapping links
// console.log("Remapping links");
var notFound = [];
$("a[href^='#']").each(function(i,element) {
var href = $(this).attr('href');
if(idMap[href] !== undefined) {
$(this).attr('href',idMap[href] + ".html" + href);
} else {
if(notFound[href]===undefined) {
notFound[href] = href;
console.error('Link not found: ' + href);
}
}
});
// save the information to generate the proper sections
console.log("Sorting out sections");
var htmlTitle = $("title").first().text();
for(var i=0; i<sections.length; i++) {
var section = sections[i];
// find the proper header
var header = $("#" + section.id);
// compute the nice title
section.title = htmlTitle + ": "+ header.text();
// find the proper section
section.node = header.parent();
while (typeof section.node[0] !== 'undefined' && section.node.get(0).tagName !== "section") {
section.node = section.node.parent();
}
// for section 4, only keep the first subsection
if (section.id === "semantics") {
var newSection = $('<section></section>');
var h2 = section.node.children().first();
var s = section.node.children().get(1);
newSection.append(h2);
newSection.append(s);
section.node = newSection;
}
}
console.log("Generating index");
// remove main to avoid having it in the index output
$("nav").next().remove();
fs.writeFileSync(baseOutputPath + "index.html",$.html());
console.log("Generating sections");
// main was removed before generating the index, so recreate it
$("nav").after('<main></main>');
// remove unnecessary heading (Version links, editors, etc.)
var current = $("h2#abstract").first();
do {
var nextElement = current.next();
current.remove();
current = nextElement;
} while(current && current.get(0).tagName !== "nav");
current = $("header").first().next();
do {
nextElement = current.next();
current.remove();
current = nextElement;
} while($(current).get(0));
// this will be our template for each section page
var sectionDocument = $.html();
for(var i=0; i<sections.length; i++) {
var id = sections[i].id;
var doc = whacko.load(sectionDocument);
var section = sections[i].node;
// insert the proper section
var main = doc("main").first().append(section);
// Adjust the table of contents
var toc = doc("nav#toc ol").first();
var item = toc.find('a[href$="#' + id + '"]').first().parent();
// find its previous and next
var previous_item = undefined, next_item = undefined;
if (i > 0) {
previous_item = toc.find('a[href$="#' + sections[i-1].id + '"]').first();
}
if ((i+1) < sections.length) {
next_item = toc.find('a[href$="#' + sections[i+1].id + '"]').first();
}
// only keep the appropriate nav toc
toc.empty();
toc.append(item);
// again, for section 4, we eliminate all subtoc after the first
if (id === "semantics") {
item.children("ol").children("li").each(function(i,element) {
if (i > 0) doc(element).remove();
});
}
// make a nice title for the document
doc("title").first().text(sections[i].title);
// insert top and botton mini navbars
var nav = "<a href='index.html#contents'>Table of contents</a>";
if(previous_item!== undefined) {
nav = "← " + previous_item.toString() + " — " + nav;
}
if(next_item!==undefined) {
nav += " — " + next_item.toString() + " →";
}
nav = "<p class='prev_next'>" + nav + "</p>";
var mainNav = doc("nav#toc");
mainNav.prepend(nav);
mainNav.parent().append(nav);
console.log('Saving ' + sections[i].title);
fs.writeFileSync(baseOutputPath + sections[i].filename + ".html",doc.html());
}