summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2017-04-22 21:04:25 +0100
committerMichael Williamson <mike@zwobble.org>2017-04-22 21:05:46 +0100
commitc1eeb07720c452209d8c35b38aec9ae2bd76ca82 (patch)
treeeb0c7ca546248233ca12446edaba07a31dd4760b
parent324ec7fd20824b0685bc0626f718a26e83dac6c3 (diff)
Extract data URI image converter to images module
-rw-r--r--NEWS4
-rw-r--r--README.md2
-rw-r--r--mammoth/conversion.py12
-rw-r--r--mammoth/images.py12
4 files changed, 19 insertions, 11 deletions
diff --git a/NEWS b/NEWS
index 4d24c57..da03d08 100644
--- a/NEWS
+++ b/NEWS
@@ -1,3 +1,7 @@
+# 1.3.6
+
+* Extract the default data URI image converter to the images module.
+
# 1.3.5
* Handle complex fields that do not have a "separate" fldChar.
diff --git a/README.md b/README.md
index 1e18119..6c074f7 100644
--- a/README.md
+++ b/README.md
@@ -374,6 +374,8 @@ def convert_image(image):
mammoth.images.img_element(convert_image)
```
+`mammoth.images.data_uri` is the default image converter.
+
### Document transforms
**The API for document transforms should be considered unstable,
diff --git a/mammoth/conversion.py b/mammoth/conversion.py
index bae503a..489c74a 100644
--- a/mammoth/conversion.py
+++ b/mammoth/conversion.py
@@ -3,7 +3,6 @@
from __future__ import unicode_literals
from functools import partial
-import base64
from . import documents, results, html_paths, images, writers, html, lists
from .docx.files import InvalidFileReferenceError
@@ -23,7 +22,7 @@ def convert_document_element_to_html(element,
id_prefix = ""
if convert_image is None:
- convert_image = images.img_element(_generate_image_attributes)
+ convert_image = images.data_uri
if isinstance(element, documents.Document):
comments = dict(
@@ -48,15 +47,6 @@ def convert_document_element_to_html(element,
writer = writers.writer(output_format)
html.write(writer, html.collapse(html.strip_empty(nodes)))
return results.Result(writer.as_string(), messages)
-
-
-def _generate_image_attributes(image):
- with image.open() as image_bytes:
- encoded_src = base64.b64encode(image_bytes.read()).decode("ascii")
-
- return {
- "src": "data:{0};base64,{1}".format(image.content_type, encoded_src)
- }
class _DocumentConverter(documents.ElementVisitor):
diff --git a/mammoth/images.py b/mammoth/images.py
index 297b0a2..9dba353 100644
--- a/mammoth/images.py
+++ b/mammoth/images.py
@@ -1,3 +1,5 @@
+import base64
+
from . import html
@@ -13,3 +15,13 @@ def img_element(func):
# Undocumented, but retained for backwards-compatibility with 0.3.x
inline = img_element
+
+
+@img_element
+def data_uri(image):
+ with image.open() as image_bytes:
+ encoded_src = base64.b64encode(image_bytes.read()).decode("ascii")
+
+ return {
+ "src": "data:{0};base64,{1}".format(image.content_type, encoded_src)
+ }