From 2c66c1e16ef035017d68b0023d02a1507a2c43cd Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 8 Jun 2014 22:10:01 +0100 Subject: Use style_name when converting paragraphs --- mammoth/conversion.py | 3 +++ mammoth/documents.py | 6 +++--- tests/conversion_tests.py | 15 ++++++++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/mammoth/conversion.py b/mammoth/conversion.py index 2d3b043..6c13225 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -115,6 +115,9 @@ def _document_matcher_matches(matcher, element, element_type): matcher.element_type == element_type and ( matcher.style_id is None or matcher.style_id == element.style_id + ) and ( + matcher.style_name is None or + matcher.style_name == element.style_name ) and ( element_type != "paragraph" or matcher.numbering is None or diff --git a/mammoth/documents.py b/mammoth/documents.py index b8da897..4438b06 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -2,7 +2,7 @@ import dodge Document = dodge.data_class("Document", ["children"]) -Paragraph = dodge.data_class("Paragraph", ["children", "style_id", "numbering"]) +Paragraph = dodge.data_class("Paragraph", ["children", "style_id", "style_name", "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,8 +20,8 @@ class Image(object): document = Document -def paragraph(children, style_id=None, numbering=None): - return Paragraph(children, style_id, numbering) +def paragraph(children, style_id=None, style_name=None, numbering=None): + return Paragraph(children, style_id, style_name, numbering) def run(children, style_id=None, is_bold=None, is_italic=None): return Run(children, style_id, bool(is_bold), bool(is_italic)) diff --git a/tests/conversion_tests.py b/tests/conversion_tests.py index edc8bb6..527ec2e 100644 --- a/tests/conversion_tests.py +++ b/tests/conversion_tests.py @@ -35,7 +35,7 @@ def empty_paragraphs_are_ignored(): @istest -def paragraphs_are_converted_by_satisfying_matching_paths(): +def style_mappings_using_style_ids_can_be_used_to_map_paragraphs(): result = convert_document_element_to_html( documents.paragraph(style_id="TipsParagraph", children=[ _run_with_text("Tip") @@ -47,6 +47,19 @@ def paragraphs_are_converted_by_satisfying_matching_paths(): assert_equal('

Tip

', result.value) +@istest +def style_mappings_using_style_names_can_be_used_to_map_paragraphs(): + result = convert_document_element_to_html( + documents.paragraph(style_id="TipsParagraph", style_name="Tips Paragraph", children=[ + _run_with_text("Tip") + ]), + style_map=[ + style_reader.read_style("p[style-name='Tips Paragraph'] => p.tip") + ] + ) + assert_equal('

Tip

', result.value) + + @istest def default_paragraph_style_is_used_if_no_matching_style_is_found(): result = convert_document_element_to_html( -- cgit v1.2.3