From a589a987d4d299b0e5ad7c4820b2413a9e42cf12 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 20 Oct 2011 17:08:27 +0200 Subject: [PATCH] Support for github-style code blocks --- src/showdown.js | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/showdown.js b/src/showdown.js index 43920d9..e86b603 100644 --- a/src/showdown.js +++ b/src/showdown.js @@ -355,6 +355,7 @@ var _RunBlockGamut = function(text) { text = text.replace(/^[ ]{0,2}([ ]?\_[ ]?){3,}[ \t]*$/gm,key); text = _DoLists(text); + text = _DoGithubCodeBlocks(text); text = _DoCodeBlocks(text); text = _DoBlockQuotes(text); @@ -889,12 +890,48 @@ var _DoCodeBlocks = function(text) { return text; } +var _DoGithubCodeBlocks = function(text) { +// +// Process Github-style code blocks +// Example: +// ```ruby +// def hello_world(x) +// puts "Hello, #{x}" +// end +// ``` +// + + + // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug + text += "~0"; + + text = text.replace(/\n```(.*)\n([^`]+)\n```/g, + function(wholeMatch,m1,m2) { + var language = m1; + var codeblock = m2; + + codeblock = _EncodeCode(codeblock); + codeblock = _Detab(codeblock); + codeblock = codeblock.replace(/^\n+/g,""); // trim leading newlines + codeblock = codeblock.replace(/\n+$/g,""); // trim trailing whitespace + + codeblock = "
" + codeblock + "\n
"; + + return hashBlock(codeblock); + } + ); + + // attacklab: strip sentinel + text = text.replace(/~0/,""); + + return text; +} + var hashBlock = function(text) { text = text.replace(/(^\n+|\n+$)/g,""); return "\n\n~K" + (g_html_blocks.push(text)-1) + "K\n\n"; } - var _DoCodeSpans = function(text) { // // * Backtick quotes are used for spans. @@ -946,7 +983,6 @@ var _DoCodeSpans = function(text) { return text; } - var _EncodeCode = function(text) { // // Encode/escape certain characters inside Markdown code runs. @@ -1299,4 +1335,4 @@ var escapeCharacters_callback = function(wholeMatch,m1) { } // end of Showdown.converter // export -if (typeof exports != 'undefined') exports.Showdown = Showdown; \ No newline at end of file +if (typeof exports != 'undefined') exports.Showdown = Showdown;