From 89e0fe2ffb65babcb0a09d384c4dcb3d1c411a2d Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Fri, 18 Jul 2014 15:21:47 +0100 Subject: Add support for optional underline conversion --- mammoth/__init__.py | 4 ++-- mammoth/conversion.py | 9 ++++++--- mammoth/underline.py | 5 +++++ 3 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 mammoth/underline.py (limited to 'mammoth') diff --git a/mammoth/__init__.py b/mammoth/__init__.py index 889cdd5..e70e8ca 100644 --- a/mammoth/__init__.py +++ b/mammoth/__init__.py @@ -1,6 +1,6 @@ -from . import docx, conversion, options, documents, images +from . import docx, conversion, options, documents, images, underline -__all__ = ["convert_to_html", "extract_raw_text", "images"] +__all__ = ["convert_to_html", "extract_raw_text", "images", "underline"] def convert_to_html(fileobj, transform_document=None, **kwargs): diff --git a/mammoth/conversion.py b/mammoth/conversion.py index a9fba51..6f9b7fc 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -4,18 +4,18 @@ from . import documents, results, html_paths, images from .html_generation import HtmlGenerator, satisfy_html_path -def convert_document_element_to_html(element, style_map=None, convert_image=None): +def convert_document_element_to_html(element, style_map=None, convert_image=None, convert_underline=None): if style_map is None: style_map = [] html_generator = HtmlGenerator() - converter = DocumentConverter(style_map, convert_image=convert_image) + converter = DocumentConverter(style_map, convert_image=convert_image, convert_underline=convert_underline) converter.convert_element_to_html(element, html_generator,) html_generator.end_all() return results.Result(html_generator.html_string(), converter.messages) class DocumentConverter(object): - def __init__(self, style_map, convert_image): + def __init__(self, style_map, convert_image, convert_underline): self.messages = [] self._style_map = style_map self._converters = { @@ -30,6 +30,7 @@ class DocumentConverter(object): documents.TableCell: self._convert_table_cell, documents.Image: convert_image or images.inline(self._convert_image), } + self._convert_underline = convert_underline def convert_element_to_html(self, element, html_generator): @@ -55,6 +56,8 @@ class DocumentConverter(object): run_generator.start("strong") if run.is_italic: run_generator.start("em") + if run.is_underline and self._convert_underline is not None: + self._convert_underline(run_generator) self._convert_elements_to_html(run.children, run_generator) run_generator.end_all() html_generator.append(run_generator) diff --git a/mammoth/underline.py b/mammoth/underline.py new file mode 100644 index 0000000..bb7d270 --- /dev/null +++ b/mammoth/underline.py @@ -0,0 +1,5 @@ +def element(name): + def convert_underline(html_generator): + html_generator.start(name) + + return convert_underline -- cgit v1.2.3