This commit is contained in:
Christopher Jeffrey 2011-08-13 21:06:34 -05:00
parent 4f14a31d0f
commit 0b200e4ab3

View File

@ -176,94 +176,6 @@ lex.token = function lex(str, tokens, line) {
* Inline Processing * Inline Processing
*/ */
// this is really bad. i should define
// some lexemes for all of the inline stuff,
// but this was just easier for the time being.
if(0) var inline = function(str) {
var hash = ['#'];
str = str.replace(/#/g, '#0#');
str = str.replace(/`([^`]+)`/g, function(__, text) {
text = '<code>' + escape(text) + '</code>';
return '#' + (hash.push(text) - 1) + '#';
});
// for <http://hello.world/> links
str = str.replace(
/<([^<>:\/ ]+:(?:\/\/)?[^>\n]+?|[^<>\n]+?(@)[^<>\n]+?)>/g,
function(__, href, at) {
if (at) {
// according to the markdown "spec"
// we need to mangle email addresses
var href = mangle(href)
, mail = mangle('mailto:') + href;
return '<a href="' + mail + '">' + href + '</a>';
}
return '<a href="' + href + '">' + href + '</a>';
}
);
str = str.replace(/<[^\n>]+>/g, function(tag) {
return '#' + (hash.push(tag) - 1) + '#';
});
str = escape(str);
// links
str = str.replace(
/\[([^\]]+)\]\(([^\)]+)\)/g,
'<a href="$2">$1</a>'
);
// This is [an example][id]
// reference-style link.
str = str.replace(
/\[([^\]]+)\]\[([^\]]+)\]/g,
function(__, text, id) {
var link = tokens.links[id];
return '<a href="'
+ link.href + '"'
+ (link.title
? ' title="'
+ link.title + '"'
: '')
+ '>' + text + '</a>';
}
);
// img
str = str.replace(
/!\[([^\]]+)\]\(([^\s\)]+)\s*([^\)]*)\)/g,
function(_, alt, src, title) {
return '<img src="'
+ src + '" alt="'
+ alt + '"'
+ (title
? ' title="' + title + '"'
: '')
+ '>';
});
// strong
str = str.replace(/__([^_]+)__/g, '<strong>$1</strong>');
str = str.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>');
// em
str = str.replace(/_([^_]+)_/g, '<em>$1</em>');
str = str.replace(/\*([^*]+)\*/g, '<em>$1</em>');
// br
str = str.replace(/ $/gm, '<br>');
str = str.replace(/#(\d+)#/g, function(__, i) {
return hash[i];
});
return str;
};
var inline = (function() { var inline = (function() {
var inline = { var inline = {
tag: /^<[^\n>]+>/, tag: /^<[^\n>]+>/,
@ -298,7 +210,7 @@ var inline = (function() {
, i , i
, key , key
, rule , rule
, out = [] , out = ''
, links = tokens.links; , links = tokens.links;
while (str.length) { while (str.length) {
@ -312,10 +224,10 @@ var inline = (function() {
switch (key) { switch (key) {
case 'tag': case 'tag':
out.push(cap[0]; out += cap[0];
break; break;
case 'img': case 'img':
out.push('<img src="' out += '<img src="'
+ escape(cap[2]) + escape(cap[2])
+ '" alt="' + escape(cap[1]) + '" alt="' + escape(cap[1])
+ '"' + '"'
@ -324,12 +236,12 @@ var inline = (function() {
+ escape(cap[3]) + escape(cap[3])
+ '"' + '"'
: '') : '')
+ '>'); + '>';
break; break;
case 'link': case 'link':
case 'reflink': case 'reflink':
var link = links[cap[2]] || ''; var link = links[cap[2]] || '';
out.push('<a href="' out += '<a href="'
+ escape(link.href || cap[2]) + escape(link.href || cap[2])
+ '"' + '"'
+ (link.title + (link.title
@ -339,7 +251,7 @@ var inline = (function() {
: '') : '')
+ '>' + '>'
+ inline.lexer(cap[1]) + inline.lexer(cap[1])
+ '</a>'); + '</a>';
break; break;
case 'autolink': case 'autolink':
var mailto = cap[2] var mailto = cap[2]
@ -349,29 +261,29 @@ var inline = (function() {
mailto = mangle(mailto); mailto = mangle(mailto);
mail = mangle('mailto:') + mailto; mail = mangle('mailto:') + mailto;
} }
out.push('<a href="' out += '<a href="'
+ (mail || escape(href)) + '"' + (mail || escape(href)) + '"'
+ '>' + '>'
+ (mailto || escape(href)) + (mailto || escape(href))
+ '</a>'); + '</a>';
break; break;
case 'strong': case 'strong':
out.push('<strong>' out += '<strong>'
+ inline.lexer(cap[2] || cap[1]) + inline.lexer(cap[2] || cap[1])
+ '</strong>'); + '</strong>';
break; break;
case 'em': case 'em':
out.push('<em>' out += '<em>'
+ inline.lexer(cap[2] || cap[1]) + inline.lexer(cap[2] || cap[1])
+ '</em>'); + '</em>';
break; break;
case 'escape': case 'escape':
out.push('<code>' out += '<code>'
+ escape(cap[2] || cap[1]) + escape(cap[2] || cap[1])
+ '</code>'); + '</code>';
break; break;
case 'text': case 'text':
out.push(escape(cap[1])); out += escape(cap[1]);
break; break;
default: default:
break; break;
@ -380,7 +292,7 @@ var inline = (function() {
} }
} }
return out.join(''); return out;
}; };
return inline.lexer; return inline.lexer;
})(); })();