diff options
author | isaacs <i@izs.me> | 2012-02-27 10:59:01 -0800 |
---|---|---|
committer | isaacs <i@izs.me> | 2012-02-29 15:15:25 -0800 |
commit | 217bb47b0271df61dc93a37a440404794679245a (patch) | |
tree | 1ffca11d439091175ab75094b4763db611693208 /tools/doc/generate.js | |
parent | 4065b241e89000588131cac5228f397964ab034f (diff) | |
download | android-node-v8-217bb47b0271df61dc93a37a440404794679245a.tar.gz android-node-v8-217bb47b0271df61dc93a37a440404794679245a.tar.bz2 android-node-v8-217bb47b0271df61dc93a37a440404794679245a.zip |
New documentation generation tool
Diffstat (limited to 'tools/doc/generate.js')
-rwxr-xr-x | tools/doc/generate.js | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/tools/doc/generate.js b/tools/doc/generate.js new file mode 100755 index 0000000000..f866c0120b --- /dev/null +++ b/tools/doc/generate.js @@ -0,0 +1,99 @@ +#!node + +var marked = require('marked'); +var fs = require('fs'); +var path = require('path'); + +// parse the args. +// Don't use nopt or whatever for this. It's simple enough. + +var args = process.argv.slice(2); +var format = 'json'; +var template = null; +var inputFile = null; + +args.forEach(function (arg) { + if (!arg.match(/^\-\-/)) { + inputFile = arg; + } else if (arg.match(/^\-\-format=/)) { + format = arg.replace(/^\-\-format=/, ''); + } else if (arg.match(/^\-\-template=/)) { + template = arg.replace(/^\-\-template=/, ''); + } +}) + + +if (!inputFile) { + throw new Error('No input file specified'); +} + + +console.error('Input file = %s', inputFile); +fs.readFile(inputFile, 'utf8', function(er, input) { + if (er) throw er; + // process the input for @include lines + processIncludes(input, next); +}); + + +var includeExpr = /^@include\s+([A-Za-z0-9-_]+)(?:\.)?([a-zA-Z]*)$/gmi; +var includeData = {}; +function processIncludes(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); + processIncludes(inc, function(er, inc) { + if (errState) return; + if (er) return cb(errState = er); + incCount--; + includeData[fname] = inc; + input = input.split(include).join(includeData[fname]); + if (incCount === 0) { + return cb(null, input); + } + }); + }); + }); +} + + +function next(er, input) { + if (er) throw er; + switch (format) { + case 'json': + require('./json.js')(input, inputFile, function(er, obj) { + console.log(JSON.stringify(obj, null, 2)); + if (er) throw er; + }); + break; + + case 'html': + require('./html.js')(input, inputFile, template, function(er, html) { + if (er) throw er; + console.log(html); + }); + break; + + default: + throw new Error('Invalid format: ' + format); + } +} |