I thought this would work but it fails horribly Revert "update markdown converter from showdown source, handles trailing parens etc."

This reverts commit e55b713c59.
This commit is contained in:
Sam Saffron 2013-02-11 15:54:26 +11:00
parent 4b92cfe80f
commit 3f7a711dd6
1 changed files with 9 additions and 101 deletions

View File

@ -1,5 +1,3 @@
/* LICENSE: http://code.google.com/p/pagedown/source/browse/LICENSE.txt */
var Markdown; var Markdown;
if (typeof exports === "object" && typeof require === "function") // we're in a CommonJS (e.g. Node.js) module if (typeof exports === "object" && typeof require === "function") // we're in a CommonJS (e.g. Node.js) module
@ -69,11 +67,7 @@ else
if (original === identity) if (original === identity)
this[hookname] = func; this[hookname] = func;
else else
this[hookname] = function (text) { this[hookname] = function (x) { return func(original(x)); }
var args = Array.prototype.slice.call(arguments, 0);
args[0] = original.apply(null, args);
return func.apply(null, args);
};
}, },
set: function (hookname, func) { set: function (hookname, func) {
if (!this[hookname]) if (!this[hookname])
@ -109,28 +103,9 @@ else
Markdown.Converter = function () { Markdown.Converter = function () {
var pluginHooks = this.hooks = new HookCollection(); 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
// 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("plainLinkText"); pluginHooks.addNoop("postConversion"); // called with the final cooked HTML code. The result of this plugin hook is the actual output of makeHtml
// 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: // Private state of the converter instance:
@ -194,8 +169,6 @@ else
// contorted like /[ \t]*\n+/ . // contorted like /[ \t]*\n+/ .
text = text.replace(/^[ \t]+$/mg, ""); text = text.replace(/^[ \t]+$/mg, "");
text = pluginHooks.postNormalization(text);
// Turn block-level HTML blocks into hash entries // Turn block-level HTML blocks into hash entries
text = _HashHTMLBlocks(text); text = _HashHTMLBlocks(text);
@ -406,16 +379,11 @@ else
return blockText; return blockText;
} }
var blockGamutHookCallback = function (t) { return _RunBlockGamut(t); }
function _RunBlockGamut(text, doNotUnhash) { function _RunBlockGamut(text, doNotUnhash) {
// //
// These are all the transformations that form block-level // These are all the transformations that form block-level
// tags like paragraphs, headers, and list items. // tags like paragraphs, headers, and list items.
// //
text = pluginHooks.preBlockGamut(text, blockGamutHookCallback);
text = _DoHeaders(text); text = _DoHeaders(text);
// Do Horizontal Rules: // Do Horizontal Rules:
@ -428,8 +396,6 @@ else
text = _DoCodeBlocks(text); text = _DoCodeBlocks(text);
text = _DoBlockQuotes(text); text = _DoBlockQuotes(text);
text = pluginHooks.postBlockGamut(text, blockGamutHookCallback);
// We already ran _HashHTMLBlocks() before, in Markdown(), but that // We already ran _HashHTMLBlocks() before, in Markdown(), but that
// was to escape raw HTML in the original Markdown source. This time, // was to escape raw HTML in the original Markdown source. This time,
// we're escaping the markup we've just created, so that we don't wrap // we're escaping the markup we've just created, so that we don't wrap
@ -446,8 +412,6 @@ else
// tags like paragraphs, headers, and list items. // tags like paragraphs, headers, and list items.
// //
text = pluginHooks.preSpanGamut(text);
text = _DoCodeSpans(text); text = _DoCodeSpans(text);
text = _EscapeSpecialCharsWithinTagAttributes(text); text = _EscapeSpecialCharsWithinTagAttributes(text);
text = _EncodeBackslashEscapes(text); text = _EncodeBackslashEscapes(text);
@ -470,8 +434,6 @@ else
// Do hard breaks: // Do hard breaks:
text = text.replace(/ +\n/g, " <br>\n"); text = text.replace(/ +\n/g, " <br>\n");
text = pluginHooks.postSpanGamut(text);
return text; return text;
} }
@ -1207,7 +1169,7 @@ else
text = text.replace(/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/g, "&amp;"); text = text.replace(/&(?!#?[xX]?(?:[0-9a-fA-F]+|\w+);)/g, "&amp;");
// Encode naked <'s // Encode naked <'s
text = text.replace(/<(?![a-z\/?!]|~D)/gi, "&lt;"); text = text.replace(/<(?![a-z\/?\$!])/gi, "&lt;");
return text; return text;
} }
@ -1233,46 +1195,14 @@ else
return text; 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) { function _DoAutoLinks(text) {
// note that at this point, all other URL in the text are already hyperlinked as <a href=""></a> // note that at this point, all other URL in the text are already hyperlinked as <a href=""></a>
// *except* for the <http://www.foo.com> case // *except* for the <http://www.foo.com> case
// automatically add < and > around unadorned raw hyperlinks // automatically add < and > around unadorned raw hyperlinks
// must be preceded by a non-word character (and not by =" or <) and followed by non-word/EOF character // must be preceded by space/BOF 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 text = text.replace(/(^|\s)(https?|ftp)(:\/\/[-A-Z0-9+&@#\/%?=~_|\[\]\(\)!:,\.;]*[-A-Z0-9+&@#\/%=~_|\[\]\)])($|\W)/gi, "$1<$2$3>$4");
// 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 <http://example.com> // autolink anything like <http://example.com>
@ -1280,30 +1210,8 @@ else
m1encoded = m1.replace(/\_\_/, '%5F%5F'); m1encoded = m1.replace(/\_\_/, '%5F%5F');
return "<a href=\"" + m1encoded + "\">" + pluginHooks.plainLinkText(m1) + "</a>"; return "<a href=\"" + m1encoded + "\">" + pluginHooks.plainLinkText(m1) + "</a>";
} }
text = text.replace(/<((https?|ftp):[^'">\s]+)>/gi, replacer); text = text.replace(/<((https?|ftp):[^'">\s]+)>/gi, replacer);
// Email addresses: <address@domain.foo>
/*
text = text.replace(/
<
(?:mailto:)?
(
[-.\w]+
\@
[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+
)
>
/gi, _DoAutoLinks_callback());
*/
/* disabling email autolinking, since we don't do that on the server, either
text = text.replace(/<(?:mailto:)?([-.\w]+\@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]+)>/gi,
function(wholeMatch,m1) {
return _EncodeEmailAddress( _UnescapeSpecialChars(m1) );
}
);
*/
return text; return text;
} }