diff options
| -rw-r--r-- | NEWS | 4 | ||||
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | mammoth/conversion.py | 12 | ||||
| -rw-r--r-- | mammoth/images.py | 12 |
4 files changed, 19 insertions, 11 deletions
@@ -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. @@ -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) + } |
