aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2014-01-08 18:01:44 +0000
committerMichael Williamson <mike@zwobble.org>2014-01-08 18:01:44 +0000
commit78d5cca66ca8f204d5db1aff81519c142705add9 (patch)
tree441137c92cecb64833b8ae05800e7799d914eebd
parentd79153e4cc40ccd13d78cfdc463b55ce6913d221 (diff)
Add transform_document argument to convert_to_html
-rw-r--r--mammoth/__init__.py6
-rw-r--r--mammoth/documents.py15
-rw-r--r--setup.py1
-rw-r--r--tests/mammoth_tests.py13
4 files changed, 25 insertions, 10 deletions
diff --git a/mammoth/__init__.py b/mammoth/__init__.py
index 90ab764..59e349f 100644
--- a/mammoth/__init__.py
+++ b/mammoth/__init__.py
@@ -2,8 +2,10 @@ from .results import Result
from . import docx, conversion, options
-def convert_to_html(fileobj, **kwargs):
- return docx.read(fileobj).bind(lambda document:
+def convert_to_html(fileobj, transform_document=None, **kwargs):
+ if transform_document is None:
+ transform_document = lambda x: x
+ return docx.read(fileobj).map(transform_document).bind(lambda document:
conversion.convert_document_element_to_html(
document,
**options.read_options(kwargs)
diff --git a/mammoth/documents.py b/mammoth/documents.py
index 0f2d767..65a9b84 100644
--- a/mammoth/documents.py
+++ b/mammoth/documents.py
@@ -1,10 +1,11 @@
-import collections
+import dodge
-Document = collections.namedtuple("Document", ["children"])
-Paragraph = collections.namedtuple("Paragraph", ["children", "style_name", "numbering"])
-Run = collections.namedtuple("Run", ["children", "style_name", "is_bold", "is_italic"])
-Text = collections.namedtuple("Text", ["value"])
-Hyperlink = collections.namedtuple("Hyperlink", ["href", "children"])
+
+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"])
+Text = dodge.data_class("Text", ["value"])
+Hyperlink = dodge.data_class("Hyperlink", ["href", "children"])
class Tab(object):
pass
@@ -40,4 +41,4 @@ hyperlink = Hyperlink
def numbering_level(level_index, is_ordered):
return _NumberingLevel(str(level_index), bool(is_ordered))
-_NumberingLevel = collections.namedtuple("NumberingLevel", ["level_index", "is_ordered"])
+_NumberingLevel = dodge.data_class("NumberingLevel", ["level_index", "is_ordered"])
diff --git a/setup.py b/setup.py
index 16a0ab4..6c4f782 100644
--- a/setup.py
+++ b/setup.py
@@ -10,6 +10,7 @@ def read(fname):
_install_requires = [
"parsimonious>=0.5,<0.6",
+ "dodge>=0.1.5,<0.2",
]
if sys.version_info[:2] <= (2, 6):
diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py
index f2469a9..77e7ec2 100644
--- a/tests/mammoth_tests.py
+++ b/tests/mammoth_tests.py
@@ -27,4 +27,15 @@ def simple_list_is_converted_to_list_elements():
result = mammoth.convert_to_html(fileobj=fileobj)
assert_equal([], result.messages)
assert_equal("<ul><li>Apple</li><li>Banana</li></ul>", result.value)
-
+
+
+@istest
+def transform_document_is_applied_to_document_before_conversion():
+ def transform_document(document):
+ document.children[0].style_name = "Heading1"
+ return document
+
+ 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)