Browse Source

Updated renderer signatures (pass env)

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
47432ce76e
  1. 105
      lib/renderer.js

105
lib/renderer.js

@ -56,23 +56,16 @@ function getBreak(tokens, idx) {
var rules = {}; var rules = {};
rules.abbr_open = function (tokens, idx/*, options*/) {
return '<abbr title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '">';
};
rules.abbr_close = function (/*tokens, idx, options*/) {
return '</abbr>';
};
rules.blockquote_open = function (/*tokens, idx, options*/) { rules.blockquote_open = function () {
return '<blockquote>\n'; return '<blockquote>\n';
}; };
rules.blockquote_close = function (tokens, idx /*, options*/) { rules.blockquote_close = function (tokens, idx) {
return '</blockquote>' + getBreak(tokens, idx); return '</blockquote>' + getBreak(tokens, idx);
}; };
rules.code = function (tokens, idx /*, options*/) { rules.code = function (tokens, idx) {
if (tokens[idx].block) { if (tokens[idx].block) {
return '<pre><code>' + escapeHtml(tokens[idx].content) + '</code></pre>' + getBreak(tokens, idx); return '<pre><code>' + escapeHtml(tokens[idx].content) + '</code></pre>' + getBreak(tokens, idx);
} }
@ -107,10 +100,10 @@ rules.fence = function (tokens, idx, options) {
}; };
rules.heading_open = function (tokens, idx /*, options*/) { rules.heading_open = function (tokens, idx) {
return '<h' + tokens[idx].hLevel + '>'; return '<h' + tokens[idx].hLevel + '>';
}; };
rules.heading_close = function (tokens, idx /*, options*/) { rules.heading_close = function (tokens, idx) {
return '</h' + tokens[idx].hLevel + '>\n'; return '</h' + tokens[idx].hLevel + '>\n';
}; };
@ -120,43 +113,43 @@ rules.hr = function (tokens, idx, options) {
}; };
rules.bullet_list_open = function (/*tokens, idx, options*/) { rules.bullet_list_open = function () {
return '<ul>\n'; return '<ul>\n';
}; };
rules.bullet_list_close = function (tokens, idx /*, options*/) { rules.bullet_list_close = function (tokens, idx) {
return '</ul>' + getBreak(tokens, idx); return '</ul>' + getBreak(tokens, idx);
}; };
rules.list_item_open = function (/*tokens, idx, options*/) { rules.list_item_open = function () {
return '<li>'; return '<li>';
}; };
rules.list_item_close = function (/*tokens, idx, options*/) { rules.list_item_close = function () {
return '</li>\n'; return '</li>\n';
}; };
rules.ordered_list_open = function (tokens, idx /*, options*/) { rules.ordered_list_open = function (tokens, idx) {
var token = tokens[idx]; var token = tokens[idx];
return '<ol' return '<ol'
+ (token.order > 1 ? ' start="' + token.order + '"' : '') + (token.order > 1 ? ' start="' + token.order + '"' : '')
+ '>\n'; + '>\n';
}; };
rules.ordered_list_close = function (tokens, idx /*, options*/) { rules.ordered_list_close = function (tokens, idx) {
return '</ol>' + getBreak(tokens, idx); return '</ol>' + getBreak(tokens, idx);
}; };
rules.paragraph_open = function (tokens, idx/*, options*/) { rules.paragraph_open = function (tokens, idx) {
return tokens[idx].tight ? '' : '<p>'; return tokens[idx].tight ? '' : '<p>';
}; };
rules.paragraph_close = function (tokens, idx /*, options*/) { rules.paragraph_close = function (tokens, idx) {
var addBreak = !(tokens[idx].tight && idx && tokens[idx - 1].type === 'inline' && !tokens[idx - 1].content); var addBreak = !(tokens[idx].tight && idx && tokens[idx - 1].type === 'inline' && !tokens[idx - 1].content);
return (tokens[idx].tight ? '' : '</p>') + (addBreak ? getBreak(tokens, idx) : ''); return (tokens[idx].tight ? '' : '</p>') + (addBreak ? getBreak(tokens, idx) : '');
}; };
rules.link_open = function (tokens, idx /*, options*/) { rules.link_open = function (tokens, idx) {
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : ''; var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : '';
return '<a href="' + escapeHtml(encodeURI(decodeURI(replaceEntities(tokens[idx].href)))) + '"' + title + '>'; return '<a href="' + escapeHtml(encodeURI(decodeURI(replaceEntities(tokens[idx].href)))) + '"' + title + '>';
}; };
rules.link_close = function (/*tokens, idx, options*/) { rules.link_close = function () {
return '</a>'; return '</a>';
}; };
@ -170,84 +163,84 @@ rules.image = function (tokens, idx, options) {
}; };
rules.table_open = function (/*tokens, idx, options*/) { rules.table_open = function () {
return '<table>\n'; return '<table>\n';
}; };
rules.table_close = function (/*tokens, idx, options*/) { rules.table_close = function () {
return '</table>\n'; return '</table>\n';
}; };
rules.thead_open = function (/*tokens, idx, options*/) { rules.thead_open = function () {
return '<thead>\n'; return '<thead>\n';
}; };
rules.thead_close = function (/*tokens, idx, options*/) { rules.thead_close = function () {
return '</thead>\n'; return '</thead>\n';
}; };
rules.tbody_open = function (/*tokens, idx, options*/) { rules.tbody_open = function () {
return '<tbody>\n'; return '<tbody>\n';
}; };
rules.tbody_close = function (/*tokens, idx, options*/) { rules.tbody_close = function () {
return '</tbody>\n'; return '</tbody>\n';
}; };
rules.tr_open = function (/*tokens, idx, options*/) { rules.tr_open = function () {
return '<tr>'; return '<tr>';
}; };
rules.tr_close = function (/*tokens, idx, options*/) { rules.tr_close = function () {
return '</tr>\n'; return '</tr>\n';
}; };
rules.th_open = function (tokens, idx /*, options*/) { rules.th_open = function (tokens, idx) {
var token = tokens[idx]; var token = tokens[idx];
return '<th' return '<th'
+ (token.align ? ' style="text-align:' + token.align + '"' : '') + (token.align ? ' style="text-align:' + token.align + '"' : '')
+ '>'; + '>';
}; };
rules.th_close = function (/*tokens, idx, options*/) { rules.th_close = function () {
return '</th>'; return '</th>';
}; };
rules.td_open = function (tokens, idx /*, options*/) { rules.td_open = function (tokens, idx) {
var token = tokens[idx]; var token = tokens[idx];
return '<td' return '<td'
+ (token.align ? ' style="text-align:' + token.align + '"' : '') + (token.align ? ' style="text-align:' + token.align + '"' : '')
+ '>'; + '>';
}; };
rules.td_close = function (/*tokens, idx, options*/) { rules.td_close = function () {
return '</td>'; return '</td>';
}; };
rules.strong_open = function(/*tokens, idx, options*/) { rules.strong_open = function() {
return '<strong>'; return '<strong>';
}; };
rules.strong_close = function(/*tokens, idx, options*/) { rules.strong_close = function() {
return '</strong>'; return '</strong>';
}; };
rules.em_open = function(/*tokens, idx, options*/) { rules.em_open = function() {
return '<em>'; return '<em>';
}; };
rules.em_close = function(/*tokens, idx, options*/) { rules.em_close = function() {
return '</em>'; return '</em>';
}; };
rules.del_open = function(/*tokens, idx, options*/) { rules.del_open = function() {
return '<del>'; return '<del>';
}; };
rules.del_close = function(/*tokens, idx, options*/) { rules.del_close = function() {
return '</del>'; return '</del>';
}; };
rules.ins_open = function(/*tokens, idx, options*/) { rules.ins_open = function() {
return '<ins>'; return '<ins>';
}; };
rules.ins_close = function(/*tokens, idx, options*/) { rules.ins_close = function() {
return '</ins>'; return '</ins>';
}; };
rules.mark_open = function(/*tokens, idx, options*/) { rules.mark_open = function() {
return '<mark>'; return '<mark>';
}; };
rules.mark_close = function(/*tokens, idx, options*/) { rules.mark_close = function() {
return '</mark>'; return '</mark>';
}; };
@ -268,19 +261,27 @@ rules.softbreak = function (tokens, idx, options) {
}; };
rules.text = function (tokens, idx /*, options*/) { rules.text = function (tokens, idx) {
return escapeHtml(tokens[idx].content); return escapeHtml(tokens[idx].content);
}; };
rules.htmlblock = function (tokens, idx /*, options*/) { rules.htmlblock = function (tokens, idx) {
return tokens[idx].content; return tokens[idx].content;
}; };
rules.htmltag = function (tokens, idx /*, options*/) { rules.htmltag = function (tokens, idx) {
return tokens[idx].content; return tokens[idx].content;
}; };
rules.abbr_open = function (tokens, idx) {
return '<abbr title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '">';
};
rules.abbr_close = function () {
return '</abbr>';
};
// Renderer class // Renderer class
function Renderer() { function Renderer() {
// Clone rules object to allow local modifications // Clone rules object to allow local modifications
@ -288,28 +289,28 @@ function Renderer() {
} }
Renderer.prototype.renderInline = function (tokens, options) { Renderer.prototype.renderInline = function (tokens, options, env) {
var result = '', var result = '',
_rules = this.rules; _rules = this.rules;
for (var i = 0, len = tokens.length; i < len; i++) { for (var i = 0, len = tokens.length; i < len; i++) {
result += _rules[tokens[i].type](tokens, i, options); result += _rules[tokens[i].type](tokens, i, options, env);
} }
return result; return result;
}; };
Renderer.prototype.render = function (tokens, options) { Renderer.prototype.render = function (tokens, options, env) {
var i, len, var i, len,
result = '', result = '',
_rules = this.rules; _rules = this.rules;
for (i = 0, len = tokens.length; i < len; i++) { for (i = 0, len = tokens.length; i < len; i++) {
if (tokens[i].type === 'inline') { if (tokens[i].type === 'inline') {
result += this.renderInline(tokens[i].children, options); result += this.renderInline(tokens[i].children, options, env);
} else { } else {
result += _rules[tokens[i].type](tokens, i, options); result += _rules[tokens[i].type](tokens, i, options, env);
} }
} }

Loading…
Cancel
Save