// // showdown.js -- A javascript port of Markdown. // // Copyright (c) 2007 John Fraser. // // Original Markdown Copyright (c) 2004-2005 John Gruber // // // Redistributable under a BSD-style open source license. // See license.txt for more information. // // The full source distribution is at: // // A A L // T C A // T K B // // // // // Wherever possible, Showdown is a straight, line-by-line port // of the Perl version of Markdown. // // This is not a normal parser design; it's basically just a // series of string substitutions. It's hard to read and // maintain this way, but keeping Showdown close to the original // design makes it easier to port new features. // // More importantly, Showdown behaves like markdown.pl in most // edge cases. So web applications can do client-side preview // in Javascript, and then build identical HTML on the server. // // This port needs the new RegExp functionality of ECMA 262, // 3rd Edition (i.e. Javascript 1.5). Most modern web browsers // should do fine. Even with the new regular expression features, // We do a lot of work to emulate Perl's regex functionality. // The tricky changes in this file mostly have the "attacklab:" // label. Major or self-explanatory changes don't. // // Smart diff tools like Araxis Merge will be able to match up // this file with markdown.pl in a useful way. A little tweaking // helps: in a copy of markdown.pl, replace "#" with "//" and // replace "$text" with "text". Be sure to ignore whitespace // and line endings. // // // Showdown usage: // // var text = "Markdown *rocks*."; // // var converter = new Showdown.converter(); // var html = converter.makeHtml(text); // // alert(html); // // Note: move the sample code to the bottom of this // file before uncommenting it. // // // Showdown namespace // var Showdown={converter:function(){var i,j,n,p=0;this.makeHtml=function(a){i=[];j=[];n=[];a=a.replace(/~/g,"~T");a=a.replace(/\$/g,"~D");a=a.replace(/\r\n/g,"\n");a=a.replace(/\r/g,"\n");a=q("\n\n"+a+"\n\n");a=a.replace(/^[ \t]+$/mg,"");a=(a+"~0").replace(/(?:^|\n)```(.*)\n([\s\S]*?)\n```/g,function(a,b,e){a=r(e);a=q(a);a=a.replace(/^\n+/g,"");a=a.replace(/\n+$/g,"");a="
"+a+"\n
";return h(a)});a=a.replace(/~0/,"");a=x(a);a=a.replace(/^[ ]{0,3}\[(.+)\]:[ \t]*\n?[ \t]*?[ \t]*\n?[ \t]*(?:(\n*)["(](.+?)[")][ \t]*)?(?:\n+|\Z)/gm, function(a,b,e,d,f){b=b.toLowerCase();i[b]=y(e);if(d)return d+f;f&&(j[b]=f.replace(/"/g,"""));return""});a=s(a);a=z(a);a=a.replace(/~D/g,"$$");return a=a.replace(/~T/g,"~")};var x=function(a){a=a.replace(/\n/g,"\n\n");a=a.replace(/^(<(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del)\b[^\r]*?\n<\/\2>[ \t]*(?=\n+))/gm,l);a=a.replace(/^(<(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|style|section|header|footer|nav|article|aside)\b[^\r]*?.*<\/\2>[ \t]*(?=\n+)\n)/gm, l);a=a.replace(/(\n[ ]{0,3}(<(hr)\b([^<>])*?\/?>)[ \t]*(?=\n{2,}))/g,l);a=a.replace(/(\n\n[ ]{0,3}[ \t]*(?=\n{2,}))/g,l);a=a.replace(/(?:\n\n)([ ]{0,3}(?:<([?%])[^\r]*?\2>)[ \t]*(?=\n{2,}))/g,l);return a=a.replace(/\n\n/g,"\n")},l=function(a,c){var b;b=c.replace(/\n\n/g,"\n");b=b.replace(/^\n/,"");b=b.replace(/\n+$/g,"");return b="\n\n~K"+(n.push(b)-1)+"K\n\n"},s=function(a){for(var c=function(a){return a.replace(/[^\w]/g,"").toLowerCase()},a=a.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm, function(a,b){return h('

'+m(b)+"

")}),a=a.replace(/^(.+)[ \t]*\n-+[ \t]*\n+/gm,function(a,b){return h('

'+m(b)+"

")}),a=a.replace(/^(\#{1,6})[ \t]*(.+?)[ \t]*\#*\n+/gm,function(a,b,f){a=b.length;return h("'+m(f)+"")}),b=h("
"),a=a.replace(/^[ ]{0,2}([ ]?\*[ ]?){3,}[ \t]*$/gm,b),a=a.replace(/^[ ]{0,2}([ ]?\-[ ]?){3,}[ \t]*$/gm,b),a=a.replace(/^[ ]{0,2}([ ]?\_[ ]?){3,}[ \t]*$/gm,b),a=A(a),a=a+"~0",a=a.replace(/(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g, function(a,b,c){a=r(t(b));a=q(a);a=a.replace(/^\n+/g,"");a=a.replace(/\n+$/g,"");a="
"+a+"\n
";return h(a)+c}),a=a.replace(/~0/,""),a=a.replace(/((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)/gm,function(a,b){var c;c=b.replace(/^[ \t]*>[ \t]?/gm,"~0");c=c.replace(/~0/g,"");c=c.replace(/^[ \t]+$/gm,"");c=s(c);c=c.replace(/(^|\n)/g,"$1 ");c=c.replace(/(\s*
[^\r]+?<\/pre>)/gm,function(a,c){var b;b=c.replace(/^  /mg,"~0");return b=b.replace(/~0/g,"")});return h("
\n"+c+"\n
")}), a=x(a),a=a.replace(/^\n+/g,""),a=a.replace(/\n+$/g,""),e=a.split(/\n{2,}/g),a=[],b=e.length,d=0;d"),f+="

",a.push(f))}b=a.length;for(d=0;d"+a+""});a=a.replace(/(<[a-z\/!$]("[^"]*"|'[^']*'|[^'">])*>|)/gi,function(a){a=a.replace(/(.)<\/?code>(?=.)/g,"$1`");return a=k(a,"\\`*_")});a=a.replace(/\\(\\)/g,u);a=a.replace(/\\([`*_{}\[\]()>#+-.!])/g,u);a=a.replace(/(!\[(.*?)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g,B);a=a.replace(/(!\[(.*?)\]\s?\([ \t]*()?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g,B);a=a.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g,v);a=a.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\]\([ \t]*()?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, v);a=a.replace(/(\[([^\[\]]+)\])()()()()()/g,v);a=a.replace(/<((https?|ftp|dict):[^'">\s]+)>/gi,'$1');a=a.replace(/<(?:mailto:)?([-.\w]+\@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi,function(a,b){var e=z(b),d=[function(a){return"&#"+a.charCodeAt(0)+";"},function(a){a=a.charCodeAt(0);return"&#x"+("0123456789ABCDEF".charAt(a>>4)+"0123456789ABCDEF".charAt(a&15))+";"},function(a){return a}],e=("mailto:"+e).replace(/./g,function(a){if("@"==a)a=d[Math.floor(2*Math.random())](a);else if(":"!= a)var b=Math.random(),a=0.9'+e+"";return e=e.replace(/">.+:/g,'">')});a=y(a);a=a.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g,"$2");a=a.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g,"$2");return a=a.replace(/ +\n/g,"
\n")},v=function(a,c,b,e,d,f,h,g){void 0==g&&(g="");a=e.toLowerCase();if(""==d)if(""==a&&(a=b.toLowerCase().replace(/ ?\n/g," ")),void 0!=i[a])d=i[a],void 0!=j[a]&&(g=j[a]);else if(-1"+b+"")},B=function(a,c,b,e,d,f,h,g){a=b;e=e.toLowerCase();g||(g="");if(""==d)if(""==e&&(e=a.toLowerCase().replace(/ ?\n/g," ")),void 0!=i[e])d=i[e],void 0!=j[e]&&(g=j[e]);else return c;a=a.replace(/"/g,""");d=k(d,"*_");c=''+a+'"},w,A=function(a){var a=a+"~0",c=/^(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm; p?a=a.replace(c,function(a,c,d){a=c;d=-1"+a+"\n"}):(c=/(\n\n|^\n?)(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/g,a=a.replace(c,function(a,c,d,f){a=d;f=-1\n"+a+"\n"}));return a=a.replace(/~0/,"")};w=function(a){p++;a=a.replace(/\n{2,}$/,"\n");a=(a+"~0").replace(/(\n)?(^[ \t]*)([*+-]|\d+[.])[ \t]+([^\r]+?(\n{1,2}))(?=\n*(~0|\2([*+-]|\d+[.])[ \t]+))/gm, function(a,b,e,d,f){a=f;b||-1"+a+"\n"});a=a.replace(/~0/g,"");p--;return a};var h=function(a){a=a.replace(/(^\n+|\n+$)/g,"");return"\n\n~K"+(n.push(a)-1)+"K\n\n"},r=function(a){a=a.replace(/&/g,"&");a=a.replace(//g,">");return a=k(a,"*_{}[]\\",!1)},y=function(a){a=a.replace(/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/g,"&");return a=a.replace(/<(?![a-z\/?\$!])/gi,"<")},z=function(a){return a= a.replace(/~E(\d+)E/g,function(a,b){var e=parseInt(b);return String.fromCharCode(e)})},t=function(a){a=a.replace(/^(\t|[ ]{1,4})/gm,"~0");return a=a.replace(/~0/g,"")},q=function(a){a=a.replace(/\t(?=\t)/g," ");a=a.replace(/\t/g,"~A~B");a=a.replace(/~B(.+?)~A/g,function(a,b){for(var e=b,d=4-e.length%4,f=0;f