Skip to content

Commit

Permalink
Added output file option to gentest.
Browse files Browse the repository at this point in the history
json doc generator now accepts output file as well.
json doc generator now generates index file automatically.
  • Loading branch information
iapain committed Jul 12, 2012
1 parent eb9b8f5 commit 5152bf5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ out/doc/%: doc/%
cp -r $< $@

out/doc/api/%.json: doc/api/%.markdown node
out/Release/node tools/doc/generate.js --format=json $< > $@
out/Release/node tools/doc/generate.js --format=json $< --output=$@

out/doc/api/%.html: doc/api/%.markdown node
out/Release/node tools/doc/generate.js --format=html --template=doc/template.html $< > $@
Expand Down
5 changes: 4 additions & 1 deletion tools/doc/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var args = process.argv.slice(2);
var format = 'json';
var template = null;
var inputFile = null;
var outputFile = null;

args.forEach(function (arg) {
if (!arg.match(/^\-\-/)) {
Expand All @@ -39,6 +40,8 @@ args.forEach(function (arg) {
format = arg.replace(/^\-\-format=/, '');
} else if (arg.match(/^\-\-template=/)) {
template = arg.replace(/^\-\-template=/, '');
} else if (arg.match(/^\-\-output=/)) {
outputFile = arg.replace(/^\-\-output=/, '');
}
})

Expand Down Expand Up @@ -100,7 +103,7 @@ function next(er, input) {
if (er) throw er;
switch (format) {
case 'json':
require('./json.js')(input, inputFile, function(er, obj) {
require('./json.js')(input, inputFile, outputFile, function(er, obj) {
console.log(JSON.stringify(obj, null, 2));
if (er) throw er;
});
Expand Down
68 changes: 65 additions & 3 deletions tools/doc/json.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ module.exports = doJSON;

// Take the lexed input, and return a JSON-encoded object
// A module looks like this: https://gist.github.com/1777387

var fs = require('fs');
var path = require('path');
var marked = require('marked');

function doJSON(input, filename, cb) {
// It shouldn't be here
var indexfile = '_index.json';

function doJSON(input, filename, outfile, cb) {
var root = {source: filename};
var stack = [root];
var depth = 0;
Expand Down Expand Up @@ -55,6 +59,10 @@ function doJSON(input, filename, cb) {
return cb(new Error('Inappropriate heading level\n'+
JSON.stringify(tok)));
}
// set first heading to title
if (current && !current.title) {
current.title = tok.text;
}

// Sometimes we have two headings with a single
// blob of description. Treat as a clone.
Expand Down Expand Up @@ -146,9 +154,63 @@ function doJSON(input, filename, cb) {
finishSection(current, stack[stack.length - 1]);
}

return cb(null, root)
if (outfile) {
writeOututToFile(root, filename, outfile, writeToIndexFile);
}
else {
return cb(null, root)
}
}

// write output object to outfile
function writeOututToFile(obj, sourcefile, outfile, cb) {
fs.writeFile(outfile, JSON.stringify(obj, null, 2), function(err) {
if(err) {
throw new Error('error saving file - '+ err);
}
cb(obj, sourcefile, path.join(path.dirname(outfile), indexfile));
});
}

// make an entry into index file
function writeToIndexFile(root, sourcefile, outfile) {
// default type of an index
var obj = {"type":"index"};
var entry = {"source":sourcefile};

// check if indexfile already exists
if (fs.existsSync(outfile)) {
var data = fs.readFileSync(outfile);
try {
obj = JSON.parse(data.toString());
}
catch(e) {
throw new Error('invalid json data - '+ e);
}
}
// check if index file is valid
if (obj.type !== "index") {
throw new Error('invalid index file - '+ outfile);
}
// construct an entry object
entry.title = root.title;
entry.url = {};
entry.url.html = path.basename(sourcefile).replace(/\.(markdown|md)/i, ".html");
entry.url.json = entry.url.html.replace(/\.html/i, ".json");

// append mode
if (obj.chapters && typeof obj.chapters === "object") {
obj.chapters.push(entry);
}
else {
obj.chapters = [entry];
}
fs.writeFile(outfile, JSON.stringify(obj, null, 2), function(err) {
if(err) {
throw new Error('error saving file - '+ err);
}
});
}

// go from something like this:
// [ { type: 'list_item_start' },
Expand Down

0 comments on commit 5152bf5

Please sign in to comment.