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