summaryrefslogtreecommitdiff
path: root/js/bin/commonmark
blob: f6e630b27502c8b6025203620b32021d80d38bfa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env node
"use strict";

var util = require('util');
var fs = require('fs');
var commonmark = require('../lib/index.js');

var inps = [];
var file;
var files = [];
var options = { sourcepos: false, time: false };
var format = 'html';
var i;

for (i = 2; i < process.argv.length; i++) {
    var arg = process.argv[i];
    if (arg === '--ast') {
        format = 'ast';
    } else if (arg === '--xml') {
        format = 'xml';
    } else if (arg === '--time') {
        options.time = true;
    } else if (arg === '--sourcepos') {
        options.sourcepos = true;
    } else if (/^--/.test(arg)) {
        process.stderr.write('Unknown option ' + arg + '\n');
        process.exit(1);
    } else {
      files.push(arg);
    }
}

var parser = new commonmark.DocParser(options);
var renderer;

if (format === 'html') {
    renderer = new commonmark.HtmlRenderer(options);
} else if (format === 'xml') {
    renderer = new commonmark.XmlRenderer(options);
} else if (format === 'ast') {
    renderer = { render: function(node) {
                   return util.inspect(node, null, 20, true) + '\n';
                 },
                 options: {} };
}

if (files.length === 0) {
  files = ['/dev/stdin'];
}

for (i = 0; i < files.length; i++) {
  file = files[i];
  inps.push(fs.readFileSync(file, 'utf8'));
}

var inp = inps.join('\n');
var doc = parser.parse(inp);

var rendered = renderer.render(doc);

if (!options.time) { process.stdout.write(rendered); }