diff options
| author | Michael Williamson <mike@zwobble.org> | 2017-04-03 11:29:54 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2017-04-03 11:29:54 +0100 |
| commit | 828bc9766ff8e734fbb85c3cff5b0d98d3487b54 (patch) | |
| tree | 5e797dee488e02a4702ec506734ebd6e671be8cf /mammoth | |
| parent | 4085795d9b8f1845aff4a4b2eb7e5bc377302bcd (diff) | |
Add separate transforms module with paragraph()
Diffstat (limited to 'mammoth')
| -rw-r--r-- | mammoth/__init__.py | 4 | ||||
| -rw-r--r-- | mammoth/documents.py | 3 | ||||
| -rw-r--r-- | mammoth/transforms.py | 22 |
3 files changed, 26 insertions, 3 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 |
