+var Markdown;\r
+\r
+if (typeof exports === "object" && typeof require === "function") // we're in a CommonJS (e.g. Node.js) module\r
+ Markdown = exports;\r
+else\r
+ Markdown = {};\r
+\r
+// The following text is included for historical reasons, but should\r
+// be taken with a pinch of salt; it's not all true anymore.\r
+\r
+//\r
+// Wherever possible, Showdown is a straight, line-by-line port\r
+// of the Perl version of Markdown.\r
+//\r
+// This is not a normal parser design; it's basically just a\r
+// series of string substitutions. It's hard to read and\r
+// maintain this way, but keeping Showdown close to the original\r
+// design makes it easier to port new features.\r
+//\r
+// More importantly, Showdown behaves like markdown.pl in most\r
+// edge cases. So web applications can do client-side preview\r
+// in Javascript, and then build identical HTML on the server.\r
+//\r
+// This port needs the new RegExp functionality of ECMA 262,\r
+// 3rd Edition (i.e. Javascript 1.5). Most modern web browsers\r
+// should do fine. Even with the new regular expression features,\r
+// We do a lot of work to emulate Perl's regex functionality.\r
+// The tricky changes in this file mostly have the "attacklab:"\r
+// label. Major or self-explanatory changes don't.\r
+//\r
+// Smart diff tools like Araxis Merge will be able to match up\r
+// this file with markdown.pl in a useful way. A little tweaking\r
+// helps: in a copy of markdown.pl, replace "#" with "//" and\r
+// replace "$text" with "text". Be sure to ignore whitespace\r
+// and line endings.\r
+//\r
+\r
+\r
+//\r
+// Usage:\r
+//\r
+// var text = "Markdown *rocks*.";\r
+//\r
+// var converter = new Markdown.Converter();\r
+// var html = converter.makeHtml(text);\r
+//\r
+// alert(html);\r
+//\r
+// Note: move the sample code to the bottom of this\r
+// file before uncommenting it.\r
+//\r
+\r
+(function () {\r
+\r
+ function identity(x) { return x; }\r
+ function returnFalse(x) { return false; }\r
+\r
+ function HookCollection() { }\r
+\r
+ HookCollection.prototype = {\r
+\r
+ chain: function (hookname, func) {\r
+ var original = this[hookname];\r
+ if (!original)\r
+ throw new Error("unknown hook " + hookname);\r
+\r
+ if (original === identity)\r
+ this[hookname] = func;\r
+ else\r
+ this[hookname] = function (text) {\r
+ var args = Array.prototype.slice.call(arguments, 0);\r
+ args[0] = original.apply(null, args);\r
+ return func.apply(null, args);\r
+ };\r
+ },\r
+ set: function (hookname, func) {\r
+ if (!this[hookname])\r
+ throw new Error("unknown hook " + hookname);\r
+ this[hookname] = func;\r
+ },\r
+ addNoop: function (hookname) {\r
+ this[hookname] = identity;\r
+ },\r
+ addFalse: function (hookname) {\r
+ this[hookname] = returnFalse;\r
+ }\r
+ };\r
+\r
+ Markdown.HookCollection = HookCollection;\r
+\r
+ // g_urls and g_titles allow arbitrary user-entered strings as keys. This\r
+ // caused an exception (and hence stopped the rendering) when the user entered\r
+ // e.g. [push] or [__proto__]. Adding a prefix to the actual key prevents this\r
+ // (since no builtin property starts with "s_"). See\r
+ // http://meta.stackoverflow.com/questions/64655/strange-wmd-bug\r
+ // (granted, switching from Array() to Object() alone would have left only __proto__\r
+ // to be a problem)\r
+ function SaveHash() { }\r
+ SaveHash.prototype = {\r
+ set: function (key, value) {\r
+ this["s_" + key] = value;\r
+ },\r
+ get: function (key) {\r
+ return this["s_" + key];\r
+ }\r
+ };\r
+\r
+ Markdown.Converter = function () {\r
+ var pluginHooks = this.hooks = new HookCollection();\r
+ \r
+ // given a URL that was encountered by itself (without markup), should return the link text that's to be given to this link\r
+ pluginHooks.addNoop("plainLinkText");\r
+ \r
+ // called with the orignal text as given to makeHtml. The result of this plugin hook is the actual markdown source that will be cooked\r
+ pluginHooks.addNoop("preConversion");\r
+ \r
+ // called with the text once all normalizations have been completed (tabs to spaces, line endings, etc.), but before any conversions have\r
+ pluginHooks.addNoop("postNormalization");\r
+ \r
+ // Called with the text before / after creating block elements like code blocks and lists. Note that this is called recursively\r
+ // with inner content, e.g. it's called with the full text, and then only with the content of a blockquote. The inner\r
+ // call will receive outdented text.\r
+ pluginHooks.addNoop("preBlockGamut");\r
+ pluginHooks.addNoop("postBlockGamut");\r
+ \r
+ // called with the text of a single block element before / after the span-level conversions (bold, code spans, etc.) have been made\r
+ pluginHooks.addNoop("preSpanGamut");\r
+ pluginHooks.addNoop("postSpanGamut");\r
+ \r
+ // called with the final cooked HTML code. The result of this plugin hook is the actual output of makeHtml\r
+ pluginHooks.addNoop("postConversion");\r
+\r
+ //\r
+ // Private state of the converter instance:\r
+ //\r
+\r
+ // Global hashes, used by various utility routines\r
+ var g_urls;\r
+ var g_titles;\r
+ var g_html_blocks;\r
+\r
+ // Used to track when we're inside an ordered or unordered list\r
+ // (see _ProcessListItems() for details):\r
+ var g_list_level;\r
+\r
+ this.makeHtml = function (text) {\r
+\r
+ //\r
+ // Main function. The order in which other subs are called here is\r
+ // essential. Link and image substitutions need to happen before\r
+ // _EscapeSpecialCharsWithinTagAttributes(), so that any *'s or _'s in the <a>\r
+ // and <img> tags get encoded.\r
+ //\r
+\r
+ // This will only happen if makeHtml on the same converter instance is called from a plugin hook.\r
+ // Don't do that.\r
+ if (g_urls)\r
+ throw new Error("Recursive call to converter.makeHtml");\r
+ \r
+ // Create the private state objects.\r
+ g_urls = new SaveHash();\r
+ g_titles = new SaveHash();\r
+ g_html_blocks = [];\r
+ g_list_level = 0;\r
+\r
+ text = pluginHooks.preConversion(text);\r
+\r
+ // attacklab: Replace ~ with ~T\r
+ // This lets us use tilde as an escape char to avoid md5 hashes\r
+ // The choice of character is arbitray; anything that isn't\r
+ // magic in Markdown will work.\r
+ text = text.replace(/~/g, "~T");\r
+\r
+ // attacklab: Replace $ with ~D\r
+ // RegExp interprets $ as a special character\r
+ // when it's in a replacement string\r
+ text = text.replace(/\$/g, "~D");\r
+\r
+ // Standardize line endings\r
+ text = text.replace(/\r\n/g, "\n"); // DOS to Unix\r
+ text = text.replace(/\r/g, "\n"); // Mac to Unix\r
+\r
+ // Make sure text begins and ends with a couple of newlines:\r
+ text = "\n\n" + text + "\n\n";\r
+\r
+ // Convert all tabs to spaces.\r
+ text = _Detab(text);\r
+\r
+ // Strip any lines consisting only of spaces and tabs.\r
+ // This makes subsequent regexen easier to write, because we can\r
+ // match consecutive blank lines with /\n+/ instead of something\r
+ // contorted like /[ \t]*\n+/ .\r
+ text = text.replace(/^[ \t]+$/mg, "");\r
+ \r
+ text = pluginHooks.postNormalization(text);\r
+\r
+ // Turn block-level HTML blocks into hash entries\r
+ text = _HashHTMLBlocks(text);\r
+\r
+ // Strip link definitions, store in hashes.\r
+ text = _StripLinkDefinitions(text);\r
+\r
+ text = _RunBlockGamut(text);\r
+\r
+ text = _UnescapeSpecialChars(text);\r
+\r
+ // attacklab: Restore dollar signs\r
+ text = text.replace(/~D/g, "$$");\r
+\r
+ // attacklab: Restore tildes\r
+ text = text.replace(/~T/g, "~");\r
+\r
+ text = pluginHooks.postConversion(text);\r
+\r
+ g_html_blocks = g_titles = g_urls = null;\r
+\r
+ return text;\r
+ };\r
+\r
+ function _StripLinkDefinitions(text) {\r
+ //\r
+ // Strips link definitions from text, stores the URLs and titles in\r
+ // hash references.\r
+ //\r
+\r
+ // Link defs are in the form: ^[id]: url "optional title"\r
+\r
+ /*\r
+ text = text.replace(/\r
+ ^[ ]{0,3}\[(.+)\]: // id = $1 attacklab: g_tab_width - 1\r
+ [ \t]*\r
+ \n? // maybe *one* newline\r
+ [ \t]*\r
+ <?(\S+?)>? // url = $2\r
+ (?=\s|$) // lookahead for whitespace instead of the lookbehind removed below\r
+ [ \t]*\r
+ \n? // maybe one newline\r
+ [ \t]*\r
+ ( // (potential) title = $3\r
+ (\n*) // any lines skipped = $4 attacklab: lookbehind removed\r
+ [ \t]+\r
+ ["(]\r
+ (.+?) // title = $5\r
+ [")]\r
+ [ \t]*\r
+ )? // title is optional\r
+ (?:\n+|$)\r
+ /gm, function(){...});\r
+ */\r
+\r
+ text = text.replace(/^[ ]{0,3}\[(.+)\]:[ \t]*\n?[ \t]*<?(\S+?)>?(?=\s|$)[ \t]*\n?[ \t]*((\n*)["(](.+?)[")][ \t]*)?(?:\n+)/gm,\r
+ function (wholeMatch, m1, m2, m3, m4, m5) {\r
+ m1 = m1.toLowerCase();\r
+ g_urls.set(m1, _EncodeAmpsAndAngles(m2)); // Link IDs are case-insensitive\r
+ if (m4) {\r
+ // Oops, found blank lines, so it's not a title.\r
+ // Put back the parenthetical statement we stole.\r
+ return m3;\r
+ } else if (m5) {\r
+ g_titles.set(m1, m5.replace(/"/g, """));\r
+ }\r
+\r
+ // Completely remove the definition from the text\r
+ return "";\r
+ }\r
+ );\r
+\r
+ return text;\r
+ }\r
+\r
+ function _HashHTMLBlocks(text) {\r
+\r
+ // Hashify HTML blocks:\r
+ // We only want to do this for block-level HTML tags, such as headers,\r
+ // lists, and tables. That's because we still want to wrap <p>s around\r
+ // "paragraphs" that are wrapped in non-block-level tags, such as anchors,\r
+ // phrase emphasis, and spans. The list of tags we're looking for is\r
+ // hard-coded:\r
+ var block_tags_a = "p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math|ins|del"\r
+ var block_tags_b = "p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math"\r
+\r
+ // First, look for nested blocks, e.g.:\r
+ // <div>\r
+ // <div>\r
+ // tags for inner block must be indented.\r
+ // </div>\r
+ // </div>\r
+ //\r
+ // The outermost tags must start at the left margin for this to match, and\r
+ // the inner nested divs must be indented.\r
+ // We need to do this before the next, more liberal match, because the next\r
+ // match will start at the first `<div>` and stop at the first `</div>`.\r
+\r
+ // attacklab: This regex can be expensive when it fails.\r
+\r
+ /*\r
+ text = text.replace(/\r
+ ( // save in $1\r
+ ^ // start of line (with /m)\r
+ <($block_tags_a) // start tag = $2\r
+ \b // word break\r
+ // attacklab: hack around khtml/pcre bug...\r
+ [^\r]*?\n // any number of lines, minimally matching\r
+ </\2> // the matching end tag\r
+ [ \t]* // trailing spaces/tabs\r
+ (?=\n+) // followed by a newline\r
+ ) // attacklab: there are sentinel newlines at end of document\r
+ /gm,function(){...}};\r
+ */\r
+ 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);\r
+\r
+ //\r
+ // Now match more liberally, simply from `\n<tag>` to `</tag>\n`\r
+ //\r
+\r
+ /*\r
+ text = text.replace(/\r
+ ( // save in $1\r
+ ^ // start of line (with /m)\r
+ <($block_tags_b) // start tag = $2\r
+ \b // word break\r
+ // attacklab: hack around khtml/pcre bug...\r
+ [^\r]*? // any number of lines, minimally matching\r
+ .*</\2> // the matching end tag\r
+ [ \t]* // trailing spaces/tabs\r
+ (?=\n+) // followed by a newline\r
+ ) // attacklab: there are sentinel newlines at end of document\r
+ /gm,function(){...}};\r
+ */\r
+ text = text.replace(/^(<(p|div|h[1-6]|blockquote|pre|table|dl|ol|ul|script|noscript|form|fieldset|iframe|math)\b[^\r]*?.*<\/\2>[ \t]*(?=\n+)\n)/gm, hashElement);\r
+\r
+ // Special case just for <hr />. It was easier to make a special case than\r
+ // to make the other regex more complicated. \r
+\r
+ /*\r
+ text = text.replace(/\r
+ \n // Starting after a blank line\r
+ [ ]{0,3}\r
+ ( // save in $1\r
+ (<(hr) // start tag = $2\r
+ \b // word break\r
+ ([^<>])*?\r
+ \/?>) // the matching end tag\r
+ [ \t]*\r
+ (?=\n{2,}) // followed by a blank line\r
+ )\r
+ /g,hashElement);\r
+ */\r
+ text = text.replace(/\n[ ]{0,3}((<(hr)\b([^<>])*?\/?>)[ \t]*(?=\n{2,}))/g, hashElement);\r
+\r
+ // Special case for standalone HTML comments:\r
+\r
+ /*\r
+ text = text.replace(/\r
+ \n\n // Starting after a blank line\r
+ [ ]{0,3} // attacklab: g_tab_width - 1\r
+ ( // save in $1\r
+ <!\r
+ (--(?:|(?:[^>-]|-[^>])(?:[^-]|-[^-])*)--) // see http://www.w3.org/TR/html-markup/syntax.html#comments and http://meta.stackoverflow.com/q/95256\r
+ >\r
+ [ \t]*\r
+ (?=\n{2,}) // followed by a blank line\r
+ )\r
+ /g,hashElement);\r
+ */\r
+ text = text.replace(/\n\n[ ]{0,3}(<!(--(?:|(?:[^>-]|-[^>])(?:[^-]|-[^-])*)--)>[ \t]*(?=\n{2,}))/g, hashElement);\r
+\r
+ // PHP and ASP-style processor instructions (<?...?> and <%...%>)\r
+\r
+ /*\r
+ text = text.replace(/\r
+ (?:\r
+ \n\n // Starting after a blank line\r
+ )\r
+ ( // save in $1\r
+ [ ]{0,3} // attacklab: g_tab_width - 1\r
+ (?:\r
+ <([?%]) // $2\r
+ [^\r]*?\r
+ \2>\r
+ )\r
+ [ \t]*\r
+ (?=\n{2,}) // followed by a blank line\r
+ )\r
+ /g,hashElement);\r
+ */\r
+ text = text.replace(/(?:\n\n)([ ]{0,3}(?:<([?%])[^\r]*?\2>)[ \t]*(?=\n{2,}))/g, hashElement);\r
+\r
+ return text;\r
+ }\r
+\r
+ function hashElement(wholeMatch, m1) {\r
+ var blockText = m1;\r
+\r
+ // Undo double lines\r
+ blockText = blockText.replace(/^\n+/, "");\r
+\r
+ // strip trailing blank lines\r
+ blockText = blockText.replace(/\n+$/g, "");\r
+\r
+ // Replace the element text with a marker ("~KxK" where x is its key)\r
+ blockText = "\n\n~K" + (g_html_blocks.push(blockText) - 1) + "K\n\n";\r
+\r
+ return blockText;\r
+ }\r
+ \r
+ var blockGamutHookCallback = function (t) { return _RunBlockGamut(t); }\r
+\r
+ function _RunBlockGamut(text, doNotUnhash) {\r
+ //\r
+ // These are all the transformations that form block-level\r
+ // tags like paragraphs, headers, and list items.\r
+ //\r
+ \r
+ text = pluginHooks.preBlockGamut(text, blockGamutHookCallback);\r
+ \r
+ text = _DoHeaders(text);\r
+\r
+ // Do Horizontal Rules:\r
+ var replacement = "<hr />\n";\r
+ text = text.replace(/^[ ]{0,2}([ ]?\*[ ]?){3,}[ \t]*$/gm, replacement);\r
+ text = text.replace(/^[ ]{0,2}([ ]?-[ ]?){3,}[ \t]*$/gm, replacement);\r
+ text = text.replace(/^[ ]{0,2}([ ]?_[ ]?){3,}[ \t]*$/gm, replacement);\r
+\r
+ text = _DoLists(text);\r
+ text = _DoCodeBlocks(text);\r
+ text = _DoBlockQuotes(text);\r
+ \r
+ text = pluginHooks.postBlockGamut(text, blockGamutHookCallback);\r
+\r
+ // We already ran _HashHTMLBlocks() before, in Markdown(), but that\r
+ // was to escape raw HTML in the original Markdown source. This time,\r
+ // we're escaping the markup we've just created, so that we don't wrap\r
+ // <p> tags around block-level tags.\r
+ text = _HashHTMLBlocks(text);\r
+ text = _FormParagraphs(text, doNotUnhash);\r
+\r
+ return text;\r
+ }\r
+\r
+ function _RunSpanGamut(text) {\r
+ //\r
+ // These are all the transformations that occur *within* block-level\r
+ // tags like paragraphs, headers, and list items.\r
+ //\r
+\r
+ text = pluginHooks.preSpanGamut(text);\r
+ \r
+ text = _DoCodeSpans(text);\r
+ text = _EscapeSpecialCharsWithinTagAttributes(text);\r
+ text = _EncodeBackslashEscapes(text);\r
+\r
+ // Process anchor and image tags. Images must come first,\r
+ // because ![foo][f] looks like an anchor.\r
+ text = _DoImages(text);\r
+ text = _DoAnchors(text);\r
+\r
+ // Make links out of things like `<http://example.com/>`\r
+ // Must come after _DoAnchors(), because you can use < and >\r
+ // delimiters in inline links like [this](<url>).\r
+ text = _DoAutoLinks(text);\r
+ \r
+ text = text.replace(/~P/g, "://"); // put in place to prevent autolinking; reset now\r
+ \r
+ text = _EncodeAmpsAndAngles(text);\r
+ text = _DoItalicsAndBold(text);\r
+\r
+ // Do hard breaks:\r
+ text = text.replace(/ +\n/g, " <br>\n");\r
+ \r
+ text = pluginHooks.postSpanGamut(text);\r
+\r
+ return text;\r
+ }\r
+\r
+ function _EscapeSpecialCharsWithinTagAttributes(text) {\r
+ //\r
+ // Within tags -- meaning between < and > -- encode [\ ` * _] so they\r
+ // don't conflict with their use in Markdown for code, italics and strong.\r
+ //\r
+\r
+ // Build a regex to find HTML tags and comments. See Friedl's \r
+ // "Mastering Regular Expressions", 2nd Ed., pp. 200-201.\r
+\r
+ // SE: changed the comment part of the regex\r
+\r
+ var regex = /(<[a-z\/!$]("[^"]*"|'[^']*'|[^'">])*>|<!(--(?:|(?:[^>-]|-[^>])(?:[^-]|-[^-])*)--)>)/gi;\r
+\r
+ text = text.replace(regex, function (wholeMatch) {\r
+ var tag = wholeMatch.replace(/(.)<\/?code>(?=.)/g, "$1`");\r
+ tag = escapeCharacters(tag, wholeMatch.charAt(1) == "!" ? "\\`*_/" : "\\`*_"); // also escape slashes in comments to prevent autolinking there -- http://meta.stackoverflow.com/questions/95987\r
+ return tag;\r
+ });\r
+\r
+ return text;\r
+ }\r
+\r
+ function _DoAnchors(text) {\r
+ //\r
+ // Turn Markdown link shortcuts into XHTML <a> tags.\r
+ //\r
+ //\r
+ // First, handle reference-style links: [link text] [id]\r
+ //\r
+\r
+ /*\r
+ text = text.replace(/\r
+ ( // wrap whole match in $1\r
+ \[\r
+ (\r
+ (?:\r
+ \[[^\]]*\] // allow brackets nested one level\r
+ |\r
+ [^\[] // or anything else\r
+ )*\r
+ )\r
+ \]\r
+\r
+ [ ]? // one optional space\r
+ (?:\n[ ]*)? // one optional newline followed by spaces\r
+\r
+ \[\r
+ (.*?) // id = $3\r
+ \]\r
+ )\r
+ ()()()() // pad remaining backreferences\r
+ /g, writeAnchorTag);\r
+ */\r
+ text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, writeAnchorTag);\r
+\r
+ //\r
+ // Next, inline-style links: [link text](url "optional title")\r
+ //\r
+\r
+ /*\r
+ text = text.replace(/\r
+ ( // wrap whole match in $1\r
+ \[\r
+ (\r
+ (?:\r
+ \[[^\]]*\] // allow brackets nested one level\r
+ |\r
+ [^\[\]] // or anything else\r
+ )*\r
+ )\r
+ \]\r
+ \( // literal paren\r
+ [ \t]*\r
+ () // no id, so leave $3 empty\r
+ <?( // href = $4\r
+ (?:\r
+ \([^)]*\) // allow one level of (correctly nested) parens (think MSDN)\r
+ |\r
+ [^()\s]\r
+ )*?\r
+ )>? \r
+ [ \t]*\r
+ ( // $5\r
+ (['"]) // quote char = $6\r
+ (.*?) // Title = $7\r
+ \6 // matching quote\r
+ [ \t]* // ignore any spaces/tabs between closing quote and )\r
+ )? // title is optional\r
+ \)\r
+ )\r
+ /g, writeAnchorTag);\r
+ */\r
+\r
+ text = text.replace(/(\[((?:\[[^\]]*\]|[^\[\]])*)\]\([ \t]*()<?((?:\([^)]*\)|[^()\s])*?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, writeAnchorTag);\r
+\r
+ //\r
+ // Last, handle reference-style shortcuts: [link text]\r
+ // These must come last in case you've also got [link test][1]\r
+ // or [link test](/foo)\r
+ //\r
+\r
+ /*\r
+ text = text.replace(/\r
+ ( // wrap whole match in $1\r
+ \[\r
+ ([^\[\]]+) // link text = $2; can't contain '[' or ']'\r
+ \]\r
+ )\r
+ ()()()()() // pad rest of backreferences\r
+ /g, writeAnchorTag);\r
+ */\r
+ text = text.replace(/(\[([^\[\]]+)\])()()()()()/g, writeAnchorTag);\r
+\r
+ return text;\r
+ }\r
+\r
+ function writeAnchorTag(wholeMatch, m1, m2, m3, m4, m5, m6, m7) {\r
+ if (m7 == undefined) m7 = "";\r
+ var whole_match = m1;\r
+ var link_text = m2.replace(/:\/\//g, "~P"); // to prevent auto-linking withing the link. will be converted back after the auto-linker runs\r
+ var link_id = m3.toLowerCase();\r
+ var url = m4;\r
+ var title = m7;\r
+\r
+ if (url == "") {\r
+ if (link_id == "") {\r
+ // lower-case and turn embedded newlines into spaces\r
+ link_id = link_text.toLowerCase().replace(/ ?\n/g, " ");\r
+ }\r
+ url = "#" + link_id;\r
+\r
+ if (g_urls.get(link_id) != undefined) {\r
+ url = g_urls.get(link_id);\r
+ if (g_titles.get(link_id) != undefined) {\r
+ title = g_titles.get(link_id);\r
+ }\r
+ }\r
+ else {\r
+ if (whole_match.search(/\(\s*\)$/m) > -1) {\r
+ // Special case for explicit empty url\r
+ url = "";\r
+ } else {\r
+ return whole_match;\r
+ }\r
+ }\r
+ }\r
+ url = encodeProblemUrlChars(url);\r
+ url = escapeCharacters(url, "*_");\r
+ var result = "<a href=\"" + url + "\"";\r
+\r
+ if (title != "") {\r
+ title = attributeEncode(title);\r
+ title = escapeCharacters(title, "*_");\r
+ result += " title=\"" + title + "\"";\r
+ }\r
+\r
+ result += ">" + link_text + "</a>";\r
+\r
+ return result;\r
+ }\r
+\r
+ function _DoImages(text) {\r
+ //\r
+ // Turn Markdown image shortcuts into <img> tags.\r
+ //\r
+\r
+ //\r
+ // First, handle reference-style labeled images: ![alt text][id]\r
+ //\r
+\r
+ /*\r
+ text = text.replace(/\r
+ ( // wrap whole match in $1\r
+ !\[\r
+ (.*?) // alt text = $2\r
+ \]\r
+\r
+ [ ]? // one optional space\r
+ (?:\n[ ]*)? // one optional newline followed by spaces\r
+\r
+ \[\r
+ (.*?) // id = $3\r
+ \]\r
+ )\r
+ ()()()() // pad rest of backreferences\r
+ /g, writeImageTag);\r
+ */\r
+ text = text.replace(/(!\[(.*?)\][ ]?(?:\n[ ]*)?\[(.*?)\])()()()()/g, writeImageTag);\r
+\r
+ //\r
+ // Next, handle inline images: ![alt text](url "optional title")\r
+ // Don't forget: encode * and _\r
+\r
+ /*\r
+ text = text.replace(/\r
+ ( // wrap whole match in $1\r
+ !\[\r
+ (.*?) // alt text = $2\r
+ \]\r
+ \s? // One optional whitespace character\r
+ \( // literal paren\r
+ [ \t]*\r
+ () // no id, so leave $3 empty\r
+ <?(\S+?)>? // src url = $4\r
+ [ \t]*\r
+ ( // $5\r
+ (['"]) // quote char = $6\r
+ (.*?) // title = $7\r
+ \6 // matching quote\r
+ [ \t]*\r
+ )? // title is optional\r
+ \)\r
+ )\r
+ /g, writeImageTag);\r
+ */\r
+ text = text.replace(/(!\[(.*?)\]\s?\([ \t]*()<?(\S+?)>?[ \t]*((['"])(.*?)\6[ \t]*)?\))/g, writeImageTag);\r
+\r
+ return text;\r
+ }\r
+ \r
+ function attributeEncode(text) {\r
+ // unconditionally replace angle brackets here -- what ends up in an attribute (e.g. alt or title)\r
+ // never makes sense to have verbatim HTML in it (and the sanitizer would totally break it)\r
+ return text.replace(/>/g, ">").replace(/</g, "<").replace(/"/g, """);\r
+ }\r
+\r
+ function writeImageTag(wholeMatch, m1, m2, m3, m4, m5, m6, m7) {\r
+ var whole_match = m1;\r
+ var alt_text = m2;\r
+ var link_id = m3.toLowerCase();\r
+ var url = m4;\r
+ var title = m7;\r
+\r
+ if (!title) title = "";\r
+\r
+ if (url == "") {\r
+ if (link_id == "") {\r
+ // lower-case and turn embedded newlines into spaces\r
+ link_id = alt_text.toLowerCase().replace(/ ?\n/g, " ");\r
+ }\r
+ url = "#" + link_id;\r
+\r
+ if (g_urls.get(link_id) != undefined) {\r
+ url = g_urls.get(link_id);\r
+ if (g_titles.get(link_id) != undefined) {\r
+ title = g_titles.get(link_id);\r
+ }\r
+ }\r
+ else {\r
+ return whole_match;\r
+ }\r
+ }\r
+ \r
+ alt_text = escapeCharacters(attributeEncode(alt_text), "*_[]()");\r
+ url = escapeCharacters(url, "*_");\r
+ var result = "<img src=\"" + url + "\" alt=\"" + alt_text + "\"";\r
+\r
+ // attacklab: Markdown.pl adds empty title attributes to images.\r
+ // Replicate this bug.\r
+\r
+ //if (title != "") {\r
+ title = attributeEncode(title);\r
+ title = escapeCharacters(title, "*_");\r
+ result += " title=\"" + title + "\"";\r
+ //}\r
+\r
+ result += " />";\r
+\r
+ return result;\r
+ }\r
+\r
+ function _DoHeaders(text) {\r
+\r
+ // Setext-style headers:\r
+ // Header 1\r
+ // ========\r
+ // \r
+ // Header 2\r
+ // --------\r
+ //\r
+ text = text.replace(/^(.+)[ \t]*\n=+[ \t]*\n+/gm,\r
+ function (wholeMatch, m1) { return "<h1>" + _RunSpanGamut(m1) + "</h1>\n\n"; }\r
+ );\r
+\r
+ text = text.replace(/^(.+)[ \t]*\n-+[ \t]*\n+/gm,\r
+ function (matchFound, m1) { return "<h2>" + _RunSpanGamut(m1) + "</h2>\n\n"; }\r
+ );\r
+\r
+ // atx-style headers:\r
+ // # Header 1\r
+ // ## Header 2\r
+ // ## Header 2 with closing hashes ##\r
+ // ...\r
+ // ###### Header 6\r
+ //\r
+\r
+ /*\r
+ text = text.replace(/\r
+ ^(\#{1,6}) // $1 = string of #'s\r
+ [ \t]*\r
+ (.+?) // $2 = Header text\r
+ [ \t]*\r
+ \#* // optional closing #'s (not counted)\r
+ \n+\r
+ /gm, function() {...});\r
+ */\r
+\r
+ text = text.replace(/^(\#{1,6})[ \t]*(.+?)[ \t]*\#*\n+/gm,\r
+ function (wholeMatch, m1, m2) {\r
+ var h_level = m1.length;\r
+ return "<h" + h_level + ">" + _RunSpanGamut(m2) + "</h" + h_level + ">\n\n";\r
+ }\r
+ );\r
+\r
+ return text;\r
+ }\r
+\r
+ function _DoLists(text, isInsideParagraphlessListItem) {\r
+ //\r
+ // Form HTML ordered (numbered) and unordered (bulleted) lists.\r
+ //\r
+\r
+ // attacklab: add sentinel to hack around khtml/safari bug:\r
+ // http://bugs.webkit.org/show_bug.cgi?id=11231\r
+ text += "~0";\r
+\r
+ // Re-usable pattern to match any entirel ul or ol list:\r
+\r
+ /*\r
+ var whole_list = /\r
+ ( // $1 = whole list\r
+ ( // $2\r
+ [ ]{0,3} // attacklab: g_tab_width - 1\r
+ ([*+-]|\d+[.]) // $3 = first list item marker\r
+ [ \t]+\r
+ )\r
+ [^\r]+?\r
+ ( // $4\r
+ ~0 // sentinel for workaround; should be $\r
+ |\r
+ \n{2,}\r
+ (?=\S)\r
+ (?! // Negative lookahead for another list item marker\r
+ [ \t]*\r
+ (?:[*+-]|\d+[.])[ \t]+\r
+ )\r
+ )\r
+ )\r
+ /g\r
+ */\r
+ var whole_list = /^(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/gm;\r
+\r
+ if (g_list_level) {\r
+ text = text.replace(whole_list, function (wholeMatch, m1, m2) {\r
+ var list = m1;\r
+ var list_type = (m2.search(/[*+-]/g) > -1) ? "ul" : "ol";\r
+\r
+ var result = _ProcessListItems(list, list_type, isInsideParagraphlessListItem);\r
+\r
+ // Trim any trailing whitespace, to put the closing `</$list_type>`\r
+ // up on the preceding line, to get it past the current stupid\r
+ // HTML block parser. This is a hack to work around the terrible\r
+ // hack that is the HTML block parser.\r
+ result = result.replace(/\s+$/, "");\r
+ result = "<" + list_type + ">" + result + "</" + list_type + ">\n";\r
+ return result;\r
+ });\r
+ } else {\r
+ whole_list = /(\n\n|^\n?)(([ ]{0,3}([*+-]|\d+[.])[ \t]+)[^\r]+?(~0|\n{2,}(?=\S)(?![ \t]*(?:[*+-]|\d+[.])[ \t]+)))/g;\r
+ text = text.replace(whole_list, function (wholeMatch, m1, m2, m3) {\r
+ var runup = m1;\r
+ var list = m2;\r
+\r
+ var list_type = (m3.search(/[*+-]/g) > -1) ? "ul" : "ol";\r
+ var result = _ProcessListItems(list, list_type);\r
+ result = runup + "<" + list_type + ">\n" + result + "</" + list_type + ">\n";\r
+ return result;\r
+ });\r
+ }\r
+\r
+ // attacklab: strip sentinel\r
+ text = text.replace(/~0/, "");\r
+\r
+ return text;\r
+ }\r
+\r
+ var _listItemMarkers = { ol: "\\d+[.]", ul: "[*+-]" };\r
+\r
+ function _ProcessListItems(list_str, list_type, isInsideParagraphlessListItem) {\r
+ //\r
+ // Process the contents of a single ordered or unordered list, splitting it\r
+ // into individual list items.\r
+ //\r
+ // list_type is either "ul" or "ol".\r
+\r
+ // The $g_list_level global keeps track of when we're inside a list.\r
+ // Each time we enter a list, we increment it; when we leave a list,\r
+ // we decrement. If it's zero, we're not in a list anymore.\r
+ //\r
+ // We do this because when we're not inside a list, we want to treat\r
+ // something like this:\r
+ //\r
+ // I recommend upgrading to version\r
+ // 8. Oops, now this line is treated\r
+ // as a sub-list.\r
+ //\r
+ // As a single paragraph, despite the fact that the second line starts\r
+ // with a digit-period-space sequence.\r
+ //\r
+ // Whereas when we're inside a list (or sub-list), that line will be\r
+ // treated as the start of a sub-list. What a kludge, huh? This is\r
+ // an aspect of Markdown's syntax that's hard to parse perfectly\r
+ // without resorting to mind-reading. Perhaps the solution is to\r
+ // change the syntax rules such that sub-lists must start with a\r
+ // starting cardinal number; e.g. "1." or "a.".\r
+\r
+ g_list_level++;\r
+\r
+ // trim trailing blank lines:\r
+ list_str = list_str.replace(/\n{2,}$/, "\n");\r
+\r
+ // attacklab: add sentinel to emulate \z\r
+ list_str += "~0";\r
+\r
+ // In the original attacklab showdown, list_type was not given to this function, and anything\r
+ // that matched /[*+-]|\d+[.]/ would just create the next <li>, causing this mismatch:\r
+ //\r
+ // Markdown rendered by WMD rendered by MarkdownSharp\r
+ // ------------------------------------------------------------------\r
+ // 1. first 1. first 1. first\r
+ // 2. second 2. second 2. second\r
+ // - third 3. third * third\r
+ //\r
+ // We changed this to behave identical to MarkdownSharp. This is the constructed RegEx,\r
+ // with {MARKER} being one of \d+[.] or [*+-], depending on list_type:\r
+ \r
+ /*\r
+ list_str = list_str.replace(/\r
+ (^[ \t]*) // leading whitespace = $1\r
+ ({MARKER}) [ \t]+ // list marker = $2\r
+ ([^\r]+? // list item text = $3\r
+ (\n+)\r
+ )\r
+ (?=\r
+ (~0 | \2 ({MARKER}) [ \t]+)\r
+ )\r
+ /gm, function(){...});\r
+ */\r
+\r
+ var marker = _listItemMarkers[list_type];\r
+ var re = new RegExp("(^[ \\t]*)(" + marker + ")[ \\t]+([^\\r]+?(\\n+))(?=(~0|\\1(" + marker + ")[ \\t]+))", "gm");\r
+ var last_item_had_a_double_newline = false;\r
+ list_str = list_str.replace(re,\r
+ function (wholeMatch, m1, m2, m3) {\r
+ var item = m3;\r
+ var leading_space = m1;\r
+ var ends_with_double_newline = /\n\n$/.test(item);\r
+ var contains_double_newline = ends_with_double_newline || item.search(/\n{2,}/) > -1;\r
+\r
+ if (contains_double_newline || last_item_had_a_double_newline) {\r
+ item = _RunBlockGamut(_Outdent(item), /* doNotUnhash = */true);\r
+ }\r
+ else {\r
+ // Recursion for sub-lists:\r
+ item = _DoLists(_Outdent(item), /* isInsideParagraphlessListItem= */ true);\r
+ item = item.replace(/\n$/, ""); // chomp(item)\r
+ if (!isInsideParagraphlessListItem) // only the outer-most item should run this, otherwise it's run multiple times for the inner ones\r
+ item = _RunSpanGamut(item);\r
+ }\r
+ last_item_had_a_double_newline = ends_with_double_newline;\r
+ return "<li>" + item + "</li>\n";\r
+ }\r
+ );\r
+\r
+ // attacklab: strip sentinel\r
+ list_str = list_str.replace(/~0/g, "");\r
+\r
+ g_list_level--;\r
+ return list_str;\r
+ }\r
+\r
+ function _DoCodeBlocks(text) {\r
+ //\r
+ // Process Markdown `<pre><code>` blocks.\r
+ // \r
+\r
+ /*\r
+ text = text.replace(/\r
+ (?:\n\n|^)\r
+ ( // $1 = the code block -- one or more lines, starting with a space/tab\r
+ (?:\r
+ (?:[ ]{4}|\t) // Lines must start with a tab or a tab-width of spaces - attacklab: g_tab_width\r
+ .*\n+\r
+ )+\r
+ )\r
+ (\n*[ ]{0,3}[^ \t\n]|(?=~0)) // attacklab: g_tab_width\r
+ /g ,function(){...});\r
+ */\r
+\r
+ // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug\r
+ text += "~0";\r
+\r
+ text = text.replace(/(?:\n\n|^\n?)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g,\r
+ function (wholeMatch, m1, m2) {\r
+ var codeblock = m1;\r
+ var nextChar = m2;\r
+\r
+ codeblock = _EncodeCode(_Outdent(codeblock));\r
+ codeblock = _Detab(codeblock);\r
+ codeblock = codeblock.replace(/^\n+/g, ""); // trim leading newlines\r
+ codeblock = codeblock.replace(/\n+$/g, ""); // trim trailing whitespace\r
+\r
+ codeblock = "<pre><code>" + codeblock + "\n</code></pre>";\r
+\r
+ return "\n\n" + codeblock + "\n\n" + nextChar;\r
+ }\r
+ );\r
+\r
+ // attacklab: strip sentinel\r
+ text = text.replace(/~0/, "");\r
+\r
+ return text;\r
+ }\r
+\r
+ function hashBlock(text) {\r
+ text = text.replace(/(^\n+|\n+$)/g, "");\r
+ return "\n\n~K" + (g_html_blocks.push(text) - 1) + "K\n\n";\r
+ }\r
+\r
+ function _DoCodeSpans(text) {\r
+ //\r
+ // * Backtick quotes are used for <code></code> spans.\r
+ // \r
+ // * You can use multiple backticks as the delimiters if you want to\r
+ // include literal backticks in the code span. So, this input:\r
+ // \r
+ // Just type ``foo `bar` baz`` at the prompt.\r
+ // \r
+ // Will translate to:\r
+ // \r
+ // <p>Just type <code>foo `bar` baz</code> at the prompt.</p>\r
+ // \r
+ // There's no arbitrary limit to the number of backticks you\r
+ // can use as delimters. If you need three consecutive backticks\r
+ // in your code, use four for delimiters, etc.\r
+ //\r
+ // * You can use spaces to get literal backticks at the edges:\r
+ // \r
+ // ... type `` `bar` `` ...\r
+ // \r
+ // Turns to:\r
+ // \r
+ // ... type <code>`bar`</code> ...\r
+ //\r
+\r
+ /*\r
+ text = text.replace(/\r
+ (^|[^\\]) // Character before opening ` can't be a backslash\r
+ (`+) // $2 = Opening run of `\r
+ ( // $3 = The code block\r
+ [^\r]*?\r
+ [^`] // attacklab: work around lack of lookbehind\r
+ )\r
+ \2 // Matching closer\r
+ (?!`)\r
+ /gm, function(){...});\r
+ */\r
+\r
+ text = text.replace(/(^|[^\\])(`+)([^\r]*?[^`])\2(?!`)/gm,\r
+ function (wholeMatch, m1, m2, m3, m4) {\r
+ var c = m3;\r
+ c = c.replace(/^([ \t]*)/g, ""); // leading whitespace\r
+ c = c.replace(/[ \t]*$/g, ""); // trailing whitespace\r
+ c = _EncodeCode(c);\r
+ c = c.replace(/:\/\//g, "~P"); // to prevent auto-linking. Not necessary in code *blocks*, but in code spans. Will be converted back after the auto-linker runs.\r
+ return m1 + "<code>" + c + "</code>";\r
+ }\r
+ );\r
+\r
+ return text;\r
+ }\r
+\r
+ function _EncodeCode(text) {\r
+ //\r
+ // Encode/escape certain characters inside Markdown code runs.\r
+ // The point is that in code, these characters are literals,\r
+ // and lose their special Markdown meanings.\r
+ //\r
+ // Encode all ampersands; HTML entities are not\r
+ // entities within a Markdown code span.\r
+ text = text.replace(/&/g, "&");\r
+\r
+ // Do the angle bracket song and dance:\r
+ text = text.replace(/</g, "<");\r
+ text = text.replace(/>/g, ">");\r
+\r
+ // Now, escape characters that are magic in Markdown:\r
+ text = escapeCharacters(text, "\*_{}[]\\", false);\r
+\r
+ // jj the line above breaks this:\r
+ //---\r
+\r
+ //* Item\r
+\r
+ // 1. Subitem\r
+\r
+ // special char: *\r
+ //---\r
+\r
+ return text;\r
+ }\r
+\r
+ function _DoItalicsAndBold(text) {\r
+\r
+ // <strong> must go first:\r
+ text = text.replace(/([\W_]|^)(\*\*|__)(?=\S)([^\r]*?\S[\*_]*)\2([\W_]|$)/g,\r
+ "$1<strong>$3</strong>$4");\r
+\r
+ text = text.replace(/([\W_]|^)(\*|_)(?=\S)([^\r\*_]*?\S)\2([\W_]|$)/g,\r
+ "$1<em>$3</em>$4");\r
+\r
+ return text;\r
+ }\r
+\r
+ function _DoBlockQuotes(text) {\r
+\r
+ /*\r
+ text = text.replace(/\r
+ ( // Wrap whole match in $1\r
+ (\r
+ ^[ \t]*>[ \t]? // '>' at the start of a line\r
+ .+\n // rest of the first line\r
+ (.+\n)* // subsequent consecutive lines\r
+ \n* // blanks\r
+ )+\r
+ )\r
+ /gm, function(){...});\r
+ */\r
+\r
+ text = text.replace(/((^[ \t]*>[ \t]?.+\n(.+\n)*\n*)+)/gm,\r
+ function (wholeMatch, m1) {\r
+ var bq = m1;\r
+\r
+ // attacklab: hack around Konqueror 3.5.4 bug:\r
+ // "----------bug".replace(/^-/g,"") == "bug"\r
+\r
+ bq = bq.replace(/^[ \t]*>[ \t]?/gm, "~0"); // trim one level of quoting\r
+\r
+ // attacklab: clean up hack\r
+ bq = bq.replace(/~0/g, "");\r
+\r
+ bq = bq.replace(/^[ \t]+$/gm, ""); // trim whitespace-only lines\r
+ bq = _RunBlockGamut(bq); // recurse\r
+\r
+ bq = bq.replace(/(^|\n)/g, "$1 ");\r
+ // These leading spaces screw with <pre> content, so we need to fix that:\r
+ bq = bq.replace(\r
+ /(\s*<pre>[^\r]+?<\/pre>)/gm,\r
+ function (wholeMatch, m1) {\r
+ var pre = m1;\r
+ // attacklab: hack around Konqueror 3.5.4 bug:\r
+ pre = pre.replace(/^ /mg, "~0");\r
+ pre = pre.replace(/~0/g, "");\r
+ return pre;\r
+ });\r
+\r
+ return hashBlock("<blockquote>\n" + bq + "\n</blockquote>");\r
+ }\r
+ );\r
+ return text;\r
+ }\r
+\r
+ function _FormParagraphs(text, doNotUnhash) {\r
+ //\r
+ // Params:\r
+ // $text - string to process with html <p> tags\r
+ //\r
+\r
+ // Strip leading and trailing lines:\r
+ text = text.replace(/^\n+/g, "");\r
+ text = text.replace(/\n+$/g, "");\r
+\r
+ var grafs = text.split(/\n{2,}/g);\r
+ var grafsOut = [];\r
+ \r
+ var markerRe = /~K(\d+)K/;\r
+\r
+ //\r
+ // Wrap <p> tags.\r
+ //\r
+ var end = grafs.length;\r
+ for (var i = 0; i < end; i++) {\r
+ var str = grafs[i];\r
+\r
+ // if this is an HTML marker, copy it\r
+ if (markerRe.test(str)) {\r
+ grafsOut.push(str);\r
+ }\r
+ else if (/\S/.test(str)) {\r
+ str = _RunSpanGamut(str);\r
+ str = str.replace(/^([ \t]*)/g, "<p>");\r
+ str += "</p>"\r
+ grafsOut.push(str);\r
+ }\r
+\r
+ }\r
+ //\r
+ // Unhashify HTML blocks\r
+ //\r
+ if (!doNotUnhash) {\r
+ end = grafsOut.length;\r
+ for (var i = 0; i < end; i++) {\r
+ var foundAny = true;\r
+ while (foundAny) { // we may need several runs, since the data may be nested\r
+ foundAny = false;\r
+ grafsOut[i] = grafsOut[i].replace(/~K(\d+)K/g, function (wholeMatch, id) {\r
+ foundAny = true;\r
+ return g_html_blocks[id];\r
+ });\r
+ }\r
+ }\r
+ }\r
+ return grafsOut.join("\n\n");\r
+ }\r
+\r
+ function _EncodeAmpsAndAngles(text) {\r
+ // Smart processing for ampersands and angle brackets that need to be encoded.\r
+\r
+ // Ampersand-encoding based entirely on Nat Irons's Amputator MT plugin:\r
+ // http://bumppo.net/projects/amputator/\r
+ text = text.replace(/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/g, "&");\r
+\r
+ // Encode naked <'s\r
+ text = text.replace(/<(?![a-z\/?!]|~D)/gi, "<");\r
+\r
+ return text;\r
+ }\r
+\r
+ function _EncodeBackslashEscapes(text) {\r
+ //\r
+ // Parameter: String.\r
+ // Returns: The string, with after processing the following backslash\r
+ // escape sequences.\r
+ //\r
+\r
+ // attacklab: The polite way to do this is with the new\r
+ // escapeCharacters() function:\r
+ //\r
+ // text = escapeCharacters(text,"\\",true);\r
+ // text = escapeCharacters(text,"`*_{}[]()>#+-.!",true);\r
+ //\r
+ // ...but we're sidestepping its use of the (slow) RegExp constructor\r
+ // as an optimization for Firefox. This function gets called a LOT.\r
+\r
+ text = text.replace(/\\(\\)/g, escapeCharacters_callback);\r
+ text = text.replace(/\\([`*_{}\[\]()>#+-.!])/g, escapeCharacters_callback);\r
+ return text;\r
+ }\r
+\r
+ var charInsideUrl = "[-A-Z0-9+&@#/%?=~_|[\\]()!:,.;]",\r
+ charEndingUrl = "[-A-Z0-9+&@#/%=~_|[\\])]",\r
+ autoLinkRegex = new RegExp("(=\"|<)?\\b(https?|ftp)(://" + charInsideUrl + "*" + charEndingUrl + ")(?=$|\\W)", "gi"),\r
+ endCharRegex = new RegExp(charEndingUrl, "i");\r
+\r
+ function handleTrailingParens(wholeMatch, lookbehind, protocol, link) {\r
+ if (lookbehind)\r
+ return wholeMatch;\r
+ if (link.charAt(link.length - 1) !== ")")\r
+ return "<" + protocol + link + ">";\r
+ var parens = link.match(/[()]/g);\r
+ var level = 0;\r
+ for (var i = 0; i < parens.length; i++) {\r
+ if (parens[i] === "(") {\r
+ if (level <= 0)\r
+ level = 1;\r
+ else\r
+ level++;\r
+ }\r
+ else {\r
+ level--;\r
+ }\r
+ }\r
+ var tail = "";\r
+ if (level < 0) {\r
+ var re = new RegExp("\\){1," + (-level) + "}$");\r
+ link = link.replace(re, function (trailingParens) {\r
+ tail = trailingParens;\r
+ return "";\r
+ });\r
+ }\r
+ if (tail) {\r
+ var lastChar = link.charAt(link.length - 1);\r
+ if (!endCharRegex.test(lastChar)) {\r
+ tail = lastChar + tail;\r
+ link = link.substr(0, link.length - 1);\r
+ }\r
+ }\r
+ return "<" + protocol + link + ">" + tail;\r
+ }\r
+ \r
+ function _DoAutoLinks(text) {\r
+\r
+ // note that at this point, all other URL in the text are already hyperlinked as <a href=""></a>\r
+ // *except* for the <http://www.foo.com> case\r
+\r
+ // automatically add < and > around unadorned raw hyperlinks\r
+ // must be preceded by a non-word character (and not by =" or <) and followed by non-word/EOF character\r
+ // simulating the lookbehind in a consuming way is okay here, since a URL can neither and with a " nor\r
+ // with a <, so there is no risk of overlapping matches.\r
+ text = text.replace(autoLinkRegex, handleTrailingParens);\r
+\r
+ // autolink anything like <http://example.com>\r
+ \r
+ var replacer = function (wholematch, m1) { return "<a href=\"" + m1 + "\">" + pluginHooks.plainLinkText(m1) + "</a>"; }\r
+ text = text.replace(/<((https?|ftp):[^'">\s]+)>/gi, replacer);\r
+\r
+ // Email addresses: <address@domain.foo>\r
+ /*\r
+ text = text.replace(/\r
+ <\r
+ (?:mailto:)?\r
+ (\r
+ [-.\w]+\r
+ \@\r
+ [-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+\r
+ )\r
+ >\r
+ /gi, _DoAutoLinks_callback());\r
+ */\r
+\r
+ /* disabling email autolinking, since we don't do that on the server, either\r
+ text = text.replace(/<(?:mailto:)?([-.\w]+\@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi,\r
+ function(wholeMatch,m1) {\r
+ return _EncodeEmailAddress( _UnescapeSpecialChars(m1) );\r
+ }\r
+ );\r
+ */\r
+ return text;\r
+ }\r
+\r
+ function _UnescapeSpecialChars(text) {\r
+ //\r
+ // Swap back in all the special characters we've hidden.\r
+ //\r
+ text = text.replace(/~E(\d+)E/g,\r
+ function (wholeMatch, m1) {\r
+ var charCodeToReplace = parseInt(m1);\r
+ return String.fromCharCode(charCodeToReplace);\r
+ }\r
+ );\r
+ return text;\r
+ }\r
+\r
+ function _Outdent(text) {\r
+ //\r
+ // Remove one level of line-leading tabs or spaces\r
+ //\r
+\r
+ // attacklab: hack around Konqueror 3.5.4 bug:\r
+ // "----------bug".replace(/^-/g,"") == "bug"\r
+\r
+ text = text.replace(/^(\t|[ ]{1,4})/gm, "~0"); // attacklab: g_tab_width\r
+\r
+ // attacklab: clean up hack\r
+ text = text.replace(/~0/g, "")\r
+\r
+ return text;\r
+ }\r
+\r
+ function _Detab(text) {\r
+ if (!/\t/.test(text))\r
+ return text;\r
+\r
+ var spaces = [" ", " ", " ", " "],\r
+ skew = 0,\r
+ v;\r
+\r
+ return text.replace(/[\n\t]/g, function (match, offset) {\r
+ if (match === "\n") {\r
+ skew = offset + 1;\r
+ return match;\r
+ }\r
+ v = (offset - skew) % 4;\r
+ skew = offset + 1;\r
+ return spaces[v];\r
+ });\r
+ }\r
+\r
+ //\r
+ // attacklab: Utility functions\r
+ //\r
+\r
+ var _problemUrlChars = /(?:["'*()[\]:]|~D)/g;\r
+\r
+ // hex-encodes some unusual "problem" chars in URLs to avoid URL detection problems \r
+ function encodeProblemUrlChars(url) {\r
+ if (!url)\r
+ return "";\r
+\r
+ var len = url.length;\r
+\r
+ return url.replace(_problemUrlChars, function (match, offset) {\r
+ if (match == "~D") // escape for dollar\r
+ return "%24";\r
+ if (match == ":") {\r
+ if (offset == len - 1 || /[0-9\/]/.test(url.charAt(offset + 1)))\r
+ return ":"\r
+ }\r
+ return "%" + match.charCodeAt(0).toString(16);\r
+ });\r
+ }\r
+\r
+\r
+ function escapeCharacters(text, charsToEscape, afterBackslash) {\r
+ // First we have to escape the escape characters so that\r
+ // we can build a character class out of them\r
+ var regexString = "([" + charsToEscape.replace(/([\[\]\\])/g, "\\$1") + "])";\r
+\r
+ if (afterBackslash) {\r
+ regexString = "\\\\" + regexString;\r
+ }\r
+\r
+ var regex = new RegExp(regexString, "g");\r
+ text = text.replace(regex, escapeCharacters_callback);\r
+\r
+ return text;\r
+ }\r
+\r
+\r
+ function escapeCharacters_callback(wholeMatch, m1) {\r
+ var charCodeToEscape = m1.charCodeAt(0);\r
+ return "~E" + charCodeToEscape + "E";\r
+ }\r
+\r
+ }; // end of the Markdown.Converter constructor\r
+\r
+})();\r