blob: 6c9955c207bbe119a8823eded6a67f884da2419e (
plain)
- #!/usr/bin/env node
- "use strict";
- var fs = require('fs');
- var commonmark = require('../lib/index.js');
- var inps = [];
- var file;
- var files = [];
- var options = { sourcepos: false };
- var format = 'html';
- var time = false;
- var i;
- for (i = 2; i < process.argv.length; i++) {
- var arg = process.argv[i];
- if (arg === '--ast') {
- format = 'ast';
- } else if (arg === '--time') {
- 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();
- var renderer;
- if (format === 'html') {
- renderer = new commonmark.HtmlRenderer(options);
- } else if (format === 'ast') {
- renderer = new commonmark.ASTRenderer(options);
- renderer.options.colors = true;
- }
- 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');
- if (time) { console.time("parsing"); }
- var doc = parser.parse(inp);
- if (time) { console.timeEnd("parsing"); }
- if (time) { console.time("rendering"); }
- var rendered = renderer.render(doc);
- if (time) { console.timeEnd("rendering"); }
- if (!time) { process.stdout.write(rendered); }
|