Permalink
Browse files

Change JSON documentation index

Added output file option to gentest. json doc generator now accepts
output file as well. json doc generator now generates index file
automatically.
  • Loading branch information...
1 parent 109f8e2 commit c636f8f78a2892faf47d38f3978d210e94995c92 @iapain iapain committed with Jul 12, 2012
Showing with 70 additions and 5 deletions.
  1. +1 −1 Makefile
  2. +4 −1 tools/doc/generate.js
  3. +65 −3 tools/doc/json.js
View
2 Makefile
@@ -174,7 +174,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 $< > $@
View
5 tools/doc/generate.js
@@ -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(/^\-\-/)) {
@@ -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=/, '');
}
})
@@ -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;
});
View
68 tools/doc/json.js
@@ -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;
@@ -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.
@@ -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' },

0 comments on commit c636f8f

Please sign in to comment.