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