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
*/
// 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 = {
tag: /^<[^\n>]+>/,
@ -298,7 +210,7 @@ var inline = (function() {
, i
, key
, rule
, out = []
, out = ''
, links = tokens.links;
while (str.length) {
@ -312,10 +224,10 @@ var inline = (function() {
switch (key) {
case 'tag':
out.push(cap[0];
out += cap[0];
break;
case 'img':
out.push('<img src="'
out += '<img src="'
+ escape(cap[2])
+ '" alt="' + escape(cap[1])
+ '"'
@ -324,12 +236,12 @@ var inline = (function() {
+ escape(cap[3])
+ '"'
: '')
+ '>');
+ '>';
break;
case 'link':
case 'reflink':
var link = links[cap[2]] || '';
out.push('<a href="'
out += '<a href="'
+ escape(link.href || cap[2])
+ '"'
+ (link.title
@ -339,7 +251,7 @@ var inline = (function() {
: '')
+ '>'
+ inline.lexer(cap[1])
+ '</a>');
+ '</a>';
break;
case 'autolink':
var mailto = cap[2]
@ -349,29 +261,29 @@ var inline = (function() {
mailto = mangle(mailto);
mail = mangle('mailto:') + mailto;
}
out.push('<a href="'
out += '<a href="'
+ (mail || escape(href)) + '"'
+ '>'
+ (mailto || escape(href))
+ '</a>');
+ '</a>';
break;
case 'strong':
out.push('<strong>'
out += '<strong>'
+ inline.lexer(cap[2] || cap[1])
+ '</strong>');
+ '</strong>';
break;
case 'em':
out.push('<em>'
out += '<em>'
+ inline.lexer(cap[2] || cap[1])
+ '</em>');
+ '</em>';
break;
case 'escape':
out.push('<code>'
out += '<code>'
+ escape(cap[2] || cap[1])
+ '</code>');
+ '</code>';
break;
case 'text':
out.push(escape(cap[1]));
out += escape(cap[1]);
break;
default:
break;
@ -380,7 +292,7 @@ var inline = (function() {
}
}
return out.join('');
return out;
};
return inline.lexer;
})();