diff options
| -rw-r--r-- | NEWS | 4 | ||||
| -rw-r--r-- | README.md | 3 | ||||
| -rw-r--r-- | mammoth/conversion.py | 11 | ||||
| -rw-r--r-- | mammoth/html_generation.py | 8 | ||||
| -rw-r--r-- | mammoth/options.py | 1 | ||||
| -rw-r--r-- | tests/mammoth_tests.py | 16 | ||||
| -rw-r--r-- | tests/test-data/empty.docx | bin | 0 -> 3306 bytes |
7 files changed, 36 insertions, 7 deletions
@@ -1,3 +1,7 @@ +# 0.3.14 + +* Add ignore_empty_paragraphs option, which defaults to True. + # 0.3.13 * Always use forward slashes in ZIP paths. This should fix image handling on @@ -197,6 +197,9 @@ Converts the source document to HTML. * `convert_underline`: by default, the underlining of any text is ignored. Set this argument to [`mammoth.underline.element(name)`](#underline) to override the default behaviour. + +* `ignore_empty_paragraphs`: by default, empty paragraphs are ignored. + Set this option to `False` to preserve empty paragraphs in the output. * Returns a result with the following properties: diff --git a/mammoth/conversion.py b/mammoth/conversion.py index e351395..1569849 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -14,7 +14,8 @@ def convert_document_element_to_html(element, convert_image=None, convert_underline=None, id_prefix=None, - output_format=None): + output_format=None, + ignore_empty_paragraphs=True): if style_map is None: style_map = [] @@ -26,17 +27,19 @@ def convert_document_element_to_html(element, converter = DocumentConverter(style_map, convert_image=convert_image, convert_underline=convert_underline, - id_prefix=id_prefix) + id_prefix=id_prefix, + ignore_empty_paragraphs=ignore_empty_paragraphs) converter.convert_element_to_html(element, html_generator,) html_generator.end_all() return results.Result(html_generator.as_string(), converter.messages) class DocumentConverter(object): - def __init__(self, style_map, convert_image, convert_underline, id_prefix): + def __init__(self, style_map, convert_image, convert_underline, id_prefix, ignore_empty_paragraphs): self.messages = [] self._style_map = style_map self._id_prefix = id_prefix + self._ignore_empty_paragraphs = ignore_empty_paragraphs self._note_references = [] self._converters = { documents.Document: self._convert_document, @@ -76,6 +79,8 @@ class DocumentConverter(object): def _convert_paragraph(self, paragraph, html_generator): html_path = self._find_html_path_for_paragraph(paragraph) satisfy_html_path(html_generator, html_path) + if not self._ignore_empty_paragraphs: + html_generator.write_all() self._convert_elements_to_html(paragraph.children, html_generator) diff --git a/mammoth/html_generation.py b/mammoth/html_generation.py index a0c35b7..c7f4f38 100644 --- a/mammoth/html_generation.py +++ b/mammoth/html_generation.py @@ -9,14 +9,14 @@ class HtmlGenerator(object): def text(self, text): if text: - self._write_all() + self.write_all() self._writer.text(text) def start(self, name, attributes=None, always_write=False): self._stack.append(_Element(name, attributes)) if always_write: - self._write_all() + self.write_all() def end(self): element = self._stack.pop() @@ -30,7 +30,7 @@ class HtmlGenerator(object): def self_closing(self, name, attributes=None): self._writer.self_closing(name, attributes) - def _write_all(self): + def write_all(self): for element in self._stack: if not element.written: element.written = True @@ -42,7 +42,7 @@ class HtmlGenerator(object): def append(self, other): other_string = other.as_string() if other_string: - self._write_all() + self.write_all() self._writer.append(other_string) def as_string(self): diff --git a/mammoth/options.py b/mammoth/options.py index 3f47562..ad20d02 100644 --- a/mammoth/options.py +++ b/mammoth/options.py @@ -4,6 +4,7 @@ from . import style_reader, lists def read_options(options): custom_style_map = _read_style_map(options.get("style_map") or "") include_default_style_map = options.get("include_default_style_map", True) + options["ignore_empty_paragraphs"] = options.get("ignore_empty_paragraphs", True) options["style_map"] = custom_style_map + \ (_default_style_map if include_default_style_map else []) diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py index ce76221..d9f1093 100644 --- a/tests/mammoth_tests.py +++ b/tests/mammoth_tests.py @@ -26,6 +26,22 @@ def can_read_xml_files_with_utf8_bom(): @istest +def empty_paragraphs_are_ignored_by_default(): + with open(test_path("empty.docx"), "rb") as fileobj: + result = mammoth.convert_to_html(fileobj=fileobj) + assert_equal("", result.value) + assert_equal([], result.messages) + + +@istest +def empty_paragraphs_are_preserved_if_ignore_empty_paragraphs_is_false(): + with open(test_path("empty.docx"), "rb") as fileobj: + result = mammoth.convert_to_html(fileobj=fileobj, ignore_empty_paragraphs=False) + assert_equal("<p></p>", result.value) + assert_equal([], result.messages) + + +@istest def inline_images_are_included_in_output(): with open(test_path("tiny-picture.docx"), "rb") as fileobj: result = mammoth.convert_to_html(fileobj=fileobj) diff --git a/tests/test-data/empty.docx b/tests/test-data/empty.docx Binary files differnew file mode 100644 index 0000000..e10b869 --- /dev/null +++ b/tests/test-data/empty.docx |
