aboutsummaryrefslogtreecommitdiff
path: root/js/bin/commonmark
blob: 6c9955c207bbe119a8823eded6a67f884da2419e (plain)
  1. #!/usr/bin/env node
  2. "use strict";
  3. var fs = require('fs');
  4. var commonmark = require('../lib/index.js');
  5. var inps = [];
  6. var file;
  7. var files = [];
  8. var options = { sourcepos: false };
  9. var format = 'html';
  10. var time = false;
  11. var i;
  12. for (i = 2; i < process.argv.length; i++) {
  13. var arg = process.argv[i];
  14. if (arg === '--ast') {
  15. format = 'ast';
  16. } else if (arg === '--time') {
  17. time = true;
  18. } else if (arg === '--sourcepos') {
  19. options.sourcepos = true;
  20. } else if (/^--/.test(arg)) {
  21. process.stderr.write('Unknown option ' + arg + '\n');
  22. process.exit(1);
  23. } else {
  24. files.push(arg);
  25. }
  26. }
  27. var parser = new commonmark.DocParser();
  28. var renderer;
  29. if (format === 'html') {
  30. renderer = new commonmark.HtmlRenderer(options);
  31. } else if (format === 'ast') {
  32. renderer = new commonmark.ASTRenderer(options);
  33. renderer.options.colors = true;
  34. }
  35. if (files.length === 0) {
  36. files = ['/dev/stdin'];
  37. }
  38. for (i = 0; i < files.length; i++) {
  39. file = files[i];
  40. inps.push(fs.readFileSync(file, 'utf8'));
  41. }
  42. var inp = inps.join('\n');
  43. if (time) { console.time("parsing"); }
  44. var doc = parser.parse(inp);
  45. if (time) { console.timeEnd("parsing"); }
  46. if (time) { console.time("rendering"); }
  47. var rendered = renderer.render(doc);
  48. if (time) { console.timeEnd("rendering"); }
  49. if (!time) { process.stdout.write(rendered); }