tools: simplify HTML generation

PR-URL: https://github.com/nodejs/node/pull/20307
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
Vse Mozhet Byt 2018-04-25 23:44:38 +03:00
parent a4b4854e2f
commit ad6a65ba11
4 changed files with 24 additions and 70 deletions

View File

@ -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))

View File

@ -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');
@ -107,7 +106,6 @@ testData.forEach((item) => {
{
input: preprocessed,
filename: 'foo',
template: path.resolve(__dirname, '../../doc/template.html'),
nodeVersion: process.version,
analytics: item.analyticsId,
},

View File

@ -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;
@ -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=')) {
@ -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);

View File

@ -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;
@ -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) {
@ -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) {