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