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