/** * marked - A markdown parser * Copyright (c) 2011, Christopher Jeffrey. (MIT Licensed) */ ;(function() { /** * Block-Level Grammar */ var block = { newline: /^\n+/, block: /^ {4,}[^\n]*(?:\n {4,}[^\n]*|\n)*(?=\n| *$)/, hr: /^( *[\-*_]){3,} *\n/, heading: /^ *(#{1,6}) *([^\0]+?) *#* *\n+/, lheading: /^([^\n]+)\n *(=|-){3,}/, blockquote: /^ *>[^\n]*(?:\n *>[^\n]*)*/, list: /^( *)([*+-]|\d+\.) [^\0]+?(?:\n{2,}(?! )|\s*$)(?!\1\2|\1\d+\.)/, html: /^ *(?:|<(\w+)[^\0]+?<\/\1>|<\w+[^>]*>) *(?:\n{2,}|\s*$)/, text: /^[^\n]+/ }; /** * Lexer */ block.lexer = function(str) { var tokens = [] , links = {}; str = str.replace(/\r\n|\r/g, '\n') .replace(/\t/g, ' '); str = str.replace( /^ {0,3}\[([^\]]+)\]: *([^ ]+)(?: +"([^\n]+)")? *$/gm, function(__, id, href, title) { links[id] = { href: href, title: title }; return ''; } ); tokens.links = links; return block.token(str, tokens); }; block.token = function(str, tokens) { var str = str.replace(/^ +$/gm, '') , loose , cap; while (str) { if (cap = block.newline.exec(str)) { str = str.substring(cap[0].length); if (cap[0].length > 1) { tokens.push({ type: 'space' }); } continue; } if (cap = block.block.exec(str)) { str = str.substring(cap[0].length); cap = cap[0].replace(/^ {4}/gm, ''); tokens.push({ type: 'block', text: cap }); continue; } if (cap = block.heading.exec(str)) { str = str.substring(cap[0].length); tokens.push({ type: 'heading', depth: cap[1].length, text: cap[2] }); continue; } if (cap = block.lheading.exec(str)) { str = str.substring(cap[0].length); tokens.push({ type: 'heading', depth: cap[2] === '=' ? 1 : 2, text: cap[1] }); continue; } if (cap = block.hr.exec(str)) { str = str.substring(cap[0].length); tokens.push({ type: 'hr' }); continue; } if (cap = block.blockquote.exec(str)) { str = str.substring(cap[0].length); tokens.push({ type: 'blockquote_start' }); cap = cap[0].replace(/^ *>/gm, ''); block.token(cap, tokens); tokens.push({ type: 'blockquote_end' }); continue; } if (cap = block.list.exec(str)) { str = str.substring(cap[0].length); tokens.push({ type: 'list_start', ordered: isFinite(cap[2]) }); loose = /\n *\n *(?:[*+-]|\d+\.)/.test(cap[0]); // get each top-level item cap = cap[0].match( /^( *)([*+-]|\d+\.)[^\n]*(?:\n(?!\1(?:\2|\d+\.))[^\n]*)*/gm ); each(cap, function(item) { // remove the list items sigil // so its seen as the next token item = item.replace(/^ *([*+-]|\d+\.) */, ''); // outdent whatever the // list item contains, hacky var space = /\n( +)/.exec(item); if (space) { space = new RegExp('^' + space[1], 'gm'); item = item.replace(space, ''); } tokens.push({ type: loose ? 'loose_item_start' : 'list_item_start' }); block.token(item, tokens); tokens.push({ type: 'list_item_end' }); }); tokens.push({ type: 'list_end' }); continue; } if (cap = block.html.exec(str)) { str = str.substring(cap[0].length); tokens.push({ type: 'html', text: cap[0] }); continue; } if (cap = block.text.exec(str)) { str = str.substring(cap[0].length); tokens.push({ type: 'text', text: cap[0] }); continue; } } return tokens; }; /** * Inline Processing */ var inline = { escape: /^\\([\\`*{}\[\]()#+\-.!_])/, autolink: /^<([^ >]+(@|:\/)[^ >]+)>/, tag: /^|^<\/?\w+[^>]*>/, link: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]\s*\(([^\)]*)\)/, reflink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]\s*\[([^\]]*)\]/, nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/, strong: /^__([^\0]+?)__(?!_)|^\*\*([^\0]+?)\*\*(?!\*)/, em: /^_([^_]+)_|^\*([^*]+)\*/, code: /^`([^`]+)`|^``([^\0]+?)``/, br: /^ {2,}\n(?!\s*$)/, text: /^/ }; // hacky, but performant inline.text = (function() { var body = []; (function push(rule) { rule = inline[rule].source; body.push(rule.replace(/(^|[^\[])\^/g, '$1')); return push; }) ('escape') ('tag') ('nolink') ('strong') ('em') ('code') ('br'); return new RegExp('^[^\\0]+?(?=' + body.join('|') + '|$)'); })(); /** * Inline Lexer */ inline.lexer = function(str) { var out = '' , links = tokens.links , link , text , href , cap; while (str) { if (cap = inline.escape.exec(str)) { str = str.substring(cap[0].length); out += cap[1]; continue; } if (cap = inline.autolink.exec(str)) { str = str.substring(cap[0].length); if (cap[2] === '@') { text = cap[1][6] === ':' ? mangle(cap[1].substring(7)) : mangle(cap[1]); href = mangle('mailto:') + text; } else { text = escape(cap[1]); href = text; } out += '' + text + ''; continue; } if (cap = inline.tag.exec(str)) { str = str.substring(cap[0].length); out += cap[0]; continue; } if (cap = inline.link.exec(str)) { str = str.substring(cap[0].length); text = /^\s*?(?:\s+"([^\n]+)")?\s*$/.exec(cap[2]); link = { href: text[1], title: text[2] }; out += mlink(cap, link); continue; } if ((cap = inline.reflink.exec(str)) || (cap = inline.nolink.exec(str))) { str = str.substring(cap[0].length); link = (cap[2] || cap[1]).replace(/\s+/g, ' '); link = links[link]; if (!link) { out += cap[0][0]; str = cap[0].substring(1) + str; continue; } out += mlink(cap, link); continue; } if (cap = inline.strong.exec(str)) { str = str.substring(cap[0].length); out += '' + inline.lexer(cap[2] || cap[1]) + ''; continue; } if (cap = inline.em.exec(str)) { str = str.substring(cap[0].length); out += '' + inline.lexer(cap[2] || cap[1]) + ''; continue; } if (cap = inline.code.exec(str)) { str = str.substring(cap[0].length); out += '' + escape(cap[2] || cap[1]) + ''; continue; } if (cap = inline.br.exec(str)) { str = str.substring(cap[0].length); out += '
'; continue; } if (cap = inline.text.exec(str)) { str = str.substring(cap[0].length); out += escape(cap[0]); continue; } } return out; }; var mlink = function(cap, link) { if (cap[0][0] !== '!') { return '' + inline.lexer(cap[1]) + ''; } else { return ''
      + escape(cap[1])
      + ''; } }; /** * Parsing */ var tokens , token; var next = function() { return token = tokens.pop(); }; var tok = function() { switch (token.type) { case 'space': { return ''; } case 'hr': { return '
'; } case 'heading': { return '' + inline.lexer(token.text) + ''; } case 'block': { return '
'
        + escape(token.text)
        + '
'; } case 'blockquote_start': { var body = []; while (next().type !== 'blockquote_end') { body.push(tok()); } return '
' + body.join('') + '
'; } case 'list_start': { var type = token.ordered ? 'ol' : 'ul' , body = []; while (next().type !== 'list_end') { body.push(tok()); } return '<' + type + '>' + body.join('') + ''; } case 'list_item_start': { var body = []; while (next().type !== 'list_item_end') { body.push(token.type === 'text' ? text() : tok()); } return '
  • ' + body.join(' ') + '
  • '; } case 'loose_item_start': { var body = []; while (next().type !== 'list_item_end') { body.push(tok()); } return '
  • ' + body.join(' ') + '
  • '; } case 'html': { return inline.lexer(token.text); } case 'text': { return '

    ' + text() + '

    '; } } }; var text = function() { var body = [ token.text ] , top; while ((top = tokens[tokens.length-1]) && top.type === 'text') { body.push(next().text); } return inline.lexer(body.join('\n')); }; var parse = function(src) { tokens = src.reverse(); var out = []; while (next()) { out.push(tok()); } tokens = null; token = null; return out.join(' '); }; /** * Helpers */ var escape = function(html) { return html .replace(/&/g, '&') .replace(//g, '>') .replace(/"/g, '"') .replace(/'/g, '''); }; var mangle = function(str) { var out = '' , ch , i = 0 , l = str.length; for (; i < l; i++) { ch = str.charCodeAt(i); if (Math.random() > 0.5) { ch = 'x' + ch.toString(16); } out += '&#' + ch + ';'; } return out; }; var each = function(obj, func) { var i = 0, l = obj.length; for (; i < l; i++) func(obj[i]); }; /** * Expose */ var marked = function(str) { return parse(block.lexer(str)); }; marked.parser = parse; marked.lexer = block.lexer; if (typeof module !== 'undefined') { module.exports = marked; } else { this.marked = marked; } }).call(this);