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