diff options
| author | Michael Williamson <mike@zwobble.org> | 2014-04-22 13:38:46 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2014-04-22 13:38:46 +0100 |
| commit | ec837dc5c639efa92fb676dc90c008ad7cfff58e (patch) | |
| tree | 44e50cb6713b22f91d7972fa8cfa83ee88cec58b /lib | |
| parent | 58ae1397f2a603a98adef5589763d9af2da636cb (diff) | |
Simplify document matcher parsing
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/document-matchers.js | 28 | ||||
| -rw-r--r-- | lib/style-reader.js | 40 |
2 files changed, 44 insertions, 24 deletions
diff --git a/lib/document-matchers.js b/lib/document-matchers.js index f116d3f..0846513 100644 --- a/lib/document-matchers.js +++ b/lib/document-matchers.js @@ -1,18 +1,34 @@ +var _ = require("underscore"); + exports.paragraph = paragraph; exports.run = run; -function paragraph(styleId) { - return new Matcher("paragraph", styleId); +function paragraph(options) { + options = options || {}; + if (_.isString(options)) { + // TODO: this is deprecated, remove usages + options = {styleId: options}; + } + return new Matcher("paragraph", options); } -function run(styleId) { - return new Matcher("run", styleId); +function run(options) { + options = options || {}; + if (_.isString(options)) { + // TODO: this is deprecated, remove usages + options = {styleId: options}; + } + return new Matcher("run", options); } -function Matcher(elementType, styleId) { +function Matcher(elementType, options) { this._elementType = elementType; - this._styleId = styleId; + this._styleId = options.styleId; + if (options.list) { + this._listIndex = options.list.levelIndex; + this._listIsOrdered = options.list.isOrdered; + } } Matcher.prototype.matches = function(element) { diff --git a/lib/style-reader.js b/lib/style-reader.js index 13f3609..2d75016 100644 --- a/lib/style-reader.js +++ b/lib/style-reader.js @@ -1,3 +1,4 @@ +var _ = require("underscore"); var lop = require("lop"); var RegexTokeniser = lop.RegexTokeniser; @@ -52,12 +53,18 @@ function documentMatcherRule() { runRule ); + var styleIdRule = lop.rules.then( + classRule, + function(styleId) { + return {styleId: styleId}; + } + ); + var listTypeRule = lop.rules.firstOf("list type", identifierToConstant("ordered-list", {isOrdered: true}), identifierToConstant("unordered-list", {isOrdered: false}) ); - var listRule = sequence( lop.rules.tokenOfType("colon"), sequence.capture(listTypeRule), @@ -66,31 +73,28 @@ function documentMatcherRule() { sequence.capture(integerRule), lop.rules.tokenOfType("close-paren") ).map(function(listType, levelNumber) { - return function(matcher) { - return matcher.list({ + return { + list: { isOrdered: listType.isOrdered, levelIndex: levelNumber - 1 - }); + } }; }); - - var optionalListRule = lop.rules.then( - lop.rules.optional(listRule), - function(option) { - return option.valueOrElse(function() { - return function(matcher) { - return matcher; - }; - }); - } + var matcherSuffix = lop.rules.firstOf("matcher suffix", + styleIdRule, + listRule ); + var matcherSuffixes = lop.rules.zeroOrMore(matcherSuffix); return sequence( sequence.capture(elementTypeRule), - sequence.capture(lop.rules.optional(classRule)), - sequence.capture(optionalListRule) - ).map(function(createMatcher, styleId, applyList) { - return applyList(createMatcher(styleId.valueOrElse())); + sequence.capture(matcherSuffixes) + ).map(function(createMatcher, suffixes) { + var matcherOptions = {}; + suffixes.forEach(function(suffix) { + _.extend(matcherOptions, suffix); + }); + return createMatcher(matcherOptions); }); } |
