diff options
| author | Michael Williamson <mike@zwobble.org> | 2014-05-11 18:01:31 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2014-05-11 18:01:31 +0100 |
| commit | 61e88321b9e5dc67a0373edc69f8114db8c1d3b7 (patch) | |
| tree | 3f43fd006b6c095f91c2424086b273928881336c | |
| parent | 7e57058cf120717cc2d2fea3b897b12c3891b28a (diff) | |
Rename existing concept of style names to style IDs to match docx
| -rw-r--r-- | mammoth/conversion.py | 8 | ||||
| -rw-r--r-- | mammoth/document_matchers.py | 12 | ||||
| -rw-r--r-- | mammoth/documents.py | 12 | ||||
| -rw-r--r-- | mammoth/docx/document_xml.py | 8 | ||||
| -rw-r--r-- | mammoth/style_reader/document_matcher_reader.py | 14 | ||||
| -rw-r--r-- | tests/conversion_tests.py | 10 | ||||
| -rw-r--r-- | tests/docx/document_xml_tests.py | 12 | ||||
| -rw-r--r-- | tests/mammoth_tests.py | 2 | ||||
| -rw-r--r-- | tests/style_reader_tests.py | 8 |
9 files changed, 43 insertions, 43 deletions
diff --git a/mammoth/conversion.py b/mammoth/conversion.py index 8123ebd..56d0fae 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -104,8 +104,8 @@ class DocumentConverter(object): if _document_matcher_matches(document_matcher, element, element_type): return style.html_path - if element.style_name is not None: - self.messages.append(results.warning("Unrecognised {0} style: {1}".format(element_type, element.style_name))) + if element.style_id is not None: + self.messages.append(results.warning("Unrecognised {0} style: {1}".format(element_type, element.style_id))) return default @@ -113,8 +113,8 @@ class DocumentConverter(object): def _document_matcher_matches(matcher, element, element_type): return ( matcher.element_type == element_type and ( - matcher.style_name is None or - matcher.style_name == element.style_name + matcher.style_id is None or + matcher.style_id == element.style_id ) and ( element_type != "paragraph" or matcher.numbering is None or diff --git a/mammoth/document_matchers.py b/mammoth/document_matchers.py index ed4171b..014e4e3 100644 --- a/mammoth/document_matchers.py +++ b/mammoth/document_matchers.py @@ -1,17 +1,17 @@ import collections -def paragraph(style_name=None, numbering=None): - return ParagraphMatcher(style_name, numbering) +def paragraph(style_id=None, numbering=None): + return ParagraphMatcher(style_id, numbering) -ParagraphMatcher = collections.namedtuple("ParagraphMatcher", ["style_name", "numbering"]) +ParagraphMatcher = collections.namedtuple("ParagraphMatcher", ["style_id", "numbering"]) ParagraphMatcher.element_type = "paragraph" -def run(style_name=None): - return RunMatcher(style_name) +def run(style_id=None): + return RunMatcher(style_id) -RunMatcher = collections.namedtuple("RunMatcher", ["style_name"]) +RunMatcher = collections.namedtuple("RunMatcher", ["style_id"]) RunMatcher.element_type = "run" diff --git a/mammoth/documents.py b/mammoth/documents.py index 65a9b84..b8da897 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -2,8 +2,8 @@ import dodge Document = dodge.data_class("Document", ["children"]) -Paragraph = dodge.data_class("Paragraph", ["children", "style_name", "numbering"]) -Run = dodge.data_class("Run", ["children", "style_name", "is_bold", "is_italic"]) +Paragraph = dodge.data_class("Paragraph", ["children", "style_id", "numbering"]) +Run = dodge.data_class("Run", ["children", "style_id", "is_bold", "is_italic"]) Text = dodge.data_class("Text", ["value"]) Hyperlink = dodge.data_class("Hyperlink", ["href", "children"]) @@ -20,11 +20,11 @@ class Image(object): document = Document -def paragraph(children, style_name=None, numbering=None): - return Paragraph(children, style_name, numbering) +def paragraph(children, style_id=None, numbering=None): + return Paragraph(children, style_id, numbering) -def run(children, style_name=None, is_bold=None, is_italic=None): - return Run(children, style_name, bool(is_bold), bool(is_italic)) +def run(children, style_id=None, is_bold=None, is_italic=None): + return Run(children, style_id, bool(is_bold), bool(is_italic)) text = Text diff --git a/mammoth/docx/document_xml.py b/mammoth/docx/document_xml.py index e4682a6..5023ec5 100644 --- a/mammoth/docx/document_xml.py +++ b/mammoth/docx/document_xml.py @@ -42,7 +42,7 @@ def _create_reader(numbering, content_types, relationships, docx_file): @handler("w:r") def run(element): properties = element.find_child_or_null("w:rPr") - style_name = properties \ + style_id = properties \ .find_child_or_null("w:rStyle") \ .attributes.get("w:val") is_bold = properties.find_child("w:b") @@ -51,7 +51,7 @@ def _create_reader(numbering, content_types, relationships, docx_file): return _read_xml_elements(element.children) \ .map(lambda children: documents.run( children=children, - style_name=style_name, + style_id=style_id, is_bold=is_bold, is_italic=is_italic, )) @@ -60,7 +60,7 @@ def _create_reader(numbering, content_types, relationships, docx_file): @handler("w:p") def paragraph(element): properties = element.find_child_or_null("w:pPr") - style_name = properties \ + style_id = properties \ .find_child_or_null("w:pStyle") \ .attributes.get("w:val") numbering_properties = properties.find_child("w:numPr") @@ -72,7 +72,7 @@ def _create_reader(numbering, content_types, relationships, docx_file): return _read_xml_elements(element.children) \ .map(lambda children: documents.paragraph( children=children, - style_name=style_name, + style_id=style_id, numbering=numbering, )) diff --git a/mammoth/style_reader/document_matcher_reader.py b/mammoth/style_reader/document_matcher_reader.py index 44a9896..5b50f24 100644 --- a/mammoth/style_reader/document_matcher_reader.py +++ b/mammoth/style_reader/document_matcher_reader.py @@ -17,14 +17,14 @@ def read_document_matcher_node(root_node): def _read_paragraph_node(paragraph_node): - style_name = _read_style_node(paragraph_node.children[1]) + style_id = _read_style_node(paragraph_node.children[1]) numbering = _read_list_node(paragraph_node.children[2]) - return document_matchers.paragraph(style_name=style_name, numbering=numbering) + return document_matchers.paragraph(style_id=style_id, numbering=numbering) def _read_run_node(run_node): - style_name = _read_style_node(run_node.children[1]) - return document_matchers.run(style_name=style_name) + style_id = _read_style_node(run_node.children[1]) + return document_matchers.run(style_id=style_id) def _read_style_node(style_node): @@ -46,11 +46,11 @@ def _read_list_node(list_node): grammar_text = r""" document_matcher = paragraph / run -paragraph = "p" style_name? list? +paragraph = "p" style_id? list? -run = "r" style_name? +run = "r" style_id? -style_name = "." style_identifier +style_id = "." style_identifier style_identifier = ~"[A-Z0-9]*"i diff --git a/tests/conversion_tests.py b/tests/conversion_tests.py index 5912a0e..edc8bb6 100644 --- a/tests/conversion_tests.py +++ b/tests/conversion_tests.py @@ -37,7 +37,7 @@ def empty_paragraphs_are_ignored(): @istest def paragraphs_are_converted_by_satisfying_matching_paths(): result = convert_document_element_to_html( - documents.paragraph(style_name="TipsParagraph", children=[ + documents.paragraph(style_id="TipsParagraph", children=[ _run_with_text("Tip") ]), style_map=[ @@ -50,7 +50,7 @@ def paragraphs_are_converted_by_satisfying_matching_paths(): @istest def default_paragraph_style_is_used_if_no_matching_style_is_found(): result = convert_document_element_to_html( - documents.paragraph(style_name="TipsParagraph", children=[ + documents.paragraph(style_id="TipsParagraph", children=[ _run_with_text("Tip") ]), ) @@ -60,7 +60,7 @@ def default_paragraph_style_is_used_if_no_matching_style_is_found(): @istest def default_paragraph_style_is_specified_by_mapping_plain_paragraphs(): result = convert_document_element_to_html( - documents.paragraph(style_name="TipsParagraph", children=[ + documents.paragraph(style_id="TipsParagraph", children=[ _run_with_text("Tip") ]), style_map=[ @@ -73,7 +73,7 @@ def default_paragraph_style_is_specified_by_mapping_plain_paragraphs(): @istest def warning_is_emitted_if_paragraph_style_is_unrecognised(): result = convert_document_element_to_html( - documents.paragraph(style_name="TipsParagraph", children=[ + documents.paragraph(style_id="TipsParagraph", children=[ _run_with_text("Tip") ]), ) @@ -133,7 +133,7 @@ def italic_runs_are_wrapped_in_emphasis_tags(): @istest def runs_are_converted_by_satisfying_matching_paths(): result = convert_document_element_to_html( - documents.run(style_name="TipsRun", children=[documents.Text("Tip")]), + documents.run(style_id="TipsRun", children=[documents.Text("Tip")]), style_map=[ style_reader.read_style("r.TipsRun => span.tip") ] diff --git a/tests/docx/document_xml_tests.py b/tests/docx/document_xml_tests.py index caeaeb8..e63cd60 100644 --- a/tests/docx/document_xml_tests.py +++ b/tests/docx/document_xml_tests.py @@ -44,15 +44,15 @@ class ReadXmlElementTests(object): @istest def paragraph_has_no_style_if_it_has_no_properties(self): element = xml_element("w:p") - assert_equal(None, _read_and_get_document_xml_element(element).style_name) + assert_equal(None, _read_and_get_document_xml_element(element).style_id) @istest - def paragraph_has_style_name_read_from_paragraph_properties_if_present(self): + def paragraph_has_style_id_read_from_paragraph_properties_if_present(self): style_xml = xml_element("w:pStyle", {"w:val": "Heading1"}) properties_xml = xml_element("w:pPr", {}, [style_xml]) paragraph_xml = xml_element("w:p", {}, [properties_xml]) paragraph = _read_and_get_document_xml_element(paragraph_xml) - assert_equal("Heading1", paragraph.style_name) + assert_equal("Heading1", paragraph.style_id) @istest def paragraph_has_no_numbering_if_it_has_no_numbering_properties(self): @@ -77,13 +77,13 @@ class ReadXmlElementTests(object): @istest def run_has_no_style_if_it_has_no_properties(self): element = xml_element("w:r") - assert_equal(None, _read_and_get_document_xml_element(element).style_name) + assert_equal(None, _read_and_get_document_xml_element(element).style_id) @istest - def run_has_style_name_read_from_run_properties_if_present(self): + def run_has_style_id_read_from_run_properties_if_present(self): style_xml = xml_element("w:rStyle", {"w:val": "Emphasis"}) run = self._read_run_with_properties([style_xml]) - assert_equal("Emphasis", run.style_name) + assert_equal("Emphasis", run.style_id) @istest def run_is_not_bold_if_bold_element_is_not_present(self): diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py index 77e7ec2..bc9f99c 100644 --- a/tests/mammoth_tests.py +++ b/tests/mammoth_tests.py @@ -32,7 +32,7 @@ def simple_list_is_converted_to_list_elements(): @istest def transform_document_is_applied_to_document_before_conversion(): def transform_document(document): - document.children[0].style_name = "Heading1" + document.children[0].style_id = "Heading1" return document with open(test_path("single-paragraph.docx"), "rb") as fileobj: diff --git a/tests/style_reader_tests.py b/tests/style_reader_tests.py index 32636db..21b857e 100644 --- a/tests/style_reader_tests.py +++ b/tests/style_reader_tests.py @@ -72,9 +72,9 @@ class ReadDocumentMatcherTests(object): @istest - def reads_paragraph_with_style_name(self): + def reads_paragraph_with_style_id(self): assert_equal( - document_matchers.paragraph(style_name="Heading1"), + document_matchers.paragraph(style_id="Heading1"), read_document_matcher("p.Heading1") ) @@ -104,9 +104,9 @@ class ReadDocumentMatcherTests(object): @istest - def reads_run_with_style_name(self): + def reads_run_with_style_id(self): assert_equal( - document_matchers.run(style_name="Emphasis"), + document_matchers.run(style_id="Emphasis"), read_document_matcher("r.Emphasis") ) |
