diff options
| author | Michael Williamson <mike@zwobble.org> | 2013-05-03 21:42:37 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2013-05-03 21:42:37 +0100 |
| commit | 6d2453fd95214ca3269c3d9f7f1e2a00d3f3ceee (patch) | |
| tree | 2cd9dc12f5080ebe21e1529582e38aa5c7c8800a | |
| parent | dc4025cae3964d0300b421b1b7ff486d9f4c5faf (diff) | |
Parse paragraph matcher
| -rw-r--r-- | lib/document-matchers.js | 15 | ||||
| -rw-r--r-- | lib/document-to-html.js | 2 | ||||
| -rw-r--r-- | lib/style-reader.js | 23 | ||||
| -rw-r--r-- | tests/style-reader.tests.js | 10 |
4 files changed, 40 insertions, 10 deletions
diff --git a/lib/document-matchers.js b/lib/document-matchers.js index 6a35b51..3232a23 100644 --- a/lib/document-matchers.js +++ b/lib/document-matchers.js @@ -3,15 +3,18 @@ exports.run = run; function paragraph(styleName) { - return matcher("paragraph", styleName); + return new Matcher("paragraph", styleName); } function run(styleName) { - return matcher("run", styleName); + return new Matcher("run", styleName); } -function matcher(elementType, styleName) { - return function(element) { - return element.type === elementType && element.styleName === styleName; - }; +function Matcher(elementType, styleName) { + this.elementType = elementType; + this.styleName = styleName; +} + +Matcher.prototype.matches = function(element) { + return element.type === this.elementType && element.styleName === this.styleName; } diff --git a/lib/document-to-html.js b/lib/document-to-html.js index 930e869..94c8b6a 100644 --- a/lib/document-to-html.js +++ b/lib/document-to-html.js @@ -87,7 +87,7 @@ function DocumentConverter(options) { function findStyle(element) { for (var i = 0; i < styleMap.length; i++) { - if (styleMap[i].from(element)) { + if (styleMap[i].from.matches(element)) { return styleMap[i]; } } diff --git a/lib/style-reader.js b/lib/style-reader.js index d749743..be5d5d3 100644 --- a/lib/style-reader.js +++ b/lib/style-reader.js @@ -1,9 +1,25 @@ var lop = require("lop"); var RegexTokeniser = lop.RegexTokeniser; +var documentMatchers = require("./document-matchers"); var htmlPaths = require("./html-paths"); exports.readHtmlPath = readHtmlPath; +exports.readDocumentMatcher = readDocumentMatcher; + + +function readDocumentMatcher(string) { + var paragraphRule = lop.rules.then( + lop.rules.token("identifier", "p"), + function() { + return documentMatchers.paragraph(); + } + ); + + var matcherRule = paragraphRule; + + return parseString(matcherRule, string); +} function readHtmlPath(string) { @@ -55,10 +71,13 @@ function readHtmlPath(string) { ), htmlPaths.elements ); - + return parseString(styleRule, string); +} + +function parseString(rule, string) { var tokens = tokenise(string); var parser = lop.Parser(); - var parseResult = parser.parseTokens(styleRule, tokens); + var parseResult = parser.parseTokens(rule, tokens); if (!parseResult.isSuccess()) { throw new Error("Failed to parse: " + describeFailure(parseResult)); } diff --git a/tests/style-reader.tests.js b/tests/style-reader.tests.js index 102bbad..23d4f88 100644 --- a/tests/style-reader.tests.js +++ b/tests/style-reader.tests.js @@ -1,10 +1,12 @@ var assert = require("assert"); var htmlPaths = require("../lib/html-paths"); +var documentMatchers = require("../lib/document-matchers"); var styleReader = require("../lib/style-reader"); var test = require("./testing").test; var readHtmlPath = styleReader.readHtmlPath; +var readDocumentMatcher = styleReader.readDocumentMatcher; describe('styleReader.readHtmlPath', function() { @@ -36,4 +38,10 @@ describe('styleReader.readHtmlPath', function() { ]) assert.deepEqual(readHtmlPath("p:fresh"), expected); }); -}) +}); + +describe("styleReader.readDocumentMatcher", function() { + test("reads plain paragraph", function() { + assert.deepEqual(readDocumentMatcher("p"), documentMatchers.paragraph()); + }); +}); |
