summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2014-10-24 21:18:00 -0700
committerJohn MacFarlane <jgm@berkeley.edu>2014-10-24 21:18:00 -0700
commit80b09d9552e57011c849fc305553501611b3079d (patch)
treee1c3d6d8dce6c850b2be4988af01a221461680dc /js
parent0ed2541d6dc3ff31126915c6ea59d92f0c23acd1 (diff)
js/bin/markdown: added `--ast` flag and multiple filename args.
Diffstat (limited to 'js')
-rwxr-xr-xjs/bin/markdown38
1 files changed, 28 insertions, 10 deletions
diff --git a/js/bin/markdown b/js/bin/markdown
index 37b0e52..1d395e0 100755
--- a/js/bin/markdown
+++ b/js/bin/markdown
@@ -3,13 +3,31 @@ var fs = require('fs');
var util = require('util');
var commonmark = require('../lib/index.js');
-file = process.argv[2] || '/dev/stdin';
-
-fs.readFile(file, 'utf8', function(err, data) {
- if (err) {
- return console.log(err);
- }
- var parser = new commonmark.DocParser();
- var renderer = new commonmark.HtmlRenderer();
- process.stdout.write(renderer.render(parser.parse(data)));
-});
+var parser = new commonmark.DocParser();
+var renderer;
+var inps = [];
+
+var output_ast = false;
+var files = [];
+
+if (process.argv[2] === '--ast') {
+ output_ast = true;
+ files = process.argv.slice(3);
+ renderer = { render: function(x) {
+ return util.inspect(x, null, Infinity) + '\n';
+ } };
+} else {
+ files = process.argv.slice(2);
+ renderer = new commonmark.HtmlRenderer();
+}
+
+if (files.length === 0) {
+ files = ['/dev/stdin'];
+}
+
+for (var i = 0; i < files.length; i++) {
+ file = files[i];
+ inps.push(fs.readFileSync(file, 'utf8'));
+}
+
+process.stdout.write(renderer.render(parser.parse(inps.join('\n'))));