summaryrefslogtreecommitdiff
path: root/js/lib/html.js
diff options
context:
space:
mode:
Diffstat (limited to 'js/lib/html.js')
-rw-r--r--js/lib/html.js50
1 files changed, 29 insertions, 21 deletions
diff --git a/js/lib/html.js b/js/lib/html.js
index 26c677b..847ed98 100644
--- a/js/lib/html.js
+++ b/js/lib/html.js
@@ -19,31 +19,38 @@ var tag = function(name, attrs, selfclosing) {
return result;
};
-var renderNodes = function(block, options) {
+var reHtmlTag = /\<[^>]*\>/;
+
+var renderNodes = function(block) {
var attrs;
var info_words;
var tagname;
var walker = block.walker();
var event, node, entering;
- var buffer = [];
+ var buffer = "";
+ var lastOut = "\n";
var disableTags = 0;
var grandparent;
var out = function(s) {
if (disableTags > 0) {
- buffer.push(s.replace(/\<[^>]*\>/g, ''));
+ buffer += s.replace(reHtmlTag, '');
} else {
- buffer.push(s);
+ buffer += s;
}
+ lastOut = s;
};
var esc = this.escape;
var cr = function() {
- if (buffer.length > 0 && buffer[buffer.length - 1] !== '\n') {
- out('\n');
+ if (lastOut !== '\n') {
+ buffer += '\n';
+ lastOut = '\n';
}
};
- options = options || {};
+ var options = this.options;
+
+ if (options.time) { console.time("rendering"); }
while ((event = walker.next())) {
entering = event.entering;
@@ -81,10 +88,6 @@ var renderNodes = function(block, options) {
out(tag(entering ? 'strong' : '/strong'));
break;
- case 'Emph':
- out(tag(entering ? 'strong' : '/strong'));
- break;
-
case 'Html':
out(node.literal);
break;
@@ -198,7 +201,7 @@ var renderNodes = function(block, options) {
}
cr();
out(tag('pre') + tag('code', attrs));
- out(this.escape(node.literal));
+ out(esc(node.literal));
out(tag('/code') + tag('/pre'));
cr();
break;
@@ -220,14 +223,15 @@ var renderNodes = function(block, options) {
break;
default:
- console.log("Unknown node type " + node.t);
+ throw("Unknown node type " + node.t);
}
}
- return buffer.join('');
+ if (options.time) { console.timeEnd("rendering"); }
+ return buffer;
};
-var sub = function(s) {
+var replaceUnsafeChar = function(s) {
switch (s) {
case '&':
return '&amp;';
@@ -242,23 +246,27 @@ var sub = function(s) {
}
};
+var reNeedsEscaping = /[&<>"]/;
// The HtmlRenderer object.
-function HtmlRenderer(){
+function HtmlRenderer(options){
return {
// default options:
- blocksep: '\n', // space between blocks
- innersep: '\n', // space between block container tag and contents
softbreak: '\n', // by default, soft breaks are rendered as newlines in HTML
// set to "<br />" to make them hard breaks
// set to " " if you want to ignore line wrapping in source
escape: function(s, preserve_entities) {
- if (preserve_entities) {
- return s.replace(/[&](?:[#](x[a-f0-9]{1,8}|[0-9]{1,8});|[a-z][a-z0-9]{1,31};)|[&<>"]/gi, sub);
+ if (reNeedsEscaping.test(s)) {
+ if (preserve_entities) {
+ return s.replace(/[&](?:[#](x[a-f0-9]{1,8}|[0-9]{1,8});|[a-z][a-z0-9]{1,31};)|[&<>"]/gi, replaceUnsafeChar);
+ } else {
+ return s.replace(/[&<>"]/g, replaceUnsafeChar);
+ }
} else {
- return s.replace(/[&<>"]/g, sub);
+ return s;
}
},
+ options: options || {},
render: renderNodes
};
}