Skip to content

Commit

Permalink
tools: simplify HTML generation
Browse files Browse the repository at this point in the history
PR-URL: #20307
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
  • Loading branch information
vsemozhetbyt authored and MylesBorins committed May 4, 2018
1 parent 8ddbac2 commit a12d13a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 70 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ tools/doc/node_modules/js-yaml/package.json:

gen-json = tools/doc/generate.js --format=json $< > $@
gen-html = tools/doc/generate.js --node-version=$(FULLVERSION) --format=html \
--template=doc/template.html --analytics=$(DOCS_ANALYTICS) $< > $@
--analytics=$(DOCS_ANALYTICS) $< > $@

out/doc/api/%.json: doc/api/%.md
$(call available-node, $(gen-json))
Expand Down
2 changes: 0 additions & 2 deletions test/doctool/test-doctool-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ try {

const assert = require('assert');
const fs = require('fs');
const path = require('path');
const fixtures = require('../common/fixtures');
const processIncludes = require('../../tools/doc/preprocess.js');
const html = require('../../tools/doc/html.js');
Expand Down Expand Up @@ -107,7 +106,6 @@ testData.forEach((item) => {
{
input: preprocessed,
filename: 'foo',
template: path.resolve(__dirname, '../../doc/template.html'),
nodeVersion: process.version,
analytics: item.analyticsId,
},
Expand Down
5 changes: 1 addition & 4 deletions tools/doc/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const fs = require('fs');

const args = process.argv.slice(2);
let format = 'json';
let template = null;
let filename = null;
let nodeVersion = null;
let analytics = null;
Expand All @@ -39,8 +38,6 @@ args.forEach(function(arg) {
filename = arg;
} else if (arg.startsWith('--format=')) {
format = arg.replace(/^--format=/, '');
} else if (arg.startsWith('--template=')) {
template = arg.replace(/^--template=/, '');
} else if (arg.startsWith('--node-version=')) {
nodeVersion = arg.replace(/^--node-version=/, '');
} else if (arg.startsWith('--analytics=')) {
Expand Down Expand Up @@ -71,7 +68,7 @@ function next(er, input) {
break;

case 'html':
require('./html')({ input, filename, template, nodeVersion, analytics },
require('./html')({ input, filename, nodeVersion, analytics },
(err, html) => {
if (err) throw err;
console.log(html);
Expand Down
85 changes: 22 additions & 63 deletions tools/doc/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const common = require('./common.js');
const fs = require('fs');
const marked = require('marked');
const path = require('path');
const preprocess = require('./preprocess.js');
const typeParser = require('./type-parser.js');

module.exports = toHTML;
Expand All @@ -42,76 +41,36 @@ marked.setOptions({
renderer: renderer
});

// TODO(chrisdickinson): never stop vomiting / fix this.
const gtocPath = path.resolve(path.join(
__dirname,
'..',
'..',
'doc',
'api',
'_toc.md'
));
var gtocLoading = null;
var gtocData = null;
const docPath = path.resolve(__dirname, '..', '..', 'doc');

const gtocPath = path.join(docPath, 'api', '_toc.md');
const gtocMD = fs.readFileSync(gtocPath, 'utf8').replace(/^@\/\/.*$/gm, '');
const gtocHTML = marked(gtocMD).replace(
/<a href="(.*?)"/g,
(all, href) => `<a class="nav-${toID(href)}" href="${href}"`
);

const templatePath = path.join(docPath, 'template.html');
const template = fs.readFileSync(templatePath, 'utf8');

var docCreated = null;
var nodeVersion = null;

/**
* opts: input, filename, template, nodeVersion.
* opts: input, filename, nodeVersion.
*/
function toHTML(opts, cb) {
const template = opts.template;

nodeVersion = opts.nodeVersion || process.version;
docCreated = opts.input.match(DOC_CREATED_REG_EXP);

if (gtocData) {
return onGtocLoaded();
}

if (gtocLoading === null) {
gtocLoading = [onGtocLoaded];
return loadGtoc(function(err, data) {
if (err) throw err;
gtocData = data;
gtocLoading.forEach(function(xs) {
xs();
});
});
}

if (gtocLoading) {
return gtocLoading.push(onGtocLoaded);
}

function onGtocLoaded() {
const lexed = marked.lexer(opts.input);
fs.readFile(template, 'utf8', function(er, template) {
if (er) return cb(er);
render({
lexed: lexed,
filename: opts.filename,
template: template,
nodeVersion: nodeVersion,
analytics: opts.analytics,
}, cb);
});
}
}

function loadGtoc(cb) {
fs.readFile(gtocPath, 'utf8', function(err, data) {
if (err) return cb(err);

preprocess(gtocPath, data, function(err, data) {
if (err) return cb(err);

data = marked(data).replace(/<a href="(.*?)"/gm, function(a, m) {
return `<a class="nav-${toID(m)}" href="${m}"`;
});
return cb(null, data);
});
});
const lexed = marked.lexer(opts.input);
render({
lexed: lexed,
filename: opts.filename,
template: template,
nodeVersion: nodeVersion,
analytics: opts.analytics,
}, cb);
}

function toID(filename) {
Expand Down Expand Up @@ -150,7 +109,7 @@ function render(opts, cb) {
template = template.replace(/__TOC__/g, toc);
template = template.replace(
/__GTOC__/g,
gtocData.replace(`class="nav-${id}`, `class="nav-${id} active`)
gtocHTML.replace(`class="nav-${id}`, `class="nav-${id} active`)
);

if (opts.analytics) {
Expand Down

0 comments on commit a12d13a

Please sign in to comment.