Browse Source

Added renderer rules rewrite test

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
f8e270f6e0
  1. 98
      lib/renderer.js
  2. 12
      test/misc.js

98
lib/renderer.js

@ -57,15 +57,15 @@ var rules = {};
rules.blockquote_open = function () {
rules.blockquote_open = function (/* tokens, idx, options, env */) {
return '<blockquote>\n';
};
rules.blockquote_close = function (tokens, idx) {
rules.blockquote_close = function (tokens, idx /*, options, env */) {
return '</blockquote>' + getBreak(tokens, idx);
};
rules.code = function (tokens, idx) {
rules.code = function (tokens, idx /*, options, env */) {
if (tokens[idx].block) {
return '<pre><code>' + escapeHtml(tokens[idx].content) + '</code></pre>' + getBreak(tokens, idx);
}
@ -74,7 +74,7 @@ rules.code = function (tokens, idx) {
};
rules.fence = function (tokens, idx, options) {
rules.fence = function (tokens, idx, options /*, env */) {
var token = tokens[idx];
var langClass = '';
var langPrefix = options.langPrefix;
@ -100,61 +100,61 @@ rules.fence = function (tokens, idx, options) {
};
rules.heading_open = function (tokens, idx) {
rules.heading_open = function (tokens, idx /*, options, env */) {
return '<h' + tokens[idx].hLevel + '>';
};
rules.heading_close = function (tokens, idx) {
rules.heading_close = function (tokens, idx /*, options, env */) {
return '</h' + tokens[idx].hLevel + '>\n';
};
rules.hr = function (tokens, idx, options) {
rules.hr = function (tokens, idx, options /*, env */) {
return (options.xhtmlOut ? '<hr />' : '<hr>') + getBreak(tokens, idx);
};
rules.bullet_list_open = function () {
rules.bullet_list_open = function (/* tokens, idx, options, env */) {
return '<ul>\n';
};
rules.bullet_list_close = function (tokens, idx) {
rules.bullet_list_close = function (tokens, idx /*, options, env */) {
return '</ul>' + getBreak(tokens, idx);
};
rules.list_item_open = function () {
rules.list_item_open = function (/* tokens, idx, options, env */) {
return '<li>';
};
rules.list_item_close = function () {
rules.list_item_close = function (/* tokens, idx, options, env */) {
return '</li>\n';
};
rules.ordered_list_open = function (tokens, idx) {
rules.ordered_list_open = function (tokens, idx /*, options, env */) {
var token = tokens[idx];
return '<ol'
+ (token.order > 1 ? ' start="' + token.order + '"' : '')
+ '>\n';
};
rules.ordered_list_close = function (tokens, idx) {
rules.ordered_list_close = function (tokens, idx /*, options, env */) {
return '</ol>' + getBreak(tokens, idx);
};
rules.paragraph_open = function (tokens, idx) {
rules.paragraph_open = function (tokens, idx /*, options, env */) {
return tokens[idx].tight ? '' : '<p>';
};
rules.paragraph_close = function (tokens, idx) {
rules.paragraph_close = function (tokens, idx /*, options, env */) {
var addBreak = !(tokens[idx].tight && idx && tokens[idx - 1].type === 'inline' && !tokens[idx - 1].content);
return (tokens[idx].tight ? '' : '</p>') + (addBreak ? getBreak(tokens, idx) : '');
};
rules.link_open = function (tokens, idx) {
rules.link_open = function (tokens, idx /*, options, env */) {
var title = tokens[idx].title ? (' title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '"') : '';
return '<a href="' + escapeHtml(encodeURI(decodeURI(replaceEntities(tokens[idx].href)))) + '"' + title + '>';
};
rules.link_close = function () {
rules.link_close = function (/* tokens, idx, options, env */) {
return '</a>';
};
rules.image = function (tokens, idx, options) {
rules.image = function (tokens, idx, options /*, env */) {
var src = ' src="' + escapeHtml(encodeURI(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)) : '') + '"';
@ -163,121 +163,121 @@ rules.image = function (tokens, idx, options) {
};
rules.table_open = function () {
rules.table_open = function (/* tokens, idx, options, env */) {
return '<table>\n';
};
rules.table_close = function () {
rules.table_close = function (/* tokens, idx, options, env */) {
return '</table>\n';
};
rules.thead_open = function () {
rules.thead_open = function (/* tokens, idx, options, env */) {
return '<thead>\n';
};
rules.thead_close = function () {
rules.thead_close = function (/* tokens, idx, options, env */) {
return '</thead>\n';
};
rules.tbody_open = function () {
rules.tbody_open = function (/* tokens, idx, options, env */) {
return '<tbody>\n';
};
rules.tbody_close = function () {
rules.tbody_close = function (/* tokens, idx, options, env */) {
return '</tbody>\n';
};
rules.tr_open = function () {
rules.tr_open = function (/* tokens, idx, options, env */) {
return '<tr>';
};
rules.tr_close = function () {
rules.tr_close = function (/* tokens, idx, options, env */) {
return '</tr>\n';
};
rules.th_open = function (tokens, idx) {
rules.th_open = function (tokens, idx /*, options, env */) {
var token = tokens[idx];
return '<th'
+ (token.align ? ' style="text-align:' + token.align + '"' : '')
+ '>';
};
rules.th_close = function () {
rules.th_close = function (/* tokens, idx, options, env */) {
return '</th>';
};
rules.td_open = function (tokens, idx) {
rules.td_open = function (tokens, idx /*, options, env */) {
var token = tokens[idx];
return '<td'
+ (token.align ? ' style="text-align:' + token.align + '"' : '')
+ '>';
};
rules.td_close = function () {
rules.td_close = function (/* tokens, idx, options, env */) {
return '</td>';
};
rules.strong_open = function() {
rules.strong_open = function (/* tokens, idx, options, env */) {
return '<strong>';
};
rules.strong_close = function() {
rules.strong_close = function (/* tokens, idx, options, env */) {
return '</strong>';
};
rules.em_open = function() {
rules.em_open = function (/* tokens, idx, options, env */) {
return '<em>';
};
rules.em_close = function() {
rules.em_close = function (/* tokens, idx, options, env */) {
return '</em>';
};
rules.del_open = function() {
rules.del_open = function (/* tokens, idx, options, env */) {
return '<del>';
};
rules.del_close = function() {
rules.del_close = function (/* tokens, idx, options, env */) {
return '</del>';
};
rules.ins_open = function() {
rules.ins_open = function (/* tokens, idx, options, env */) {
return '<ins>';
};
rules.ins_close = function() {
rules.ins_close = function (/* tokens, idx, options, env */) {
return '</ins>';
};
rules.mark_open = function() {
rules.mark_open = function (/* tokens, idx, options, env */) {
return '<mark>';
};
rules.mark_close = function() {
rules.mark_close = function (/* tokens, idx, options, env */) {
return '</mark>';
};
rules.sub = function(tokens, idx/*, options*/) {
rules.sub = function (tokens, idx /*, options, env */) {
return '<sub>' + escapeHtml(tokens[idx].content) + '</sub>';
};
rules.sup = function(tokens, idx/*, options*/) {
rules.sup = function (tokens, idx /*, options, env */) {
return '<sup>' + escapeHtml(tokens[idx].content) + '</sup>';
};
rules.hardbreak = function (tokens, idx, options) {
rules.hardbreak = function (tokens, idx, options /*, env */) {
return options.xhtmlOut ? '<br />\n' : '<br>\n';
};
rules.softbreak = function (tokens, idx, options) {
rules.softbreak = function (tokens, idx, options /*, env */) {
return options.breaks ? (options.xhtmlOut ? '<br />\n' : '<br>\n') : '\n';
};
rules.text = function (tokens, idx) {
rules.text = function (tokens, idx /*, options, env */) {
return escapeHtml(tokens[idx].content);
};
rules.htmlblock = function (tokens, idx) {
rules.htmlblock = function (tokens, idx /*, options, env */) {
return tokens[idx].content;
};
rules.htmltag = function (tokens, idx) {
rules.htmltag = function (tokens, idx /*, options, env */) {
return tokens[idx].content;
};
rules.abbr_open = function (tokens, idx) {
rules.abbr_open = function (tokens, idx /*, options, env */) {
return '<abbr title="' + escapeHtml(replaceEntities(tokens[idx].title)) + '">';
};
rules.abbr_close = function () {
rules.abbr_close = function (/* tokens, idx, options, env */) {
return '</abbr>';
};

12
test/misc.js

@ -127,6 +127,7 @@ describe('API', function () {
assert.strictEqual(md.render('![]()'), '<p><img src="" alt=""></p>\n');
assert.strictEqual(md.render('a \\\nb'), '<p>a <br>\nb</p>\n');
});
});
@ -151,6 +152,17 @@ describe('Misc', function () {
assert.strictEqual(md.renderInline('a *b* c'), 'a <em>b</em> c');
});
it('Renderer should have pluggable inline and block rules', function () {
var md = new Remarkable();
md.renderer.rules.em_open = function () { return '<it>'; };
md.renderer.rules.em_close = function () { return '</it>'; };
md.renderer.rules.paragraph_open = function () { return '<par>'; };
md.renderer.rules.paragraph_close = function () { return '</par>'; };
assert.strictEqual(md.render('*b*'), '<par><it>b</it></par>');
});
});

Loading…
Cancel
Save