From d59cbb83734f64e9681eefa49f47b9a2c1e5e65d Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Thu, 22 Mar 2018 22:39:49 +0200 Subject: [PATCH 1/2] tools: simplify tools/doc/preprocess.js --- tools/doc/preprocess.js | 54 ++++++++++++----------------------------- 1 file changed, 16 insertions(+), 38 deletions(-) diff --git a/tools/doc/preprocess.js b/tools/doc/preprocess.js index 5b6901153dc61f..5c6be6d2440780 100644 --- a/tools/doc/preprocess.js +++ b/tools/doc/preprocess.js @@ -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, '')); + 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] = `\n` + - `${inc}\n\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 = `\n` + + `${inc}\n\n`; + input = input.split(`${include}\n`).join(`${inc}\n`); + + if (incCount === 0) return cb(null, input.replace(commentExpr, '')); }); }); } From bf2efccb9f6d1e07974b760f60877952d4bc38cc Mon Sep 17 00:00:00 2001 From: Vse Mozhet Byt Date: Fri, 23 Mar 2018 12:05:26 +0200 Subject: [PATCH 2/2] fixup: put long `if` bodies in a new line --- tools/doc/preprocess.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tools/doc/preprocess.js b/tools/doc/preprocess.js index 5c6be6d2440780..f8d394735dd30b 100644 --- a/tools/doc/preprocess.js +++ b/tools/doc/preprocess.js @@ -10,7 +10,8 @@ const commentExpr = /^@\/\/.*$/gmi; function processIncludes(inputFile, input, cb) { const includes = input.match(includeExpr); - if (includes === null) return cb(null, input.replace(commentExpr, '')); + if (includes === null) + return cb(null, input.replace(commentExpr, '')); let errState = null; let incCount = includes.length; @@ -30,7 +31,8 @@ function processIncludes(inputFile, input, cb) { `${inc}\n\n`; input = input.split(`${include}\n`).join(`${inc}\n`); - if (incCount === 0) return cb(null, input.replace(commentExpr, '')); + if (incCount === 0) + return cb(null, input.replace(commentExpr, '')); }); }); }