summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-05-03 21:50:57 +0100
committerMichael Williamson <mike@zwobble.org>2013-05-03 21:50:57 +0100
commit2558a4cc0df494f7b9380e9b34b85c5dd1c83010 (patch)
tree4b0a5877f5fd0dc93fecb7896145fe123082c8db
parent6d2453fd95214ca3269c3d9f7f1e2a00d3f3ceee (diff)
Add parsing of paragraph matcher with style name
-rw-r--r--lib/style-reader.js27
-rw-r--r--tests/style-reader.tests.js7
2 files changed, 24 insertions, 10 deletions
diff --git a/lib/style-reader.js b/lib/style-reader.js
index be5d5d3..757dc30 100644
--- a/lib/style-reader.js
+++ b/lib/style-reader.js
@@ -9,14 +9,21 @@ exports.readDocumentMatcher = readDocumentMatcher;
function readDocumentMatcher(string) {
+ var sequence = lop.rules.sequence;
+
var paragraphRule = lop.rules.then(
lop.rules.token("identifier", "p"),
function() {
- return documentMatchers.paragraph();
+ return documentMatchers.paragraph;
}
);
- var matcherRule = paragraphRule;
+ var matcherRule = sequence(
+ sequence.capture(paragraphRule),
+ sequence.capture(lop.rules.optional(classRule))
+ ).map(function(createMatcher, styleName) {
+ return createMatcher(styleName.valueOrElse());
+ });
return parseString(matcherRule, string);
}
@@ -24,14 +31,7 @@ function readDocumentMatcher(string) {
function readHtmlPath(string) {
var capture = lop.rules.sequence.capture;
-
- var identifierRule = lop.rules.tokenOfType("identifier");
var whitespaceRule = lop.rules.tokenOfType("whitespace");
-
- var classRule = lop.rules.sequence(
- lop.rules.tokenOfType("dot"),
- capture(identifierRule)
- ).head();
var freshRule = lop.rules.then(
lop.rules.optional(lop.rules.sequence(
lop.rules.tokenOfType("colon"),
@@ -73,6 +73,13 @@ function readHtmlPath(string) {
);
return parseString(styleRule, string);
}
+
+var identifierRule = lop.rules.tokenOfType("identifier");
+
+var classRule = lop.rules.sequence(
+ lop.rules.tokenOfType("dot"),
+ lop.rules.sequence.capture(identifierRule)
+).head();
function parseString(rule, string) {
var tokens = tokenise(string);
@@ -94,7 +101,7 @@ function describeFailure(parseResult) {
function tokenise(string) {
var tokeniser = new RegexTokeniser([
- {name: "identifier", regex: /([a-z][a-z0-9\-]*)/},
+ {name: "identifier", regex: /([a-zA-Z][a-zA-Z0-9\-]*)/},
{name: "dot", regex: /\./},
{name: "colon", regex: /:/},
{name: "gt", regex: />/},
diff --git a/tests/style-reader.tests.js b/tests/style-reader.tests.js
index 23d4f88..d96ba87 100644
--- a/tests/style-reader.tests.js
+++ b/tests/style-reader.tests.js
@@ -44,4 +44,11 @@ describe("styleReader.readDocumentMatcher", function() {
test("reads plain paragraph", function() {
assert.deepEqual(readDocumentMatcher("p"), documentMatchers.paragraph());
});
+
+ test("reads paragraph with style name", function() {
+ assert.deepEqual(
+ readDocumentMatcher("p.Heading1"),
+ documentMatchers.paragraph("Heading1")
+ );
+ });
});