2 * marked - a markdown parser
\r
3 * Copyright (c) 2011-2014, Christopher Jeffrey. (MIT Licensed)
\r
4 * https://github.com/chjj/marked
\r
10 * Block-Level Grammar
\r
15 code: /^( {4}[^\n]+\n*)+/,
\r
17 hr: /^( *[-*_]){3,} *(?:\n+|$)/,
\r
18 heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,
\r
20 lheading: /^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,
\r
21 blockquote: /^( *>[^\n]+(\n(?!def)[^\n]+)*\n*)+/,
\r
22 list: /^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
\r
23 html: /^ *(?:comment *(?:\n|\s*$)|closed *(?:\n{2,}|\s*$)|closing *(?:\n{2,}|\s*$))/,
\r
24 def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/,
\r
26 paragraph: /^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,
\r
30 block.bullet = /(?:[*+-]|\d+\.)/;
\r
31 block.item = /^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/;
\r
32 block.item = replace(block.item, 'gm')
\r
33 (/bull/g, block.bullet)
\r
36 block.list = replace(block.list)
\r
37 (/bull/g, block.bullet)
\r
38 ('hr', '\\n+(?=\\1?(?:[-*_] *){3,}(?:\\n+|$))')
\r
39 ('def', '\\n+(?=' + block.def.source + ')')
\r
42 block.blockquote = replace(block.blockquote)
\r
46 block._tag = '(?!(?:'
\r
47 + 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code'
\r
48 + '|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo'
\r
49 + '|span|br|wbr|ins|del|img)\\b)\\w+(?!:/|[^\\w\\s@]*@)\\b';
\r
51 block.html = replace(block.html)
\r
52 ('comment', /<!--[\s\S]*?-->/)
\r
53 ('closed', /<(tag)[\s\S]+?<\/\1>/)
\r
54 ('closing', /<tag(?:"[^"]*"|'[^']*'|[^'">])*?>/)
\r
55 (/tag/g, block._tag)
\r
58 block.paragraph = replace(block.paragraph)
\r
60 ('heading', block.heading)
\r
61 ('lheading', block.lheading)
\r
62 ('blockquote', block.blockquote)
\r
63 ('tag', '<' + block._tag)
\r
68 * Normal Block Grammar
\r
71 block.normal = merge({}, block);
\r
77 block.gfm = merge({}, block.normal, {
\r
78 fences: /^ *(`{3,}|~{3,}) *(\S+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/,
\r
82 block.gfm.paragraph = replace(block.paragraph)
\r
84 + block.gfm.fences.source.replace('\\1', '\\2') + '|'
\r
85 + block.list.source.replace('\\1', '\\3') + '|')
\r
89 * GFM + Tables Block Grammar
\r
92 block.tables = merge({}, block.gfm, {
\r
93 nptable: /^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/,
\r
94 table: /^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/
\r
101 function Lexer(options) {
\r
103 this.tokens.links = {};
\r
104 this.options = options || marked.defaults;
\r
105 this.rules = block.normal;
\r
107 if (this.options.gfm) {
\r
108 if (this.options.tables) {
\r
109 this.rules = block.tables;
\r
111 this.rules = block.gfm;
\r
117 * Expose Block Rules
\r
120 Lexer.rules = block;
\r
123 * Static Lex Method
\r
126 Lexer.lex = function(src, options) {
\r
127 var lexer = new Lexer(options);
\r
128 return lexer.lex(src);
\r
135 Lexer.prototype.lex = function(src) {
\r
137 .replace(/\r\n|\r/g, '\n')
\r
138 .replace(/\t/g, ' ')
\r
139 .replace(/\u00a0/g, ' ')
\r
140 .replace(/\u2424/g, '\n');
\r
142 return this.token(src, true);
\r
149 Lexer.prototype.token = function(src, top, bq) {
\r
150 var src = src.replace(/^ +$/gm, '')
\r
163 if (cap = this.rules.newline.exec(src)) {
\r
164 src = src.substring(cap[0].length);
\r
165 if (cap[0].length > 1) {
\r
173 if (cap = this.rules.code.exec(src)) {
\r
174 src = src.substring(cap[0].length);
\r
175 cap = cap[0].replace(/^ {4}/gm, '');
\r
178 text: !this.options.pedantic
\r
179 ? cap.replace(/\n+$/, '')
\r
186 if (cap = this.rules.fences.exec(src)) {
\r
187 src = src.substring(cap[0].length);
\r
197 if (cap = this.rules.heading.exec(src)) {
\r
198 src = src.substring(cap[0].length);
\r
201 depth: cap[1].length,
\r
207 // table no leading pipe (gfm)
\r
208 if (top && (cap = this.rules.nptable.exec(src))) {
\r
209 src = src.substring(cap[0].length);
\r
213 header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */),
\r
214 align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
\r
215 cells: cap[3].replace(/\n$/, '').split('\n')
\r
218 for (i = 0; i < item.align.length; i++) {
\r
219 if (/^ *-+: *$/.test(item.align[i])) {
\r
220 item.align[i] = 'right';
\r
221 } else if (/^ *:-+: *$/.test(item.align[i])) {
\r
222 item.align[i] = 'center';
\r
223 } else if (/^ *:-+ *$/.test(item.align[i])) {
\r
224 item.align[i] = 'left';
\r
226 item.align[i] = null;
\r
230 for (i = 0; i < item.cells.length; i++) {
\r
231 item.cells[i] = item.cells[i].split(/ *\| */);
\r
234 this.tokens.push(item);
\r
240 if (cap = this.rules.lheading.exec(src)) {
\r
241 src = src.substring(cap[0].length);
\r
244 depth: cap[2] === '=' ? 1 : 2,
\r
251 if (cap = this.rules.hr.exec(src)) {
\r
252 src = src.substring(cap[0].length);
\r
260 if (cap = this.rules.blockquote.exec(src)) {
\r
261 src = src.substring(cap[0].length);
\r
264 type: 'blockquote_start'
\r
267 cap = cap[0].replace(/^ *> ?/gm, '');
\r
269 // Pass `top` to keep the current
\r
270 // "toplevel" state. This is exactly
\r
271 // how markdown.pl works.
\r
272 this.token(cap, top, true);
\r
275 type: 'blockquote_end'
\r
282 if (cap = this.rules.list.exec(src)) {
\r
283 src = src.substring(cap[0].length);
\r
287 type: 'list_start',
\r
288 ordered: bull.length > 1
\r
291 // Get each top-level item.
\r
292 cap = cap[0].match(this.rules.item);
\r
298 for (; i < l; i++) {
\r
301 // Remove the list item's bullet
\r
302 // so it is seen as the next token.
\r
303 space = item.length;
\r
304 item = item.replace(/^ *([*+-]|\d+\.) +/, '');
\r
306 // Outdent whatever the
\r
307 // list item contains. Hacky.
\r
308 if (~item.indexOf('\n ')) {
\r
309 space -= item.length;
\r
310 item = !this.options.pedantic
\r
311 ? item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '')
\r
312 : item.replace(/^ {1,4}/gm, '');
\r
315 // Determine whether the next list item belongs here.
\r
316 // Backpedal if it does not belong in this list.
\r
317 if (this.options.smartLists && i !== l - 1) {
\r
318 b = block.bullet.exec(cap[i + 1])[0];
\r
319 if (bull !== b && !(bull.length > 1 && b.length > 1)) {
\r
320 src = cap.slice(i + 1).join('\n') + src;
\r
325 // Determine whether item is loose or not.
\r
326 // Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/
\r
327 // for discount behavior.
\r
328 loose = next || /\n\n(?!\s*$)/.test(item);
\r
330 next = item.charAt(item.length - 1) === '\n';
\r
331 if (!loose) loose = next;
\r
336 ? 'loose_item_start'
\r
337 : 'list_item_start'
\r
341 this.token(item, false, bq);
\r
344 type: 'list_item_end'
\r
356 if (cap = this.rules.html.exec(src)) {
\r
357 src = src.substring(cap[0].length);
\r
359 type: this.options.sanitize
\r
362 pre: cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style',
\r
369 if ((!bq && top) && (cap = this.rules.def.exec(src))) {
\r
370 src = src.substring(cap[0].length);
\r
371 this.tokens.links[cap[1].toLowerCase()] = {
\r
379 if (top && (cap = this.rules.table.exec(src))) {
\r
380 src = src.substring(cap[0].length);
\r
384 header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */),
\r
385 align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
\r
386 cells: cap[3].replace(/(?: *\| *)?\n$/, '').split('\n')
\r
389 for (i = 0; i < item.align.length; i++) {
\r
390 if (/^ *-+: *$/.test(item.align[i])) {
\r
391 item.align[i] = 'right';
\r
392 } else if (/^ *:-+: *$/.test(item.align[i])) {
\r
393 item.align[i] = 'center';
\r
394 } else if (/^ *:-+ *$/.test(item.align[i])) {
\r
395 item.align[i] = 'left';
\r
397 item.align[i] = null;
\r
401 for (i = 0; i < item.cells.length; i++) {
\r
402 item.cells[i] = item.cells[i]
\r
403 .replace(/^ *\| *| *\| *$/g, '')
\r
407 this.tokens.push(item);
\r
412 // top-level paragraph
\r
413 if (top && (cap = this.rules.paragraph.exec(src))) {
\r
414 src = src.substring(cap[0].length);
\r
417 text: cap[1].charAt(cap[1].length - 1) === '\n'
\r
418 ? cap[1].slice(0, -1)
\r
425 if (cap = this.rules.text.exec(src)) {
\r
426 // Top-level should never reach here.
\r
427 src = src.substring(cap[0].length);
\r
437 Error('Infinite loop on byte: ' + src.charCodeAt(0));
\r
441 return this.tokens;
\r
445 * Inline-Level Grammar
\r
449 escape: /^\\([\\`*{}\[\]()#+\-.!_>])/,
\r
450 autolink: /^<([^ >]+(@|:\/)[^ >]+)>/,
\r
452 tag: /^<!--[\s\S]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^'">])*?>/,
\r
453 link: /^!?\[(inside)\]\(href\)/,
\r
454 reflink: /^!?\[(inside)\]\s*\[([^\]]*)\]/,
\r
455 nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
\r
456 strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,
\r
457 em: /^\b_((?:__|[\s\S])+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,
\r
458 code: /^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/,
\r
459 br: /^ {2,}\n(?!\s*$)/,
\r
461 text: /^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)/
\r
464 inline._inside = /(?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/;
\r
465 inline._href = /\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/;
\r
467 inline.link = replace(inline.link)
\r
468 ('inside', inline._inside)
\r
469 ('href', inline._href)
\r
472 inline.reflink = replace(inline.reflink)
\r
473 ('inside', inline._inside)
\r
477 * Normal Inline Grammar
\r
480 inline.normal = merge({}, inline);
\r
483 * Pedantic Inline Grammar
\r
486 inline.pedantic = merge({}, inline.normal, {
\r
487 strong: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
\r
488 em: /^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/
\r
492 * GFM Inline Grammar
\r
495 inline.gfm = merge({}, inline.normal, {
\r
496 escape: replace(inline.escape)('])', '~|])')(),
\r
497 url: /^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/,
\r
498 del: /^~~(?=\S)([\s\S]*?\S)~~/,
\r
499 text: replace(inline.text)
\r
501 ('|', '|https?://|')
\r
506 * GFM + Line Breaks Inline Grammar
\r
509 inline.breaks = merge({}, inline.gfm, {
\r
510 br: replace(inline.br)('{2,}', '*')(),
\r
511 text: replace(inline.gfm.text)('{2,}', '*')()
\r
515 * Inline Lexer & Compiler
\r
518 function InlineLexer(links, options) {
\r
519 this.options = options || marked.defaults;
\r
520 this.links = links;
\r
521 this.rules = inline.normal;
\r
522 this.renderer = this.options.renderer || new Renderer;
\r
523 this.renderer.options = this.options;
\r
527 Error('Tokens array requires a `links` property.');
\r
530 if (this.options.gfm) {
\r
531 if (this.options.breaks) {
\r
532 this.rules = inline.breaks;
\r
534 this.rules = inline.gfm;
\r
536 } else if (this.options.pedantic) {
\r
537 this.rules = inline.pedantic;
\r
542 * Expose Inline Rules
\r
545 InlineLexer.rules = inline;
\r
548 * Static Lexing/Compiling Method
\r
551 InlineLexer.output = function(src, links, options) {
\r
552 var inline = new InlineLexer(links, options);
\r
553 return inline.output(src);
\r
560 InlineLexer.prototype.output = function(src) {
\r
569 if (cap = this.rules.escape.exec(src)) {
\r
570 src = src.substring(cap[0].length);
\r
576 if (cap = this.rules.autolink.exec(src)) {
\r
577 src = src.substring(cap[0].length);
\r
578 if (cap[2] === '@') {
\r
579 text = cap[1].charAt(6) === ':'
\r
580 ? this.mangle(cap[1].substring(7))
\r
581 : this.mangle(cap[1]);
\r
582 href = this.mangle('mailto:') + text;
\r
584 text = escape(cap[1]);
\r
587 out += this.renderer.link(href, null, text);
\r
592 if (!this.inLink && (cap = this.rules.url.exec(src))) {
\r
593 src = src.substring(cap[0].length);
\r
594 text = escape(cap[1]);
\r
596 out += this.renderer.link(href, null, text);
\r
601 if (cap = this.rules.tag.exec(src)) {
\r
602 if (!this.inLink && /^<a /i.test(cap[0])) {
\r
603 this.inLink = true;
\r
604 } else if (this.inLink && /^<\/a>/i.test(cap[0])) {
\r
605 this.inLink = false;
\r
607 src = src.substring(cap[0].length);
\r
608 out += this.options.sanitize
\r
615 if (cap = this.rules.link.exec(src)) {
\r
616 src = src.substring(cap[0].length);
\r
617 this.inLink = true;
\r
618 out += this.outputLink(cap, {
\r
622 this.inLink = false;
\r
627 if ((cap = this.rules.reflink.exec(src))
\r
628 || (cap = this.rules.nolink.exec(src))) {
\r
629 src = src.substring(cap[0].length);
\r
630 link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
\r
631 link = this.links[link.toLowerCase()];
\r
632 if (!link || !link.href) {
\r
633 out += cap[0].charAt(0);
\r
634 src = cap[0].substring(1) + src;
\r
637 this.inLink = true;
\r
638 out += this.outputLink(cap, link);
\r
639 this.inLink = false;
\r
644 if (cap = this.rules.strong.exec(src)) {
\r
645 src = src.substring(cap[0].length);
\r
646 out += this.renderer.strong(this.output(cap[2] || cap[1]));
\r
651 if (cap = this.rules.em.exec(src)) {
\r
652 src = src.substring(cap[0].length);
\r
653 out += this.renderer.em(this.output(cap[2] || cap[1]));
\r
658 if (cap = this.rules.code.exec(src)) {
\r
659 src = src.substring(cap[0].length);
\r
660 out += this.renderer.codespan(escape(cap[2], true));
\r
665 if (cap = this.rules.br.exec(src)) {
\r
666 src = src.substring(cap[0].length);
\r
667 out += this.renderer.br();
\r
672 if (cap = this.rules.del.exec(src)) {
\r
673 src = src.substring(cap[0].length);
\r
674 out += this.renderer.del(this.output(cap[1]));
\r
679 if (cap = this.rules.text.exec(src)) {
\r
680 src = src.substring(cap[0].length);
\r
681 out += escape(this.smartypants(cap[0]));
\r
687 Error('Infinite loop on byte: ' + src.charCodeAt(0));
\r
698 InlineLexer.prototype.outputLink = function(cap, link) {
\r
699 var href = escape(link.href)
\r
700 , title = link.title ? escape(link.title) : null;
\r
702 return cap[0].charAt(0) !== '!'
\r
703 ? this.renderer.link(href, title, this.output(cap[1]))
\r
704 : this.renderer.image(href, title, escape(cap[1]));
\r
708 * Smartypants Transformations
\r
711 InlineLexer.prototype.smartypants = function(text) {
\r
712 if (!this.options.smartypants) return text;
\r
715 .replace(/--/g, '\u2014')
\r
717 .replace(/(^|[-\u2014/(\[{"\s])'/g, '$1\u2018')
\r
718 // closing singles & apostrophes
\r
719 .replace(/'/g, '\u2019')
\r
721 .replace(/(^|[-\u2014/(\[{\u2018\s])"/g, '$1\u201c')
\r
723 .replace(/"/g, '\u201d')
\r
725 .replace(/\.{3}/g, '\u2026');
\r
732 InlineLexer.prototype.mangle = function(text) {
\r
738 for (; i < l; i++) {
\r
739 ch = text.charCodeAt(i);
\r
740 if (Math.random() > 0.5) {
\r
741 ch = 'x' + ch.toString(16);
\r
743 out += '&#' + ch + ';';
\r
753 function Renderer(options) {
\r
754 this.options = options || {};
\r
757 Renderer.prototype.code = function(code, lang, escaped) {
\r
758 if (this.options.highlight) {
\r
759 var out = this.options.highlight(code, lang);
\r
760 if (out != null && out !== code) {
\r
767 return '<pre><code>'
\r
768 + (escaped ? code : escape(code, true))
\r
769 + '\n</code></pre>';
\r
772 return '<pre><code class="'
\r
773 + this.options.langPrefix
\r
774 + escape(lang, true)
\r
776 + (escaped ? code : escape(code, true))
\r
777 + '\n</code></pre>\n';
\r
780 Renderer.prototype.blockquote = function(quote) {
\r
781 return '<blockquote>\n' + quote + '</blockquote>\n';
\r
784 Renderer.prototype.html = function(html) {
\r
788 Renderer.prototype.heading = function(text, level, raw) {
\r
792 + this.options.headerPrefix
\r
793 + raw.toLowerCase().replace(/[^\w]+/g, '-')
\r
801 Renderer.prototype.hr = function() {
\r
802 return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
\r
805 Renderer.prototype.list = function(body, ordered) {
\r
806 var type = ordered ? 'ol' : 'ul';
\r
807 return '<' + type + '>\n' + body + '</' + type + '>\n';
\r
810 Renderer.prototype.listitem = function(text) {
\r
811 return '<li>' + text + '</li>\n';
\r
814 Renderer.prototype.paragraph = function(text) {
\r
815 return '<p>' + text + '</p>\n';
\r
818 Renderer.prototype.table = function(header, body) {
\r
829 Renderer.prototype.tablerow = function(content) {
\r
830 return '<tr>\n' + content + '</tr>\n';
\r
833 Renderer.prototype.tablecell = function(content, flags) {
\r
834 var type = flags.header ? 'th' : 'td';
\r
835 var tag = flags.align
\r
836 ? '<' + type + ' style="text-align:' + flags.align + '">'
\r
837 : '<' + type + '>';
\r
838 return tag + content + '</' + type + '>\n';
\r
841 // span level renderer
\r
842 Renderer.prototype.strong = function(text) {
\r
843 return '<strong>' + text + '</strong>';
\r
846 Renderer.prototype.em = function(text) {
\r
847 return '<em>' + text + '</em>';
\r
850 Renderer.prototype.codespan = function(text) {
\r
851 return '<code>' + text + '</code>';
\r
854 Renderer.prototype.br = function() {
\r
855 return this.options.xhtml ? '<br/>' : '<br>';
\r
858 Renderer.prototype.del = function(text) {
\r
859 return '<del>' + text + '</del>';
\r
862 Renderer.prototype.link = function(href, title, text) {
\r
863 if (this.options.sanitize) {
\r
865 var prot = decodeURIComponent(unescape(href))
\r
866 .replace(/[^\w:]/g, '')
\r
871 if (prot.indexOf('javascript:') === 0) {
\r
875 var out = '<a href="' + href + '"';
\r
877 out += ' title="' + title + '"';
\r
879 out += '>' + text + '</a>';
\r
883 Renderer.prototype.image = function(href, title, text) {
\r
884 var out = '<img src="' + href + '" alt="' + text + '"';
\r
886 out += ' title="' + title + '"';
\r
888 out += this.options.xhtml ? '/>' : '>';
\r
893 * Parsing & Compiling
\r
896 function Parser(options) {
\r
899 this.options = options || marked.defaults;
\r
900 this.options.renderer = this.options.renderer || new Renderer;
\r
901 this.renderer = this.options.renderer;
\r
902 this.renderer.options = this.options;
\r
906 * Static Parse Method
\r
909 Parser.parse = function(src, options, renderer) {
\r
910 var parser = new Parser(options, renderer);
\r
911 return parser.parse(src);
\r
918 Parser.prototype.parse = function(src) {
\r
919 this.inline = new InlineLexer(src.links, this.options, this.renderer);
\r
920 this.tokens = src.reverse();
\r
923 while (this.next()) {
\r
934 Parser.prototype.next = function() {
\r
935 return this.token = this.tokens.pop();
\r
939 * Preview Next Token
\r
942 Parser.prototype.peek = function() {
\r
943 return this.tokens[this.tokens.length - 1] || 0;
\r
947 * Parse Text Tokens
\r
950 Parser.prototype.parseText = function() {
\r
951 var body = this.token.text;
\r
953 while (this.peek().type === 'text') {
\r
954 body += '\n' + this.next().text;
\r
957 return this.inline.output(body);
\r
961 * Parse Current Token
\r
964 Parser.prototype.tok = function() {
\r
965 switch (this.token.type) {
\r
970 return this.renderer.hr();
\r
973 return this.renderer.heading(
\r
974 this.inline.output(this.token.text),
\r
979 return this.renderer.code(this.token.text,
\r
981 this.token.escaped);
\r
994 for (i = 0; i < this.token.header.length; i++) {
\r
995 flags = { header: true, align: this.token.align[i] };
\r
996 cell += this.renderer.tablecell(
\r
997 this.inline.output(this.token.header[i]),
\r
998 { header: true, align: this.token.align[i] }
\r
1001 header += this.renderer.tablerow(cell);
\r
1003 for (i = 0; i < this.token.cells.length; i++) {
\r
1004 row = this.token.cells[i];
\r
1007 for (j = 0; j < row.length; j++) {
\r
1008 cell += this.renderer.tablecell(
\r
1009 this.inline.output(row[j]),
\r
1010 { header: false, align: this.token.align[j] }
\r
1014 body += this.renderer.tablerow(cell);
\r
1016 return this.renderer.table(header, body);
\r
1018 case 'blockquote_start': {
\r
1021 while (this.next().type !== 'blockquote_end') {
\r
1022 body += this.tok();
\r
1025 return this.renderer.blockquote(body);
\r
1027 case 'list_start': {
\r
1029 , ordered = this.token.ordered;
\r
1031 while (this.next().type !== 'list_end') {
\r
1032 body += this.tok();
\r
1035 return this.renderer.list(body, ordered);
\r
1037 case 'list_item_start': {
\r
1040 while (this.next().type !== 'list_item_end') {
\r
1041 body += this.token.type === 'text'
\r
1042 ? this.parseText()
\r
1046 return this.renderer.listitem(body);
\r
1048 case 'loose_item_start': {
\r
1051 while (this.next().type !== 'list_item_end') {
\r
1052 body += this.tok();
\r
1055 return this.renderer.listitem(body);
\r
1058 var html = !this.token.pre && !this.options.pedantic
\r
1059 ? this.inline.output(this.token.text)
\r
1060 : this.token.text;
\r
1061 return this.renderer.html(html);
\r
1063 case 'paragraph': {
\r
1064 return this.renderer.paragraph(this.inline.output(this.token.text));
\r
1067 return this.renderer.paragraph(this.parseText());
\r
1076 function escape(html, encode) {
\r
1078 .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
\r
1079 .replace(/</g, '<')
\r
1080 .replace(/>/g, '>')
\r
1081 .replace(/"/g, '"')
\r
1082 .replace(/'/g, ''');
\r
1085 function unescape(html) {
\r
1086 return html.replace(/&([#\w]+);/g, function(_, n) {
\r
1087 n = n.toLowerCase();
\r
1088 if (n === 'colon') return ':';
\r
1089 if (n.charAt(0) === '#') {
\r
1090 return n.charAt(1) === 'x'
\r
1091 ? String.fromCharCode(parseInt(n.substring(2), 16))
\r
1092 : String.fromCharCode(+n.substring(1));
\r
1098 function replace(regex, opt) {
\r
1099 regex = regex.source;
\r
1101 return function self(name, val) {
\r
1102 if (!name) return new RegExp(regex, opt);
\r
1103 val = val.source || val;
\r
1104 val = val.replace(/(^|[^\[])\^/g, '$1');
\r
1105 regex = regex.replace(name, val);
\r
1110 function noop() {}
\r
1113 function merge(obj) {
\r
1118 for (; i < arguments.length; i++) {
\r
1119 target = arguments[i];
\r
1120 for (key in target) {
\r
1121 if (Object.prototype.hasOwnProperty.call(target, key)) {
\r
1122 obj[key] = target[key];
\r
1135 function marked(src, opt, callback) {
\r
1136 if (callback || typeof opt === 'function') {
\r
1142 opt = merge({}, marked.defaults, opt || {});
\r
1144 var highlight = opt.highlight
\r
1150 tokens = Lexer.lex(src, opt)
\r
1152 return callback(e);
\r
1155 pending = tokens.length;
\r
1157 var done = function(err) {
\r
1159 opt.highlight = highlight;
\r
1160 return callback(err);
\r
1166 out = Parser.parse(tokens, opt);
\r
1171 opt.highlight = highlight;
\r
1175 : callback(null, out);
\r
1178 if (!highlight || highlight.length < 3) {
\r
1182 delete opt.highlight;
\r
1184 if (!pending) return done();
\r
1186 for (; i < tokens.length; i++) {
\r
1187 (function(token) {
\r
1188 if (token.type !== 'code') {
\r
1189 return --pending || done();
\r
1191 return highlight(token.text, token.lang, function(err, code) {
\r
1192 if (err) return done(err);
\r
1193 if (code == null || code === token.text) {
\r
1194 return --pending || done();
\r
1196 token.text = code;
\r
1197 token.escaped = true;
\r
1198 --pending || done();
\r
1206 if (opt) opt = merge({}, marked.defaults, opt);
\r
1207 return Parser.parse(Lexer.lex(src, opt), opt);
\r
1209 e.message += '\nPlease report this to https://github.com/chjj/marked.';
\r
1210 if ((opt || marked.defaults).silent) {
\r
1211 return '<p>An error occured:</p><pre>'
\r
1212 + escape(e.message + '', true)
\r
1224 marked.setOptions = function(opt) {
\r
1225 merge(marked.defaults, opt);
\r
1229 marked.defaults = {
\r
1235 smartLists: false,
\r
1238 langPrefix: 'lang-',
\r
1239 smartypants: false,
\r
1241 renderer: new Renderer,
\r
1249 marked.Parser = Parser;
\r
1250 marked.parser = Parser.parse;
\r
1252 marked.Renderer = Renderer;
\r
1254 marked.Lexer = Lexer;
\r
1255 marked.lexer = Lexer.lex;
\r
1257 marked.InlineLexer = InlineLexer;
\r
1258 marked.inlineLexer = InlineLexer.output;
\r
1260 marked.parse = marked;
\r
1262 if (typeof module !== 'undefined' && typeof exports === 'object') {
\r
1263 module.exports = marked;
\r
1264 } else if (typeof define === 'function' && define.amd) {
\r
1265 define(function() { return marked; });
\r
1267 this.marked = marked;
\r
1270 }).call(function() {
\r
1271 return this || (typeof window !== 'undefined' ? window : global);
\r