showdown/src/subParsers/paragraphs.js

64 lines
1.9 KiB
JavaScript
Raw Normal View History

2015-01-16 05:21:33 +08:00
/**
*
*/
showdown.subParser('paragraphs', function (text, options, globals) {
2015-01-19 19:37:21 +08:00
'use strict';
2015-01-16 05:21:33 +08:00
text = globals.converter._dispatch('paragraphs.before', text, options);
2015-01-19 19:37:21 +08:00
// Strip leading and trailing lines:
text = text.replace(/^\n+/g, '');
text = text.replace(/\n+$/g, '');
2015-01-16 05:21:33 +08:00
2015-01-19 22:57:43 +08:00
var grafs = text.split(/\n{2,}/g),
grafsOut = [],
end = grafs.length; // Wrap <p> tags
2015-01-16 05:21:33 +08:00
2015-01-19 19:37:21 +08:00
for (var i = 0; i < end; i++) {
var str = grafs[i];
// if this is an HTML marker, copy it
if (str.search(/~(K|G)(\d+)\1/g) >= 0) {
2015-01-19 19:37:21 +08:00
grafsOut.push(str);
} else {
2015-01-19 19:37:21 +08:00
str = showdown.subParser('spanGamut')(str, options, globals);
str = str.replace(/^([ \t]*)/g, '<p>');
str += '</p>';
grafsOut.push(str);
2015-01-16 05:21:33 +08:00
}
2015-01-19 19:37:21 +08:00
}
2015-01-16 05:21:33 +08:00
2015-01-19 19:37:21 +08:00
/** Unhashify HTML blocks */
end = grafsOut.length;
for (i = 0; i < end; i++) {
var blockText = '',
grafsOutIt = grafsOut[i],
child = false,
codeFlag = false;
2015-01-19 19:37:21 +08:00
// if this is a marker for an html block...
while (grafsOutIt.search(/~(K|G)(\d+)\1/) >= 0) {
var delim = RegExp.$1,
num = RegExp.$2;
if (delim === 'K') {
blockText = globals.gHtmlBlocks[num];
} else {
// we need to check if ghBlock is a false positive
blockText = (codeFlag) ? globals.ghCodeBlocks[num].text : globals.ghCodeBlocks[num].codeblock;
}
2015-01-19 19:37:21 +08:00
blockText = blockText.replace(/\$/g, '$$$$'); // Escape any dollar signs
grafsOutIt = grafsOutIt.replace(/(\n\n)?~(K|G)\d+\2(\n\n)?/, blockText);
// Check if grafsOutIt is a pre->code
if (/^<pre\b[^>]*>\s*<code\b[^>]*>/.test(grafsOutIt)) {
codeFlag = true;
}
child = true;
2015-01-16 05:21:33 +08:00
}
grafsOut[i] = grafsOutIt;
2015-01-19 19:37:21 +08:00
}
text = grafsOut.join('\n\n');
// Strip leading and trailing lines:
text = text.replace(/^\n+/g, '');
text = text.replace(/\n+$/g, '');
return globals.converter._dispatch('paragraphs.after', text, options);
2015-01-16 05:21:33 +08:00
});