2 * Copyright 2016 ZTE Corporation.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
19 * Block-Level Grammar
\r
24 code: /^( {4}[^\n]+\n*)+/,
\r
26 hr: /^( *[-*_]){3,} *(?:\n+|$)/,
\r
27 heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,
\r
29 lheading: /^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,
\r
30 blockquote: /^( *>[^\n]+(\n(?!def)[^\n]+)*\n*)+/,
\r
31 list: /^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
\r
32 html: /^ *(?:comment *(?:\n|\s*$)|closed *(?:\n{2,}|\s*$)|closing *(?:\n{2,}|\s*$))/,
\r
33 def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/,
\r
35 paragraph: /^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,
\r
39 block.bullet = /(?:[*+-]|\d+\.)/;
\r
40 block.item = /^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/;
\r
41 block.item = replace(block.item, 'gm')
\r
42 (/bull/g, block.bullet)
\r
45 block.list = replace(block.list)
\r
46 (/bull/g, block.bullet)
\r
47 ('hr', '\\n+(?=\\1?(?:[-*_] *){3,}(?:\\n+|$))')
\r
48 ('def', '\\n+(?=' + block.def.source + ')')
\r
51 block.blockquote = replace(block.blockquote)
\r
55 block._tag = '(?!(?:'
\r
56 + 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code'
\r
57 + '|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo'
\r
58 + '|span|br|wbr|ins|del|img)\\b)\\w+(?!:/|[^\\w\\s@]*@)\\b';
\r
60 block.html = replace(block.html)
\r
61 ('comment', /<!--[\s\S]*?-->/)
\r
62 ('closed', /<(tag)[\s\S]+?<\/\1>/)
\r
63 ('closing', /<tag(?:"[^"]*"|'[^']*'|[^'">])*?>/)
\r
64 (/tag/g, block._tag)
\r
67 block.paragraph = replace(block.paragraph)
\r
69 ('heading', block.heading)
\r
70 ('lheading', block.lheading)
\r
71 ('blockquote', block.blockquote)
\r
72 ('tag', '<' + block._tag)
\r
77 * Normal Block Grammar
\r
80 block.normal = merge({}, block);
\r
86 block.gfm = merge({}, block.normal, {
\r
87 fences: /^ *(`{3,}|~{3,}) *(\S+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/,
\r
91 block.gfm.paragraph = replace(block.paragraph)
\r
93 + block.gfm.fences.source.replace('\\1', '\\2') + '|'
\r
94 + block.list.source.replace('\\1', '\\3') + '|')
\r
98 * GFM + Tables Block Grammar
\r
101 block.tables = merge({}, block.gfm, {
\r
102 nptable: /^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/,
\r
103 table: /^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/
\r
110 function Lexer(options) {
\r
112 this.tokens.links = {};
\r
113 this.options = options || marked.defaults;
\r
114 this.rules = block.normal;
\r
116 if (this.options.gfm) {
\r
117 if (this.options.tables) {
\r
118 this.rules = block.tables;
\r
120 this.rules = block.gfm;
\r
126 * Expose Block Rules
\r
129 Lexer.rules = block;
\r
132 * Static Lex Method
\r
135 Lexer.lex = function(src, options) {
\r
136 var lexer = new Lexer(options);
\r
137 return lexer.lex(src);
\r
144 Lexer.prototype.lex = function(src) {
\r
146 .replace(/\r\n|\r/g, '\n')
\r
147 .replace(/\t/g, ' ')
\r
148 .replace(/\u00a0/g, ' ')
\r
149 .replace(/\u2424/g, '\n');
\r
151 return this.token(src, true);
\r
158 Lexer.prototype.token = function(src, top, bq) {
\r
159 var src = src.replace(/^ +$/gm, '')
\r
172 if (cap = this.rules.newline.exec(src)) {
\r
173 src = src.substring(cap[0].length);
\r
174 if (cap[0].length > 1) {
\r
182 if (cap = this.rules.code.exec(src)) {
\r
183 src = src.substring(cap[0].length);
\r
184 cap = cap[0].replace(/^ {4}/gm, '');
\r
187 text: !this.options.pedantic
\r
188 ? cap.replace(/\n+$/, '')
\r
195 if (cap = this.rules.fences.exec(src)) {
\r
196 src = src.substring(cap[0].length);
\r
206 if (cap = this.rules.heading.exec(src)) {
\r
207 src = src.substring(cap[0].length);
\r
210 depth: cap[1].length,
\r
216 // table no leading pipe (gfm)
\r
217 if (top && (cap = this.rules.nptable.exec(src))) {
\r
218 src = src.substring(cap[0].length);
\r
222 header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */),
\r
223 align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
\r
224 cells: cap[3].replace(/\n$/, '').split('\n')
\r
227 for (i = 0; i < item.align.length; i++) {
\r
228 if (/^ *-+: *$/.test(item.align[i])) {
\r
229 item.align[i] = 'right';
\r
230 } else if (/^ *:-+: *$/.test(item.align[i])) {
\r
231 item.align[i] = 'center';
\r
232 } else if (/^ *:-+ *$/.test(item.align[i])) {
\r
233 item.align[i] = 'left';
\r
235 item.align[i] = null;
\r
239 for (i = 0; i < item.cells.length; i++) {
\r
240 item.cells[i] = item.cells[i].split(/ *\| */);
\r
243 this.tokens.push(item);
\r
249 if (cap = this.rules.lheading.exec(src)) {
\r
250 src = src.substring(cap[0].length);
\r
253 depth: cap[2] === '=' ? 1 : 2,
\r
260 if (cap = this.rules.hr.exec(src)) {
\r
261 src = src.substring(cap[0].length);
\r
269 if (cap = this.rules.blockquote.exec(src)) {
\r
270 src = src.substring(cap[0].length);
\r
273 type: 'blockquote_start'
\r
276 cap = cap[0].replace(/^ *> ?/gm, '');
\r
278 // Pass `top` to keep the current
\r
279 // "toplevel" state. This is exactly
\r
280 // how markdown.pl works.
\r
281 this.token(cap, top, true);
\r
284 type: 'blockquote_end'
\r
291 if (cap = this.rules.list.exec(src)) {
\r
292 src = src.substring(cap[0].length);
\r
296 type: 'list_start',
\r
297 ordered: bull.length > 1
\r
300 // Get each top-level item.
\r
301 cap = cap[0].match(this.rules.item);
\r
307 for (; i < l; i++) {
\r
310 // Remove the list item's bullet
\r
311 // so it is seen as the next token.
\r
312 space = item.length;
\r
313 item = item.replace(/^ *([*+-]|\d+\.) +/, '');
\r
315 // Outdent whatever the
\r
316 // list item contains. Hacky.
\r
317 if (~item.indexOf('\n ')) {
\r
318 space -= item.length;
\r
319 item = !this.options.pedantic
\r
320 ? item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '')
\r
321 : item.replace(/^ {1,4}/gm, '');
\r
324 // Determine whether the next list item belongs here.
\r
325 // Backpedal if it does not belong in this list.
\r
326 if (this.options.smartLists && i !== l - 1) {
\r
327 b = block.bullet.exec(cap[i + 1])[0];
\r
328 if (bull !== b && !(bull.length > 1 && b.length > 1)) {
\r
329 src = cap.slice(i + 1).join('\n') + src;
\r
334 // Determine whether item is loose or not.
\r
335 // Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/
\r
336 // for discount behavior.
\r
337 loose = next || /\n\n(?!\s*$)/.test(item);
\r
339 next = item.charAt(item.length - 1) === '\n';
\r
340 if (!loose) loose = next;
\r
345 ? 'loose_item_start'
\r
346 : 'list_item_start'
\r
350 this.token(item, false, bq);
\r
353 type: 'list_item_end'
\r
365 if (cap = this.rules.html.exec(src)) {
\r
366 src = src.substring(cap[0].length);
\r
368 type: this.options.sanitize
\r
371 pre: cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style',
\r
378 if ((!bq && top) && (cap = this.rules.def.exec(src))) {
\r
379 src = src.substring(cap[0].length);
\r
380 this.tokens.links[cap[1].toLowerCase()] = {
\r
388 if (top && (cap = this.rules.table.exec(src))) {
\r
389 src = src.substring(cap[0].length);
\r
393 header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */),
\r
394 align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
\r
395 cells: cap[3].replace(/(?: *\| *)?\n$/, '').split('\n')
\r
398 for (i = 0; i < item.align.length; i++) {
\r
399 if (/^ *-+: *$/.test(item.align[i])) {
\r
400 item.align[i] = 'right';
\r
401 } else if (/^ *:-+: *$/.test(item.align[i])) {
\r
402 item.align[i] = 'center';
\r
403 } else if (/^ *:-+ *$/.test(item.align[i])) {
\r
404 item.align[i] = 'left';
\r
406 item.align[i] = null;
\r
410 for (i = 0; i < item.cells.length; i++) {
\r
411 item.cells[i] = item.cells[i]
\r
412 .replace(/^ *\| *| *\| *$/g, '')
\r
416 this.tokens.push(item);
\r
421 // top-level paragraph
\r
422 if (top && (cap = this.rules.paragraph.exec(src))) {
\r
423 src = src.substring(cap[0].length);
\r
426 text: cap[1].charAt(cap[1].length - 1) === '\n'
\r
427 ? cap[1].slice(0, -1)
\r
434 if (cap = this.rules.text.exec(src)) {
\r
435 // Top-level should never reach here.
\r
436 src = src.substring(cap[0].length);
\r
446 Error('Infinite loop on byte: ' + src.charCodeAt(0));
\r
450 return this.tokens;
\r
454 * Inline-Level Grammar
\r
458 escape: /^\\([\\`*{}\[\]()#+\-.!_>])/,
\r
459 autolink: /^<([^ >]+(@|:\/)[^ >]+)>/,
\r
461 tag: /^<!--[\s\S]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^'">])*?>/,
\r
462 link: /^!?\[(inside)\]\(href\)/,
\r
463 reflink: /^!?\[(inside)\]\s*\[([^\]]*)\]/,
\r
464 nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
\r
465 strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,
\r
466 em: /^\b_((?:__|[\s\S])+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,
\r
467 code: /^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/,
\r
468 br: /^ {2,}\n(?!\s*$)/,
\r
470 text: /^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)/
\r
473 inline._inside = /(?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/;
\r
474 inline._href = /\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/;
\r
476 inline.link = replace(inline.link)
\r
477 ('inside', inline._inside)
\r
478 ('href', inline._href)
\r
481 inline.reflink = replace(inline.reflink)
\r
482 ('inside', inline._inside)
\r
486 * Normal Inline Grammar
\r
489 inline.normal = merge({}, inline);
\r
492 * Pedantic Inline Grammar
\r
495 inline.pedantic = merge({}, inline.normal, {
\r
496 strong: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
\r
497 em: /^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/
\r
501 * GFM Inline Grammar
\r
504 inline.gfm = merge({}, inline.normal, {
\r
505 escape: replace(inline.escape)('])', '~|])')(),
\r
506 url: /^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/,
\r
507 del: /^~~(?=\S)([\s\S]*?\S)~~/,
\r
508 text: replace(inline.text)
\r
510 ('|', '|https?://|')
\r
515 * GFM + Line Breaks Inline Grammar
\r
518 inline.breaks = merge({}, inline.gfm, {
\r
519 br: replace(inline.br)('{2,}', '*')(),
\r
520 text: replace(inline.gfm.text)('{2,}', '*')()
\r
524 * Inline Lexer & Compiler
\r
527 function InlineLexer(links, options) {
\r
528 this.options = options || marked.defaults;
\r
529 this.links = links;
\r
530 this.rules = inline.normal;
\r
531 this.renderer = this.options.renderer || new Renderer;
\r
532 this.renderer.options = this.options;
\r
536 Error('Tokens array requires a `links` property.');
\r
539 if (this.options.gfm) {
\r
540 if (this.options.breaks) {
\r
541 this.rules = inline.breaks;
\r
543 this.rules = inline.gfm;
\r
545 } else if (this.options.pedantic) {
\r
546 this.rules = inline.pedantic;
\r
551 * Expose Inline Rules
\r
554 InlineLexer.rules = inline;
\r
557 * Static Lexing/Compiling Method
\r
560 InlineLexer.output = function(src, links, options) {
\r
561 var inline = new InlineLexer(links, options);
\r
562 return inline.output(src);
\r
569 InlineLexer.prototype.output = function(src) {
\r
578 if (cap = this.rules.escape.exec(src)) {
\r
579 src = src.substring(cap[0].length);
\r
585 if (cap = this.rules.autolink.exec(src)) {
\r
586 src = src.substring(cap[0].length);
\r
587 if (cap[2] === '@') {
\r
588 text = cap[1].charAt(6) === ':'
\r
589 ? this.mangle(cap[1].substring(7))
\r
590 : this.mangle(cap[1]);
\r
591 href = this.mangle('mailto:') + text;
\r
593 text = escape(cap[1]);
\r
596 out += this.renderer.link(href, null, text);
\r
601 if (!this.inLink && (cap = this.rules.url.exec(src))) {
\r
602 src = src.substring(cap[0].length);
\r
603 text = escape(cap[1]);
\r
605 out += this.renderer.link(href, null, text);
\r
610 if (cap = this.rules.tag.exec(src)) {
\r
611 if (!this.inLink && /^<a /i.test(cap[0])) {
\r
612 this.inLink = true;
\r
613 } else if (this.inLink && /^<\/a>/i.test(cap[0])) {
\r
614 this.inLink = false;
\r
616 src = src.substring(cap[0].length);
\r
617 out += this.options.sanitize
\r
624 if (cap = this.rules.link.exec(src)) {
\r
625 src = src.substring(cap[0].length);
\r
626 this.inLink = true;
\r
627 out += this.outputLink(cap, {
\r
631 this.inLink = false;
\r
636 if ((cap = this.rules.reflink.exec(src))
\r
637 || (cap = this.rules.nolink.exec(src))) {
\r
638 src = src.substring(cap[0].length);
\r
639 link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
\r
640 link = this.links[link.toLowerCase()];
\r
641 if (!link || !link.href) {
\r
642 out += cap[0].charAt(0);
\r
643 src = cap[0].substring(1) + src;
\r
646 this.inLink = true;
\r
647 out += this.outputLink(cap, link);
\r
648 this.inLink = false;
\r
653 if (cap = this.rules.strong.exec(src)) {
\r
654 src = src.substring(cap[0].length);
\r
655 out += this.renderer.strong(this.output(cap[2] || cap[1]));
\r
660 if (cap = this.rules.em.exec(src)) {
\r
661 src = src.substring(cap[0].length);
\r
662 out += this.renderer.em(this.output(cap[2] || cap[1]));
\r
667 if (cap = this.rules.code.exec(src)) {
\r
668 src = src.substring(cap[0].length);
\r
669 out += this.renderer.codespan(escape(cap[2], true));
\r
674 if (cap = this.rules.br.exec(src)) {
\r
675 src = src.substring(cap[0].length);
\r
676 out += this.renderer.br();
\r
681 if (cap = this.rules.del.exec(src)) {
\r
682 src = src.substring(cap[0].length);
\r
683 out += this.renderer.del(this.output(cap[1]));
\r
688 if (cap = this.rules.text.exec(src)) {
\r
689 src = src.substring(cap[0].length);
\r
690 out += escape(this.smartypants(cap[0]));
\r
696 Error('Infinite loop on byte: ' + src.charCodeAt(0));
\r
707 InlineLexer.prototype.outputLink = function(cap, link) {
\r
708 var href = escape(link.href)
\r
709 , title = link.title ? escape(link.title) : null;
\r
711 return cap[0].charAt(0) !== '!'
\r
712 ? this.renderer.link(href, title, this.output(cap[1]))
\r
713 : this.renderer.image(href, title, escape(cap[1]));
\r
717 * Smartypants Transformations
\r
720 InlineLexer.prototype.smartypants = function(text) {
\r
721 if (!this.options.smartypants) return text;
\r
724 .replace(/--/g, '\u2014')
\r
726 .replace(/(^|[-\u2014/(\[{"\s])'/g, '$1\u2018')
\r
727 // closing singles & apostrophes
\r
728 .replace(/'/g, '\u2019')
\r
730 .replace(/(^|[-\u2014/(\[{\u2018\s])"/g, '$1\u201c')
\r
732 .replace(/"/g, '\u201d')
\r
734 .replace(/\.{3}/g, '\u2026');
\r
741 InlineLexer.prototype.mangle = function(text) {
\r
747 for (; i < l; i++) {
\r
748 ch = text.charCodeAt(i);
\r
749 if (Math.random() > 0.5) {
\r
750 ch = 'x' + ch.toString(16);
\r
752 out += '&#' + ch + ';';
\r
762 function Renderer(options) {
\r
763 this.options = options || {};
\r
766 Renderer.prototype.code = function(code, lang, escaped) {
\r
767 if (this.options.highlight) {
\r
768 var out = this.options.highlight(code, lang);
\r
769 if (out != null && out !== code) {
\r
776 return '<pre><code>'
\r
777 + (escaped ? code : escape(code, true))
\r
778 + '\n</code></pre>';
\r
781 return '<pre><code class="'
\r
782 + this.options.langPrefix
\r
783 + escape(lang, true)
\r
785 + (escaped ? code : escape(code, true))
\r
786 + '\n</code></pre>\n';
\r
789 Renderer.prototype.blockquote = function(quote) {
\r
790 return '<blockquote>\n' + quote + '</blockquote>\n';
\r
793 Renderer.prototype.html = function(html) {
\r
797 Renderer.prototype.heading = function(text, level, raw) {
\r
801 + this.options.headerPrefix
\r
802 + raw.toLowerCase().replace(/[^\w]+/g, '-')
\r
810 Renderer.prototype.hr = function() {
\r
811 return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
\r
814 Renderer.prototype.list = function(body, ordered) {
\r
815 var type = ordered ? 'ol' : 'ul';
\r
816 return '<' + type + '>\n' + body + '</' + type + '>\n';
\r
819 Renderer.prototype.listitem = function(text) {
\r
820 return '<li>' + text + '</li>\n';
\r
823 Renderer.prototype.paragraph = function(text) {
\r
824 return '<p>' + text + '</p>\n';
\r
827 Renderer.prototype.table = function(header, body) {
\r
838 Renderer.prototype.tablerow = function(content) {
\r
839 return '<tr>\n' + content + '</tr>\n';
\r
842 Renderer.prototype.tablecell = function(content, flags) {
\r
843 var type = flags.header ? 'th' : 'td';
\r
844 var tag = flags.align
\r
845 ? '<' + type + ' style="text-align:' + flags.align + '">'
\r
846 : '<' + type + '>';
\r
847 return tag + content + '</' + type + '>\n';
\r
850 // span level renderer
\r
851 Renderer.prototype.strong = function(text) {
\r
852 return '<strong>' + text + '</strong>';
\r
855 Renderer.prototype.em = function(text) {
\r
856 return '<em>' + text + '</em>';
\r
859 Renderer.prototype.codespan = function(text) {
\r
860 return '<code>' + text + '</code>';
\r
863 Renderer.prototype.br = function() {
\r
864 return this.options.xhtml ? '<br/>' : '<br>';
\r
867 Renderer.prototype.del = function(text) {
\r
868 return '<del>' + text + '</del>';
\r
871 Renderer.prototype.link = function(href, title, text) {
\r
872 if (this.options.sanitize) {
\r
874 var prot = decodeURIComponent(unescape(href))
\r
875 .replace(/[^\w:]/g, '')
\r
880 if (prot.indexOf('javascript:') === 0) {
\r
884 var out = '<a href="' + href + '"';
\r
886 out += ' title="' + title + '"';
\r
888 out += '>' + text + '</a>';
\r
892 Renderer.prototype.image = function(href, title, text) {
\r
893 var out = '<img src="' + href + '" alt="' + text + '"';
\r
895 out += ' title="' + title + '"';
\r
897 out += this.options.xhtml ? '/>' : '>';
\r
902 * Parsing & Compiling
\r
905 function Parser(options) {
\r
908 this.options = options || marked.defaults;
\r
909 this.options.renderer = this.options.renderer || new Renderer;
\r
910 this.renderer = this.options.renderer;
\r
911 this.renderer.options = this.options;
\r
915 * Static Parse Method
\r
918 Parser.parse = function(src, options, renderer) {
\r
919 var parser = new Parser(options, renderer);
\r
920 return parser.parse(src);
\r
927 Parser.prototype.parse = function(src) {
\r
928 this.inline = new InlineLexer(src.links, this.options, this.renderer);
\r
929 this.tokens = src.reverse();
\r
932 while (this.next()) {
\r
943 Parser.prototype.next = function() {
\r
944 return this.token = this.tokens.pop();
\r
948 * Preview Next Token
\r
951 Parser.prototype.peek = function() {
\r
952 return this.tokens[this.tokens.length - 1] || 0;
\r
956 * Parse Text Tokens
\r
959 Parser.prototype.parseText = function() {
\r
960 var body = this.token.text;
\r
962 while (this.peek().type === 'text') {
\r
963 body += '\n' + this.next().text;
\r
966 return this.inline.output(body);
\r
970 * Parse Current Token
\r
973 Parser.prototype.tok = function() {
\r
974 switch (this.token.type) {
\r
979 return this.renderer.hr();
\r
982 return this.renderer.heading(
\r
983 this.inline.output(this.token.text),
\r
988 return this.renderer.code(this.token.text,
\r
990 this.token.escaped);
\r
1003 for (i = 0; i < this.token.header.length; i++) {
\r
1004 flags = { header: true, align: this.token.align[i] };
\r
1005 cell += this.renderer.tablecell(
\r
1006 this.inline.output(this.token.header[i]),
\r
1007 { header: true, align: this.token.align[i] }
\r
1010 header += this.renderer.tablerow(cell);
\r
1012 for (i = 0; i < this.token.cells.length; i++) {
\r
1013 row = this.token.cells[i];
\r
1016 for (j = 0; j < row.length; j++) {
\r
1017 cell += this.renderer.tablecell(
\r
1018 this.inline.output(row[j]),
\r
1019 { header: false, align: this.token.align[j] }
\r
1023 body += this.renderer.tablerow(cell);
\r
1025 return this.renderer.table(header, body);
\r
1027 case 'blockquote_start': {
\r
1030 while (this.next().type !== 'blockquote_end') {
\r
1031 body += this.tok();
\r
1034 return this.renderer.blockquote(body);
\r
1036 case 'list_start': {
\r
1038 , ordered = this.token.ordered;
\r
1040 while (this.next().type !== 'list_end') {
\r
1041 body += this.tok();
\r
1044 return this.renderer.list(body, ordered);
\r
1046 case 'list_item_start': {
\r
1049 while (this.next().type !== 'list_item_end') {
\r
1050 body += this.token.type === 'text'
\r
1051 ? this.parseText()
\r
1055 return this.renderer.listitem(body);
\r
1057 case 'loose_item_start': {
\r
1060 while (this.next().type !== 'list_item_end') {
\r
1061 body += this.tok();
\r
1064 return this.renderer.listitem(body);
\r
1067 var html = !this.token.pre && !this.options.pedantic
\r
1068 ? this.inline.output(this.token.text)
\r
1069 : this.token.text;
\r
1070 return this.renderer.html(html);
\r
1072 case 'paragraph': {
\r
1073 return this.renderer.paragraph(this.inline.output(this.token.text));
\r
1076 return this.renderer.paragraph(this.parseText());
\r
1085 function escape(html, encode) {
\r
1087 .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&')
\r
1088 .replace(/</g, '<')
\r
1089 .replace(/>/g, '>')
\r
1090 .replace(/"/g, '"')
\r
1091 .replace(/'/g, ''');
\r
1094 function unescape(html) {
\r
1095 return html.replace(/&([#\w]+);/g, function(_, n) {
\r
1096 n = n.toLowerCase();
\r
1097 if (n === 'colon') return ':';
\r
1098 if (n.charAt(0) === '#') {
\r
1099 return n.charAt(1) === 'x'
\r
1100 ? String.fromCharCode(parseInt(n.substring(2), 16))
\r
1101 : String.fromCharCode(+n.substring(1));
\r
1107 function replace(regex, opt) {
\r
1108 regex = regex.source;
\r
1110 return function self(name, val) {
\r
1111 if (!name) return new RegExp(regex, opt);
\r
1112 val = val.source || val;
\r
1113 val = val.replace(/(^|[^\[])\^/g, '$1');
\r
1114 regex = regex.replace(name, val);
\r
1119 function noop() {}
\r
1122 function merge(obj) {
\r
1127 for (; i < arguments.length; i++) {
\r
1128 target = arguments[i];
\r
1129 for (key in target) {
\r
1130 if (Object.prototype.hasOwnProperty.call(target, key)) {
\r
1131 obj[key] = target[key];
\r
1144 function marked(src, opt, callback) {
\r
1145 if (callback || typeof opt === 'function') {
\r
1151 opt = merge({}, marked.defaults, opt || {});
\r
1153 var highlight = opt.highlight
\r
1159 tokens = Lexer.lex(src, opt)
\r
1161 return callback(e);
\r
1164 pending = tokens.length;
\r
1166 var done = function(err) {
\r
1168 opt.highlight = highlight;
\r
1169 return callback(err);
\r
1175 out = Parser.parse(tokens, opt);
\r
1180 opt.highlight = highlight;
\r
1184 : callback(null, out);
\r
1187 if (!highlight || highlight.length < 3) {
\r
1191 delete opt.highlight;
\r
1193 if (!pending) return done();
\r
1195 for (; i < tokens.length; i++) {
\r
1196 (function(token) {
\r
1197 if (token.type !== 'code') {
\r
1198 return --pending || done();
\r
1200 return highlight(token.text, token.lang, function(err, code) {
\r
1201 if (err) return done(err);
\r
1202 if (code == null || code === token.text) {
\r
1203 return --pending || done();
\r
1205 token.text = code;
\r
1206 token.escaped = true;
\r
1207 --pending || done();
\r
1215 if (opt) opt = merge({}, marked.defaults, opt);
\r
1216 return Parser.parse(Lexer.lex(src, opt), opt);
\r
1218 e.message += '\nPlease report this to https://github.com/chjj/marked.';
\r
1219 if ((opt || marked.defaults).silent) {
\r
1220 return '<p>An error occured:</p><pre>'
\r
1221 + escape(e.message + '', true)
\r
1233 marked.setOptions = function(opt) {
\r
1234 merge(marked.defaults, opt);
\r
1238 marked.defaults = {
\r
1244 smartLists: false,
\r
1247 langPrefix: 'lang-',
\r
1248 smartypants: false,
\r
1250 renderer: new Renderer,
\r
1258 marked.Parser = Parser;
\r
1259 marked.parser = Parser.parse;
\r
1261 marked.Renderer = Renderer;
\r
1263 marked.Lexer = Lexer;
\r
1264 marked.lexer = Lexer.lex;
\r
1266 marked.InlineLexer = InlineLexer;
\r
1267 marked.inlineLexer = InlineLexer.output;
\r
1269 marked.parse = marked;
\r
1271 if (typeof module !== 'undefined' && typeof exports === 'object') {
\r
1272 module.exports = marked;
\r
1273 } else if (typeof define === 'function' && define.amd) {
\r
1274 define(function() { return marked; });
\r
1276 this.marked = marked;
\r
1279 }).call(function() {
\r
1280 return this || (typeof window !== 'undefined' ? window : global);
\r