0
0
mirror of https://github.com/nodejs/node.git synced 2024-12-01 16:10:02 +01:00
nodejs/tools/doc/preprocess.js
Chris Dickinson 9120f2b1fd doc: update style for iojs
* updates the styling for the iojs docs
* pulls the processing step for markdown files into
  a separate module
* adds the ability to insert comments into the markdown

PR-URL: https://github.com/iojs/io.js/pull/297
Fixes: https://github.com/iojs/iojs.github.io/issues/23
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2015-01-12 13:44:50 -08:00

58 lines
1.6 KiB
JavaScript

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(/\.markdown$/)) fname += '.markdown';
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);
}
});
});
});
}