aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn MacFarlane <jgm@berkeley.edu>2015-01-10 17:17:09 -0800
committerJohn MacFarlane <jgm@berkeley.edu>2015-01-10 17:17:09 -0800
commit2fe4743a1303ca5dcba4018519a044e9de8e11db (patch)
treee5ce227f23b113c3bc2b8bbb9f819d15618cec7c
parent0f3d82b846fb70835a059542405e849a59fa2e0a (diff)
Made 'options' a parameter of the renderer constructors...
as opposed to the 'render' function. Added a 'colors' parameter for the ast renderer.
-rw-r--r--dingus.html3
-rwxr-xr-xjs/bin/commonmark17
-rw-r--r--js/lib/html.js7
-rwxr-xr-xjs/lib/index.js14
4 files changed, 28 insertions, 13 deletions
diff --git a/dingus.html b/dingus.html
index 6b379eb..ee1622f 100644
--- 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
index 22ebd62..e48e05e 100755
--- 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
index 9cb1676..9f0c4e3 100644
--- 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
index d0532c6..22a2184 100755
--- 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 || {}
+ };
+}