aboutsummaryrefslogtreecommitdiff
path: root/mammoth
diff options
context:
space:
mode:
Diffstat (limited to 'mammoth')
-rw-r--r--mammoth/conversion.py11
-rw-r--r--mammoth/html_generation.py8
-rw-r--r--mammoth/options.py1
3 files changed, 13 insertions, 7 deletions
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 [])