showdown/src/subParsers/codeBlocks.js

43 lines
1.3 KiB
JavaScript
Raw Normal View History

2015-01-16 05:21:33 +08:00
/**
* Process Markdown `<pre><code>` blocks.
*/
showdown.subParser('codeBlocks', function (text, options, globals) {
2015-01-19 19:37:21 +08:00
'use strict';
/*
text = text.replace(text,
/(?:\n\n|^)
( // $1 = the code block -- one or more lines, starting with a space/tab
(?:
(?:[ ]{4}|\t) // Lines must start with a tab or a tab-width of spaces - attacklab: g_tab_width
.*\n+
)+
)
(\n*[ ]{0,3}[^ \t\n]|(?=~0)) // attacklab: g_tab_width
/g,function(){...});
*/
// attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug
text += '~0';
var pattern = /(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g;
text = text.replace(pattern, function (wholeMatch, m1, m2) {
var codeblock = m1, nextChar = m2;
2015-01-19 19:37:21 +08:00
codeblock = showdown.subParser('outdent')(codeblock);
codeblock = showdown.subParser('encodeCode')(codeblock);
codeblock = showdown.subParser('detab')(codeblock);
codeblock = codeblock.replace(/^\n+/g, ''); // trim leading newlines
codeblock = codeblock.replace(/\n+$/g, ''); // trim trailing whitespace
2015-01-19 19:37:21 +08:00
codeblock = '<pre><code>' + codeblock + '\n</code></pre>';
2015-01-19 19:37:21 +08:00
return showdown.subParser('hashBlock')(codeblock, options, globals) + nextChar;
});
2015-01-19 19:37:21 +08:00
// attacklab: strip sentinel
text = text.replace(/~0/, '');
return text;
2015-01-16 05:21:33 +08:00
});