update Marked; suppress IDs in headings (#582)

This commit is contained in:
Jeff Williams 2014-02-15 14:57:35 -08:00
parent 98d646a270
commit d51faa18d1
5 changed files with 351 additions and 163 deletions

View File

@ -77,18 +77,28 @@ function unescapeUrls(source) {
function getParseFunction(parserName, conf) {
var logger = require('jsdoc/util/logger');
var marked = require('marked');
var markedRenderer;
var parserFunction;
conf = conf || {};
if (parserName === parserNames.marked) {
// Marked generates an "id" attribute for headers; this custom renderer suppresses it
markedRenderer = new marked.Renderer();
markedRenderer.heading = function(text, level) {
var util = require('util');
return util.format('<h%s>%s</h%s>', level, text, level);
};
parserFunction = function(source) {
var result;
source = escapeUnderscores(source);
source = escapeUrls(source);
result = marked(source)
result = marked(source, { renderer: markedRenderer })
.replace(/\s+$/, '')
.replace(/&#39;/g, "'");
result = unescapeUrls(result);

2
node_modules/marked/LICENSE generated vendored
View File

@ -1,4 +1,4 @@
Copyright (c) 2011-2013, Christopher Jeffrey (https://github.com/chjj/)
Copyright (c) 2011-2014, Christopher Jeffrey (https://github.com/chjj/)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

476
node_modules/marked/lib/marked.js generated vendored
View File

@ -17,7 +17,7 @@ var block = {
hr: /^( *[-*_]){3,} *(?:\n+|$)/,
heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,
nptable: noop,
lheading: /^([^\n]+)\n *(=|-){3,} *\n*/,
lheading: /^([^\n]+)\n *(=|-){2,} *(?:\n+|$)/,
blockquote: /^( *>[^\n]+(\n[^\n]+)*\n*)+/,
list: /^( *)(bull) [\s\S]+?(?:hr|\n{2,}(?! )(?!\1bull )\n*|\s*$)/,
html: /^ *(?:comment|closed|closing) *(?:\n{2,}|\s*$)/,
@ -41,7 +41,7 @@ block.list = replace(block.list)
block._tag = '(?!(?:'
+ 'a|em|strong|small|s|cite|q|dfn|abbr|data|time|code'
+ '|var|samp|kbd|sub|sup|i|b|u|mark|ruby|rt|rp|bdi|bdo'
+ '|span|br|wbr|ins|del|img)\\b)\\w+(?!:/|@)\\b';
+ '|span|br|wbr|ins|del|img)\\b)\\w+(?!:/|[^\\w\\s@]*@)\\b';
block.html = replace(block.html)
('comment', /<!--[\s\S]*?-->/)
@ -70,12 +70,14 @@ block.normal = merge({}, block);
*/
block.gfm = merge({}, block.normal, {
fences: /^ *(`{3,}|~{3,}) *(\w+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/,
fences: /^ *(`{3,}|~{3,}) *(\S+)? *\n([\s\S]+?)\s*\1 *(?:\n+|$)/,
paragraph: /^/
});
block.gfm.paragraph = replace(block.paragraph)
('(?!', '(?!' + block.gfm.fences.source.replace('\\1', '\\2') + '|')
('(?!', '(?!'
+ block.gfm.fences.source.replace('\\1', '\\2') + '|'
+ block.list.source.replace('\\1', '\\3') + '|')
();
/**
@ -274,20 +276,16 @@ Lexer.prototype.token = function(src, top) {
// list
if (cap = this.rules.list.exec(src)) {
src = src.substring(cap[0].length);
bull = cap[2];
this.tokens.push({
type: 'list_start',
ordered: isFinite(cap[2])
ordered: bull.length > 1
});
// Get each top-level item.
cap = cap[0].match(this.rules.item);
// Get bullet.
if (this.options.smartLists) {
bull = block.bullet.exec(cap[0])[0];
}
next = false;
l = cap.length;
i = 0;
@ -313,7 +311,7 @@ Lexer.prototype.token = function(src, top) {
// Backpedal if it does not belong in this list.
if (this.options.smartLists && i !== l - 1) {
b = block.bullet.exec(cap[i + 1])[0];
if (bull !== b && !(bull[1] === '.' && b[1] === '.')) {
if (bull !== b && !(bull.length > 1 && b.length > 1)) {
src = cap.slice(i + 1).join('\n') + src;
i = l - 1;
}
@ -324,7 +322,7 @@ Lexer.prototype.token = function(src, top) {
// for discount behavior.
loose = next || /\n\n(?!\s*$)/.test(item);
if (i !== l - 1) {
next = item[item.length-1] === '\n';
next = item.charAt(item.length - 1) === '\n';
if (!loose) loose = next;
}
@ -356,7 +354,7 @@ Lexer.prototype.token = function(src, top) {
type: this.options.sanitize
? 'paragraph'
: 'html',
pre: cap[1] === 'pre',
pre: cap[1] === 'pre' || cap[1] === 'script' || cap[1] === 'style',
text: cap[0]
});
continue;
@ -411,7 +409,7 @@ Lexer.prototype.token = function(src, top) {
src = src.substring(cap[0].length);
this.tokens.push({
type: 'paragraph',
text: cap[1][cap[1].length-1] === '\n'
text: cap[1].charAt(cap[1].length - 1) === '\n'
? cap[1].slice(0, -1)
: cap[1]
});
@ -458,8 +456,8 @@ var inline = {
text: /^[\s\S]+?(?=[\\<!\[_*`]| {2,}\n|$)/
};
inline._inside = /(?:\[[^\]]*\]|[^\]]|\](?=[^\[]*\]))*/;
inline._href = /\s*<?([^\s]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/;
inline._inside = /(?:\[[^\]]*\]|[^\[\]]|\](?=[^\[]*\]))*/;
inline._href = /\s*<?([\s\S]*?)>?(?:\s+['"]([\s\S]*?)['"])?\s*/;
inline.link = replace(inline.link)
('inside', inline._inside)
@ -516,6 +514,8 @@ function InlineLexer(links, options) {
this.options = options || marked.defaults;
this.links = links;
this.rules = inline.normal;
this.renderer = this.options.renderer || new Renderer;
this.renderer.options = this.options;
if (!this.links) {
throw new
@ -571,7 +571,7 @@ InlineLexer.prototype.output = function(src) {
if (cap = this.rules.autolink.exec(src)) {
src = src.substring(cap[0].length);
if (cap[2] === '@') {
text = cap[1][6] === ':'
text = cap[1].charAt(6) === ':'
? this.mangle(cap[1].substring(7))
: this.mangle(cap[1]);
href = this.mangle('mailto:') + text;
@ -579,11 +579,7 @@ InlineLexer.prototype.output = function(src) {
text = escape(cap[1]);
href = text;
}
out += '<a href="'
+ href
+ '">'
+ text
+ '</a>';
out += this.renderer.link(href, null, text);
continue;
}
@ -592,11 +588,7 @@ InlineLexer.prototype.output = function(src) {
src = src.substring(cap[0].length);
text = escape(cap[1]);
href = text;
out += '<a href="'
+ href
+ '">'
+ text
+ '</a>';
out += this.renderer.link(href, null, text);
continue;
}
@ -626,7 +618,7 @@ InlineLexer.prototype.output = function(src) {
link = (cap[2] || cap[1]).replace(/\s+/g, ' ');
link = this.links[link.toLowerCase()];
if (!link || !link.href) {
out += cap[0][0];
out += cap[0].charAt(0);
src = cap[0].substring(1) + src;
continue;
}
@ -637,50 +629,42 @@ InlineLexer.prototype.output = function(src) {
// strong
if (cap = this.rules.strong.exec(src)) {
src = src.substring(cap[0].length);
out += '<strong>'
+ this.output(cap[2] || cap[1])
+ '</strong>';
out += this.renderer.strong(this.output(cap[2] || cap[1]));
continue;
}
// em
if (cap = this.rules.em.exec(src)) {
src = src.substring(cap[0].length);
out += '<em>'
+ this.output(cap[2] || cap[1])
+ '</em>';
out += this.renderer.em(this.output(cap[2] || cap[1]));
continue;
}
// code
if (cap = this.rules.code.exec(src)) {
src = src.substring(cap[0].length);
out += '<code>'
+ escape(cap[2], true)
+ '</code>';
out += this.renderer.codespan(escape(cap[2], true));
continue;
}
// br
if (cap = this.rules.br.exec(src)) {
src = src.substring(cap[0].length);
out += '<br>';
out += this.renderer.br();
continue;
}
// del (gfm)
if (cap = this.rules.del.exec(src)) {
src = src.substring(cap[0].length);
out += '<del>'
+ this.output(cap[1])
+ '</del>';
out += this.renderer.del(this.output(cap[1]));
continue;
}
// text
if (cap = this.rules.text.exec(src)) {
src = src.substring(cap[0].length);
out += escape(cap[0]);
out += escape(this.smartypants(cap[0]));
continue;
}
@ -698,31 +682,33 @@ InlineLexer.prototype.output = function(src) {
*/
InlineLexer.prototype.outputLink = function(cap, link) {
if (cap[0][0] !== '!') {
return '<a href="'
+ escape(link.href)
+ '"'
+ (link.title
? ' title="'
+ escape(link.title)
+ '"'
: '')
+ '>'
+ this.output(cap[1])
+ '</a>';
} else {
return '<img src="'
+ escape(link.href)
+ '" alt="'
+ escape(cap[1])
+ '"'
+ (link.title
? ' title="'
+ escape(link.title)
+ '"'
: '')
+ '>';
}
var href = escape(link.href)
, title = link.title ? escape(link.title) : null;
return cap[0].charAt(0) !== '!'
? this.renderer.link(href, title, this.output(cap[1]))
: this.renderer.image(href, title, escape(cap[1]));
};
/**
* Smartypants Transformations
*/
InlineLexer.prototype.smartypants = function(text) {
if (!this.options.smartypants) return text;
return text
// em-dashes
.replace(/--/g, '\u2014')
// opening singles
.replace(/(^|[-\u2014/(\[{"\s])'/g, '$1\u2018')
// closing singles & apostrophes
.replace(/'/g, '\u2019')
// opening doubles
.replace(/(^|[-\u2014/(\[{\u2018\s])"/g, '$1\u201c')
// closing doubles
.replace(/"/g, '\u201d')
// ellipses
.replace(/\.{3}/g, '\u2026');
};
/**
@ -746,6 +732,149 @@ InlineLexer.prototype.mangle = function(text) {
return out;
};
/**
* Renderer
*/
function Renderer(options) {
this.options = options || {};
}
Renderer.prototype.code = function(code, lang, escaped) {
if (this.options.highlight) {
var out = this.options.highlight(code, lang);
if (out != null && out !== code) {
escaped = true;
code = out;
}
}
if (!lang) {
return '<pre><code>'
+ (escaped ? code : escape(code, true))
+ '\n</code></pre>';
}
return '<pre><code class="'
+ this.options.langPrefix
+ escape(lang, true)
+ '">'
+ (escaped ? code : escape(code, true))
+ '\n</code></pre>\n';
};
Renderer.prototype.blockquote = function(quote) {
return '<blockquote>\n' + quote + '</blockquote>\n';
};
Renderer.prototype.html = function(html) {
return html;
};
Renderer.prototype.heading = function(text, level, raw) {
return '<h'
+ level
+ ' id="'
+ this.options.headerPrefix
+ raw.toLowerCase().replace(/[^\w]+/g, '-')
+ '">'
+ text
+ '</h'
+ level
+ '>\n';
};
Renderer.prototype.hr = function() {
return this.options.xhtml ? '<hr/>\n' : '<hr>\n';
};
Renderer.prototype.list = function(body, ordered) {
var type = ordered ? 'ol' : 'ul';
return '<' + type + '>\n' + body + '</' + type + '>\n';
};
Renderer.prototype.listitem = function(text) {
return '<li>' + text + '</li>\n';
};
Renderer.prototype.paragraph = function(text) {
return '<p>' + text + '</p>\n';
};
Renderer.prototype.table = function(header, body) {
return '<table>\n'
+ '<thead>\n'
+ header
+ '</thead>\n'
+ '<tbody>\n'
+ body
+ '</tbody>\n'
+ '</table>\n';
};
Renderer.prototype.tablerow = function(content) {
return '<tr>\n' + content + '</tr>\n';
};
Renderer.prototype.tablecell = function(content, flags) {
var type = flags.header ? 'th' : 'td';
var tag = flags.align
? '<' + type + ' style="text-align:' + flags.align + '">'
: '<' + type + '>';
return tag + content + '</' + type + '>\n';
};
// span level renderer
Renderer.prototype.strong = function(text) {
return '<strong>' + text + '</strong>';
};
Renderer.prototype.em = function(text) {
return '<em>' + text + '</em>';
};
Renderer.prototype.codespan = function(text) {
return '<code>' + text + '</code>';
};
Renderer.prototype.br = function() {
return this.options.xhtml ? '<br/>' : '<br>';
};
Renderer.prototype.del = function(text) {
return '<del>' + text + '</del>';
};
Renderer.prototype.link = function(href, title, text) {
if (this.options.sanitize) {
try {
var prot = decodeURIComponent(unescape(href))
.replace(/[^\w:]/g, '')
.toLowerCase();
} catch (e) {
return '';
}
if (prot.indexOf('javascript:') === 0) {
return '';
}
}
var out = '<a href="' + href + '"';
if (title) {
out += ' title="' + title + '"';
}
out += '>' + text + '</a>';
return out;
};
Renderer.prototype.image = function(href, title, text) {
var out = '<img src="' + href + '" alt="' + text + '"';
if (title) {
out += ' title="' + title + '"';
}
out += this.options.xhtml ? '/>' : '>';
return out;
};
/**
* Parsing & Compiling
*/
@ -754,14 +883,17 @@ function Parser(options) {
this.tokens = [];
this.token = null;
this.options = options || marked.defaults;
this.options.renderer = this.options.renderer || new Renderer;
this.renderer = this.options.renderer;
this.renderer.options = this.options;
}
/**
* Static Parse Method
*/
Parser.parse = function(src, options) {
var parser = new Parser(options);
Parser.parse = function(src, options, renderer) {
var parser = new Parser(options, renderer);
return parser.parse(src);
};
@ -770,7 +902,7 @@ Parser.parse = function(src, options) {
*/
Parser.prototype.parse = function(src) {
this.inline = new InlineLexer(src.links, this.options);
this.inline = new InlineLexer(src.links, this.options, this.renderer);
this.tokens = src.reverse();
var out = '';
@ -821,77 +953,53 @@ Parser.prototype.tok = function() {
return '';
}
case 'hr': {
return '<hr>\n';
return this.renderer.hr();
}
case 'heading': {
return '<h'
+ this.token.depth
+ '>'
+ this.inline.output(this.token.text)
+ '</h'
+ this.token.depth
+ '>\n';
return this.renderer.heading(
this.inline.output(this.token.text),
this.token.depth,
this.token.text);
}
case 'code': {
if (this.options.highlight) {
var code = this.options.highlight(this.token.text, this.token.lang);
if (code != null && code !== this.token.text) {
this.token.escaped = true;
this.token.text = code;
}
}
if (!this.token.escaped) {
this.token.text = escape(this.token.text, true);
}
return '<pre><code'
+ (this.token.lang
? ' class="'
+ this.options.langPrefix
+ this.token.lang
+ '"'
: '')
+ '>'
+ this.token.text
+ '</code></pre>\n';
return this.renderer.code(this.token.text,
this.token.lang,
this.token.escaped);
}
case 'table': {
var body = ''
, heading
var header = ''
, body = ''
, i
, row
, cell
, flags
, j;
// header
body += '<thead>\n<tr>\n';
cell = '';
for (i = 0; i < this.token.header.length; i++) {
heading = this.inline.output(this.token.header[i]);
body += this.token.align[i]
? '<th align="' + this.token.align[i] + '">' + heading + '</th>\n'
: '<th>' + heading + '</th>\n';
flags = { header: true, align: this.token.align[i] };
cell += this.renderer.tablecell(
this.inline.output(this.token.header[i]),
{ header: true, align: this.token.align[i] }
);
}
body += '</tr>\n</thead>\n';
header += this.renderer.tablerow(cell);
// body
body += '<tbody>\n'
for (i = 0; i < this.token.cells.length; i++) {
row = this.token.cells[i];
body += '<tr>\n';
for (j = 0; j < row.length; j++) {
cell = this.inline.output(row[j]);
body += this.token.align[j]
? '<td align="' + this.token.align[j] + '">' + cell + '</td>\n'
: '<td>' + cell + '</td>\n';
}
body += '</tr>\n';
}
body += '</tbody>\n';
return '<table>\n'
+ body
+ '</table>\n';
cell = '';
for (j = 0; j < row.length; j++) {
cell += this.renderer.tablecell(
this.inline.output(row[j]),
{ header: false, align: this.token.align[j] }
);
}
body += this.renderer.tablerow(cell);
}
return this.renderer.table(header, body);
}
case 'blockquote_start': {
var body = '';
@ -900,25 +1008,17 @@ Parser.prototype.tok = function() {
body += this.tok();
}
return '<blockquote>\n'
+ body
+ '</blockquote>\n';
return this.renderer.blockquote(body);
}
case 'list_start': {
var type = this.token.ordered ? 'ol' : 'ul'
, body = '';
var body = ''
, ordered = this.token.ordered;
while (this.next().type !== 'list_end') {
body += this.tok();
}
return '<'
+ type
+ '>\n'
+ body
+ '</'
+ type
+ '>\n';
return this.renderer.list(body, ordered);
}
case 'list_item_start': {
var body = '';
@ -929,9 +1029,7 @@ Parser.prototype.tok = function() {
: this.tok();
}
return '<li>'
+ body
+ '</li>\n';
return this.renderer.listitem(body);
}
case 'loose_item_start': {
var body = '';
@ -940,24 +1038,19 @@ Parser.prototype.tok = function() {
body += this.tok();
}
return '<li>'
+ body
+ '</li>\n';
return this.renderer.listitem(body);
}
case 'html': {
return !this.token.pre && !this.options.pedantic
var html = !this.token.pre && !this.options.pedantic
? this.inline.output(this.token.text)
: this.token.text;
return this.renderer.html(html);
}
case 'paragraph': {
return '<p>'
+ this.inline.output(this.token.text)
+ '</p>\n';
return this.renderer.paragraph(this.inline.output(this.token.text));
}
case 'text': {
return '<p>'
+ this.parseText()
+ '</p>\n';
return this.renderer.paragraph(this.parseText());
}
}
};
@ -975,6 +1068,19 @@ function escape(html, encode) {
.replace(/'/g, '&#39;');
}
function unescape(html) {
return html.replace(/&([#\w]+);/g, function(_, n) {
n = n.toLowerCase();
if (n === 'colon') return ':';
if (n.charAt(0) === '#') {
return n.charAt(1) === 'x'
? String.fromCharCode(parseInt(n.substring(2), 16))
: String.fromCharCode(+n.substring(1));
}
return '';
});
}
function replace(regex, opt) {
regex = regex.source;
opt = opt || '';
@ -1007,11 +1113,75 @@ function merge(obj) {
return obj;
}
/**
* Marked
*/
function marked(src, opt) {
function marked(src, opt, callback) {
if (callback || typeof opt === 'function') {
if (!callback) {
callback = opt;
opt = null;
}
opt = merge({}, marked.defaults, opt || {});
var highlight = opt.highlight
, tokens
, pending
, i = 0;
try {
tokens = Lexer.lex(src, opt)
} catch (e) {
return callback(e);
}
pending = tokens.length;
var done = function() {
var out, err;
try {
out = Parser.parse(tokens, opt);
} catch (e) {
err = e;
}
opt.highlight = highlight;
return err
? callback(err)
: callback(null, out);
};
if (!highlight || highlight.length < 3) {
return done();
}
delete opt.highlight;
if (!pending) return done();
for (; i < tokens.length; i++) {
(function(token) {
if (token.type !== 'code') {
return --pending || done();
}
return highlight(token.text, token.lang, function(err, code) {
if (code == null || code === token.text) {
return --pending || done();
}
token.text = code;
token.escaped = true;
--pending || done();
});
})(tokens[i]);
}
return;
}
try {
if (opt) opt = merge({}, marked.defaults, opt);
return Parser.parse(Lexer.lex(src, opt), opt);
@ -1045,7 +1215,11 @@ marked.defaults = {
smartLists: false,
silent: false,
highlight: null,
langPrefix: 'lang-'
langPrefix: 'lang-',
smartypants: false,
headerPrefix: '',
renderer: new Renderer,
xhtml: false
};
/**
@ -1055,6 +1229,8 @@ marked.defaults = {
marked.Parser = Parser;
marked.parser = Parser.parse;
marked.Renderer = Renderer;
marked.Lexer = Lexer;
marked.lexer = Lexer.lex;

18
node_modules/marked/package.json generated vendored

File diff suppressed because one or more lines are too long

View File

@ -22,7 +22,7 @@
"catharsis": "~0.7.0",
"esprima": "~1.0.4",
"js2xmlparser": "~0.1.0",
"marked": "~0.2.8",
"marked": "~0.3.1",
"taffydb": "https://github.com/hegemonic/taffydb/tarball/master",
"underscore": "~1.4.2",
"wrench": "~1.3.9"