From 4d09c8116dbf01487368c16a66ea13f07b3349a3 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 13 Dec 2013 18:02:12 +0000 Subject: Parse styles --- mammoth/style_reader/__init__.py | 29 +++++++++++++++++++++++-- mammoth/style_reader/document_matcher_reader.py | 12 ++++++---- mammoth/style_reader/html_path_reader.py | 13 ++++++----- mammoth/styles.py | 8 +++++++ tests/style_reader_tests.py | 12 ++++++++-- 5 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 mammoth/styles.py diff --git a/mammoth/style_reader/__init__.py b/mammoth/style_reader/__init__.py index 705abff..41dcffa 100644 --- a/mammoth/style_reader/__init__.py +++ b/mammoth/style_reader/__init__.py @@ -1,2 +1,27 @@ -from .html_path_reader import read_html_path -from .document_matcher_reader import read_document_matcher +from parsimonious.grammar import Grammar + +from .html_path_reader import read_html_path, grammar_text as html_path_grammar, read_html_path_node +from .document_matcher_reader import read_document_matcher, grammar_text as document_matcher_grammar, read_document_matcher_node +from ..styles import Style + + +def read_style(string): + style_node = _grammar.parse(string) + return _read_style_node(style_node) + + +def _read_style_node(style_node): + document_matcher_node = style_node.children[0] + html_path_node = style_node.children[4] + + document_matcher = read_document_matcher_node(document_matcher_node) + html_path = read_html_path_node(html_path_node) + + return Style(document_matcher, html_path) + + + +_grammar = Grammar(""" +style = document_matcher whitespace "=>" whitespace html_path +""" + html_path_grammar + document_matcher_grammar +) diff --git a/mammoth/style_reader/document_matcher_reader.py b/mammoth/style_reader/document_matcher_reader.py index 7fc8bcb..fba6092 100644 --- a/mammoth/style_reader/document_matcher_reader.py +++ b/mammoth/style_reader/document_matcher_reader.py @@ -5,6 +5,10 @@ from .. import document_matchers, documents def read_document_matcher(string): root_node = _grammar.parse(string) + return read_document_matcher_node(root_node) + + +def read_document_matcher_node(root_node): element_node = root_node.children[0] if element_node.expr_name == "paragraph": return _read_paragraph_node(element_node) @@ -39,9 +43,8 @@ def _read_list_node(list_node): else: return None - -_grammar = Grammar(r""" -matcher = paragraph / run +grammar_text = r""" +document_matcher = paragraph / run paragraph = "p" style_name? list? @@ -54,5 +57,6 @@ style_identifier = ~"[A-Z0-9]*"i list = ":" list_type "(" ~"[0-9]+" ")" list_type = "ordered-list" / "unordered-list" -""") +""" +_grammar = Grammar(grammar_text) diff --git a/mammoth/style_reader/html_path_reader.py b/mammoth/style_reader/html_path_reader.py index 0de6e8c..0f1331b 100644 --- a/mammoth/style_reader/html_path_reader.py +++ b/mammoth/style_reader/html_path_reader.py @@ -5,11 +5,10 @@ from .. import html_paths def read_html_path(string): path_node = _grammar.parse(string) - return _read_path_node(path_node) - + return read_html_path_node(path_node) -def _read_path_node(path_node): +def read_html_path_node(path_node): elements = [ _read_element_node(child) for child in _repeated_children_with_separator(path_node, has_whitespace=True) @@ -59,9 +58,9 @@ def _repeated_children_with_separator(node, has_whitespace): yield child.children[sequence_node_index] -_grammar = Grammar(r""" +grammar_text = r""" -path = element whitespace* (">" whitespace* element)* +html_path = element whitespace* (">" whitespace* element)* element = tag_names class_name* fresh? @@ -75,5 +74,7 @@ identifier = ~"[A-Z0-9]*"i whitespace = ~"\s"* -""") +""" + +_grammar = Grammar(grammar_text) diff --git a/mammoth/styles.py b/mammoth/styles.py new file mode 100644 index 0000000..a90012b --- /dev/null +++ b/mammoth/styles.py @@ -0,0 +1,8 @@ +import collections + + +def style(document_matcher, html_path): + return Style(document_matcher, html_path) + + +Style = collections.namedtuple("Style", ["document_matcher", "html_path"]) diff --git a/tests/style_reader_tests.py b/tests/style_reader_tests.py index 971cfac..2f852e5 100644 --- a/tests/style_reader_tests.py +++ b/tests/style_reader_tests.py @@ -1,7 +1,7 @@ from nose.tools import istest, assert_equal -from mammoth import html_paths, document_matchers, documents -from mammoth.style_reader import read_html_path, read_document_matcher +from mammoth import html_paths, document_matchers, documents, styles +from mammoth.style_reader import read_html_path, read_document_matcher, read_style @istest @@ -109,3 +109,11 @@ class ReadDocumentMatcherTests(object): document_matchers.run(style_name="Emphasis"), read_document_matcher("r.Emphasis") ) + + +@istest +def document_matcher_is_mapped_to_html_path_using_fat_arrow(): + assert_equal( + styles.style(document_matchers.paragraph(), html_paths.path([html_paths.element(["h1"])])), + read_style("p => h1") + ) -- cgit v1.2.3