aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2017-04-03 11:29:54 +0100
committerMichael Williamson <mike@zwobble.org>2017-04-03 11:29:54 +0100
commit828bc9766ff8e734fbb85c3cff5b0d98d3487b54 (patch)
tree5e797dee488e02a4702ec506734ebd6e671be8cf
parent4085795d9b8f1845aff4a4b2eb7e5bc377302bcd (diff)
Add separate transforms module with paragraph()
-rw-r--r--mammoth/__init__.py4
-rw-r--r--mammoth/documents.py3
-rw-r--r--mammoth/transforms.py22
-rw-r--r--setup.py2
-rw-r--r--tests/mammoth_tests.py11
5 files changed, 38 insertions, 4 deletions
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
@@ -306,6 +306,17 @@ def transform_document_is_applied_to_document_before_conversion():
@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("<h1>Walking on imported air</h1>", 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:
result = mammoth.convert_to_markdown(fileobj=fileobj)