aboutsummaryrefslogtreecommitdiff
path: root/mammoth
diff options
context:
space:
mode:
Diffstat (limited to 'mammoth')
-rw-r--r--mammoth/style_reader/__init__.py29
-rw-r--r--mammoth/style_reader/document_matcher_reader.py12
-rw-r--r--mammoth/style_reader/html_path_reader.py13
-rw-r--r--mammoth/styles.py8
4 files changed, 50 insertions, 12 deletions
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"])