summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2016-11-19 20:02:19 +0000
committerMichael Williamson <mike@zwobble.org>2016-11-19 20:02:19 +0000
commitdbc3dac871616c8549cc3ddce01cd0e98f83c1f6 (patch)
tree471ff3ef5ad09bd9b04f24068a842c75cd278bd1
parent6b6163f5aebd604e76add5e383f91453f906d3f7 (diff)
Include embedded style map when explicit style map is set
-rw-r--r--NEWS7
-rw-r--r--README.md7
-rw-r--r--mammoth/__init__.py11
-rw-r--r--mammoth/options.py23
-rw-r--r--tests/mammoth_tests.py16
5 files changed, 54 insertions, 10 deletions
diff --git a/NEWS b/NEWS
index 9105d08..fb917a0 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,10 @@
+# 1.3.0
+
+* Add include_embedded_style_map argument to allow embedded style maps to be
+ disabled.
+
+* Include embedded styles when explicit style map is passed.
+
# 1.2.2
* Ignore bold, italic, underline and strikethrough elements that have a value of
diff --git a/README.md b/README.md
index 428cf8a..b0e83e5 100644
--- a/README.md
+++ b/README.md
@@ -270,6 +270,11 @@ Converts the source document to HTML.
* `style_map`: a string to specify the mapping of Word styles to HTML.
See the section "Writing style maps" for a description of the syntax.
+* `include_embedded_style_map`: by default,
+ if the document contains an embedded style, then it is combined with the default style map.
+ To ignore any embedded style maps,
+ pass `include_embedded_style_map=False`.
+
* `include_default_style_map`: by default, the style map passed in `style_map` is combined with the default style map.
To stop using the default style map altogether,
pass `include_default_style_map=False`.
@@ -316,7 +321,7 @@ Each paragraph is followed by two newlines.
Embeds the style map `style_map` into `fileobj`.
When Mammoth reads a file object,
-it will use the embedded style if no explicit style map is provided.
+it will use the embedded style map.
* `fileobj`: a file-like object containing the source document.
Files should be opened for reading and writing in binary mode.
diff --git a/mammoth/__init__.py b/mammoth/__init__.py
index 7735fcf..fe480ec 100644
--- a/mammoth/__init__.py
+++ b/mammoth/__init__.py
@@ -5,6 +5,9 @@ from .docx.style_map import write_style_map, read_style_map
__all__ = ["convert_to_html", "extract_raw_text", "images", "underline"]
+_undefined = object()
+
+
def convert_to_html(*args, **kwargs):
return convert(*args, output_format="html", **kwargs)
@@ -13,11 +16,13 @@ def convert_to_markdown(*args, **kwargs):
return convert(*args, output_format="markdown", **kwargs)
-def convert(fileobj, transform_document=None, id_prefix=None, **kwargs):
+def convert(fileobj, transform_document=None, id_prefix=None, include_embedded_style_map=_undefined, **kwargs):
+ if include_embedded_style_map is _undefined:
+ include_embedded_style_map = True
if transform_document is None:
transform_document = lambda x: x
- if kwargs.get("style_map") is None:
- kwargs["style_map"] = read_style_map(fileobj)
+ if include_embedded_style_map:
+ kwargs["embedded_style_map"] = read_style_map(fileobj)
return options.read_options(kwargs).bind(lambda convert_options:
docx.read(fileobj).map(transform_document).bind(lambda document:
conversion.convert_document_element_to_html(
diff --git a/mammoth/options.py b/mammoth/options.py
index ddac3de..d33b7ba 100644
--- a/mammoth/options.py
+++ b/mammoth/options.py
@@ -2,13 +2,24 @@ from . import style_reader, lists, results
def read_options(options):
- custom_style_map_result = _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)
+ custom_style_map_text = options.pop("style_map", "") or ""
+ embedded_style_map_text = options.pop("embedded_style_map", "") or ""
+ include_default_style_map = options.pop("include_default_style_map", True)
+
+ read_style_map_result = results.combine([
+ _read_style_map(custom_style_map_text),
+ _read_style_map(embedded_style_map_text),
+ ])
+
+ custom_style_map, embedded_style_map = read_style_map_result.value
+ style_map = custom_style_map + embedded_style_map
- options["style_map"] = custom_style_map_result.value + \
- (_default_style_map if include_default_style_map else [])
- return custom_style_map_result.map(lambda _: options)
+ if include_default_style_map:
+ style_map += _default_style_map
+
+ options["ignore_empty_paragraphs"] = options.get("ignore_empty_paragraphs", True)
+ options["style_map"] = style_map
+ return read_style_map_result.map(lambda _: options)
def _read_style_map(style_text):
diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py
index 4a524e8..322ebf7 100644
--- a/tests/mammoth_tests.py
+++ b/tests/mammoth_tests.py
@@ -64,6 +64,22 @@ def explicit_style_map_takes_precedence_over_embedded_style_map():
@istest
+def explicit_style_map_is_combined_with_embedded_style_map():
+ with open(test_path("embedded-style-map.docx"), "rb") as fileobj:
+ result = mammoth.convert_to_html(fileobj=fileobj, style_map="r => strong")
+ assert_equal("<h1><strong>Walking on imported air</strong></h1>", result.value)
+ assert_equal([], result.messages)
+
+
+@istest
+def embedded_style_maps_can_be_disabled():
+ with open(test_path("embedded-style-map.docx"), "rb") as fileobj:
+ result = mammoth.convert_to_html(fileobj=fileobj, include_embedded_style_map=False)
+ assert_equal("<p>Walking on imported air</p>", result.value)
+ assert_equal([], result.messages)
+
+
+@istest
def embedded_style_map_can_be_written_and_then_read():
with _copy_of_test_data("single-paragraph.docx") as fileobj:
mammoth.embed_style_map(fileobj, "p => h1")