Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: simplify tools/doc/preprocess.js #19539

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 16 additions & 38 deletions tools/doc/preprocess.js
Original file line number Diff line number Diff line change
@@ -1,58 +1,36 @@
'use strict';

module.exports = preprocess;
module.exports = processIncludes;

const path = require('path');
const fs = require('fs');

const includeExpr = /^@include\s+[\w-]+\.?[a-zA-Z]*$/gmi;
const includeData = {};

function preprocess(inputFile, input, cb) {
input = stripComments(input);
processIncludes(inputFile, input, cb);
}

function stripComments(input) {
return input.replace(/^@\/\/.*$/gmi, '');
}
const includeExpr = /^@include\s+([\w-]+)(?:\.md)?$/gmi;
const commentExpr = /^@\/\/.*$/gmi;

function processIncludes(inputFile, input, cb) {
const includes = input.match(includeExpr);
if (includes === null) return cb(null, input);
if (includes === null) return cb(null, input.replace(commentExpr, ''));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think dominant style is to put if body in a new line.


let errState = null;
let incCount = includes.length;

includes.forEach((include) => {
let fname = include.replace(/^@include\s+/, '');
if (!/\.md$/.test(fname)) fname = `${fname}.md`;

if (includeData.hasOwnProperty(fname)) {
input = input.split(include).join(includeData[fname]);
incCount--;
if (incCount === 0) {
return cb(null, input);
}
}

const fname = include.replace(includeExpr, '$1.md');
const fullFname = path.resolve(path.dirname(inputFile), fname);

fs.readFile(fullFname, 'utf8', function(er, inc) {
if (errState) return;
if (er) return cb(errState = er);
preprocess(inputFile, inc, function(er, inc) {
if (errState) return;
if (er) return cb(errState = er);
incCount--;

// Add comments to let the HTML generator know how the anchors for
// headings should look like.
includeData[fname] = `<!-- [start-include:${fname}] -->\n` +
`${inc}\n<!-- [end-include:${fname}] -->\n`;
input = input.split(`${include}\n`).join(`${includeData[fname]}\n`);
if (incCount === 0) {
return cb(null, input);
}
});
incCount--;

// Add comments to let the HTML generator know
// how the anchors for headings should look like.
inc = `<!-- [start-include:${fname}] -->\n` +
`${inc}\n<!-- [end-include:${fname}] -->\n`;
input = input.split(`${include}\n`).join(`${inc}\n`);

if (incCount === 0) return cb(null, input.replace(commentExpr, ''));
});
});
}