-
Notifications
You must be signed in to change notification settings - Fork 29.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools: simplify tools/doc/preprocess.js
PR-URL: #19539 Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Daijiro Wachi <[email protected]>
- Loading branch information
1 parent
fe4e511
commit 4a1b064
Showing
1 changed file
with
18 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,38 @@ | ||
'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] = `<!-- [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, '')); | ||
}); | ||
}); | ||
} |