diff options
| author | Michael Williamson <mike@zwobble.org> | 2017-11-04 11:06:59 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2017-11-04 11:06:59 +0000 |
| commit | 1ae8ec5852c9ad94f1773d0014da2bb18887ce9e (patch) | |
| tree | be6a8045ed7ab6e40d54d7d7fcf4695e711ce30c | |
| parent | eae1dd0b509fbe201ba5f7ed530fd1bf8329904a (diff) | |
Parse style mappings for tables
| -rw-r--r-- | lib/style-reader.js | 46 | ||||
| -rw-r--r-- | test/style-reader.tests.js | 24 |
2 files changed, 56 insertions, 14 deletions
diff --git a/lib/style-reader.js b/lib/style-reader.js index 692672e..f686b28 100644 --- a/lib/style-reader.js +++ b/lib/style-reader.js @@ -52,7 +52,7 @@ function documentMatcherRule() { var paragraphRule = identifierToConstant("p", documentMatchers.paragraph); var runRule = identifierToConstant("r", documentMatchers.run); - var elementTypeRule = lop.rules.firstOf("p or r", + var elementTypeRule = lop.rules.firstOf("p or r or table", paragraphRule, runRule ); @@ -115,24 +115,43 @@ function documentMatcherRule() { } }; }); - var matcherSuffix = lop.rules.firstOf("matcher suffix", - styleIdRule, - styleNameRule, - listRule - ); - var matcherSuffixes = lop.rules.zeroOrMore(matcherSuffix); + + function createMatcherSuffixesRule(rules) { + var matcherSuffix = lop.rules.firstOf.apply( + lop.rules.firstOf, + ["matcher suffix"].concat(rules) + ); + var matcherSuffixes = lop.rules.zeroOrMore(matcherSuffix); + return lop.rules.then(matcherSuffixes, function(suffixes) { + var matcherOptions = {}; + suffixes.forEach(function(suffix) { + _.extend(matcherOptions, suffix); + }); + return matcherOptions; + }); + } var paragraphOrRun = sequence( sequence.capture(elementTypeRule), - sequence.capture(matcherSuffixes) - ).map(function(createMatcher, suffixes) { - var matcherOptions = {}; - suffixes.forEach(function(suffix) { - _.extend(matcherOptions, suffix); - }); + sequence.capture(createMatcherSuffixesRule([ + styleIdRule, + styleNameRule, + listRule + ])) + ).map(function(createMatcher, matcherOptions) { return createMatcher(matcherOptions); }); + var table = sequence( + lop.rules.token("identifier", "table"), + sequence.capture(createMatcherSuffixesRule([ + styleIdRule, + styleNameRule + ])) + ).map(function(options) { + return documentMatchers.table(options); + }); + var bold = identifierToConstant("b", documentMatchers.bold); var italic = identifierToConstant("i", documentMatchers.italic); var underline = identifierToConstant("u", documentMatchers.underline); @@ -163,6 +182,7 @@ function documentMatcherRule() { return lop.rules.firstOf("element type", paragraphOrRun, + table, bold, italic, underline, diff --git a/test/style-reader.tests.js b/test/style-reader.tests.js index 20d4127..dc8cb3f 100644 --- a/test/style-reader.tests.js +++ b/test/style-reader.tests.js @@ -165,7 +165,29 @@ test("styleReader.readDocumentMatcher", { documentMatchers.smallCaps ); }, - + + "reads plain table": function() { + assertDocumentMatcher("table", documentMatchers.table()); + }, + + "reads table with style ID": function() { + assertDocumentMatcher( + "table.TableNormal", + documentMatchers.table({ + styleId: "TableNormal" + }) + ); + }, + + "reads table with style name": function() { + assertDocumentMatcher( + "table[style-name='Normal Table']", + documentMatchers.table({ + styleName: documentMatchers.equalTo("Normal Table") + }) + ); + }, + "reads comment-reference": function() { assertDocumentMatcher( "comment-reference", |
