summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2013-12-14 16:50:19 +0000
committerMichael Williamson <mike@zwobble.org>2013-12-14 16:50:19 +0000
commita96ab058644e43bb5fdf32c0b3b238e1a9e1360f (patch)
tree2cf713367b8ef656571da1203f7945b1c37f6924
parent93e945c2ca0b1118c8e119a09988d8b4093625d8 (diff)
Use image alt text if available
-rw-r--r--mammoth/conversion.py10
-rw-r--r--tests/conversion_tests.py7
2 files changed, 14 insertions, 3 deletions
diff --git a/mammoth/conversion.py b/mammoth/conversion.py
index 5580925..635b57c 100644
--- a/mammoth/conversion.py
+++ b/mammoth/conversion.py
@@ -77,10 +77,14 @@ class DocumentConverter(object):
output = io.BytesIO()
base64.encode(image_bytes, output)
encoded_src = output.getvalue().rstrip("\n")
-
- html_generator.self_closing("img", {
+
+ attributes = {
"src": "data:{0};base64,{1}".format(image.content_type, encoded_src)
- })
+ }
+ if image.alt_text:
+ attributes["alt"] = image.alt_text
+
+ html_generator.self_closing("img", attributes)
def _convert_elements_to_html(self, elements, html_generator):
diff --git a/tests/conversion_tests.py b/tests/conversion_tests.py
index 8935bb5..5088288 100644
--- a/tests/conversion_tests.py
+++ b/tests/conversion_tests.py
@@ -124,5 +124,12 @@ def images_are_converted_to_img_tags_with_data_uri():
assert_equal('<img src="data:image/png;base64,YWJj" />', result.value)
+@istest
+def images_have_alt_tags_if_available():
+ image = documents.image(alt_text="It's a hat", content_type="image/png", open=lambda: io.BytesIO("abc"))
+ result = convert_document_element_to_html(image)
+ assert_equal('<img src="data:image/png;base64,YWJj" alt="It\'s a hat" />', result.value)
+
+
def _run_with_text(text):
return documents.run(children=[documents.text(text)])