cmark

My personal build of CMark ✏️

Commit
2fe4743a1303ca5dcba4018519a044e9de8e11db
Parent
0f3d82b846fb70835a059542405e849a59fa2e0a
Author
John MacFarlane <jgm@berkeley.edu>
Date

Made 'options' a parameter of the renderer constructors...

as opposed to the 'render' function.

Added a 'colors' parameter for the ast renderer.

Diffstat

4 files changed, 28 insertions, 13 deletions

Status File Name N° Changes Insertions Deletions
Modified dingus.html 3 2 1
Modified js/bin/commonmark 17 13 4
Modified js/lib/html.js 7 4 3
Modified js/lib/index.js 14 9 5
diff --git a/dingus.html b/dingus.html
@@ -10,6 +10,7 @@
   <script type="text/javascript">
 
 var writer = new commonmark.HtmlRenderer();
+var astwriter = new commonmark.ASTRenderer();
 var reader = new commonmark.DocParser();
 
 function getQueryVariable(variable) {
@@ -57,7 +58,7 @@ $(document).ready(function() {
     var renderTime = endTime - startTime;
     $("#preview").html(result);
     $("#html").text(result);
-    $("#ast").text(commonmark.ASTRenderer(parsed));
+    $("#ast").text(astwriter.render(parsed));
     $("#rendertime").text(renderTime);
   };
   var parseAndRender = function() {
diff --git a/js/bin/commonmark b/js/bin/commonmark
@@ -4,18 +4,17 @@
 var fs = require('fs');
 var commonmark = require('../lib/index.js');
 
-var parser = new commonmark.DocParser();
-var renderer = new commonmark.HtmlRenderer();
 var inps = [];
 var file;
 var files = [];
 var options = { sourcepos: false };
+var format = 'html';
 var i;
 
 for (i = 2; i < process.argv.length; i++) {
     var arg = process.argv[i];
     if (arg === '--ast') {
-        renderer = { render: commonmark.ASTRenderer };
+        format = 'ast';
     } else if (arg === '--sourcepos') {
         options.sourcepos = true;
     } else if (/^--/.test(arg)) {
@@ -26,6 +25,16 @@ for (i = 2; i < process.argv.length; i++) {
     }
 }
 
+var parser = new commonmark.DocParser();
+var renderer;
+
+if (format === 'html') {
+    renderer = new commonmark.HtmlRenderer(options);
+} else if (format === 'ast') {
+    renderer = new commonmark.ASTRenderer(options);
+    renderer.options.colors = true;
+}
+
 if (files.length === 0) {
   files = ['/dev/stdin'];
 }
@@ -35,4 +44,4 @@ for (i = 0; i < files.length; i++) {
   inps.push(fs.readFileSync(file, 'utf8'));
 }
 
-process.stdout.write(renderer.render(parser.parse(inps.join('\n')), options));
+process.stdout.write(renderer.render(parser.parse(inps.join('\n'))));
diff --git a/js/lib/html.js b/js/lib/html.js
@@ -21,7 +21,7 @@ var tag = function(name, attrs, selfclosing) {
 
 var reHtmlTag = /\<[^>]*\>/;
 
-var renderNodes = function(block, options) {
+var renderNodes = function(block) {
 
     var attrs;
     var info_words;
@@ -48,7 +48,7 @@ var renderNodes = function(block, options) {
         }
     };
 
-    options = options || {};
+    var options = this.options;
 
     while ((event = walker.next())) {
         entering = event.entering;
@@ -250,7 +250,7 @@ var replaceUnsafeChar = function(s) {
 var reNeedsEscaping = /[&<>"]/;
 
 // The HtmlRenderer object.
-function HtmlRenderer(){
+function HtmlRenderer(options){
     return {
         // default options:
         softbreak: '\n', // by default, soft breaks are rendered as newlines in HTML
@@ -267,6 +267,7 @@ function HtmlRenderer(){
                 return s;
             }
         },
+        options: options || {},
         render: renderNodes
     };
 }
diff --git a/js/lib/index.js b/js/lib/index.js
@@ -13,11 +13,15 @@
 
 var util = require('util');
 
-var renderAST = function(tree) {
-    return util.inspect(tree.toAST(), {depth: 20}) + '\n';
-};
-
 module.exports.Node = require('./node');
 module.exports.DocParser = require('./blocks');
 module.exports.HtmlRenderer = require('./html');
-module.exports.ASTRenderer = renderAST;
+module.exports.ASTRenderer = function(options) {
+    return {
+        render: function(tree) {
+            return util.inspect(tree.toAST(), null, 20,
+                                this.options.colors) + '\n';
+        },
+        options: options || {}
+    };
+}