mirror of
https://github.com/nodejs/node.git
synced 2024-11-30 23:43:09 +01:00
0800c0aa72
* doc: rename .markdown references in content * doc: rename to .md in tools * doc: rename to .md in CONTRIBUTING.md PR-URL: https://github.com/nodejs/node/pull/4747 Reviewed-By: Myles Borins <myles.borins@gmail.com> Reviewed-By: techjeffharris Reviewed-By: Johan Bergström <bugs@bergstroem.nu> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net>
60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
module.exports = preprocess;
|
|
|
|
var path = require('path');
|
|
var fs = require('fs');
|
|
|
|
var includeExpr = /^@include\s+([A-Za-z0-9-_]+)(?:\.)?([a-zA-Z]*)$/gmi;
|
|
var includeData = {};
|
|
|
|
function preprocess(inputFile, input, cb) {
|
|
input = stripComments(input);
|
|
processIncludes(inputFile, input, function(err, data) {
|
|
if (err) return cb(err);
|
|
|
|
cb(null, data);
|
|
});
|
|
}
|
|
|
|
function stripComments(input) {
|
|
return input.replace(/^@\/\/.*$/gmi, '');
|
|
}
|
|
|
|
function processIncludes(inputFile, input, cb) {
|
|
var includes = input.match(includeExpr);
|
|
if (includes === null) return cb(null, input);
|
|
var errState = null;
|
|
console.error(includes);
|
|
var incCount = includes.length;
|
|
if (incCount === 0) cb(null, input);
|
|
includes.forEach(function(include) {
|
|
var fname = include.replace(/^@include\s+/, '');
|
|
if (!fname.match(/\.md$/)) fname += '.md';
|
|
|
|
if (includeData.hasOwnProperty(fname)) {
|
|
input = input.split(include).join(includeData[fname]);
|
|
incCount--;
|
|
if (incCount === 0) {
|
|
return cb(null, input);
|
|
}
|
|
}
|
|
|
|
var 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--;
|
|
includeData[fname] = inc;
|
|
input = input.split(include + '\n').join(includeData[fname] + '\n');
|
|
if (incCount === 0) {
|
|
return cb(null, input);
|
|
}
|
|
});
|
|
});
|
|
});
|
|
}
|