refactor options.

This commit is contained in:
Christopher Jeffrey 2013-01-03 00:45:07 -06:00
parent 87a72cda33
commit aef3a954da

View File

@ -72,7 +72,15 @@ block.gfm.paragraph = replace(block.paragraph)
function Lexer(options) { function Lexer(options) {
this.tokens = []; this.tokens = [];
this.tokens.links = {}; this.tokens.links = {};
this.options = options || {}; this.options = options || marked.defaults;
if (this.options.gfm) {
block.fences = block.gfm.fences;
block.paragraph = block.gfm.paragraph;
} else {
block.fences = block.normal.fences;
block.paragraph = block.normal.paragraph;
}
} }
Lexer.lex = function(src, options) { Lexer.lex = function(src, options) {
@ -406,7 +414,7 @@ Parser.prototype.compileInline = function(src) {
// tag // tag
if (cap = inline.tag.exec(src)) { if (cap = inline.tag.exec(src)) {
src = src.substring(cap[0].length); src = src.substring(cap[0].length);
out += options.sanitize out += this.options.sanitize
? escape(cap[0]) ? escape(cap[0])
: cap[0]; : cap[0];
continue; continue;
@ -522,7 +530,23 @@ Parser.prototype.outputLink = function(cap, link) {
function Parser(options) { function Parser(options) {
this.tokens = []; this.tokens = [];
this.token = null; this.token = null;
this.options = options || {}; this.options = options || marked.defaults;
if (this.options.gfm) {
inline.text = inline.gfm.text;
inline.url = inline.gfm.url;
} else {
inline.text = inline.normal.text;
inline.url = inline.normal.url;
}
if (this.options.pedantic) {
inline.em = inline.pedantic.em;
inline.strong = inline.pedantic.strong;
} else {
inline.em = inline.normal.em;
inline.strong = inline.normal.strong;
}
} }
Parser.parse = function(src, options) { Parser.parse = function(src, options) {
@ -728,12 +752,11 @@ noop.exec = noop;
*/ */
function marked(src, opt) { function marked(src, opt) {
setOptions(opt);
try { try {
return Parser.parse(Lexer.lex(src, opt), opt); return Parser.parse(Lexer.lex(src, opt), opt);
} catch (e) { } catch (e) {
e.message += '\nPlease report this to https://github.com/chjj/marked.'; e.message += '\nPlease report this to https://github.com/chjj/marked.';
if (marked.options.silent) { if ((opt || marked.defaults).silent) {
return 'An error occured: ' + e.message; return 'An error occured: ' + e.message;
} }
throw e; throw e;
@ -744,61 +767,31 @@ function marked(src, opt) {
* Options * Options
*/ */
var options
, defaults;
function setOptions(opt) {
if (!opt) opt = defaults;
if (options === opt) return;
options = opt;
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.options = marked.options =
marked.setOptions = function(opt) { marked.setOptions = function(opt) {
defaults = opt; marked.defaults = opt;
setOptions(opt);
return marked; return marked;
}; };
marked.setOptions({ marked.defaults = {
gfm: true, gfm: true,
pedantic: false, pedantic: false,
sanitize: false, sanitize: false,
highlight: null highlight: null
}); };
/** /**
* Expose * Expose
*/ */
marked.Parser = Parser;
marked.parser = function(src, opt) { marked.parser = function(src, opt) {
setOptions(opt); return Parser.parse(src, opt);
return parse(src);
}; };
marked.Lexer = Lexer;
marked.lexer = function(src, opt) { marked.lexer = function(src, opt) {
setOptions(opt); return Lexer.lex(src, opt);
return block.lexer(src);
}; };
marked.parse = marked; marked.parse = marked;