diff options
| author | Michael Williamson <mike@zwobble.org> | 2013-12-01 13:28:57 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2013-12-01 13:28:57 +0000 |
| commit | 4a35a848117c4fe646d36419096d099030878469 (patch) | |
| tree | 9c83c8b8c0aee665311839477163776250db4b85 | |
| parent | 8fc128a3e70465b82605b4c0bdb3d1a6ffe87ef6 (diff) | |
Add style_name to Run
| -rw-r--r-- | mammoth/documents.py | 14 | ||||
| -rw-r--r-- | mammoth/docx/document_xml.py | 2 | ||||
| -rw-r--r-- | tests/docx/document_xml_tests.py | 13 | ||||
| -rw-r--r-- | tests/docx/docx_tests.py | 6 |
4 files changed, 23 insertions, 12 deletions
diff --git a/mammoth/documents.py b/mammoth/documents.py index 08e3bba..215df8e 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -1,11 +1,17 @@ import collections +Document = collections.namedtuple("Document", ["children"]) +Paragraph = collections.namedtuple("Paragraph", ["children", "style_name", "numbering"]) +Run = collections.namedtuple("Run", ["children", "style_name"]) +Text = collections.namedtuple("Text", ["value"]) + + +document = Document def paragraph(children, style_name=None, numbering=None): return Paragraph(children, style_name, numbering) +def run(children, style_name=None): + return Run(children, style_name) -Document = collections.namedtuple("Document", ["children"]) -Paragraph = collections.namedtuple("Paragraph", ["children", "style_name", "numbering"]) -Run = collections.namedtuple("Run", ["children"]) -Text = collections.namedtuple("Text", ["value"]) +text = Text diff --git a/mammoth/docx/document_xml.py b/mammoth/docx/document_xml.py index 425ba37..403f880 100644 --- a/mammoth/docx/document_xml.py +++ b/mammoth/docx/document_xml.py @@ -22,7 +22,7 @@ def _create_reader(numbering): @handler("w:r") def run(element): - return documents.Run(_read_xml_elements(element.children)) + return documents.run(_read_xml_elements(element.children)) @handler("w:p") diff --git a/tests/docx/document_xml_tests.py b/tests/docx/document_xml_tests.py index b31ac9d..6539c54 100644 --- a/tests/docx/document_xml_tests.py +++ b/tests/docx/document_xml_tests.py @@ -17,7 +17,7 @@ class ReadXmlElementTests(object): def can_read_text_within_run(self): element = _run_element_with_text("Hello!") assert_equal( - documents.Run([documents.Text("Hello!")]), + documents.run([documents.Text("Hello!")]), read_document_xml_element(element) ) @@ -25,7 +25,7 @@ class ReadXmlElementTests(object): def can_read_text_within_paragraph(self): element = _paragraph_element_with_text("Hello!") assert_equal( - documents.paragraph([documents.Run([documents.Text("Hello!")])]), + documents.paragraph([documents.run([documents.Text("Hello!")])]), read_document_xml_element(element) ) @@ -33,7 +33,7 @@ class ReadXmlElementTests(object): def can_read_text_within_document(self): element = _document_element_with_text("Hello!") assert_equal( - documents.Document([documents.paragraph([documents.Run([documents.Text("Hello!")])])]), + documents.Document([documents.paragraph([documents.run([documents.Text("Hello!")])])]), read_document_xml_element(element) ) @@ -70,6 +70,11 @@ class ReadXmlElementTests(object): assert_equal("1", paragraph.numbering.level_index) assert_equal(True, paragraph.numbering.is_ordered) + @istest + def run_has_no_style_if_it_has_no_properties(self): + element = xml_element("w:r") + assert_equal(None, read_document_xml_element(element).style_name) + @istest def unrecognised_elements_are_ignored(self): @@ -80,7 +85,7 @@ class ReadXmlElementTests(object): def unrecognised_children_are_ignored(self): element = xml_element("w:r", {}, [_text_element("Hello!"), xml_element("w:huh", {}, [])]) assert_equal( - documents.Run([documents.Text("Hello!")]), + documents.run([documents.Text("Hello!")]), read_document_xml_element(element) ) diff --git a/tests/docx/docx_tests.py b/tests/docx/docx_tests.py index c20a41d..15bca51 100644 --- a/tests/docx/docx_tests.py +++ b/tests/docx/docx_tests.py @@ -10,10 +10,10 @@ class ReadTests(object): def can_read_document_with_single_paragraph_with_single_run_of_text(self): with open(test_path("single-paragraph.docx")) as fileobj: result = docx.read(fileobj=fileobj) - expected_document = documents.Document([ + expected_document = documents.document([ documents.paragraph([ - documents.Run([ - documents.Text("Walking on imported air") + documents.run([ + documents.text("Walking on imported air") ]) ]) ]) |
