Skip to content

Commit 5d6d6fb

Browse files
rubystargos
authored andcommitted
tools: build all.html by combining generated HTML
Combine the toc and api contents from the generated doc/api/*.html files. This ensures that the single page version of the documentation exactly matches the individual pages. PR-URL: #21568 Fixes: #20100 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Vse Mozhet Byt <[email protected]>
1 parent c870372 commit 5d6d6fb

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

Makefile

+6-2
Original file line numberDiff line numberDiff line change
@@ -657,12 +657,16 @@ gen-json = tools/doc/generate.js --format=json $< > $@
657657
gen-html = tools/doc/generate.js --node-version=$(FULLVERSION) --format=html \
658658
--analytics=$(DOCS_ANALYTICS) $< > $@
659659

660-
out/doc/api/%.json: doc/api/%.md
660+
out/doc/api/%.json: doc/api/%.md tools/doc/generate.js tools/doc/json.js
661661
$(call available-node, $(gen-json))
662662

663-
out/doc/api/%.html: doc/api/%.md
663+
out/doc/api/%.html: doc/api/%.md tools/doc/generate.js tools/doc/html.js
664664
$(call available-node, $(gen-html))
665665

666+
out/doc/api/all.html: $(filter-out out/doc/api/all.html, $(apidocs_html)) \
667+
tools/doc/allhtml.js
668+
$(call available-node, tools/doc/allhtml.js)
669+
666670
.PHONY: docopen
667671
docopen: $(apidocs_html)
668672
@$(PYTHON) -mwebbrowser file://$(PWD)/out/doc/api/all.html

tools/doc/allhtml.js

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
'use strict';
2+
3+
// Build all.html by combining the generated toc and apicontent from each
4+
// of the generated html files.
5+
6+
const fs = require('fs');
7+
8+
const source = `${__dirname}/../../out/doc/api`;
9+
10+
// Get a list of generated API documents.
11+
const htmlFiles = fs.readdirSync(source, 'utf8')
12+
.filter((name) => name.includes('.html') && name !== 'all.html');
13+
14+
// Read the table of contents.
15+
const toc = fs.readFileSync(source + '/_toc.html', 'utf8');
16+
17+
// Extract (and concatenate) the toc and apicontent from each document.
18+
let contents = '';
19+
let apicontent = '';
20+
21+
// Identify files that should be skipped. As files are processed, they
22+
// are added to this list to prevent dupes.
23+
const seen = {
24+
'all.html': true,
25+
'index.html': true
26+
};
27+
28+
for (const link of toc.match(/<a.*?>/g)) {
29+
const href = /href="(.*?)"/.exec(link)[1];
30+
if (!htmlFiles.includes(href) || seen[href]) continue;
31+
const data = fs.readFileSync(source + '/' + href, 'utf8');
32+
33+
// Split the doc.
34+
const match = /(<\/ul>\s*)?<\/div>\s*<div id="apicontent">/.exec(data);
35+
36+
contents += data.slice(0, match.index)
37+
.replace(/[\s\S]*?<div id="toc">\s*<h2>.*?<\/h2>\s*(<ul>\s*)?/, '');
38+
39+
apicontent += data.slice(match.index + match[0].length)
40+
.replace(/(<\/div>\s*)*\s*<script[\s\S]*/, '')
41+
.replace(/<a href="(\w[^#"]*)#/g, (match, href) => {
42+
return htmlFiles.includes(href) ? '<a href="#' : match;
43+
})
44+
.trim() + '\n';
45+
46+
// Mark source as seen.
47+
seen[href] = true;
48+
}
49+
50+
// Replace various mentions of _toc with all.
51+
let all = toc.replace(/_toc\.html/g, 'all.html')
52+
.replace('_toc.json', 'all.json')
53+
.replace('api-section-_toc', 'api-section-all')
54+
.replace('data-id="_toc"', 'data-id="all"');
55+
56+
// Clean up the title.
57+
all = all.replace(/<title>.*?\| /, '<title>');
58+
59+
// Insert the combined table of contents.
60+
const tocStart = /<div id="toc">\s*<h2>.*?<\/h2>\s*/.exec(all);
61+
all = all.slice(0, tocStart.index + tocStart[0].length) +
62+
'<ul>\n' + contents + '</ul>\n' +
63+
all.slice(tocStart.index + tocStart[0].length);
64+
65+
// Replace apicontent with the concatenated set of apicontents from each source.
66+
const apiStart = /<div id="apicontent">\s*/.exec(all);
67+
const apiEnd = /(\s*<\/div>)*\s*<script /.exec(all);
68+
all = all.slice(0, apiStart.index + apiStart[0].length) +
69+
apicontent +
70+
all.slice(apiEnd.index);
71+
72+
// Write results.
73+
fs.writeFileSync(source + '/all.html', all, 'utf8');

0 commit comments

Comments
 (0)