From 22caa7b6a89a8e4bb1e0b9b12e46f75aa5849492 Mon Sep 17 00:00:00 2001 From: balpha Date: Sun, 24 Feb 2013 13:40:06 +0100 Subject: [PATCH 1/3] merge upstream PageDown changes up to current revision c06c6e0d465e --- .../external/Markdown.Converter.js | 87 +++++++++-- .../javascripts/external/Markdown.Editor.js | 144 ++++++++++++------ 2 files changed, 173 insertions(+), 58 deletions(-) diff --git a/app/assets/javascripts/external/Markdown.Converter.js b/app/assets/javascripts/external/Markdown.Converter.js index c9059e640..13c601e08 100644 --- a/app/assets/javascripts/external/Markdown.Converter.js +++ b/app/assets/javascripts/external/Markdown.Converter.js @@ -67,7 +67,11 @@ else if (original === identity) this[hookname] = func; else - this[hookname] = function (x) { return func(original(x)); } + this[hookname] = function (text) { + var args = Array.prototype.slice.call(arguments, 0); + args[0] = original.apply(null, args); + return func.apply(null, args); + }; }, set: function (hookname, func) { if (!this[hookname]) @@ -103,9 +107,28 @@ else Markdown.Converter = function () { var pluginHooks = this.hooks = new HookCollection(); - pluginHooks.addNoop("plainLinkText"); // given a URL that was encountered by itself (without markup), should return the link text that's to be given to this link - pluginHooks.addNoop("preConversion"); // called with the orignal text as given to makeHtml. The result of this plugin hook is the actual markdown source that will be cooked - pluginHooks.addNoop("postConversion"); // called with the final cooked HTML code. The result of this plugin hook is the actual output of makeHtml + + // given a URL that was encountered by itself (without markup), should return the link text that's to be given to this link + pluginHooks.addNoop("plainLinkText"); + + // called with the orignal text as given to makeHtml. The result of this plugin hook is the actual markdown source that will be cooked + pluginHooks.addNoop("preConversion"); + + // called with the text once all normalizations have been completed (tabs to spaces, line endings, etc.), but before any conversions have + pluginHooks.addNoop("postNormalization"); + + // Called with the text before / after creating block elements like code blocks and lists. Note that this is called recursively + // with inner content, e.g. it's called with the full text, and then only with the content of a blockquote. The inner + // call will receive outdented text. + pluginHooks.addNoop("preBlockGamut"); + pluginHooks.addNoop("postBlockGamut"); + + // called with the text of a single block element before / after the span-level conversions (bold, code spans, etc.) have been made + pluginHooks.addNoop("preSpanGamut"); + pluginHooks.addNoop("postSpanGamut"); + + // called with the final cooked HTML code. The result of this plugin hook is the actual output of makeHtml + pluginHooks.addNoop("postConversion"); // // Private state of the converter instance: @@ -168,6 +191,8 @@ else // match consecutive blank lines with /\n+/ instead of something // contorted like /[ \t]*\n+/ . text = text.replace(/^[ \t]+$/mg, ""); + + text = pluginHooks.postNormalization(text); // Turn block-level HTML blocks into hash entries text = _HashHTMLBlocks(text); @@ -378,12 +403,17 @@ else return blockText; } + + var blockGamutHookCallback = function (t) { return _RunBlockGamut(t); } function _RunBlockGamut(text, doNotUnhash) { // // These are all the transformations that form block-level // tags like paragraphs, headers, and list items. // + + text = pluginHooks.preBlockGamut(text, blockGamutHookCallback); + text = _DoHeaders(text); // Do Horizontal Rules: @@ -395,6 +425,8 @@ else text = _DoLists(text); text = _DoCodeBlocks(text); text = _DoBlockQuotes(text); + + text = pluginHooks.postBlockGamut(text, blockGamutHookCallback); // We already ran _HashHTMLBlocks() before, in Markdown(), but that // was to escape raw HTML in the original Markdown source. This time, @@ -412,6 +444,8 @@ else // tags like paragraphs, headers, and list items. // + text = pluginHooks.preSpanGamut(text); + text = _DoCodeSpans(text); text = _EscapeSpecialCharsWithinTagAttributes(text); text = _EncodeBackslashEscapes(text); @@ -433,6 +467,8 @@ else // Do hard breaks: text = text.replace(/ +\n/g, "
\n"); + + text = pluginHooks.postSpanGamut(text); return text; } @@ -938,7 +974,7 @@ else // attacklab: sentinel workarounds for lack of \A and \Z, safari\khtml bug text += "~0"; - text = text.replace(/(?:\n\n|^)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g, + text = text.replace(/(?:\n\n|^\n?)((?:(?:[ ]{4}|\t).*\n+)+)(\n*[ ]{0,3}[^ \t\n]|(?=~0))/g, function (wholeMatch, m1, m2) { var codeblock = m1; var nextChar = m2; @@ -1194,6 +1230,36 @@ else text = text.replace(/\\([`*_{}\[\]()>#+-.!])/g, escapeCharacters_callback); return text; } + + function handleTrailingParens(wholeMatch, lookbehind, protocol, link) { + if (lookbehind) + return wholeMatch; + if (link.charAt(link.length - 1) !== ")") + return "<" + protocol + link + ">"; + var parens = link.match(/[()]/g); + var level = 0; + for (var i = 0; i < parens.length; i++) { + if (parens[i] === "(") { + if (level <= 0) + level = 1; + else + level++; + } + else { + level--; + } + } + var tail = ""; + if (level < 0) { + var re = new RegExp("\\){1," + (-level) + "}$"); + link = link.replace(re, function (trailingParens) { + tail = trailingParens; + return ""; + }); + } + + return "<" + protocol + link + ">" + tail; + } function _DoAutoLinks(text) { @@ -1201,15 +1267,14 @@ else // *except* for the case // automatically add < and > around unadorned raw hyperlinks - // must be preceded by space/BOF and followed by non-word/EOF character - text = text.replace(/(^|\s)(https?|ftp)(:\/\/[-A-Z0-9+&@#\/%?=~_|\[\]\(\)!:,\.;]*[-A-Z0-9+&@#\/%=~_|\[\]\)])($|\W)/gi, "$1<$2$3>$4"); + // must be preceded by a non-word character (and not by =" or <) and followed by non-word/EOF character + // simulating the lookbehind in a consuming way is okay here, since a URL can neither and with a " nor + // with a <, so there is no risk of overlapping matches. + text = text.replace(/(="|<)?\b(https?|ftp)(:\/\/[-A-Z0-9+&@#\/%?=~_|\[\]\(\)!:,\.;]*[-A-Z0-9+&@#\/%=~_|\[\])])(?=$|\W)/gi, handleTrailingParens); // autolink anything like - var replacer = function (wholematch, m1) { - m1encoded = m1.replace(/\_\_/, '%5F%5F'); - return "" + pluginHooks.plainLinkText(m1) + ""; - } + var replacer = function (wholematch, m1) { return "" + pluginHooks.plainLinkText(m1) + ""; } text = text.replace(/<((https?|ftp):[^'">\s]+)>/gi, replacer); return text; diff --git a/app/assets/javascripts/external/Markdown.Editor.js b/app/assets/javascripts/external/Markdown.Editor.js index 80aa78fc2..37ea886f3 100644 --- a/app/assets/javascripts/external/Markdown.Editor.js +++ b/app/assets/javascripts/external/Markdown.Editor.js @@ -36,6 +36,43 @@ isOpera: /opera/.test(nav.userAgent.toLowerCase()) }; + var defaultsStrings = { + bold: "Strong Ctrl+B", + boldexample: "strong text", + + italic: "Emphasis Ctrl+I", + italicexample: "emphasized text", + + link: "Hyperlink Ctrl+L", + linkdescription: "enter link description here", + linkdialog: "

Insert Hyperlink

http://example.com/ \"optional title\"

", + + quote: "Blockquote
Ctrl+Q", + quoteexample: "Blockquote", + + code: "Code Sample
 Ctrl+K",
+        codeexample: "enter code here",
+
+        image: "Image  Ctrl+G",
+        imagedescription: "enter image description here",
+        imagedialog: "

Insert Image

http://example.com/images/diagram.jpg \"optional title\"

Need
free image hosting?

", + + olist: "Numbered List
    Ctrl+O", + ulist: "Bulleted List