optimize options more

This commit is contained in:
Christopher Jeffrey 2012-02-19 20:29:35 -06:00
parent 79975978b0
commit b2189ab99c
3 changed files with 108 additions and 24 deletions

View File

@ -64,6 +64,11 @@ Along with implementing every markdown feature, marked also implements
## Usage
``` js
marked.setDefaults({
gfm: true,
pedantic: false,
sanitize: true
});
var html = marked(markdown, options);
```

View File

@ -5,12 +5,6 @@
;(function() {
/**
* Options
*/
var options;
/**
* Block-Level Grammar
*/
@ -18,7 +12,7 @@ var options;
var block = {
newline: /^\n+/,
code: /^ {4,}[^\n]*(?:\n {4,}[^\n]*|\n)*(?:\n+|$)/,
gfm_code: /^ *``` *(\w+)? *\n([^\0]+?)\s*``` *(?:\n+|$)/,
fences: noop,
hr: /^( *[\-*_]){3,} *(?:\n+|$)/,
heading: /^ *(#{1,6}) *([^\n]+?) *#* *(?:\n+|$)/,
lheading: /^([^\n]+)\n *(=|-){3,} *\n*/,
@ -60,7 +54,6 @@ block.paragraph = (function() {
body.push(rule.replace(/(^|[^\[])\^/g, '$1'));
return push;
})
('gfm_code')
('hr')
('heading')
('lheading')
@ -72,6 +65,26 @@ block.paragraph = (function() {
RegExp(paragraph.replace('body', body.join('|')));
})();
block.normal = {
fences: block.fences,
paragraph: block.paragraph
};
block.gfm = {
fences: /^ *``` *(\w+)? *\n([^\0]+?)\s*``` *(?:\n+|$)/,
paragraph: /^/
};
block.gfm.paragraph = (function() {
var paragraph = block.paragraph.source
, fences = block.gfm.fences.source;
fences = fences.replace(/(^|[^\[])\^/g, '$1');
paragraph = paragraph.replace(/(\(\?!)/, '$1' + fences + '|');
return new RegExp(paragraph);
})();
/**
* Block Lexer
*/
@ -122,8 +135,8 @@ block.token = function(src, tokens, top) {
continue;
}
// gfm_code
if (options.gfm && (cap = block.gfm_code.exec(src))) {
// fences (gfm)
if (cap = block.fences.exec(src)) {
src = src.substring(cap[0].length);
tokens.push({
type: 'code',
@ -304,23 +317,41 @@ block.token = function(src, tokens, top) {
var inline = {
escape: /^\\([\\`*{}\[\]()#+\-.!_>])/,
autolink: /^<([^ >]+(@|:\/)[^ >]+)>/,
gfm_autolink: /^(\w+:\/\/[^\s]+[^.,:;"')\]\s])/,
url: noop,
tag: /^<!--[^\0]*?-->|^<\/?\w+(?:"[^"]*"|'[^']*'|[^'">])*?>/,
link: /^!?\[((?:\[[^\]]*\]|[^\[\]]|\[|\](?=[^[\]]*\]))*)\]\(([^\)]*)\)/,
reflink: /^!?\[((?:\[[^\]]*\]|[^\[\]]|\[|\](?=[^[\]]*\]))*)\]\s*\[([^\]]*)\]/,
nolink: /^!?\[((?:\[[^\]]*\]|[^\[\]])*)\]/,
strong: /^__([^\0]+?)__(?!_)|^\*\*([^\0]+?)\*\*(?!\*)/,
em: /^\b_((?:__|[^\0])+?)_\b|^\*((?:\*\*|[^\0])+?)\*(?!\*)/,
// pedantic strong/em
p_strong: /^__(?=\S)([^\0]*?\S)__(?!_)|^\*\*(?=\S)([^\0]*?\S)\*\*(?!\*)/,
p_em: /^\b_(?=\S)([^\0]*?\S)_\b|^\*(?=\S)([^\0]*?\S)\*(?!\*)/,
code: /^(`+)([^\0]*?[^`])\1(?!`)/,
br: /^ {2,}\n(?!\s*$)/,
text: /^[^\0]+?(?=[\\<!\[_*`]| {2,}\n|$)/
};
inline.normal = {
url: inline.url,
strong: inline.strong,
em: inline.em,
text: inline.text
};
inline.pedantic = {
strong: /^__(?=\S)([^\0]*?\S)__(?!_)|^\*\*(?=\S)([^\0]*?\S)\*\*(?!\*)/,
em: /^_(?=\S)([^\0]*?\S)_(?!_)|^\*(?=\S)([^\0]*?\S)\*(?!\*)/
};
inline.gfm = {
url: /^(\w+:\/\/[^\s]+[^.,:;"')\]\s])/,
// br: /^\n(?!\s*$)/,
text: /^[^\0]+?(?=[\\<!\[_*`]|\w+:\/\/| {2,}\n|$)/
};
inline.discount = {
strong: /^__([^\0]+?)__(?!_)|^\*\*([^\0]+?)\*\*(?!\*)/,
em: /^\b_(?=\S)([^\0]*?\S)_\b|^\*(?=\S)([^\0]*?(?:\*\*|[^\s*]))\*(?!\*)/
};
/**
* Inline Lexer
*/
@ -361,8 +392,8 @@ inline.lexer = function(src) {
continue;
}
// gfm_autolink
if (options.gfm && (cap = inline.gfm_autolink.exec(src))) {
// url (gfm)
if (cap = inline.url.exec(src)) {
src = src.substring(cap[0].length);
text = escape(cap[1]);
href = text;
@ -662,20 +693,58 @@ function tag() {
return tag;
}
function noop() {}
noop.exec = noop;
/**
* Marked
*/
var marked = function(src, opt) {
options = opt || marked.defaults;
if (opt) setOptions(opt);
return parse(block.lexer(src));
};
marked.defaults = {
/**
* Options
*/
var options;
var setOptions = function(opt) {
options = opt || marked.defaults;
if (options.gfm) {
block.fences = block.gfm.fences;
block.paragraph = block.gfm.paragraph;
inline.text = inline.gfm.text;
inline.url = inline.gfm.url;
} else {
block.fences = block.normal.fences;
block.paragraph = block.normal.paragraph;
inline.text = inline.normal.text;
inline.url = inline.normal.url;
}
if (options.pedantic) {
inline.em = inline.pedantic.em;
inline.strong = inline.pedantic.strong;
} else {
inline.em = inline.normal.em;
inline.strong = inline.normal.strong;
}
};
marked.setDefaults = function(opt) {
marked.defaults = opt;
setOptions(opt);
};
marked.setDefaults({
gfm: true,
pedantic: false,
sanitize: false
};
});
options = marked.defaults;
@ -683,8 +752,15 @@ options = marked.defaults;
* Expose
*/
marked.parser = parse;
marked.lexer = block.lexer;
marked.parser = function(src, opt) {
if (opt) setOptions(opt);
return parse(src);
};
marked.lexer = function(src, opt) {
if (opt) setOptions(opt);
return block.lexer(src);
};
marked.parse = marked;

View File

@ -132,9 +132,12 @@ main.bench = function(name, func) {
};
var bench = function() {
var marked = require('../');
marked.setDefaults({ gfm: false });
main.bench('marked', marked);
marked.setDefaults({ gfm: true });
main.bench('marked (with gfm)', marked);
var discount = require('discount').parse;
main.bench('discount', discount);