From 828bc9766ff8e734fbb85c3cff5b0d98d3487b54 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 3 Apr 2017 11:29:54 +0100 Subject: Add separate transforms module with paragraph() --- mammoth/__init__.py | 4 ++-- mammoth/documents.py | 3 ++- mammoth/transforms.py | 22 ++++++++++++++++++++++ setup.py | 2 +- tests/mammoth_tests.py | 11 +++++++++++ 5 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 mammoth/transforms.py diff --git a/mammoth/__init__.py b/mammoth/__init__.py index fe480ec..27cc580 100644 --- a/mammoth/__init__.py +++ b/mammoth/__init__.py @@ -1,8 +1,8 @@ -from . import docx, conversion, options, images, underline +from . import docx, conversion, options, images, transforms, underline from .raw_text import extract_raw_text_from_element from .docx.style_map import write_style_map, read_style_map -__all__ = ["convert_to_html", "extract_raw_text", "images", "underline"] +__all__ = ["convert_to_html", "extract_raw_text", "images", "transforms", "underline"] _undefined = object() diff --git a/mammoth/documents.py b/mammoth/documents.py index a8c3fb2..fe1cbcb 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -2,7 +2,8 @@ import cobble class Element(object): - pass + def copy(self, **kwargs): + return cobble.copy(self, **kwargs) class HasChildren(Element): diff --git a/mammoth/transforms.py b/mammoth/transforms.py new file mode 100644 index 0000000..6eb6d92 --- /dev/null +++ b/mammoth/transforms.py @@ -0,0 +1,22 @@ +from . import documents + + +def paragraph(transform_paragraph): + def transform_element(element): + if isinstance(element, documents.Paragraph): + return transform_paragraph(element) + else: + return element + + return _each_element(transform_element) + + +def _each_element(transform_element): + def transform_element_and_children(element): + if isinstance(element, documents.HasChildren): + children = list(map(transform_element, element.children)) + element = element.copy(children=children) + + return transform_element(element) + + return transform_element_and_children diff --git a/setup.py b/setup.py index 5ce5336..a68eb9c 100644 --- a/setup.py +++ b/setup.py @@ -9,7 +9,7 @@ def read(fname): _install_requires = [ - "cobble>=0.1.1,<0.2", + "cobble>=0.1.2,<0.2", ] if sys.version_info[:2] <= (2, 6): diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py index bcdf5a8..1dcbaa9 100644 --- a/tests/mammoth_tests.py +++ b/tests/mammoth_tests.py @@ -305,6 +305,17 @@ def transform_document_is_applied_to_document_before_conversion(): assert_equal([], result.messages) +@istest +def paragraph_transform_only_transforms_paragraphs(): + def transform_paragraph(paragraph): + return paragraph.copy(style_id="Heading1") + transform_document = mammoth.transforms.paragraph(transform_paragraph) + with open(test_path("single-paragraph.docx"), "rb") as fileobj: + result = mammoth.convert_to_html(fileobj=fileobj, transform_document=transform_document) + assert_equal("

Walking on imported air

", result.value) + assert_equal([], result.messages) + + @istest def docx_containing_one_paragraph_can_be_converted_to_markdown(): with open(test_path("single-paragraph.docx"), "rb") as fileobj: -- cgit v1.2.3