'use strict';
var assign = require('./common/utils').assign;
var escapeHtml = require('./helpers').escapeHtml;
var unescapeMd = require('./helpers').unescapeMd;
var replaceEntities = require('./helpers').replaceEntities;
function escapeUrl(str) {
try {
return encodeURI(str);
} catch (__) {}
return '';
}
function unescapeUrl(str) {
try {
return decodeURI(str);
} catch (__) {}
return '';
}
// check if we need to hide '\n' before next token
function getBreak(tokens, idx) {
if (++idx < tokens.length &&
tokens[idx].type === 'list_item_close') {
return '';
}
return '\n';
}
var rules = {};
rules.blockquote_open = function (/*tokens, idx, options*/) {
return '
\n';
};
rules.blockquote_close = function (tokens, idx /*, options*/) {
return '
' + getBreak(tokens, idx);
};
rules.code = function (tokens, idx /*, options*/) {
if (tokens[idx].block) {
return '' + escapeHtml(tokens[idx].content) + '
' + getBreak(tokens, idx);
}
return '' + escapeHtml(tokens[idx].content) + '
';
};
rules.fence = function (tokens, idx, options) {
var token = tokens[idx];
var langClass = '';
var langPrefix = options.langPrefix || '';
var params, langName = '';
var highlighted;
if (token.params) {
params = token.params.split(/ +/g);
langName = escapeHtml(replaceEntities(unescapeMd(params[0])));
langClass = ' class="' + langPrefix + langName + '"';
}
highlighted = options.highlight(token.content, langName) || escapeHtml(token.content);
return ''
+ highlighted
+ '
' + getBreak(tokens, idx);
};
rules.heading_open = function (tokens, idx /*, options*/) {
return '';
};
rules.heading_close = function (tokens, idx /*, options*/) {
return '\n';
};
rules.hr = function (tokens, idx, options) {
return (options.xhtmlOut ? '
' : '
') + getBreak(tokens, idx);
};
rules.bullet_list_open = function (/*tokens, idx, options*/) {
return '\n';
};
rules.bullet_list_close = function (tokens, idx /*, options*/) {
return '
' + getBreak(tokens, idx);
};
rules.list_item_open = function (/*tokens, idx, options*/) {
return '';
};
rules.list_item_close = function (/*tokens, idx, options*/) {
return '\n';
};
rules.ordered_list_open = function (tokens, idx /*, options*/) {
var token = tokens[idx];
return ' 1 ? ' start="' + token.order + '"' : '')
+ '>\n';
};
rules.ordered_list_close = function (tokens, idx /*, options*/) {
return '
' + getBreak(tokens, idx);
};
rules.paragraph_open = function (/*tokens, idx, options*/) {
return '';
};
rules.paragraph_close = function (tokens, idx /*, options*/) {
return '
' + getBreak(tokens, idx);
};
rules.link_open = function (tokens, idx /*, options*/) {
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : '';
return '';
};
rules.link_close = function (/*tokens, idx, options*/) {
return '';
};
rules.image = function (tokens, idx, options) {
var src = ' src="' + escapeHtml(escapeUrl(tokens[idx].src)) + '"';
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : '';
var alt = ' alt="' + (tokens[idx].alt ? escapeHtml(replaceEntities(tokens[idx].alt)) : '') + '"';
var suffix = options.xhtmlOut ? ' /' : '';
return '';
};
rules.table_open = function (/*tokens, idx, options*/) {
return '\n';
};
rules.table_close = function (/*tokens, idx, options*/) {
return '
\n';
};
rules.tr_open = function (/*tokens, idx, options*/) {
return '\n';
};
rules.tr_close = function (/*tokens, idx, options*/) {
return '
\n';
};
rules.th_open = function (tokens, idx /*, options*/) {
var token = tokens[idx];
return '';
};
rules.th_close = function (/*tokens, idx, options*/) {
return ' | \n';
};
rules.td_open = function (tokens, idx /*, options*/) {
var token = tokens[idx];
return '';
};
rules.td_close = function (/*tokens, idx, options*/) {
return ' | \n';
};
rules.strong_open = function(/*tokens, idx, options*/) {
return '';
};
rules.strong_close = function(/*tokens, idx, options*/) {
return '';
};
rules.em_open = function(/*tokens, idx, options*/) {
return '';
};
rules.em_close = function(/*tokens, idx, options*/) {
return '';
};
rules.del_open = function(/*tokens, idx, options*/) {
return '';
};
rules.del_close = function(/*tokens, idx, options*/) {
return '';
};
rules.hardbreak = function (tokens, idx, options) {
return options.xhtmlOut ? '
\n' : '
\n';
};
rules.softbreak = function (tokens, idx, options) {
return options.breaks ? (options.xhtmlOut ? '
\n' : '
\n') : '\n';
};
rules.text = function (tokens, idx /*, options*/) {
return tokens[idx].content;
};
rules.htmlblock = function (tokens, idx /*, options*/) {
return tokens[idx].content;
};
rules.htmltag = function (tokens, idx /*, options*/) {
return tokens[idx].content;
};
// Renderer class
function Renderer() {
// Clone rules object to allow local modifications
this.rules = assign({}, rules);
}
Renderer.prototype.render = function (tokens, options) {
var i, len, rule, name, next,
result = '',
rules = this.rules,
tightStack = [];
// wrap paragraphs on top level by default
var tight = false;
for (i = 0, len = tokens.length; i < len; i++) {
name = tokens[i].type;
rule = rules[name];
// Dirty stack machine to track lists style (loose/tight)
if (name === 'ordered_list_open' || name === 'bullet_list_open') {
tightStack.push(tight);
tight = tokens[i].tight;
}
if (name === 'ordered_list_close' || name === 'bullet_list_close') {
tight = tightStack.pop();
}
if (name === 'blockquote_open') {
tightStack.push(tight);
tight = false;
}
if (name === 'blockquote_close') {
tight = tightStack.pop();
}
// in tight mode just ignore paragraphs for lists
// TODO - track right nesting to blockquotes
if (name === 'paragraph_open' && tight) {
continue;
}
if (name === 'paragraph_close' && tight) {
// Quick hack - texts should have LF if followed by blocks
if (i + 1 < tokens.length) {
next = tokens[i + 1].type;
if (next === 'bullet_list_open' ||
next === 'ordered_list_open' ||
next === 'blockquote_open') {
result += '\n';
}
}
continue;
}
if (tokens[i].type === 'inline') {
result += this.render(tokens[i].children, options);
} else {
// TODO: temporary check
if (!rule) {
throw new Error('Renderer error: unknown token ' + name);
}
result += rule(tokens, i, options);
}
}
return result;
};
module.exports = Renderer;