aboutsummaryrefslogtreecommitdiff
path: root/js/bin/commonmark
blob: 6570ef61b346f5a58d30b240d7ff7f694cdea420 (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, time: false };
  9. var format = 'html';
  10. var i;
  11. for (i = 2; i < process.argv.length; i++) {
  12. var arg = process.argv[i];
  13. if (arg === '--ast') {
  14. format = 'ast';
  15. } else if (arg === '--xml') {
  16. format = 'xml';
  17. } else if (arg === '--time') {
  18. options.time = true;
  19. } else if (arg === '--sourcepos') {
  20. options.sourcepos = true;
  21. } else if (/^--/.test(arg)) {
  22. process.stderr.write('Unknown option ' + arg + '\n');
  23. process.exit(1);
  24. } else {
  25. files.push(arg);
  26. }
  27. }
  28. var parser = new commonmark.DocParser(options);
  29. var renderer;
  30. if (format === 'html') {
  31. renderer = new commonmark.HtmlRenderer(options);
  32. } else if (format === 'xml') {
  33. renderer = new commonmark.XMLRenderer(options);
  34. } else if (format === 'ast') {
  35. renderer = new commonmark.ASTRenderer(options);
  36. renderer.options.colors = true;
  37. }
  38. if (files.length === 0) {
  39. files = ['/dev/stdin'];
  40. }
  41. for (i = 0; i < files.length; i++) {
  42. file = files[i];
  43. inps.push(fs.readFileSync(file, 'utf8'));
  44. }
  45. var inp = inps.join('\n');
  46. var doc = parser.parse(inp);
  47. var rendered = renderer.render(doc);
  48. if (!options.time) { process.stdout.write(rendered); }