Issue-id: OCS-9
[msb/apigateway.git] / msb-core / apiroute / apiroute-service / src / main / resources / api-doc / lib / marked.js
1 /*
2  * Copyright 2016 2015-2016 ZTE, Inc. and others. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  *     Author: Zhaoxing Meng
17  *     email: meng.zhaoxing1@zte.com.cn
18  */
19 ;(function() {
20
21 /**
22  * Block-Level Grammar
23  */
24
25 var block = {
26   newline: /^\n+/,
27   code: /^( {4}[^\n]+\n*)+/,
28   fences: noop,
29   hr: /^( *[-*_]){3,} *(?:\n+|$)/,
30   heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,
31   nptable: noop,
32   lheading: /^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,
33   blockquote: /^( *>[^\n]+(\n(?!def)[^\n]+)*\n*)+/,
34   list: /^( *)(bull) [\s\S]+?(?:hr|def|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
35   html: /^ *(?:comment *(?:\n|\s*$)|closed *(?:\n{2,}|\s*$)|closing *(?:\n{2,}|\s*$))/,
36   def: /^ *\[([^\]]+)\]: *<?([^\s>]+)>?(?: +["(]([^\n]+)[")])? *(?:\n+|$)/,
37   table: noop,
38   paragraph: /^((?:[^\n]+\n?(?!hr|heading|lheading|blockquote|tag|def))+)\n*/,
39   text: /^[^\n]+/
40 };
41
42 block.bullet = /(?:[*+-]|\d+\.)/;
43 block.item = /^( *)(bull) [^\n]*(?:\n(?!\1bull )[^\n]*)*/;
44 block.item = replace(block.item, 'gm')
45   (/bull/g, block.bullet)
46   ();
47
48 block.list = replace(block.list)
49   (/bull/g, block.bullet)
50   ('hr', '\\n+(?=\\1?(?:[-*_] *){3,}(?:\\n+|$))')
51   ('def', '\\n+(?=' + block.def.source + ')')
52   ();
53
54 block.blockquote = replace(block.blockquote)
55   ('def', block.def)
56   ();
57
58 block._tag = '(?!(?:'
59   + 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code'
60   + '|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo'
61   + '|span|br|wbr|ins|del|img)\\b)\\w+(?!:/|[^\\w\\s@]*@)\\b';
62
63 block.html = replace(block.html)
64   ('comment', /<!--[\s\S]*?-->/)
65   ('closed', /<(tag)[\s\S]+?<\/\1>/)
66   ('closing', /<tag(?:"[^"]*"|'[^']*'|[^'">])*?>/)
67   (/tag/g, block._tag)
68   ();
69
70 block.paragraph = replace(block.paragraph)
71   ('hr', block.hr)
72   ('heading', block.heading)
73   ('lheading', block.lheading)
74   ('blockquote', block.blockquote)
75   ('tag', '<' + block._tag)
76   ('def', block.def)
77   ();
78
79 /**
80  * Normal Block Grammar
81  */
82
83 block.normal = merge({}, block);
84
85 /**
86  * GFM Block Grammar
87  */
88
89 block.gfm = merge({}, block.normal, {
90   fences: /^ *(`{3,}|~{3,}) *(\S+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/,
91   paragraph: /^/
92 });
93
94 block.gfm.paragraph = replace(block.paragraph)
95   ('(?!', '(?!'
96     + block.gfm.fences.source.replace('\\1', '\\2') + '|'
97     + block.list.source.replace('\\1', '\\3') + '|')
98   ();
99
100 /**
101  * GFM + Tables Block Grammar
102  */
103
104 block.tables = merge({}, block.gfm, {
105   nptable: /^ *(\S.*\|.*)\n *([-:]+ *\|[-| :]*)\n((?:.*\|.*(?:\n|$))*)\n*/,
106   table: /^ *\|(.+)\n *\|( *[-:]+[-| :]*)\n((?: *\|.*(?:\n|$))*)\n*/
107 });
108
109 /**
110  * Block Lexer
111  */
112
113 function Lexer(options) {
114   this.tokens = [];
115   this.tokens.links = {};
116   this.options = options || marked.defaults;
117   this.rules = block.normal;
118
119   if (this.options.gfm) {
120     if (this.options.tables) {
121       this.rules = block.tables;
122     } else {
123       this.rules = block.gfm;
124     }
125   }
126 }
127
128 /**
129  * Expose Block Rules
130  */
131
132 Lexer.rules = block;
133
134 /**
135  * Static Lex Method
136  */
137
138 Lexer.lex = function(src, options) {
139   var lexer = new Lexer(options);
140   return lexer.lex(src);
141 };
142
143 /**
144  * Preprocessing
145  */
146
147 Lexer.prototype.lex = function(src) {
148   src = src
149     .replace(/\r\n|\r/g, '\n')
150     .replace(/\t/g, '    ')
151     .replace(/\u00a0/g, ' ')
152     .replace(/\u2424/g, '\n');
153
154   return this.token(src, true);
155 };
156
157 /**
158  * Lexing
159  */
160
161 Lexer.prototype.token = function(src, top, bq) {
162   var src = src.replace(/^ +$/gm, '')
163     , next
164     , loose
165     , cap
166     , bull
167     , b
168     , item
169     , space
170     , i
171     , l;
172
173   while (src) {
174     // newline
175     if (cap = this.rules.newline.exec(src)) {
176       src = src.substring(cap[0].length);
177       if (cap[0].length > 1) {
178         this.tokens.push({
179           type: 'space'
180         });
181       }
182     }
183
184     // code
185     if (cap = this.rules.code.exec(src)) {
186       src = src.substring(cap[0].length);
187       cap = cap[0].replace(/^ {4}/gm, '');
188       this.tokens.push({
189         type: 'code',
190         text: !this.options.pedantic
191           ? cap.replace(/\n+$/, '')
192           : cap
193       });
194       continue;
195     }
196
197     // fences (gfm)
198     if (cap = this.rules.fences.exec(src)) {
199       src = src.substring(cap[0].length);
200       this.tokens.push({
201         type: 'code',
202         lang: cap[2],
203         text: cap[3]
204       });
205       continue;
206     }
207
208     // heading
209     if (cap = this.rules.heading.exec(src)) {
210       src = src.substring(cap[0].length);
211       this.tokens.push({
212         type: 'heading',
213         depth: cap[1].length,
214         text: cap[2]
215       });
216       continue;
217     }
218
219     // table no leading pipe (gfm)
220     if (top && (cap = this.rules.nptable.exec(src))) {
221       src = src.substring(cap[0].length);
222
223       item = {
224         type: 'table',
225         header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */),
226         align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
227         cells: cap[3].replace(/\n$/, '').split('\n')
228       };
229
230       for (i = 0; i < item.align.length; i++) {
231         if (/^ *-+: *$/.test(item.align[i])) {
232           item.align[i] = 'right';
233         } else if (/^ *:-+: *$/.test(item.align[i])) {
234           item.align[i] = 'center';
235         } else if (/^ *:-+ *$/.test(item.align[i])) {
236           item.align[i] = 'left';
237         } else {
238           item.align[i] = null;
239         }
240       }
241
242       for (i = 0; i < item.cells.length; i++) {
243         item.cells[i] = item.cells[i].split(/ *\| */);
244       }
245
246       this.tokens.push(item);
247
248       continue;
249     }
250
251     // lheading
252     if (cap = this.rules.lheading.exec(src)) {
253       src = src.substring(cap[0].length);
254       this.tokens.push({
255         type: 'heading',
256         depth: cap[2] === '=' ? 1 : 2,
257         text: cap[1]
258       });
259       continue;
260     }
261
262     // hr
263     if (cap = this.rules.hr.exec(src)) {
264       src = src.substring(cap[0].length);
265       this.tokens.push({
266         type: 'hr'
267       });
268       continue;
269     }
270
271     // blockquote
272     if (cap = this.rules.blockquote.exec(src)) {
273       src = src.substring(cap[0].length);
274
275       this.tokens.push({
276         type: 'blockquote_start'
277       });
278
279       cap = cap[0].replace(/^ *> ?/gm, '');
280
281       // Pass `top` to keep the current
282       // "toplevel" state. This is exactly
283       // how markdown.pl works.
284       this.token(cap, top, true);
285
286       this.tokens.push({
287         type: 'blockquote_end'
288       });
289
290       continue;
291     }
292
293     // list
294     if (cap = this.rules.list.exec(src)) {
295       src = src.substring(cap[0].length);
296       bull = cap[2];
297
298       this.tokens.push({
299         type: 'list_start',
300         ordered: bull.length > 1
301       });
302
303       // Get each top-level item.
304       cap = cap[0].match(this.rules.item);
305
306       next = false;
307       l = cap.length;
308       i = 0;
309
310       for (; i < l; i++) {
311         item = cap[i];
312
313         // Remove the list item's bullet
314         // so it is seen as the next token.
315         space = item.length;
316         item = item.replace(/^ *([*+-]|\d+\.) +/, '');
317
318         // Outdent whatever the
319         // list item contains. Hacky.
320         if (~item.indexOf('\n ')) {
321           space -= item.length;
322           item = !this.options.pedantic
323             ? item.replace(new RegExp('^ {1,' + space + '}', 'gm'), '')
324             : item.replace(/^ {1,4}/gm, '');
325         }
326
327         // Determine whether the next list item belongs here.
328         // Backpedal if it does not belong in this list.
329         if (this.options.smartLists && i !== l - 1) {
330           b = block.bullet.exec(cap[i + 1])[0];
331           if (bull !== b && !(bull.length > 1 && b.length > 1)) {
332             src = cap.slice(i + 1).join('\n') + src;
333             i = l - 1;
334           }
335         }
336
337         // Determine whether item is loose or not.
338         // Use: /(^|\n)(?! )[^\n]+\n\n(?!\s*$)/
339         // for discount behavior.
340         loose = next || /\n\n(?!\s*$)/.test(item);
341         if (i !== l - 1) {
342           next = item.charAt(item.length - 1) === '\n';
343           if (!loose) loose = next;
344         }
345
346         this.tokens.push({
347           type: loose
348             ? 'loose_item_start'
349             : 'list_item_start'
350         });
351
352         // Recurse.
353         this.token(item, false, bq);
354
355         this.tokens.push({
356           type: 'list_item_end'
357         });
358       }
359
360       this.tokens.push({
361         type: 'list_end'
362       });
363
364       continue;
365     }
366
367     // html
368     if (cap = this.rules.html.exec(src)) {
369       src = src.substring(cap[0].length);
370       this.tokens.push({
371         type: this.options.sanitize
372           ? 'paragraph'
373           : 'html',
374         pre: cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style',
375         text: cap[0]
376       });
377       continue;
378     }
379
380     // def
381     if ((!bq && top) && (cap = this.rules.def.exec(src))) {
382       src = src.substring(cap[0].length);
383       this.tokens.links[cap[1].toLowerCase()] = {
384         href: cap[2],
385         title: cap[3]
386       };
387       continue;
388     }
389
390     // table (gfm)
391     if (top && (cap = this.rules.table.exec(src))) {
392       src = src.substring(cap[0].length);
393
394       item = {
395         type: 'table',
396         header: cap[1].replace(/^ *| *\| *$/g, '').split(/ *\| */),
397         align: cap[2].replace(/^ *|\| *$/g, '').split(/ *\| */),
398         cells: cap[3].replace(/(?: *\| *)?\n$/, '').split('\n')
399       };
400
401       for (i = 0; i < item.align.length; i++) {
402         if (/^ *-+: *$/.test(item.align[i])) {
403           item.align[i] = 'right';
404         } else if (/^ *:-+: *$/.test(item.align[i])) {
405           item.align[i] = 'center';
406         } else if (/^ *:-+ *$/.test(item.align[i])) {
407           item.align[i] = 'left';
408         } else {
409           item.align[i] = null;
410         }
411       }
412
413       for (i = 0; i < item.cells.length; i++) {
414         item.cells[i] = item.cells[i]
415           .replace(/^ *\| *| *\| *$/g, '')
416           .split(/ *\| */);
417       }
418
419       this.tokens.push(item);
420
421       continue;
422     }
423
424     // top-level paragraph
425     if (top && (cap = this.rules.paragraph.exec(src))) {
426       src = src.substring(cap[0].length);
427       this.tokens.push({
428         type: 'paragraph',
429         text: cap[1].charAt(cap[1].length - 1) === '\n'
430           ? cap[1].slice(0, -1)
431           : cap[1]
432       });
433       continue;
434     }
435
436     // text
437     if (cap = this.rules.text.exec(src)) {
438       // Top-level should never reach here.
439       src = src.substring(cap[0].length);
440       this.tokens.push({
441         type: 'text',
442         text: cap[0]
443       });
444       continue;
445     }
446
447     if (src) {
448       throw new
449         Error('Infinite loop on byte: ' + src.charCodeAt(0));
450     }
451   }
452
453   return this.tokens;
454 };
455
456 /**
457  * Inline-Level Grammar
458  */
459
460 var inline = {
461   escape: /^\\([\\`*{}\[\]()#+\-.!_>])/,
462   autolink: /^<([^ >]+(@|:\/)[^ >]+)>/,
463   url: noop,
464   tag: /^<!--[\s\S]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^'">])*?>/,
465   link: /^!?\[(inside)\]\(href\)/,
466   reflink: /^!?\[(inside)\]\s*\[([^\]]*)\]/,
467   nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
468   strong: /^__([\s\S]+?)__(?!_)|^\*\*([\s\S]+?)\*\*(?!\*)/,
469   em: /^\b_((?:__|[\s\S])+?)_\b|^\*((?:\*\*|[\s\S])+?)\*(?!\*)/,
470   code: /^(`+)\s*([\s\S]*?[^`])\s*\1(?!`)/,
471   br: /^ {2,}\n(?!\s*$)/,
472   del: noop,
473   text: /^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)/
474 };
475
476 inline._inside = /(?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/;
477 inline._href = /\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/;
478
479 inline.link = replace(inline.link)
480   ('inside', inline._inside)
481   ('href', inline._href)
482   ();
483
484 inline.reflink = replace(inline.reflink)
485   ('inside', inline._inside)
486   ();
487
488 /**
489  * Normal Inline Grammar
490  */
491
492 inline.normal = merge({}, inline);
493
494 /**
495  * Pedantic Inline Grammar
496  */
497
498 inline.pedantic = merge({}, inline.normal, {
499   strong: /^__(?=\S)([\s\S]*?\S)__(?!_)|^\*\*(?=\S)([\s\S]*?\S)\*\*(?!\*)/,
500   em: /^_(?=\S)([\s\S]*?\S)_(?!_)|^\*(?=\S)([\s\S]*?\S)\*(?!\*)/
501 });
502
503 /**
504  * GFM Inline Grammar
505  */
506
507 inline.gfm = merge({}, inline.normal, {
508   escape: replace(inline.escape)('])', '~|])')(),
509   url: /^(https?:\/\/[^\s<]+[^<.,:;"')\]\s])/,
510   del: /^~~(?=\S)([\s\S]*?\S)~~/,
511   text: replace(inline.text)
512     (']|', '~]|')
513     ('|', '|https?://|')
514     ()
515 });
516
517 /**
518  * GFM + Line Breaks Inline Grammar
519  */
520
521 inline.breaks = merge({}, inline.gfm, {
522   br: replace(inline.br)('{2,}', '*')(),
523   text: replace(inline.gfm.text)('{2,}', '*')()
524 });
525
526 /**
527  * Inline Lexer & Compiler
528  */
529
530 function InlineLexer(links, options) {
531   this.options = options || marked.defaults;
532   this.links = links;
533   this.rules = inline.normal;
534   this.renderer = this.options.renderer || new Renderer;
535   this.renderer.options = this.options;
536
537   if (!this.links) {
538     throw new
539       Error('Tokens array requires a `links` property.');
540   }
541
542   if (this.options.gfm) {
543     if (this.options.breaks) {
544       this.rules = inline.breaks;
545     } else {
546       this.rules = inline.gfm;
547     }
548   } else if (this.options.pedantic) {
549     this.rules = inline.pedantic;
550   }
551 }
552
553 /**
554  * Expose Inline Rules
555  */
556
557 InlineLexer.rules = inline;
558
559 /**
560  * Static Lexing/Compiling Method
561  */
562
563 InlineLexer.output = function(src, links, options) {
564   var inline = new InlineLexer(links, options);
565   return inline.output(src);
566 };
567
568 /**
569  * Lexing/Compiling
570  */
571
572 InlineLexer.prototype.output = function(src) {
573   var out = ''
574     , link
575     , text
576     , href
577     , cap;
578
579   while (src) {
580     // escape
581     if (cap = this.rules.escape.exec(src)) {
582       src = src.substring(cap[0].length);
583       out += cap[1];
584       continue;
585     }
586
587     // autolink
588     if (cap = this.rules.autolink.exec(src)) {
589       src = src.substring(cap[0].length);
590       if (cap[2] === '@') {
591         text = cap[1].charAt(6) === ':'
592           ? this.mangle(cap[1].substring(7))
593           : this.mangle(cap[1]);
594         href = this.mangle('mailto:') + text;
595       } else {
596         text = escape(cap[1]);
597         href = text;
598       }
599       out += this.renderer.link(href, null, text);
600       continue;
601     }
602
603     // url (gfm)
604     if (!this.inLink && (cap = this.rules.url.exec(src))) {
605       src = src.substring(cap[0].length);
606       text = escape(cap[1]);
607       href = text;
608       out += this.renderer.link(href, null, text);
609       continue;
610     }
611
612     // tag
613     if (cap = this.rules.tag.exec(src)) {
614       if (!this.inLink && /^<a /i.test(cap[0])) {
615         this.inLink = true;
616       } else if (this.inLink && /^<\/a>/i.test(cap[0])) {
617         this.inLink = false;
618       }
619       src = src.substring(cap[0].length);
620       out += this.options.sanitize
621         ? escape(cap[0])
622         : cap[0];
623       continue;
624     }
625
626     // link
627     if (cap = this.rules.link.exec(src)) {
628       src = src.substring(cap[0].length);
629       this.inLink = true;
630       out += this.outputLink(cap, {
631         href: cap[2],
632         title: cap[3]
633       });
634       this.inLink = false;
635       continue;
636     }
637
638     // reflink, nolink
639     if ((cap = this.rules.reflink.exec(src))
640         || (cap = this.rules.nolink.exec(src))) {
641       src = src.substring(cap[0].length);
642       link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
643       link = this.links[link.toLowerCase()];
644       if (!link || !link.href) {
645         out += cap[0].charAt(0);
646         src = cap[0].substring(1) + src;
647         continue;
648       }
649       this.inLink = true;
650       out += this.outputLink(cap, link);
651       this.inLink = false;
652       continue;
653     }
654
655     // strong
656     if (cap = this.rules.strong.exec(src)) {
657       src = src.substring(cap[0].length);
658       out += this.renderer.strong(this.output(cap[2] || cap[1]));
659       continue;
660     }
661
662     // em
663     if (cap = this.rules.em.exec(src)) {
664       src = src.substring(cap[0].length);
665       out += this.renderer.em(this.output(cap[2] || cap[1]));
666       continue;
667     }
668
669     // code
670     if (cap = this.rules.code.exec(src)) {
671       src = src.substring(cap[0].length);
672       out += this.renderer.codespan(escape(cap[2], true));
673       continue;
674     }
675
676     // br
677     if (cap = this.rules.br.exec(src)) {
678       src = src.substring(cap[0].length);
679       out += this.renderer.br();
680       continue;
681     }
682
683     // del (gfm)
684     if (cap = this.rules.del.exec(src)) {
685       src = src.substring(cap[0].length);
686       out += this.renderer.del(this.output(cap[1]));
687       continue;
688     }
689
690     // text
691     if (cap = this.rules.text.exec(src)) {
692       src = src.substring(cap[0].length);
693       out += escape(this.smartypants(cap[0]));
694       continue;
695     }
696
697     if (src) {
698       throw new
699         Error('Infinite loop on byte: ' + src.charCodeAt(0));
700     }
701   }
702
703   return out;
704 };
705
706 /**
707  * Compile Link
708  */
709
710 InlineLexer.prototype.outputLink = function(cap, link) {
711   var href = escape(link.href)
712     , title = link.title ? escape(link.title) : null;
713
714   return cap[0].charAt(0) !== '!'
715     ? this.renderer.link(href, title, this.output(cap[1]))
716     : this.renderer.image(href, title, escape(cap[1]));
717 };
718
719 /**
720  * Smartypants Transformations
721  */
722
723 InlineLexer.prototype.smartypants = function(text) {
724   if (!this.options.smartypants) return text;
725   return text
726     // em-dashes
727     .replace(/--/g, '\u2014')
728     // opening singles
729     .replace(/(^|[-\u2014/(\[{"\s])'/g, '$1\u2018')
730     // closing singles & apostrophes
731     .replace(/'/g, '\u2019')
732     // opening doubles
733     .replace(/(^|[-\u2014/(\[{\u2018\s])"/g, '$1\u201c')
734     // closing doubles
735     .replace(/"/g, '\u201d')
736     // ellipses
737     .replace(/\.{3}/g, '\u2026');
738 };
739
740 /**
741  * Mangle Links
742  */
743
744 InlineLexer.prototype.mangle = function(text) {
745   var out = ''
746     , l = text.length
747     , i = 0
748     , ch;
749
750   for (; i < l; i++) {
751     ch = text.charCodeAt(i);
752     if (Math.random() > 0.5) {
753       ch = 'x' + ch.toString(16);
754     }
755     out += '&#' + ch + ';';
756   }
757
758   return out;
759 };
760
761 /**
762  * Renderer
763  */
764
765 function Renderer(options) {
766   this.options = options || {};
767 }
768
769 Renderer.prototype.code = function(code, lang, escaped) {
770   if (this.options.highlight) {
771     var out = this.options.highlight(code, lang);
772     if (out != null && out !== code) {
773       escaped = true;
774       code = out;
775     }
776   }
777
778   if (!lang) {
779     return '<pre><code>'
780       + (escaped ? code : escape(code, true))
781       + '\n</code></pre>';
782   }
783
784   return '<pre><code class="'
785     + this.options.langPrefix
786     + escape(lang, true)
787     + '">'
788     + (escaped ? code : escape(code, true))
789     + '\n</code></pre>\n';
790 };
791
792 Renderer.prototype.blockquote = function(quote) {
793   return '<blockquote>\n' + quote + '</blockquote>\n';
794 };
795
796 Renderer.prototype.html = function(html) {
797   return html;
798 };
799
800 Renderer.prototype.heading = function(text, level, raw) {
801   return '<h'
802     + level
803     + ' id="'
804     + this.options.headerPrefix
805     + raw.toLowerCase().replace(/[^\w]+/g, '-')
806     + '">'
807     + text
808     + '</h'
809     + level
810     + '>\n';
811 };
812
813 Renderer.prototype.hr = function() {
814   return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
815 };
816
817 Renderer.prototype.list = function(body, ordered) {
818   var type = ordered ? 'ol' : 'ul';
819   return '<' + type + '>\n' + body + '</' + type + '>\n';
820 };
821
822 Renderer.prototype.listitem = function(text) {
823   return '<li>' + text + '</li>\n';
824 };
825
826 Renderer.prototype.paragraph = function(text) {
827   return '<p>' + text + '</p>\n';
828 };
829
830 Renderer.prototype.table = function(header, body) {
831   return '<table>\n'
832     + '<thead>\n'
833     + header
834     + '</thead>\n'
835     + '<tbody>\n'
836     + body
837     + '</tbody>\n'
838     + '</table>\n';
839 };
840
841 Renderer.prototype.tablerow = function(content) {
842   return '<tr>\n' + content + '</tr>\n';
843 };
844
845 Renderer.prototype.tablecell = function(content, flags) {
846   var type = flags.header ? 'th' : 'td';
847   var tag = flags.align
848     ? '<' + type + ' style="text-align:' + flags.align + '">'
849     : '<' + type + '>';
850   return tag + content + '</' + type + '>\n';
851 };
852
853 // span level renderer
854 Renderer.prototype.strong = function(text) {
855   return '<strong>' + text + '</strong>';
856 };
857
858 Renderer.prototype.em = function(text) {
859   return '<em>' + text + '</em>';
860 };
861
862 Renderer.prototype.codespan = function(text) {
863   return '<code>' + text + '</code>';
864 };
865
866 Renderer.prototype.br = function() {
867   return this.options.xhtml ? '<br/>' : '<br>';
868 };
869
870 Renderer.prototype.del = function(text) {
871   return '<del>' + text + '</del>';
872 };
873
874 Renderer.prototype.link = function(href, title, text) {
875   if (this.options.sanitize) {
876     try {
877       var prot = decodeURIComponent(unescape(href))
878         .replace(/[^\w:]/g, '')
879         .toLowerCase();
880     } catch (e) {
881       return '';
882     }
883     if (prot.indexOf('javascript:') === 0) {
884       return '';
885     }
886   }
887   var out = '<a href="' + href + '"';
888   if (title) {
889     out += ' title="' + title + '"';
890   }
891   out += '>' + text + '</a>';
892   return out;
893 };
894
895 Renderer.prototype.image = function(href, title, text) {
896   var out = '<img src="' + href + '" alt="' + text + '"';
897   if (title) {
898     out += ' title="' + title + '"';
899   }
900   out += this.options.xhtml ? '/>' : '>';
901   return out;
902 };
903
904 /**
905  * Parsing & Compiling
906  */
907
908 function Parser(options) {
909   this.tokens = [];
910   this.token = null;
911   this.options = options || marked.defaults;
912   this.options.renderer = this.options.renderer || new Renderer;
913   this.renderer = this.options.renderer;
914   this.renderer.options = this.options;
915 }
916
917 /**
918  * Static Parse Method
919  */
920
921 Parser.parse = function(src, options, renderer) {
922   var parser = new Parser(options, renderer);
923   return parser.parse(src);
924 };
925
926 /**
927  * Parse Loop
928  */
929
930 Parser.prototype.parse = function(src) {
931   this.inline = new InlineLexer(src.links, this.options, this.renderer);
932   this.tokens = src.reverse();
933
934   var out = '';
935   while (this.next()) {
936     out += this.tok();
937   }
938
939   return out;
940 };
941
942 /**
943  * Next Token
944  */
945
946 Parser.prototype.next = function() {
947   return this.token = this.tokens.pop();
948 };
949
950 /**
951  * Preview Next Token
952  */
953
954 Parser.prototype.peek = function() {
955   return this.tokens[this.tokens.length - 1] || 0;
956 };
957
958 /**
959  * Parse Text Tokens
960  */
961
962 Parser.prototype.parseText = function() {
963   var body = this.token.text;
964
965   while (this.peek().type === 'text') {
966     body += '\n' + this.next().text;
967   }
968
969   return this.inline.output(body);
970 };
971
972 /**
973  * Parse Current Token
974  */
975
976 Parser.prototype.tok = function() {
977   switch (this.token.type) {
978     case 'space': {
979       return '';
980     }
981     case 'hr': {
982       return this.renderer.hr();
983     }
984     case 'heading': {
985       return this.renderer.heading(
986         this.inline.output(this.token.text),
987         this.token.depth,
988         this.token.text);
989     }
990     case 'code': {
991       return this.renderer.code(this.token.text,
992         this.token.lang,
993         this.token.escaped);
994     }
995     case 'table': {
996       var header = ''
997         , body = ''
998         , i
999         , row
1000         , cell
1001         , flags
1002         , j;
1003
1004       // header
1005       cell = '';
1006       for (i = 0; i < this.token.header.length; i++) {
1007         flags = { header: true, align: this.token.align[i] };
1008         cell += this.renderer.tablecell(
1009           this.inline.output(this.token.header[i]),
1010           { header: true, align: this.token.align[i] }
1011         );
1012       }
1013       header += this.renderer.tablerow(cell);
1014
1015       for (i = 0; i < this.token.cells.length; i++) {
1016         row = this.token.cells[i];
1017
1018         cell = '';
1019         for (j = 0; j < row.length; j++) {
1020           cell += this.renderer.tablecell(
1021             this.inline.output(row[j]),
1022             { header: false, align: this.token.align[j] }
1023           );
1024         }
1025
1026         body += this.renderer.tablerow(cell);
1027       }
1028       return this.renderer.table(header, body);
1029     }
1030     case 'blockquote_start': {
1031       var body = '';
1032
1033       while (this.next().type !== 'blockquote_end') {
1034         body += this.tok();
1035       }
1036
1037       return this.renderer.blockquote(body);
1038     }
1039     case 'list_start': {
1040       var body = ''
1041         , ordered = this.token.ordered;
1042
1043       while (this.next().type !== 'list_end') {
1044         body += this.tok();
1045       }
1046
1047       return this.renderer.list(body, ordered);
1048     }
1049     case 'list_item_start': {
1050       var body = '';
1051
1052       while (this.next().type !== 'list_item_end') {
1053         body += this.token.type === 'text'
1054           ? this.parseText()
1055           : this.tok();
1056       }
1057
1058       return this.renderer.listitem(body);
1059     }
1060     case 'loose_item_start': {
1061       var body = '';
1062
1063       while (this.next().type !== 'list_item_end') {
1064         body += this.tok();
1065       }
1066
1067       return this.renderer.listitem(body);
1068     }
1069     case 'html': {
1070       var html = !this.token.pre && !this.options.pedantic
1071         ? this.inline.output(this.token.text)
1072         : this.token.text;
1073       return this.renderer.html(html);
1074     }
1075     case 'paragraph': {
1076       return this.renderer.paragraph(this.inline.output(this.token.text));
1077     }
1078     case 'text': {
1079       return this.renderer.paragraph(this.parseText());
1080     }
1081   }
1082 };
1083
1084 /**
1085  * Helpers
1086  */
1087
1088 function escape(html, encode) {
1089   return html
1090     .replace(!encode ? /&(?!#?\w+;)/g : /&/g, '&amp;')
1091     .replace(/</g, '&lt;')
1092     .replace(/>/g, '&gt;')
1093     .replace(/"/g, '&quot;')
1094     .replace(/'/g, '&#39;');
1095 }
1096
1097 function unescape(html) {
1098   return html.replace(/&([#\w]+);/g, function(_, n) {
1099     n = n.toLowerCase();
1100     if (n === 'colon') return ':';
1101     if (n.charAt(0) === '#') {
1102       return n.charAt(1) === 'x'
1103         ? String.fromCharCode(parseInt(n.substring(2), 16))
1104         : String.fromCharCode(+n.substring(1));
1105     }
1106     return '';
1107   });
1108 }
1109
1110 function replace(regex, opt) {
1111   regex = regex.source;
1112   opt = opt || '';
1113   return function self(name, val) {
1114     if (!name) return new RegExp(regex, opt);
1115     val = val.source || val;
1116     val = val.replace(/(^|[^\[])\^/g, '$1');
1117     regex = regex.replace(name, val);
1118     return self;
1119   };
1120 }
1121
1122 function noop() {}
1123 noop.exec = noop;
1124
1125 function merge(obj) {
1126   var i = 1
1127     , target
1128     , key;
1129
1130   for (; i < arguments.length; i++) {
1131     target = arguments[i];
1132     for (key in target) {
1133       if (Object.prototype.hasOwnProperty.call(target, key)) {
1134         obj[key] = target[key];
1135       }
1136     }
1137   }
1138
1139   return obj;
1140 }
1141
1142
1143 /**
1144  * Marked
1145  */
1146
1147 function marked(src, opt, callback) {
1148   if (callback || typeof opt === 'function') {
1149     if (!callback) {
1150       callback = opt;
1151       opt = null;
1152     }
1153
1154     opt = merge({}, marked.defaults, opt || {});
1155
1156     var highlight = opt.highlight
1157       , tokens
1158       , pending
1159       , i = 0;
1160
1161     try {
1162       tokens = Lexer.lex(src, opt)
1163     } catch (e) {
1164       return callback(e);
1165     }
1166
1167     pending = tokens.length;
1168
1169     var done = function(err) {
1170       if (err) {
1171         opt.highlight = highlight;
1172         return callback(err);
1173       }
1174
1175       var out;
1176
1177       try {
1178         out = Parser.parse(tokens, opt);
1179       } catch (e) {
1180         err = e;
1181       }
1182
1183       opt.highlight = highlight;
1184
1185       return err
1186         ? callback(err)
1187         : callback(null, out);
1188     };
1189
1190     if (!highlight || highlight.length < 3) {
1191       return done();
1192     }
1193
1194     delete opt.highlight;
1195
1196     if (!pending) return done();
1197
1198     for (; i < tokens.length; i++) {
1199       (function(token) {
1200         if (token.type !== 'code') {
1201           return --pending || done();
1202         }
1203         return highlight(token.text, token.lang, function(err, code) {
1204           if (err) return done(err);
1205           if (code == null || code === token.text) {
1206             return --pending || done();
1207           }
1208           token.text = code;
1209           token.escaped = true;
1210           --pending || done();
1211         });
1212       })(tokens[i]);
1213     }
1214
1215     return;
1216   }
1217   try {
1218     if (opt) opt = merge({}, marked.defaults, opt);
1219     return Parser.parse(Lexer.lex(src, opt), opt);
1220   } catch (e) {
1221     e.message += '\nPlease report this to https://github.com/chjj/marked.';
1222     if ((opt || marked.defaults).silent) {
1223       return '<p>An error occured:</p><pre>'
1224         + escape(e.message + '', true)
1225         + '</pre>';
1226     }
1227     throw e;
1228   }
1229 }
1230
1231 /**
1232  * Options
1233  */
1234
1235 marked.options =
1236 marked.setOptions = function(opt) {
1237   merge(marked.defaults, opt);
1238   return marked;
1239 };
1240
1241 marked.defaults = {
1242   gfm: true,
1243   tables: true,
1244   breaks: false,
1245   pedantic: false,
1246   sanitize: false,
1247   smartLists: false,
1248   silent: false,
1249   highlight: null,
1250   langPrefix: 'lang-',
1251   smartypants: false,
1252   headerPrefix: '',
1253   renderer: new Renderer,
1254   xhtml: false
1255 };
1256
1257 /**
1258  * Expose
1259  */
1260
1261 marked.Parser = Parser;
1262 marked.parser = Parser.parse;
1263
1264 marked.Renderer = Renderer;
1265
1266 marked.Lexer = Lexer;
1267 marked.lexer = Lexer.lex;
1268
1269 marked.InlineLexer = InlineLexer;
1270 marked.inlineLexer = InlineLexer.output;
1271
1272 marked.parse = marked;
1273
1274 if (typeof module !== 'undefined' && typeof exports === 'object') {
1275   module.exports = marked;
1276 } else if (typeof define === 'function' && define.amd) {
1277   define(function() { return marked; });
1278 } else {
1279   this.marked = marked;
1280 }
1281
1282 }).call(function() {
1283   return this || (typeof window !== 'undefined' ? window : global);
1284 }());