aboutsummaryrefslogtreecommitdiff
path: root/js/bin/commonmark
blob: 4fcb09d17759e2438e0db0d141025afe9cc84937 (plain)
  1. #!/usr/bin/env node
  2. "use strict";
  3. var fs = require('fs');
  4. var util = require('util');
  5. var commonmark = require('../lib/index.js');
  6. var parser = new commonmark.DocParser();
  7. var renderer = new commonmark.HtmlRenderer();
  8. var inps = [];
  9. var file;
  10. var files = [];
  11. var options = { sourcepos: false, };
  12. for (var i = 2; i < process.argv.length; i++) {
  13. var arg = process.argv[i];
  14. if (arg == '--ast') {
  15. renderer = { render: function(x) {
  16. return util.inspect(x.toAST(), null, 20, true) + '\n';
  17. } };
  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. if (files.length === 0) {
  28. files = ['/dev/stdin'];
  29. }
  30. for (var i = 0; i < files.length; i++) {
  31. file = files[i];
  32. inps.push(fs.readFileSync(file, 'utf8'));
  33. }
  34. process.stdout.write(renderer.render(parser.parse(inps.join('\n')), options));