2011-07-24 08:15:35 -05:00
|
|
|
/**
|
|
|
|
* marked - A markdown parser
|
|
|
|
* Copyright (c) 2011, Christopher Jeffrey. (MIT Licensed)
|
|
|
|
*/
|
|
|
|
|
2011-08-13 23:19:46 -05:00
|
|
|
;(function() {
|
|
|
|
|
2011-07-24 08:15:35 -05:00
|
|
|
/**
|
|
|
|
* Block-Level Grammar
|
|
|
|
*/
|
|
|
|
|
2011-08-13 23:19:46 -05:00
|
|
|
var block = {
|
2011-08-18 18:59:42 -05:00
|
|
|
newline: /^\n+/,
|
2011-08-21 22:42:17 -05:00
|
|
|
block: /^ {4,}[^\n]*(?:\n {4,}[^\n]*|\n)*(?=\n| *$)/,
|
2011-08-21 17:00:42 -05:00
|
|
|
hr: /^( *[\-*_]){3,} *\n/,
|
2011-09-14 12:41:51 -05:00
|
|
|
heading: /^ *(#{1,6}) *([^\0]+?) *#* *\n+/,
|
2011-07-24 08:15:35 -05:00
|
|
|
lheading: /^([^\n]+)\n *(=|-){3,}/,
|
|
|
|
blockquote: /^ *>[^\n]*(?:\n *>[^\n]*)*/,
|
2011-08-22 10:58:51 -05:00
|
|
|
list: /^( *)([*+-]|\d+\.) [^\0]+?(?:\n{2,}(?! )|\s*$)(?!\1\2|\1\d+\.)/,
|
2011-10-04 20:46:35 -05:00
|
|
|
html: /^ *(?:<!--[^\0]*?-->|<(\w+)[^\0]+?<\/\1>|<\w+[^>]*>) *(?:\n{2,}|\s*$)/,
|
2011-07-24 08:15:35 -05:00
|
|
|
text: /^[^\n]+/
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2011-08-13 17:06:08 -05:00
|
|
|
* Lexer
|
2011-07-24 08:15:35 -05:00
|
|
|
*/
|
|
|
|
|
2011-08-13 23:19:46 -05:00
|
|
|
block.lexer = function(str) {
|
2011-08-13 17:06:08 -05:00
|
|
|
var tokens = []
|
|
|
|
, links = {};
|
2011-07-24 08:15:35 -05:00
|
|
|
|
2011-10-04 17:40:33 -05:00
|
|
|
str = str.replace(/\r\n|\r/g, '\n')
|
2011-08-14 02:47:56 -05:00
|
|
|
.replace(/\t/g, ' ');
|
2011-08-13 23:19:46 -05:00
|
|
|
|
2011-08-13 17:06:08 -05:00
|
|
|
str = str.replace(
|
2011-10-04 17:32:59 -05:00
|
|
|
/^ {0,3}\[([^\]]+)\]: *([^ ]+)(?: +"([^\n]+)")? *$/gm,
|
2011-08-21 09:01:03 -05:00
|
|
|
function(__, id, href, title) {
|
2011-08-13 23:19:46 -05:00
|
|
|
links[id] = {
|
|
|
|
href: href,
|
2011-08-21 09:01:03 -05:00
|
|
|
title: title
|
2011-08-13 23:19:46 -05:00
|
|
|
};
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
);
|
2011-08-13 17:06:08 -05:00
|
|
|
|
|
|
|
tokens.links = links;
|
2011-07-24 08:15:35 -05:00
|
|
|
|
2011-08-18 18:59:42 -05:00
|
|
|
return block.token(str, tokens);
|
2011-08-13 17:06:08 -05:00
|
|
|
};
|
2011-08-13 16:53:15 -05:00
|
|
|
|
2011-08-18 18:59:42 -05:00
|
|
|
block.token = function(str, tokens) {
|
2011-10-22 06:09:28 -05:00
|
|
|
var str = str.replace(/^ +$/gm, '')
|
|
|
|
, loose
|
|
|
|
, cap;
|
|
|
|
|
|
|
|
while (str) {
|
|
|
|
if (cap = block.newline.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
if (cap[0].length > 1) {
|
|
|
|
tokens.push({
|
|
|
|
type: 'space'
|
|
|
|
});
|
2011-08-14 01:02:14 -05:00
|
|
|
}
|
2011-10-22 06:09:28 -05:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cap = block.block.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
cap = cap[0].replace(/^ {4}/gm, '');
|
|
|
|
tokens.push({
|
|
|
|
type: 'block',
|
|
|
|
text: cap
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cap = block.heading.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
tokens.push({
|
|
|
|
type: 'heading',
|
|
|
|
depth: cap[1].length,
|
|
|
|
text: cap[2]
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cap = block.lheading.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
tokens.push({
|
|
|
|
type: 'heading',
|
|
|
|
depth: cap[2] === '=' ? 1 : 2,
|
|
|
|
text: cap[1]
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cap = block.hr.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
tokens.push({
|
|
|
|
type: 'hr'
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cap = block.blockquote.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
tokens.push({
|
|
|
|
type: 'blockquote_start'
|
|
|
|
});
|
|
|
|
cap = cap[0].replace(/^ *>/gm, '');
|
|
|
|
block.token(cap, tokens);
|
|
|
|
tokens.push({
|
|
|
|
type: 'blockquote_end'
|
|
|
|
});
|
|
|
|
continue;
|
2011-08-14 01:02:14 -05:00
|
|
|
}
|
2011-10-22 06:09:28 -05:00
|
|
|
|
|
|
|
if (cap = block.list.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
|
|
|
|
tokens.push({
|
|
|
|
type: 'list_start',
|
|
|
|
ordered: isFinite(cap[2])
|
|
|
|
});
|
|
|
|
|
|
|
|
loose = /\n *\n *(?:[*+-]|\d+\.)/.test(cap[0]);
|
|
|
|
|
|
|
|
// get each top-level item
|
|
|
|
cap = cap[0].match(
|
|
|
|
/^( *)([*+-]|\d+\.)[^\n]*(?:\n(?!\1(?:\2|\d+\.))[^\n]*)*/gm
|
|
|
|
);
|
|
|
|
|
|
|
|
each(cap, function(item) {
|
|
|
|
// remove the list items sigil
|
|
|
|
// so its seen as the next token
|
|
|
|
item = item.replace(/^ *([*+-]|\d+\.) */, '');
|
|
|
|
// outdent whatever the
|
|
|
|
// list item contains, hacky
|
|
|
|
var space = /\n( +)/.exec(item);
|
|
|
|
if (space) {
|
|
|
|
space = new RegExp('^' + space[1], 'gm');
|
|
|
|
item = item.replace(space, '');
|
2011-08-18 18:59:42 -05:00
|
|
|
}
|
2011-08-14 01:04:35 -05:00
|
|
|
tokens.push({
|
2011-10-22 06:09:28 -05:00
|
|
|
type: loose
|
|
|
|
? 'loose_item_start'
|
|
|
|
: 'list_item_start'
|
2011-08-14 01:04:35 -05:00
|
|
|
});
|
2011-10-22 06:09:28 -05:00
|
|
|
block.token(item, tokens);
|
2011-08-14 01:04:35 -05:00
|
|
|
tokens.push({
|
2011-10-22 06:09:28 -05:00
|
|
|
type: 'list_item_end'
|
2011-08-14 01:04:35 -05:00
|
|
|
});
|
2011-10-22 06:09:28 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
tokens.push({
|
|
|
|
type: 'list_end'
|
|
|
|
});
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cap = block.html.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
tokens.push({
|
|
|
|
type: 'html',
|
|
|
|
text: cap[0]
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cap = block.text.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
tokens.push({
|
|
|
|
type: 'text',
|
|
|
|
text: cap[0]
|
|
|
|
});
|
|
|
|
continue;
|
2011-08-14 01:04:35 -05:00
|
|
|
}
|
2011-08-13 16:53:15 -05:00
|
|
|
}
|
2011-07-24 08:15:35 -05:00
|
|
|
|
|
|
|
return tokens;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inline Processing
|
|
|
|
*/
|
|
|
|
|
2011-08-13 23:19:46 -05:00
|
|
|
var inline = {
|
2011-08-21 18:19:08 -05:00
|
|
|
escape: /^\\([\\`*{}\[\]()#+\-.!_])/,
|
2011-08-18 19:31:53 -05:00
|
|
|
autolink: /^<([^ >]+(@|:\/)[^ >]+)>/,
|
2011-10-04 19:41:12 -05:00
|
|
|
tag: /^<!--[^\0]*?-->|^<\/?\w+[^>]*>/,
|
2011-08-22 11:02:21 -05:00
|
|
|
link: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]\s*\(([^\)]*)\)/,
|
|
|
|
reflink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]\s*\[([^\]]*)\]/,
|
|
|
|
nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
|
2011-08-21 22:39:43 -05:00
|
|
|
strong: /^__([^\0]+?)__(?!_)|^\*\*([^\0]+?)\*\*(?!\*)/,
|
2011-08-13 23:19:46 -05:00
|
|
|
em: /^_([^_]+)_|^\*([^*]+)\*/,
|
2011-08-22 16:19:43 -05:00
|
|
|
code: /^`([^`]+)`|^``([^\0]+?)``/,
|
2011-08-24 10:17:01 -05:00
|
|
|
br: /^ {2,}\n(?!\s*$)/,
|
2011-08-22 16:19:43 -05:00
|
|
|
text: /^/
|
2011-08-13 23:19:46 -05:00
|
|
|
};
|
2011-08-13 21:04:18 -05:00
|
|
|
|
2011-08-14 02:35:37 -05:00
|
|
|
// hacky, but performant
|
2011-08-22 16:19:43 -05:00
|
|
|
inline.text = (function() {
|
|
|
|
var body = [];
|
2011-08-14 02:35:37 -05:00
|
|
|
|
2011-08-22 16:19:43 -05:00
|
|
|
(function push(rule) {
|
|
|
|
rule = inline[rule].source;
|
|
|
|
body.push(rule.replace(/(^|[^\[])\^/g, '$1'));
|
|
|
|
return push;
|
|
|
|
})
|
|
|
|
('escape')
|
|
|
|
('tag')
|
|
|
|
('nolink')
|
|
|
|
('strong')
|
|
|
|
('em')
|
2011-08-24 10:10:34 -05:00
|
|
|
('code')
|
|
|
|
('br');
|
2011-08-14 02:35:37 -05:00
|
|
|
|
2011-10-04 17:32:59 -05:00
|
|
|
return new
|
2011-08-22 16:19:43 -05:00
|
|
|
RegExp('^[^\\0]+?(?=' + body.join('|') + '|$)');
|
|
|
|
})();
|
2011-08-13 23:19:46 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Inline Lexer
|
|
|
|
*/
|
|
|
|
|
|
|
|
inline.lexer = function(str) {
|
2011-08-14 01:02:14 -05:00
|
|
|
var out = ''
|
|
|
|
, links = tokens.links
|
2011-08-18 19:26:23 -05:00
|
|
|
, link
|
2011-08-14 01:02:14 -05:00
|
|
|
, text
|
2011-10-22 06:09:28 -05:00
|
|
|
, href
|
2011-08-14 01:02:14 -05:00
|
|
|
, cap;
|
2011-08-13 23:36:44 -05:00
|
|
|
|
2011-10-22 06:09:28 -05:00
|
|
|
while (str) {
|
|
|
|
if (cap = inline.escape.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
out += cap[1];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cap = inline.autolink.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
if (cap[2] === '@') {
|
|
|
|
text = cap[1][6] === ':'
|
|
|
|
? mangle(cap[1].substring(7))
|
|
|
|
: mangle(cap[1]);
|
|
|
|
href = mangle('mailto:') + text;
|
|
|
|
} else {
|
|
|
|
text = escape(cap[1]);
|
|
|
|
href = text;
|
2011-08-13 21:04:18 -05:00
|
|
|
}
|
2011-10-22 06:09:28 -05:00
|
|
|
out += '<a href="' + href + '">'
|
|
|
|
+ text
|
|
|
|
+ '</a>';
|
|
|
|
continue;
|
2011-08-13 21:04:18 -05:00
|
|
|
}
|
2011-10-22 06:09:28 -05:00
|
|
|
|
|
|
|
if (cap = inline.tag.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
out += cap[0];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cap = inline.link.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
text = /^\s*<?([^\s]*?)>?(?:\s+"([^\n]+)")?\s*$/.exec(cap[2]);
|
|
|
|
link = {
|
|
|
|
href: text[1],
|
|
|
|
title: text[2]
|
|
|
|
};
|
|
|
|
out += mlink(cap, link);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((cap = inline.reflink.exec(str))
|
|
|
|
|| (cap = inline.nolink.exec(str))) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
|
|
|
|
link = links[link];
|
|
|
|
if (!link) {
|
|
|
|
out += cap[0][0];
|
|
|
|
str = cap[0].substring(1) + str;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
out += mlink(cap, link);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cap = inline.strong.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
out += '<strong>'
|
|
|
|
+ inline.lexer(cap[2] || cap[1])
|
|
|
|
+ '</strong>';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cap = inline.em.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
out += '<em>'
|
|
|
|
+ inline.lexer(cap[2] || cap[1])
|
|
|
|
+ '</em>';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cap = inline.code.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
out += '<code>'
|
|
|
|
+ escape(cap[2] || cap[1])
|
|
|
|
+ '</code>';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cap = inline.br.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
out += '<br>';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cap = inline.text.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
out += escape(cap[0]);
|
|
|
|
continue;
|
2011-08-14 01:04:35 -05:00
|
|
|
}
|
2011-08-13 23:19:46 -05:00
|
|
|
}
|
2011-08-13 21:04:18 -05:00
|
|
|
|
2011-08-13 23:19:46 -05:00
|
|
|
return out;
|
|
|
|
};
|
2011-08-13 21:04:18 -05:00
|
|
|
|
2011-10-22 06:09:28 -05:00
|
|
|
var mlink = function(cap, link) {
|
|
|
|
if (cap[0][0] !== '!') {
|
|
|
|
return '<a href="'
|
|
|
|
+ escape(link.href)
|
|
|
|
+ '"'
|
|
|
|
+ (link.title
|
|
|
|
? ' title="'
|
|
|
|
+ escape(link.title)
|
|
|
|
+ '"'
|
|
|
|
: '')
|
|
|
|
+ '>'
|
|
|
|
+ inline.lexer(cap[1])
|
|
|
|
+ '</a>';
|
|
|
|
} else {
|
|
|
|
return '<img src="'
|
|
|
|
+ escape(link.href)
|
|
|
|
+ '" alt="'
|
|
|
|
+ escape(cap[1])
|
|
|
|
+ '"'
|
|
|
|
+ (link.title
|
|
|
|
? ' title="'
|
|
|
|
+ escape(link.title)
|
|
|
|
+ '"'
|
|
|
|
: '')
|
|
|
|
+ '>';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-07-24 08:15:35 -05:00
|
|
|
/**
|
|
|
|
* Parsing
|
|
|
|
*/
|
|
|
|
|
2011-08-13 17:06:08 -05:00
|
|
|
var tokens
|
|
|
|
, token;
|
2011-08-13 16:38:46 -05:00
|
|
|
|
2011-07-24 08:15:35 -05:00
|
|
|
var next = function() {
|
|
|
|
return token = tokens.pop();
|
|
|
|
};
|
|
|
|
|
|
|
|
var tok = function() {
|
|
|
|
switch (token.type) {
|
2011-10-22 06:18:15 -05:00
|
|
|
case 'space': {
|
2011-08-18 18:59:42 -05:00
|
|
|
return '';
|
2011-10-22 06:18:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'hr': {
|
2011-07-24 08:15:35 -05:00
|
|
|
return '<hr>';
|
2011-10-22 06:18:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'heading': {
|
|
|
|
return '<h'
|
|
|
|
+ token.depth
|
|
|
|
+ '>'
|
2011-08-13 23:19:46 -05:00
|
|
|
+ inline.lexer(token.text)
|
2011-10-22 06:18:15 -05:00
|
|
|
+ '</h'
|
|
|
|
+ token.depth
|
|
|
|
+ '>';
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'block': {
|
2011-10-04 17:32:59 -05:00
|
|
|
return '<pre><code>'
|
2011-07-24 08:15:35 -05:00
|
|
|
+ escape(token.text)
|
|
|
|
+ '</code></pre>';
|
2011-10-22 06:18:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'blockquote_start': {
|
2011-07-24 08:15:35 -05:00
|
|
|
var body = [];
|
2011-10-22 06:18:15 -05:00
|
|
|
|
2011-07-24 08:15:35 -05:00
|
|
|
while (next().type !== 'blockquote_end') {
|
|
|
|
body.push(tok());
|
|
|
|
}
|
2011-10-22 06:18:15 -05:00
|
|
|
|
2011-10-04 17:32:59 -05:00
|
|
|
return '<blockquote>'
|
|
|
|
+ body.join('')
|
2011-07-24 08:15:35 -05:00
|
|
|
+ '</blockquote>';
|
2011-10-22 06:18:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'list_start': {
|
2011-10-04 17:32:59 -05:00
|
|
|
var type = token.ordered ? 'ol' : 'ul'
|
|
|
|
, body = [];
|
2011-10-22 06:18:15 -05:00
|
|
|
|
2011-07-24 08:15:35 -05:00
|
|
|
while (next().type !== 'list_end') {
|
|
|
|
body.push(tok());
|
|
|
|
}
|
2011-10-22 06:18:15 -05:00
|
|
|
|
|
|
|
return '<'
|
|
|
|
+ type
|
|
|
|
+ '>'
|
2011-10-04 17:32:59 -05:00
|
|
|
+ body.join('')
|
2011-10-22 06:18:15 -05:00
|
|
|
+ '</'
|
|
|
|
+ type
|
|
|
|
+ '>';
|
|
|
|
}
|
|
|
|
|
|
|
|
case 'list_item_start': {
|
2011-07-24 08:15:35 -05:00
|
|
|
var body = [];
|
2011-10-22 06:18:15 -05:00
|
|
|
|
2011-07-24 08:15:35 -05:00
|
|
|
while (next().type !== 'list_item_end') {
|
2011-08-20 10:46:35 -05:00
|
|
|
body.push(token.type === 'text'
|
2011-10-04 17:32:59 -05:00
|
|
|
? text()
|
2011-08-15 20:35:57 -05:00
|
|
|
: tok());
|
2011-07-24 08:15:35 -05:00
|
|
|
}
|
2011-10-22 06:18:15 -05:00
|
|
|
|
2011-10-04 17:32:59 -05:00
|
|
|
return '<li>'
|
|
|
|
+ body.join(' ')
|
2011-07-24 08:15:35 -05:00
|
|
|
+ '</li>';
|
2011-10-22 06:18:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'loose_item_start': {
|
2011-08-20 08:59:47 -05:00
|
|
|
var body = [];
|
2011-10-22 06:18:15 -05:00
|
|
|
|
2011-08-20 08:59:47 -05:00
|
|
|
while (next().type !== 'list_item_end') {
|
|
|
|
body.push(tok());
|
|
|
|
}
|
2011-10-22 06:18:15 -05:00
|
|
|
|
2011-10-04 17:32:59 -05:00
|
|
|
return '<li>'
|
|
|
|
+ body.join(' ')
|
2011-08-20 10:46:35 -05:00
|
|
|
+ '</li>';
|
2011-10-22 06:18:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'html': {
|
2011-08-13 23:19:46 -05:00
|
|
|
return inline.lexer(token.text);
|
2011-10-22 06:18:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
case 'text': {
|
2011-10-22 06:09:28 -05:00
|
|
|
return '<p>'
|
|
|
|
+ text()
|
|
|
|
+ '</p>';
|
2011-10-22 06:18:15 -05:00
|
|
|
}
|
|
|
|
}
|
2011-10-04 17:32:59 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
var text = function() {
|
|
|
|
var body = [ token.text ]
|
|
|
|
, top;
|
|
|
|
|
|
|
|
while ((top = tokens[tokens.length-1])
|
|
|
|
&& top.type === 'text') {
|
|
|
|
body.push(next().text);
|
2011-07-24 08:15:35 -05:00
|
|
|
}
|
2011-10-04 17:32:59 -05:00
|
|
|
|
|
|
|
return inline.lexer(body.join('\n'));
|
2011-07-24 08:15:35 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
var parse = function(src) {
|
|
|
|
tokens = src.reverse();
|
|
|
|
|
|
|
|
var out = [];
|
|
|
|
while (next()) {
|
|
|
|
out.push(tok());
|
|
|
|
}
|
|
|
|
|
|
|
|
tokens = null;
|
|
|
|
token = null;
|
|
|
|
|
2011-07-25 15:37:42 -05:00
|
|
|
return out.join(' ');
|
2011-07-24 08:15:35 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helpers
|
|
|
|
*/
|
|
|
|
|
2011-07-25 15:37:42 -05:00
|
|
|
var escape = function(html) {
|
2011-07-24 08:15:35 -05:00
|
|
|
return html
|
2011-07-25 15:37:42 -05:00
|
|
|
.replace(/&/g, '&')
|
2011-07-24 08:15:35 -05:00
|
|
|
.replace(/</g, '<')
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
.replace(/'/g, ''');
|
|
|
|
};
|
|
|
|
|
|
|
|
var mangle = function(str) {
|
2011-08-14 02:47:56 -05:00
|
|
|
var out = ''
|
|
|
|
, ch
|
2011-07-24 08:15:35 -05:00
|
|
|
, i = 0
|
2011-08-14 02:47:56 -05:00
|
|
|
, l = str.length;
|
2011-07-24 08:15:35 -05:00
|
|
|
|
|
|
|
for (; i < l; i++) {
|
2011-08-13 23:19:46 -05:00
|
|
|
ch = str.charCodeAt(i);
|
|
|
|
if (Math.random() > 0.5) {
|
2011-07-24 08:15:35 -05:00
|
|
|
ch = 'x' + ch.toString(16);
|
|
|
|
}
|
|
|
|
out += '&#' + ch + ';';
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
2011-08-14 02:35:37 -05:00
|
|
|
var each = function(obj, func) {
|
|
|
|
var i = 0, l = obj.length;
|
|
|
|
for (; i < l; i++) func(obj[i]);
|
|
|
|
};
|
|
|
|
|
2011-07-24 08:15:35 -05:00
|
|
|
/**
|
|
|
|
* Expose
|
|
|
|
*/
|
|
|
|
|
2011-08-13 23:19:46 -05:00
|
|
|
var marked = function(str) {
|
|
|
|
return parse(block.lexer(str));
|
2011-07-24 08:15:35 -05:00
|
|
|
};
|
|
|
|
|
2011-08-13 23:19:46 -05:00
|
|
|
marked.parser = parse;
|
|
|
|
marked.lexer = block.lexer;
|
|
|
|
|
|
|
|
if (typeof module !== 'undefined') {
|
|
|
|
module.exports = marked;
|
|
|
|
} else {
|
|
|
|
this.marked = marked;
|
|
|
|
}
|
2011-07-24 08:15:35 -05:00
|
|
|
|
2011-08-13 23:19:46 -05:00
|
|
|
}).call(this);
|