From 9f2a2399cdabf91d0766c9a6ddbeed0a4e2597cf Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 3 May 2013 22:01:34 +0100 Subject: Add parsing for styles --- lib/style-reader.js | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/style-reader.js b/lib/style-reader.js index be5ab20..423d9d0 100644 --- a/lib/style-reader.js +++ b/lib/style-reader.js @@ -6,9 +6,33 @@ var htmlPaths = require("./html-paths"); exports.readHtmlPath = readHtmlPath; exports.readDocumentMatcher = readDocumentMatcher; +exports.readStyle = readStyle; +function readStyle(string) { + return parseString(styleRule(), string); +} + +function styleRule() { + return lop.rules.sequence( + lop.rules.sequence.capture(documentMatcherRule()), + lop.rules.tokenOfType("whitespace"), + lop.rules.tokenOfType("arrow"), + lop.rules.tokenOfType("whitespace"), + lop.rules.sequence.capture(htmlPathRule()) + ).map(function(documentMatcher, htmlPath) { + return { + from: documentMatcher, + to: htmlPath + }; + }); +} + function readDocumentMatcher(string) { + return parseString(documentMatcherRule(), string); +} + +function documentMatcherRule() { var sequence = lop.rules.sequence; var paragraphRule = lop.rules.then( @@ -30,18 +54,19 @@ function readDocumentMatcher(string) { runRule ); - var matcherRule = sequence( + return sequence( sequence.capture(elementTypeRule), sequence.capture(lop.rules.optional(classRule)) ).map(function(createMatcher, styleName) { return createMatcher(styleName.valueOrElse()); }); - - return parseString(matcherRule, string); } - function readHtmlPath(string) { + return parseString(htmlPathRule(), string); +} + +function htmlPathRule() { var capture = lop.rules.sequence.capture; var whitespaceRule = lop.rules.tokenOfType("whitespace"); var freshRule = lop.rules.then( @@ -72,7 +97,7 @@ function readHtmlPath(string) { return htmlPaths.element(tagName, attributes, options); }); - var styleRule = lop.rules.then( + return lop.rules.then( lop.rules.zeroOrMoreWithSeparator( styleElementRule, lop.rules.sequence( @@ -83,7 +108,6 @@ function readHtmlPath(string) { ), htmlPaths.elements ); - return parseString(styleRule, string); } var identifierRule = lop.rules.tokenOfType("identifier"); @@ -117,7 +141,8 @@ function tokenise(string) { {name: "dot", regex: /\./}, {name: "colon", regex: /:/}, {name: "gt", regex: />/}, - {name: "whitespace", regex: /\s+/} + {name: "whitespace", regex: /\s+/}, + {name: "arrow", regex: /=>/} ]); return tokeniser.tokenise(string); } -- cgit v1.2.3