123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344 |
- var Showdown = {};
- Showdown.converter = function() {
- var g_urls;
- var g_titles;
- var g_html_blocks;
- var g_list_level = 0;
- this.makeHtml = function(text) {
-
-
-
-
- g_urls = new Array();
- g_titles = new Array();
- g_html_blocks = new Array();
-
-
-
-
- text = text.replace(/~/g,"~T");
-
-
-
- text = text.replace(/\$/g,"~D");
-
- text = text.replace(/\r\n/g,"\n");
- text = text.replace(/\r/g,"\n");
-
- text = "\n\n" + text + "\n\n";
-
- text = _Detab(text);
-
-
-
-
- text = text.replace(/^[ \t]+$/mg,"");
-
-
- text = _DoGithubCodeBlocks(text);
-
- text = _HashHTMLBlocks(text);
-
- text = _StripLinkDefinitions(text);
- text = _RunBlockGamut(text);
- text = _UnescapeSpecialChars(text);
-
- text = text.replace(/~D/g,"$$");
-
- text = text.replace(/~T/g,"~");
- return text;
- };
- var _StripLinkDefinitions = function(text) {
-
-
- var text = text.replace(/^[ ]{0,3}\[(.+)\]:[ \t]*\n?[ \t]*<?(\S+?)>?[ \t]*\n?[ \t]*(?:(\n*)["(](.+?)[")][ \t]*)?(?:\n+|\Z)/gm,
- function (wholeMatch,m1,m2,m3,m4) {
- m1 = m1.toLowerCase();
- g_urls[m1] = _EncodeAmpsAndAngles(m2);
- if (m3) {
-
-
- return m3+m4;
- } else if (m4) {
- g_titles[m1] = m4.replace(/"/g,""");
- }
-
- return "";
- }
- );
- return text;
- }
- var _HashHTMLBlocks = function(text) {
-
- text = text.replace(/\n/g,"\n\n");
-
-
-
-
-
-
- var block_tags_a = "p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del|style|section|header|footer|nav|article|aside";
- var block_tags_b = "p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|style|section|header|footer|nav|article|aside";
-
-
-
-
-
-
-
-
-
-
-
-
-
- text = text.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,hashElement);
-
-
-
-
- text = text.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,hashElement);
-
-
-
- text = text.replace(/(\n[ ]{0,3}(<(hr)\b([^<>])*?\/?>)[ \t]*(?=\n{2,}))/g,hashElement);
-
-
- text = text.replace(/(\n\n[ ]{0,3}<!(--[^\r]*?--\s*)+>[ \t]*(?=\n{2,}))/g,hashElement);
-
-
- text = text.replace(/(?:\n\n)([ ]{0,3}(?:<([?%])[^\r]*?\2>)[ \t]*(?=\n{2,}))/g,hashElement);
-
- text = text.replace(/\n\n/g,"\n");
- return text;
- }
- var hashElement = function(wholeMatch,m1) {
- var blockText = m1;
-
- blockText = blockText.replace(/\n\n/g,"\n");
- blockText = blockText.replace(/^\n/,"");
-
- blockText = blockText.replace(/\n+$/g,"");
-
- blockText = "\n\n~K" + (g_html_blocks.push(blockText)-1) + "K\n\n";
- return blockText;
- };
- var _RunBlockGamut = function(text) {
- text = _DoHeaders(text);
-
- var key = hashBlock("<hr />");
- text = text.replace(/^[ ]{0,2}([ ]?\*[ ]?){3,}[ \t]*$/gm,key);
- text = text.replace(/^[ ]{0,2}([ ]?\-[ ]?){3,}[ \t]*$/gm,key);
- text = text.replace(/^[ ]{0,2}([ ]?\_[ ]?){3,}[ \t]*$/gm,key);
- text = _DoLists(text);
- text = _DoCodeBlocks(text);
- text = _DoBlockQuotes(text);
-
-
-
-
- text = _HashHTMLBlocks(text);
- text = _FormParagraphs(text);
- return text;
- };
- var _RunSpanGamut = function(text) {
- text = _DoCodeSpans(text);
- text = _EscapeSpecialCharsWithinTagAttributes(text);
- text = _EncodeBackslashEscapes(text);
-
-
- text = _DoImages(text);
- text = _DoAnchors(text);
-
-
-
- text = _DoAutoLinks(text);
- text = _EncodeAmpsAndAngles(text);
- text = _DoItalicsAndBold(text);
-
- text = text.replace(/ +\n/g," <br />\n");
- return text;
- }
- var _EscapeSpecialCharsWithinTagAttributes = function(text) {
-
-
- var regex = /(<[a-z\/!$]("[^"]*"|'[^']*'|[^'">])*>|<!(--.*?--\s*)+>)/gi;
- text = text.replace(regex, function(wholeMatch) {
- var tag = wholeMatch.replace(/(.)<\/?code>(?=.)/g,"$1`");
- tag = escapeCharacters(tag,"\\`*_");
- return tag;
- });
- return text;
- }
- var _DoAnchors = function(text) {
-
-
-
-
- text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g,writeAnchorTag);
-
-
-
-
- text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\]\([ \t]*()<?(.*?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g,writeAnchorTag);
-
-
-
-
-
-
- text = text.replace(/(\[([^\[\]]+)\])()()()()()/g, writeAnchorTag);
- return text;
- }
- var writeAnchorTag = function(wholeMatch,m1,m2,m3,m4,m5,m6,m7) {
- if (m7 == undefined) m7 = "";
- var whole_match = m1;
- var link_text = m2;
- var link_id = m3.toLowerCase();
- var url = m4;
- var title = m7;
- if (url == "") {
- if (link_id == "") {
-
- link_id = link_text.toLowerCase().replace(/ ?\n/g," ");
- }
- url = "#"+link_id;
- if (g_urls[link_id] != undefined) {
- url = g_urls[link_id];
- if (g_titles[link_id] != undefined) {
- title = g_titles[link_id];
- }
- }
- else {
- if (whole_match.search(/\(\s*\)$/m)>-1) {
-
- url = "";
- } else {
- return whole_match;
- }
- }
- }
- url = escapeCharacters(url,"*_");
- var result = "<a href=\"" + url + "\"";
- if (title != "") {
- title = title.replace(/"/g,""");
- title = escapeCharacters(title,"*_");
- result += " title=\"" + title + "\"";
- }
- result += ">" + link_text + "</a>";
- return result;
- }
- var _DoImages = function(text) {
-
-
-
-
- text = text.replace(/(!\[(.*?)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g,writeImageTag);
-
-
-
-
- text = text.replace(/(!\[(.*?)\]\s?\([ \t]*()<?(\S+?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g,writeImageTag);
- return text;
- }
- var writeImageTag = function(wholeMatch,m1,m2,m3,m4,m5,m6,m7) {
- var whole_match = m1;
- var alt_text = m2;
- var link_id = m3.toLowerCase();
- var url = m4;
- var title = m7;
- if (!title) title = "";
- if (url == "") {
- if (link_id == "") {
-
- link_id = alt_text.toLowerCase().replace(/ ?\n/g," ");
- }
- url = "#"+link_id;
- if (g_urls[link_id] != undefined) {
- url = g_urls[link_id];
- if (g_titles[link_id] != undefined) {
- title = g_titles[link_id];
- }
- }
- else {
- return whole_match;
- }
- }
- alt_text = alt_text.replace(/"/g,""");
- url = escapeCharacters(url,"*_");
- var result = "<img src=\"" + url + "\" alt=\"" + alt_text + "\"";
-
-
-
- title = title.replace(/"/g,""");
- title = escapeCharacters(title,"*_");
- result += " title=\"" + title + "\"";
-
- result += " />";
- return result;
- }
- var _DoHeaders = function(text) {
-
-
-
-
-
-
-
- text = text.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm,
- function(wholeMatch,m1){return hashBlock('<h1 id="' + headerId(m1) + '">' + _RunSpanGamut(m1) + "</h1>");});
- text = text.replace(/^(.+)[ \t]*\n-+[ \t]*\n+/gm,
- function(matchFound,m1){return hashBlock('<h2 id="' + headerId(m1) + '">' + _RunSpanGamut(m1) + "</h2>");});
-
-
-
-
-
-
-
-
- text = text.replace(/^(\#{1,6})[ \t]*(.+?)[ \t]*\#*\n+/gm,
- function(wholeMatch,m1,m2) {
- var h_level = m1.length;
- return hashBlock("<h" + h_level + ' id="' + headerId(m2) + '">' + _RunSpanGamut(m2) + "</h" + h_level + ">");
- });
- function headerId(m) {
- return m.replace(/[^\w]/g, '').toLowerCase();
- }
- return text;
- }
- var _ProcessListItems;
- var _DoLists = function(text) {
-
-
- text += "~0";
-
-
- var whole_list = /^(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;
- if (g_list_level) {
- text = text.replace(whole_list,function(wholeMatch,m1,m2) {
- var list = m1;
- var list_type = (m2.search(/[*+-]/g)>-1) ? "ul" : "ol";
-
-
- list = list.replace(/\n{2,}/g,"\n\n\n");;
- var result = _ProcessListItems(list);
-
-
-
-
- result = result.replace(/\s+$/,"");
- result = "<"+list_type+">" + result + "</"+list_type+">\n";
- return result;
- });
- } else {
- whole_list = /(\n\n|^\n?)(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/g;
- text = text.replace(whole_list,function(wholeMatch,m1,m2,m3) {
- var runup = m1;
- var list = m2;
- var list_type = (m3.search(/[*+-]/g)>-1) ? "ul" : "ol";
-
-
- var list = list.replace(/\n{2,}/g,"\n\n\n");;
- var result = _ProcessListItems(list);
- result = runup + "<"+list_type+">\n" + result + "</"+list_type+">\n";
- return result;
- });
- }
-
- text = text.replace(/~0/,"");
- return text;
- }
- _ProcessListItems = function(list_str) {
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- g_list_level++;
-
- list_str = list_str.replace(/\n{2,}$/,"\n");
-
- list_str += "~0";
-
- list_str = list_str.replace(/(\n)?(^[ \t]*)([*+-]|\d+[.])[ \t]+([^\r]+?(\n{1,2}))(?=\n*(~0|\2([*+-]|\d+[.])[ \t]+))/gm,
- function(wholeMatch,m1,m2,m3,m4){
- var item = m4;
- var leading_line = m1;
- var leading_space = m2;
- if (leading_line || (item.search(/\n{2,}/)>-1)) {
- item = _RunBlockGamut(_Outdent(item));
- }
- else {
-
- item = _DoLists(_Outdent(item));
- item = item.replace(/\n$/,"");
- item = _RunSpanGamut(item);
- }
- return "<li>" + item + "</li>\n";
- }
- );
-
- list_str = list_str.replace(/~0/g,"");
- g_list_level--;
- return list_str;
- }
- var _DoCodeBlocks = function(text) {
-
-
- text += "~0";
- text = text.replace(/(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g,
- function(wholeMatch,m1,m2) {
- var codeblock = m1;
- var nextChar = m2;
- codeblock = _EncodeCode( _Outdent(codeblock));
- codeblock = _Detab(codeblock);
- codeblock = codeblock.replace(/^\n+/g,"");
- codeblock = codeblock.replace(/\n+$/g,"");
- codeblock = "<pre><code>" + codeblock + "\n</code></pre>";
- return hashBlock(codeblock) + nextChar;
- }
- );
-
- text = text.replace(/~0/,"");
- return text;
- };
- var _DoGithubCodeBlocks = function(text) {
-
- text += "~0";
- text = text.replace(/(?:^|\n)```(.*)\n([\s\S]*?)\n```/g,
- function(wholeMatch,m1,m2) {
- var language = m1;
- var codeblock = m2;
- codeblock = _EncodeCode(codeblock);
- codeblock = _Detab(codeblock);
- codeblock = codeblock.replace(/^\n+/g,"");
- codeblock = codeblock.replace(/\n+$/g,"");
- codeblock = "<pre><code" + (language ? " class=\"" + language + '"' : "") + ">" + codeblock + "\n</code></pre>";
- return hashBlock(codeblock);
- }
- );
-
- 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) {
-
- text = text.replace(/(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm,
- function(wholeMatch,m1,m2,m3,m4) {
- var c = m3;
- c = c.replace(/^([ \t]*)/g,"");
- c = c.replace(/[ \t]*$/g,"");
- c = _EncodeCode(c);
- return m1+"<code>"+c+"</code>";
- });
- return text;
- }
- var _EncodeCode = function(text) {
-
-
-
-
-
-
-
-
-
- text = escapeCharacters(text,"\*_{}[]\\",false);
- return text;
- }
- var _DoItalicsAndBold = function(text) {
-
- text = text.replace(/(\*\*|__)(?=\S)([^\r]*?\S[*_]*)\1/g,
- "<strong>$2</strong>");
- text = text.replace(/(\*|_)(?=\S)([^\r]*?\S)\1/g,
- "<em>$2</em>");
- return text;
- }
- var _DoBlockQuotes = function(text) {
-
- text = text.replace(/((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)/gm,
- function(wholeMatch,m1) {
- var bq = m1;
-
-
- bq = bq.replace(/^[ \t]*>[ \t]?/gm,"~0");
-
- bq = bq.replace(/~0/g,"");
- bq = bq.replace(/^[ \t]+$/gm,"");
- bq = _RunBlockGamut(bq);
- bq = bq.replace(/(^|\n)/g,"$1 ");
-
- bq = bq.replace(
- /(\s*<pre>[^\r]+?<\/pre>)/gm,
- function(wholeMatch,m1) {
- var pre = m1;
-
- pre = pre.replace(/^ /mg,"~0");
- pre = pre.replace(/~0/g,"");
- return pre;
- });
- return hashBlock("<blockquote>\n" + bq + "\n</blockquote>");
- });
- return text;
- }
- var _FormParagraphs = function(text) {
-
- text = text.replace(/^\n+/g,"");
- text = text.replace(/\n+$/g,"");
- var grafs = text.split(/\n{2,}/g);
- var grafsOut = new Array();
-
-
-
- var end = grafs.length;
- for (var i=0; i<end; i++) {
- var str = grafs[i];
-
- if (str.search(/~K(\d+)K/g) >= 0) {
- grafsOut.push(str);
- }
- else if (str.search(/\S/) >= 0) {
- str = _RunSpanGamut(str);
- str = str.replace(/^([ \t]*)/g,"<p>");
- str += "</p>"
- grafsOut.push(str);
- }
- }
-
-
-
- end = grafsOut.length;
- for (var i=0; i<end; i++) {
-
- while (grafsOut[i].search(/~K(\d+)K/) >= 0) {
- var blockText = g_html_blocks[RegExp.$1];
- blockText = blockText.replace(/\$/g,"$$$$");
- grafsOut[i] = grafsOut[i].replace(/~K\d+K/,blockText);
- }
- }
- return grafsOut.join("\n\n");
- }
- var _EncodeAmpsAndAngles = function(text) {
-
-
- text = text.replace(/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/g,"&");
-
- text = text.replace(/<(?![a-z\/?\$!])/gi,"<");
- return text;
- }
- var _EncodeBackslashEscapes = function(text) {
-
-
-
-
-
-
-
-
- text = text.replace(/\\(\\)/g,escapeCharacters_callback);
- text = text.replace(/\\([`*_{}\[\]()>#+-.!])/g,escapeCharacters_callback);
- return text;
- }
- var _DoAutoLinks = function(text) {
- text = text.replace(/<((https?|ftp|dict):[^'">\s]+)>/gi,"<a href=\"$1\">$1</a>");
-
-
- text = text.replace(/<(?:mailto:)?([-.\w]+\@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi,
- function(wholeMatch,m1) {
- return _EncodeEmailAddress( _UnescapeSpecialChars(m1) );
- }
- );
- return text;
- }
- var _EncodeEmailAddress = function(addr) {
-
- function char2hex(ch) {
- var hexDigits = '0123456789ABCDEF';
- var dec = ch.charCodeAt(0);
- return(hexDigits.charAt(dec>>4) + hexDigits.charAt(dec&15));
- }
- var encode = [
- function(ch){return "&#"+ch.charCodeAt(0)+";";},
- function(ch){return "&#x"+char2hex(ch)+";";},
- function(ch){return ch;}
- ];
- addr = "mailto:" + addr;
- addr = addr.replace(/./g, function(ch) {
- if (ch == "@") {
-
- ch = encode[Math.floor(Math.random()*2)](ch);
- } else if (ch !=":") {
-
- var r = Math.random();
-
- ch = (
- r > .9 ? encode[2](ch) :
- r > .45 ? encode[1](ch) :
- encode[0](ch)
- );
- }
- return ch;
- });
- addr = "<a href=\"" + addr + "\">" + addr + "</a>";
- addr = addr.replace(/">.+:/g,"\">");
- return addr;
- }
- var _UnescapeSpecialChars = function(text) {
- text = text.replace(/~E(\d+)E/g,
- function(wholeMatch,m1) {
- var charCodeToReplace = parseInt(m1);
- return String.fromCharCode(charCodeToReplace);
- }
- );
- return text;
- }
- var _Outdent = function(text) {
-
-
- text = text.replace(/^(\t|[ ]{1,4})/gm,"~0");
-
- text = text.replace(/~0/g,"")
- return text;
- }
- var _Detab = function(text) {
-
- text = text.replace(/\t(?=\t)/g," ");
-
- text = text.replace(/\t/g,"~A~B");
-
- text = text.replace(/~B(.+?)~A/g,
- function(wholeMatch,m1,m2) {
- var leadingText = m1;
- var numSpaces = 4 - leadingText.length % 4;
-
- for (var i=0; i<numSpaces; i++) leadingText+=" ";
- return leadingText;
- }
- );
-
- text = text.replace(/~A/g," ");
- text = text.replace(/~B/g,"");
- return text;
- }
- var escapeCharacters = function(text, charsToEscape, afterBackslash) {
-
-
- var regexString = "([" + charsToEscape.replace(/([\[\]\\])/g,"\\$1") + "])";
- if (afterBackslash) {
- regexString = "\\\\" + regexString;
- }
- var regex = new RegExp(regexString,"g");
- text = text.replace(regex,escapeCharacters_callback);
- return text;
- }
- var escapeCharacters_callback = function(wholeMatch,m1) {
- var charCodeToEscape = m1.charCodeAt(0);
- return "~E"+charCodeToEscape+"E";
- }
- }
- if (typeof module !== 'undefined') module.exports = Showdown;
|