Skip to content

Instantly share code, notes, and snippets.

@rubys
Created June 27, 2018 13:20
Show Gist options
  • Save rubys/26fb5b75d624098b021575c4b58874b0 to your computer and use it in GitHub Desktop.
Save rubys/26fb5b75d624098b021575c4b58874b0 to your computer and use it in GitHub Desktop.
regex version of allhtml.js
'use strict';
// Build all.html by combining the generated toc and apicontent from each
// of the generated html files.
const fs = require('fs');
const source = `${__dirname}/../../out/doc/api`;
// get a list of generated API documents
const htmlFiles = fs.readdirSync(source, 'utf8')
.filter((name) => name.includes('.html') && name !== 'all.html');
// read the table of contents
const toc = fs.readFileSync(source + '/_toc.html', 'utf8');
// extract (and concatenate) the toc and apicontent from each document
let contents = '';
let apicontent = '';
for (const link of toc.match(/<a.*?>/g)) {
const href = /href="(.*?)"/.exec(link)[1];
if (!htmlFiles.includes(href)) continue;
const data = fs.readFileSync(source + '/' + href, 'utf8');
// split the doc
const match = /(<\/ul>\s*)?<\/div>\s*<div id="apicontent">\s*/.exec(data);
contents += data.slice(0, match.index)
.replace(/[\s\S]*?<div id="toc">\s*<h2>.*?<\/h2>\s*(<ul>\s*)?/, '');
apicontent += data.slice(match.index + match[0].length)
.replace(/\s*<script[\s\S]*/, '');
}
// replace various mentions of _toc with all
let all = toc.replace(/_toc\.html/g, 'all.html')
.replace('_toc.json', 'all.json')
.replace('api-section-_toc', 'api-section-all')
.replace('data-id="_toc"', 'data-id="all"');
// clean up the title
all = all.replace(/<title>.*?| /, '<title');
// insert the table of contents
const tocStart = /<div id="toc">\s*<h2>.*?<\/h2>\s*/.exec(all);
all = all.slice(0, tocStart.index + tocStart[0].length) +
'<ul>\n' + contents + '</ul>\n' +
all.slice(tocStart.index + tocStart[0].length);
// replace apicontent
const apiStart = /<div id="apicontent">\s*/.exec(all);
const apiEnd = /\s*<script>/;
all = all.slice(0, apiStart.index + apiStart[0].length) +
apicontent +
all.slice(apiEnd);
fs.writeFileSync(source + '/all.html', all, 'utf8');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment