2011-07-24 08:15:35 -05:00
|
|
|
/**
|
2012-01-03 00:08:07 -06:00
|
|
|
* marked - A markdown parser (https://github.com/chjj/marked)
|
2012-01-03 00:35:47 -06:00
|
|
|
* Copyright (c) 2011-2012, Christopher Jeffrey. (MIT Licensed)
|
2011-07-24 08:15:35 -05:00
|
|
|
*/
|
|
|
|
|
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-10-22 08:05:31 -05:00
|
|
|
code: /^ {4,}[^\n]*(?:\n {4,}[^\n]*|\n)*(?=\n| *$)/,
|
2012-01-02 04:10:23 -06:00
|
|
|
gfm_code: /^ *``` *(\w+)? *\n([^\0]+?)\s*```(?= *\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+\.)/,
|
2012-01-06 14:41:27 -06:00
|
|
|
html: /^ *(?:comment|closed|closing) *(?:\n{2,}|\s*$)/,
|
|
|
|
paragraph: /^([^\n]+\n?(?!body))+\n*/,
|
|
|
|
text: /^[^\n]+/
|
2011-07-24 08:15:35 -05:00
|
|
|
};
|
|
|
|
|
2012-01-06 14:41:27 -06:00
|
|
|
block.html = (function() {
|
|
|
|
var html = block.html.source;
|
|
|
|
|
|
|
|
html = html
|
|
|
|
.replace('comment', /<!--[^\0]*?-->/.source)
|
|
|
|
.replace('closed', /<(\w+)[^\0]+?<\/\1>/.source)
|
|
|
|
.replace('closing', /<\w+[^>]*>/.source);
|
|
|
|
|
|
|
|
return new
|
|
|
|
RegExp(html);
|
|
|
|
})();
|
2012-01-06 01:07:45 -06:00
|
|
|
|
2012-01-03 13:56:58 -06:00
|
|
|
block.paragraph = (function() {
|
2012-01-06 14:41:27 -06:00
|
|
|
var paragraph = block.paragraph.source
|
|
|
|
, body = [];
|
2012-01-03 13:56:58 -06:00
|
|
|
|
|
|
|
(function push(rule) {
|
2012-01-04 00:54:45 -06:00
|
|
|
rule = rule.source || block[rule].source;
|
2012-01-03 13:56:58 -06:00
|
|
|
body.push(rule.replace(/(^|[^\[])\^/g, '$1'));
|
|
|
|
return push;
|
|
|
|
})
|
2012-01-03 23:10:50 -06:00
|
|
|
('gfm_code')
|
2012-01-03 13:56:58 -06:00
|
|
|
('hr')
|
2012-01-03 21:42:37 -06:00
|
|
|
('heading')
|
|
|
|
('lheading')
|
2012-01-03 13:56:58 -06:00
|
|
|
('blockquote')
|
2012-01-04 00:54:45 -06:00
|
|
|
(new RegExp('<'
|
|
|
|
+ '(?:article|aside|audio|blockquote|canvas|caption|col|colgroup|dialog|div'
|
|
|
|
+ '|d[ltd]|embed|fieldset|figure|figcaption|footer|form|h[1-6r]|header'
|
|
|
|
+ '|hgroup|input|label|legend|li|nav|noscript|object|[ou]l|optgroup|option'
|
|
|
|
+ '|p|param|pre|script|section|select|source|table|t(?:body|foot|head)'
|
2012-01-06 01:07:45 -06:00
|
|
|
+ '|t[dhr]|textarea|video)\\b'));
|
2012-01-03 13:56:58 -06:00
|
|
|
|
2012-01-03 14:16:31 -06:00
|
|
|
return new
|
2012-01-06 14:41:27 -06:00
|
|
|
RegExp(paragraph.replace('body', body.join('|')));
|
2012-01-03 13:56:58 -06:00
|
|
|
})();
|
|
|
|
|
2011-07-24 08:15:35 -05:00
|
|
|
/**
|
2011-10-22 23:39:04 -05:00
|
|
|
* Block 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
|
|
|
|
2012-01-03 00:18:49 -06:00
|
|
|
return block.token(str, tokens, true);
|
2011-08-13 17:06:08 -05:00
|
|
|
};
|
2011-08-13 16:53:15 -05:00
|
|
|
|
2012-01-03 00:18:49 -06:00
|
|
|
block.token = function(str, tokens, top) {
|
2011-10-22 06:09:28 -05:00
|
|
|
var str = str.replace(/^ +$/gm, '')
|
|
|
|
, loose
|
2011-10-22 07:49:11 -05:00
|
|
|
, cap
|
|
|
|
, item
|
|
|
|
, space
|
|
|
|
, i
|
|
|
|
, l;
|
2011-10-22 06:09:28 -05:00
|
|
|
|
|
|
|
while (str) {
|
2011-10-22 23:39:04 -05:00
|
|
|
// newline
|
2011-10-22 06:09:28 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2011-10-22 23:39:04 -05:00
|
|
|
// code
|
2011-10-22 08:05:31 -05:00
|
|
|
if (cap = block.code.exec(str)) {
|
2011-10-22 06:09:28 -05:00
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
cap = cap[0].replace(/^ {4}/gm, '');
|
|
|
|
tokens.push({
|
2011-10-22 08:05:31 -05:00
|
|
|
type: 'code',
|
2011-12-06 09:52:44 -06:00
|
|
|
text: cap[cap.length-1] === '\n'
|
|
|
|
? cap.slice(0, -1)
|
|
|
|
: cap
|
2011-10-22 06:09:28 -05:00
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-01-02 04:10:23 -06:00
|
|
|
// gfm_code
|
|
|
|
if (cap = block.gfm_code.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
tokens.push({
|
|
|
|
type: 'code',
|
|
|
|
lang: cap[1],
|
|
|
|
text: cap[2]
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-22 23:39:04 -05:00
|
|
|
// heading
|
2011-10-22 06:09:28 -05:00
|
|
|
if (cap = block.heading.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
tokens.push({
|
|
|
|
type: 'heading',
|
|
|
|
depth: cap[1].length,
|
|
|
|
text: cap[2]
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-22 23:39:04 -05:00
|
|
|
// lheading
|
2011-10-22 06:09:28 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-10-22 23:39:04 -05:00
|
|
|
// hr
|
2011-10-22 06:09:28 -05:00
|
|
|
if (cap = block.hr.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
tokens.push({
|
|
|
|
type: 'hr'
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-22 23:39:04 -05:00
|
|
|
// blockquote
|
2011-10-22 06:09:28 -05:00
|
|
|
if (cap = block.blockquote.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
tokens.push({
|
|
|
|
type: 'blockquote_start'
|
|
|
|
});
|
|
|
|
cap = cap[0].replace(/^ *>/gm, '');
|
2012-01-06 01:07:45 -06:00
|
|
|
|
|
|
|
// Pass `top` to keep the current
|
|
|
|
// "toplevel" state. This is exactly
|
2012-01-03 15:27:55 -06:00
|
|
|
// how markdown.pl works.
|
|
|
|
block.token(cap, tokens, top);
|
2012-01-06 01:07:45 -06:00
|
|
|
|
2011-10-22 06:09:28 -05:00
|
|
|
tokens.push({
|
|
|
|
type: 'blockquote_end'
|
|
|
|
});
|
|
|
|
continue;
|
2011-08-14 01:02:14 -05:00
|
|
|
}
|
2011-10-22 06:09:28 -05:00
|
|
|
|
2011-10-22 23:39:04 -05:00
|
|
|
// list
|
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]);
|
|
|
|
|
2012-01-06 01:07:45 -06:00
|
|
|
// Get each top-level item.
|
2011-10-22 06:09:28 -05:00
|
|
|
cap = cap[0].match(
|
2012-01-03 00:08:07 -06:00
|
|
|
/^( *)([*+-]|\d+\.)[^\n]*(?:\n(?!\1\2|\1\d+\.)[^\n]*)*/gm
|
2011-10-22 06:09:28 -05:00
|
|
|
);
|
|
|
|
|
2011-10-22 23:39:04 -05:00
|
|
|
i = 0;
|
|
|
|
l = cap.length;
|
2011-10-22 07:49:11 -05:00
|
|
|
|
|
|
|
for (; i < l; i++) {
|
2012-01-06 01:07:45 -06:00
|
|
|
// Remove the list item's bullet
|
|
|
|
// so it is seen as the next token.
|
2011-10-22 07:49:11 -05:00
|
|
|
item = cap[i].replace(/^ *([*+-]|\d+\.) */, '');
|
2012-01-06 01:07:45 -06:00
|
|
|
|
|
|
|
// Outdent whatever the
|
|
|
|
// list item contains. Hacky.
|
2011-10-22 07:49:11 -05:00
|
|
|
space = /\n( +)/.exec(item);
|
2011-10-22 06:09:28 -05:00
|
|
|
if (space) {
|
|
|
|
space = new RegExp('^' + space[1], 'gm');
|
|
|
|
item = item.replace(space, '');
|
2011-08-18 18:59:42 -05:00
|
|
|
}
|
2012-01-06 01:07:45 -06: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
|
|
|
});
|
2012-01-06 01:07:45 -06:00
|
|
|
|
|
|
|
// Recurse.
|
2011-10-22 06:09:28 -05:00
|
|
|
block.token(item, tokens);
|
2012-01-06 01:07:45 -06:00
|
|
|
|
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 07:49:11 -05:00
|
|
|
}
|
2011-10-22 06:09:28 -05:00
|
|
|
|
|
|
|
tokens.push({
|
|
|
|
type: 'list_end'
|
|
|
|
});
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-22 23:39:04 -05:00
|
|
|
// html
|
2011-10-22 06:09:28 -05:00
|
|
|
if (cap = block.html.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
tokens.push({
|
|
|
|
type: 'html',
|
|
|
|
text: cap[0]
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-01-06 01:07:45 -06:00
|
|
|
// top-level paragraph
|
2012-01-03 13:56:58 -06:00
|
|
|
if (top && (cap = block.paragraph.exec(str))) {
|
2011-10-22 06:09:28 -05:00
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
tokens.push({
|
2012-01-03 14:16:31 -06:00
|
|
|
type: 'paragraph',
|
2011-10-22 06:09:28 -05:00
|
|
|
text: cap[0]
|
|
|
|
});
|
|
|
|
continue;
|
2011-08-14 01:04:35 -05:00
|
|
|
}
|
2012-01-03 02:25:47 -06:00
|
|
|
|
2012-01-06 01:07:45 -06:00
|
|
|
// text
|
2012-01-03 11:13:36 -06:00
|
|
|
if (cap = block.text.exec(str)) {
|
2012-01-06 01:07:45 -06:00
|
|
|
// Top-level should never reach here.
|
2012-01-03 02:25:47 -06:00
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
tokens.push({
|
|
|
|
type: 'text',
|
|
|
|
text: cap[0]
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
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: /^<([^ >]+(@|:\/)[^ >]+)>/,
|
2012-01-02 23:59:13 -06:00
|
|
|
gfm_autolink: /^(\w+:\/\/[^\s]+[^.,:;"')\]\s])/,
|
2011-10-04 19:41:12 -05:00
|
|
|
tag: /^<!--[^\0]*?-->|^<\/?\w+[^>]*>/,
|
2011-11-26 10:22:02 +04:00
|
|
|
link: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]\(([^\)]*)\)/,
|
2011-08-22 11:02:21 -05:00
|
|
|
reflink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]\s*\[([^\]]*)\]/,
|
|
|
|
nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
|
2012-01-02 23:59:13 -06:00
|
|
|
strong: /^__(?=\S)([^\0]*?\S)__(?!_)|^\*\*(?=\S)([^\0]*?\S)\*\*(?!\*)/,
|
|
|
|
em: /^\b_(?=\S)([^\0]*?\S)_\b|^\*(?=\S)([^\0]*?\S)\*/,
|
2011-08-22 16:19:43 -05:00
|
|
|
code: /^`([^`]+)`|^``([^\0]+?)``/,
|
2011-08-24 10:17:01 -05:00
|
|
|
br: /^ {2,}\n(?!\s*$)/,
|
2012-01-06 14:41:27 -06:00
|
|
|
text: /^[^\0]+?(?=body|$)/
|
2011-08-13 23:19:46 -05:00
|
|
|
};
|
2011-08-13 21:04:18 -05:00
|
|
|
|
2011-08-22 16:19:43 -05:00
|
|
|
inline.text = (function() {
|
2012-01-06 14:41:27 -06:00
|
|
|
var text = inline.text.source
|
|
|
|
, 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')
|
2012-01-02 23:59:13 -06:00
|
|
|
('gfm_autolink')
|
2011-08-22 16:19:43 -05:00
|
|
|
('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
|
2012-01-06 14:41:27 -06:00
|
|
|
RegExp(text.replace('body', body.join('|')));
|
2011-08-22 16:19:43 -05:00
|
|
|
})();
|
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) {
|
2011-10-22 23:39:04 -05:00
|
|
|
// escape
|
2011-10-22 06:09:28 -05:00
|
|
|
if (cap = inline.escape.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
out += cap[1];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-22 23:39:04 -05:00
|
|
|
// autolink
|
2011-10-22 06:09:28 -05:00
|
|
|
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 08:05:31 -05:00
|
|
|
out += '<a href="'
|
|
|
|
+ href
|
|
|
|
+ '">'
|
2011-10-22 06:09:28 -05:00
|
|
|
+ text
|
|
|
|
+ '</a>';
|
|
|
|
continue;
|
2011-08-13 21:04:18 -05:00
|
|
|
}
|
2011-10-22 06:09:28 -05:00
|
|
|
|
2012-01-02 23:59:13 -06:00
|
|
|
// gfm_autolink
|
|
|
|
if (cap = inline.gfm_autolink.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
text = escape(cap[1]);
|
|
|
|
href = text;
|
|
|
|
out += '<a href="'
|
|
|
|
+ href
|
|
|
|
+ '">'
|
|
|
|
+ text
|
|
|
|
+ '</a>';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-22 23:39:04 -05:00
|
|
|
// tag
|
2011-10-22 06:09:28 -05:00
|
|
|
if (cap = inline.tag.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
out += cap[0];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-22 23:39:04 -05:00
|
|
|
// link
|
2011-10-22 06:09:28 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-10-22 23:39:04 -05:00
|
|
|
// reflink, nolink
|
2011-10-22 06:09:28 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-10-22 23:39:04 -05:00
|
|
|
// strong
|
2011-10-22 06:09:28 -05:00
|
|
|
if (cap = inline.strong.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
out += '<strong>'
|
|
|
|
+ inline.lexer(cap[2] || cap[1])
|
|
|
|
+ '</strong>';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-22 23:39:04 -05:00
|
|
|
// em
|
2011-10-22 06:09:28 -05:00
|
|
|
if (cap = inline.em.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
out += '<em>'
|
|
|
|
+ inline.lexer(cap[2] || cap[1])
|
|
|
|
+ '</em>';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-22 23:39:04 -05:00
|
|
|
// code
|
2011-10-22 06:09:28 -05:00
|
|
|
if (cap = inline.code.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
out += '<code>'
|
2011-11-30 07:31:25 -06:00
|
|
|
+ escape(cap[2] || cap[1], true)
|
2011-10-22 06:09:28 -05:00
|
|
|
+ '</code>';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-22 23:39:04 -05:00
|
|
|
// br
|
2011-10-22 06:09:28 -05:00
|
|
|
if (cap = inline.br.exec(str)) {
|
|
|
|
str = str.substring(cap[0].length);
|
|
|
|
out += '<br>';
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-22 23:39:04 -05:00
|
|
|
// text
|
2011-10-22 06:09:28 -05:00
|
|
|
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
|
2012-01-03 23:10:50 -06:00
|
|
|
? ' title="'
|
|
|
|
+ escape(link.title)
|
|
|
|
+ '"'
|
|
|
|
: '')
|
2011-10-22 06:09:28 -05:00
|
|
|
+ '>'
|
|
|
|
+ inline.lexer(cap[1])
|
|
|
|
+ '</a>';
|
|
|
|
} else {
|
|
|
|
return '<img src="'
|
|
|
|
+ escape(link.href)
|
|
|
|
+ '" alt="'
|
|
|
|
+ escape(cap[1])
|
|
|
|
+ '"'
|
|
|
|
+ (link.title
|
2012-01-03 23:10:50 -06:00
|
|
|
? ' title="'
|
|
|
|
+ escape(link.title)
|
|
|
|
+ '"'
|
|
|
|
: '')
|
2011-10-22 06:09:28 -05:00
|
|
|
+ '>';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
+ '>';
|
|
|
|
}
|
2011-10-22 08:05:31 -05:00
|
|
|
case 'code': {
|
2012-01-03 21:42:37 -06:00
|
|
|
return '<pre><code'
|
|
|
|
+ (token.lang
|
2012-01-04 00:54:45 -06:00
|
|
|
? ' class="'
|
2012-01-03 21:42:37 -06:00
|
|
|
+ token.lang
|
|
|
|
+ '"'
|
|
|
|
: '')
|
|
|
|
+ '>'
|
2012-01-02 23:59:13 -06:00
|
|
|
+ (token.escaped
|
2012-01-03 00:08:07 -06:00
|
|
|
? token.text
|
|
|
|
: escape(token.text, true))
|
2011-07-24 08:15:35 -05:00
|
|
|
+ '</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
|
|
|
}
|
2012-01-03 14:16:31 -06:00
|
|
|
case 'paragraph': {
|
|
|
|
return '<p>'
|
|
|
|
+ inline.lexer(token.text)
|
|
|
|
+ '</p>';
|
|
|
|
}
|
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() {
|
2011-12-17 16:42:05 -06:00
|
|
|
var body = [ token.text ]
|
|
|
|
, top;
|
2011-10-04 17:32:59 -05:00
|
|
|
|
2011-12-17 16:42:05 -06:00
|
|
|
while ((top = tokens[tokens.length-1])
|
|
|
|
&& top.type === 'text') {
|
2011-10-04 17:32:59 -05:00
|
|
|
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-12-30 19:30:32 -06:00
|
|
|
return out.join('\n');
|
2011-07-24 08:15:35 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helpers
|
|
|
|
*/
|
|
|
|
|
2011-11-30 07:31:25 -06:00
|
|
|
var escape = function(html, dbl) {
|
2011-07-24 08:15:35 -05:00
|
|
|
return html
|
2011-11-30 07:31:25 -06:00
|
|
|
.replace(!dbl
|
|
|
|
? /&(?!#?\w+;)/g
|
|
|
|
: /&/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;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2011-11-26 22:12:02 -06:00
|
|
|
marked.parse = marked;
|
|
|
|
|
2011-08-13 23:19:46 -05:00
|
|
|
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);
|