aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-12-13 17:41:57 +0000
committerMichael Williamson <mike@zwobble.org>2013-12-13 17:41:57 +0000
commit52a0281824ebe97f4233066b73a1e9739b639d9d (patch)
tree6e704b641c11b97fc141a7c0f4a44269a3f0ec7f
parentd091e47adf6cd69d986cac935de9d61b7da17f82 (diff)
Parse ordered list document matcher
-rw-r--r--mammoth/document_matchers.py6
-rw-r--r--mammoth/documents.py5
-rw-r--r--mammoth/style_reader/document_matcher_reader.py19
-rw-r--r--tests/style_reader_tests.py10
4 files changed, 29 insertions, 11 deletions
diff --git a/mammoth/document_matchers.py b/mammoth/document_matchers.py
index 1049c94..5cfe92d 100644
--- a/mammoth/document_matchers.py
+++ b/mammoth/document_matchers.py
@@ -1,11 +1,11 @@
import collections
-def paragraph(style_name=None):
- return ParagraphMatcher(style_name)
+def paragraph(style_name=None, numbering=None):
+ return ParagraphMatcher(style_name, numbering)
-ParagraphMatcher = collections.namedtuple("ParagraphMatcher", ["style_name"])
+ParagraphMatcher = collections.namedtuple("ParagraphMatcher", ["style_name", "numbering"])
def run(style_name=None):
diff --git a/mammoth/documents.py b/mammoth/documents.py
index 7fb34b6..ba03e92 100644
--- a/mammoth/documents.py
+++ b/mammoth/documents.py
@@ -37,7 +37,4 @@ image = Image
hyperlink = Hyperlink
-class NumberingLevel(object):
- def __init__(self, level_index, is_ordered):
- self.level_index = level_index
- self.is_ordered = is_ordered
+NumberingLevel = collections.namedtuple("NumberingLevel", ["level_index", "is_ordered"])
diff --git a/mammoth/style_reader/document_matcher_reader.py b/mammoth/style_reader/document_matcher_reader.py
index ba08e09..837da51 100644
--- a/mammoth/style_reader/document_matcher_reader.py
+++ b/mammoth/style_reader/document_matcher_reader.py
@@ -1,6 +1,6 @@
from parsimonious.grammar import Grammar
-from .. import document_matchers
+from .. import document_matchers, documents
def read_document_matcher(string):
@@ -14,7 +14,8 @@ def read_document_matcher(string):
def _read_paragraph_node(paragraph_node):
style_name = _read_style_node(paragraph_node.children[1])
- return document_matchers.paragraph(style_name=style_name)
+ numbering = _read_list_node(paragraph_node.children[2])
+ return document_matchers.paragraph(style_name=style_name, numbering=numbering)
def _read_run_node(run_node):
@@ -29,15 +30,27 @@ def _read_style_node(style_node):
return None
+def _read_list_node(list_node):
+ if list_node.children:
+ return documents.NumberingLevel(
+ int(list_node.children[0].children[1].text),
+ is_ordered=True,
+ )
+ else:
+ return None
+
+
_grammar = Grammar(r"""
matcher = paragraph / run
-paragraph = "p" style_name?
+paragraph = "p" style_name? list?
run = "r" style_name?
style_name = "." style_identifier
style_identifier = ~"[A-Z0-9]*"i
+
+list = ":ordered-list(" ~"[0-9]+" ")"
""")
diff --git a/tests/style_reader_tests.py b/tests/style_reader_tests.py
index cd26a80..b1afdb7 100644
--- a/tests/style_reader_tests.py
+++ b/tests/style_reader_tests.py
@@ -1,6 +1,6 @@
from nose.tools import istest, assert_equal
-from mammoth import html_paths, document_matchers
+from mammoth import html_paths, document_matchers, documents
from mammoth.style_reader import read_html_path, read_document_matcher
@@ -80,6 +80,14 @@ class ReadDocumentMatcherTests(object):
@istest
+ def reads_paragraph_ordered_list(self):
+ assert_equal(
+ document_matchers.paragraph(numbering=documents.NumberingLevel(2, is_ordered=True)),
+ read_document_matcher("p:ordered-list(2)")
+ )
+
+
+ @istest
def reads_plain_run(self):
assert_equal(
document_matchers.run(),