From 74c9f604aa9118ef3693a00714baa4e3f24e21d5 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 17 May 2015 16:32:49 +0100 Subject: Add ignore_empty_paragraphs option --- NEWS | 4 ++++ README.md | 3 +++ mammoth/conversion.py | 11 ++++++++--- mammoth/html_generation.py | 8 ++++---- mammoth/options.py | 1 + tests/mammoth_tests.py | 16 ++++++++++++++++ tests/test-data/empty.docx | Bin 0 -> 3306 bytes 7 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 tests/test-data/empty.docx diff --git a/NEWS b/NEWS index 6638eae..cb06b60 100644 --- a/NEWS +++ b/NEWS @@ -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 diff --git a/README.md b/README.md index 9d08a38..3e9fb8f 100644 --- a/README.md +++ b/README.md @@ -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 @@ -25,6 +25,22 @@ def can_read_xml_files_with_utf8_bom(): assert_equal([], result.messages) +@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("

", result.value) + assert_equal([], result.messages) + + @istest def inline_images_are_included_in_output(): with open(test_path("tiny-picture.docx"), "rb") as fileobj: diff --git a/tests/test-data/empty.docx b/tests/test-data/empty.docx new file mode 100644 index 0000000..e10b869 Binary files /dev/null and b/tests/test-data/empty.docx differ -- cgit v1.2.3