From 06fe4b9e53d34845b9763469471b8470100e207b Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Wed, 29 Oct 2014 17:12:27 +0300 Subject: [PATCH] Fixed fuckup on minification mangling, related to #89 --- lib/linkifier.js | 4 +- lib/parser_block.js | 27 +++++------ lib/parser_inline.js | 34 ++++++------- lib/ruler.js | 113 +++++++++++++++---------------------------- lib/typographer.js | 6 +-- 5 files changed, 74 insertions(+), 110 deletions(-) diff --git a/lib/linkifier.js b/lib/linkifier.js index 3880e13..310b0bd 100644 --- a/lib/linkifier.js +++ b/lib/linkifier.js @@ -8,7 +8,7 @@ var Ruler = require('./ruler'); var _rules = [ - require('./rules_text/linkify') + [ 'linkify', require('./rules_text/linkify') ] ]; @@ -20,7 +20,7 @@ function Linkifier() { this.ruler = new Ruler(this.rulesUpdate.bind(this)); for (var i = 0; i < _rules.length; i++) { - this.ruler.after(_rules[i]); + this.ruler.push(_rules[i][0], _rules[i][1]); } } diff --git a/lib/parser_block.js b/lib/parser_block.js index 1610d54..3297c20 100644 --- a/lib/parser_block.js +++ b/lib/parser_block.js @@ -8,19 +8,18 @@ var Ruler = require('./ruler'); var State = require('./rules_block/state_block'); -var _rules = []; - -// `list` should be after `hr`, but before `heading` -_rules.push([ require('./rules_block/code') ]); -_rules.push([ require('./rules_block/fences'), 'paragraph', 'blockquote', 'list' ]); -_rules.push([ require('./rules_block/blockquote'), 'paragraph', 'blockquote', 'list' ]); -_rules.push([ require('./rules_block/hr'), 'paragraph', 'blockquote', 'list' ]); -_rules.push([ require('./rules_block/list'), 'paragraph', 'blockquote' ]); -_rules.push([ require('./rules_block/heading'), 'paragraph', 'blockquote' ]); -_rules.push([ require('./rules_block/lheading') ]); -_rules.push([ require('./rules_block/htmlblock'), 'paragraph', 'blockquote' ]); -_rules.push([ require('./rules_block/table'), 'paragraph' ]); -_rules.push([ require('./rules_block/paragraph') ]); +var _rules = [ + [ 'code', require('./rules_block/code') ], + [ 'fences', require('./rules_block/fences'), [ 'paragraph', 'blockquote', 'list' ] ], + [ 'blockquote', require('./rules_block/blockquote'), [ 'paragraph', 'blockquote', 'list' ] ], + [ 'hr', require('./rules_block/hr'), [ 'paragraph', 'blockquote', 'list' ] ], + [ 'list', require('./rules_block/list'), [ 'paragraph', 'blockquote' ] ], + [ 'heading', require('./rules_block/heading'), [ 'paragraph', 'blockquote' ] ], + [ 'lheading', require('./rules_block/lheading') ], + [ 'htmlblock', require('./rules_block/htmlblock'), [ 'paragraph', 'blockquote' ] ], + [ 'table', require('./rules_block/table'), [ 'paragraph' ] ], + [ 'paragraph', require('./rules_block/paragraph') ] +]; // Block Parser class @@ -34,7 +33,7 @@ function ParserBlock() { this.ruler = new Ruler(this.rulesUpdate.bind(this)); for (var i = 0; i < _rules.length; i++) { - this.ruler.after(_rules[i][0], _rules[i].slice(1)); + this.ruler.push(_rules[i][0], _rules[i][1], { alt: (_rules[i][2] || []).slice() }); } } diff --git a/lib/parser_inline.js b/lib/parser_inline.js index 6e61b1d..b7f4dc2 100644 --- a/lib/parser_inline.js +++ b/lib/parser_inline.js @@ -9,22 +9,22 @@ var StateInline = require('./rules_inline/state_inline'); //////////////////////////////////////////////////////////////////////////////// // Parser rules -var _rules = []; - -_rules.push(require('./rules_inline/text')); -_rules.push(require('./rules_inline/newline')); -_rules.push(require('./rules_inline/escape')); -_rules.push(require('./rules_inline/backticks')); -_rules.push(require('./rules_inline/del')); -_rules.push(require('./rules_inline/ins')); -_rules.push(require('./rules_inline/mark')); -_rules.push(require('./rules_inline/emphasis')); -_rules.push(require('./rules_inline/sub')); -_rules.push(require('./rules_inline/sup')); -_rules.push(require('./rules_inline/links')); -_rules.push(require('./rules_inline/autolink')); -_rules.push(require('./rules_inline/htmltag')); -_rules.push(require('./rules_inline/entity')); +var _rules = [ + [ 'text', require('./rules_inline/text') ], + [ 'newline', require('./rules_inline/newline') ], + [ 'escape', require('./rules_inline/escape') ], + [ 'backticks', require('./rules_inline/backticks') ], + [ 'del', require('./rules_inline/del') ], + [ 'ins', require('./rules_inline/ins') ], + [ 'mark', require('./rules_inline/mark') ], + [ 'emphasis', require('./rules_inline/emphasis') ], + [ 'sub', require('./rules_inline/sub') ], + [ 'sup', require('./rules_inline/sup') ], + [ 'links', require('./rules_inline/links') ], + [ 'autolink', require('./rules_inline/autolink') ], + [ 'htmltag', require('./rules_inline/htmltag') ], + [ 'entity', require('./rules_inline/entity') ] +]; var BAD_PROTOCOLS = [ 'vbscript', 'javascript', 'file' ]; @@ -60,7 +60,7 @@ function ParserInline() { this.ruler = new Ruler(this.rulesUpdate.bind(this)); for (var i = 0; i < _rules.length; i++) { - this.ruler.after(_rules[i]); + this.ruler.push(_rules[i][0], _rules[i][1]); } } diff --git a/lib/ruler.js b/lib/ruler.js index 4c0e173..964a90c 100644 --- a/lib/ruler.js +++ b/lib/ruler.js @@ -7,20 +7,6 @@ 'use strict'; -//////////////////////////////////////////////////////////////////////////////// -// helpers - -function _class(obj) { return Object.prototype.toString.call(obj); } -function isFunction(obj) { return _class(obj) === '[object Function]'; } - -function functionName(fn) { - var ret = fn.toString(); - ret = ret.substr('function '.length); - ret = ret.substr(0, ret.indexOf('(')); - return ret; -} - - //////////////////////////////////////////////////////////////////////////////// function Ruler(compileFn) { @@ -51,89 +37,68 @@ Ruler.prototype.find = function (name) { }; -// Replace/delete parser function +// Replace rule function // -Ruler.prototype.at = function (name, fn, altNames) { +Ruler.prototype.at = function (name, fn, options) { var index = this.find(name); + var opt = options || {}; - if (index === -1) { - throw new Error('Parser rule not found: ' + name); - } - - if (isFunction(fn)) { - this.rules[index].fn = fn; - if (altNames) { - this.rules[index].alt = altNames; - } - } else { - this.rules = this.rules.slice(0, index).concat(this.rules.slice(index + 1)); - } + if (index === -1) { throw new Error('Parser rule not found: ' + name); } + this.rules[index].fn = fn; + this.rules[index].alt = opt.alt || []; this.compile(); }; -// Add function to parser chain before one with given name. -// Or add to start, if name not defined +// Add rule to chain before one with given name. // -Ruler.prototype.before = function (name, fn, altNames) { - var index, rule; +Ruler.prototype.before = function (beforeName, ruleName, fn, options) { + var index = this.find(beforeName); + var opt = options || {}; - if (isFunction(name)) { - altNames = fn; - fn = name; - name = ''; - } + if (index === -1) { throw new Error('Parser rule not found: ' + beforeName); } - rule = { - name: functionName(fn), + this.rules.splice(index, 0, { + name: ruleName, enabled: true, fn: fn, - alt: altNames || [] - }; - - if (!name) { - this.rules.unshift(rule); - } else { - index = this.find(name); - if (index === -1) { - throw new Error('Parser rule not found: ' + name); - } - this.rules.splice(index, 0, rule); - } + alt: opt.alt || [] + }); this.compile(); }; -// Add function to parser chain after one with given name. -// Or add to end, if name not defined +// Add rule to chain after one with given name. // -Ruler.prototype.after = function (name, fn, altNames) { - var index, rule; +Ruler.prototype.after = function (afterName, ruleName, fn, options) { + var index = this.find(afterName); + var opt = options || {}; - if (isFunction(name)) { - altNames = fn; - fn = name; - name = ''; - } + if (index === -1) { throw new Error('Parser rule not found: ' + afterName); } - rule = { - name: functionName(fn), + this.rules.splice(index + 1, 0, { + name: ruleName, enabled: true, fn: fn, - alt: altNames || [] - }; - - if (!name) { - this.rules.push(rule); - } else { - index = this.find(name); - if (index === -1) { - throw new Error('Parser rule not found: ' + name); - } - this.rules.splice(index + 1, 0, rule); - } + alt: opt.alt || [] + }); + + this.compile(); +}; + +// Add rule to the end of chain. +// +Ruler.prototype.push = function (ruleName, fn, options) { + var opt = options || {}; + + this.rules.push({ + name: ruleName, + enabled: true, + fn: fn, + alt: opt.alt || [] + }); this.compile(); }; diff --git a/lib/typographer.js b/lib/typographer.js index 2166ae0..72da807 100644 --- a/lib/typographer.js +++ b/lib/typographer.js @@ -12,8 +12,8 @@ var Ruler = require('./ruler'); var _rules = [ - require('./rules_text/replace'), - require('./rules_text/smartquotes') + [ 'replace', require('./rules_text/replace') ], + [ 'smartquotes', require('./rules_text/smartquotes') ] ]; @@ -25,7 +25,7 @@ function Typographer() { this.ruler = new Ruler(this.rulesUpdate.bind(this)); for (var i = 0; i < _rules.length; i++) { - this.ruler.after(_rules[i]); + this.ruler.push(_rules[i][0], _rules[i][1]); } }