aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2017-01-23 20:17:18 +0000
committerMichael Williamson <mike@zwobble.org>2017-01-23 20:17:34 +0000
commitde55a714711a27207367bc8c5c8370549a9bd219 (patch)
tree61765502ea99e740c2a6d1f7ab1391794e5f0e9d
parentcd52af70959fa7a549f2283a36408e5144c19112 (diff)
Handle target URIs relative to the base
-rw-r--r--mammoth/docx/body_xml.py3
-rw-r--r--mammoth/zips.py7
-rw-r--r--tests/mammoth_tests.py10
-rw-r--r--tests/test-data/tiny-picture-target-base-relative.docxbin0 -> 10881 bytes
-rw-r--r--tests/zips_tests.py19
5 files changed, 37 insertions, 2 deletions
diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py
index 1fcd567..433d5a2 100644
--- a/mammoth/docx/body_xml.py
+++ b/mammoth/docx/body_xml.py
@@ -3,6 +3,7 @@ import contextlib
from .. import documents
from .. import results
from .. import lists
+from ..zips import uri_to_zip_entry_name
from .xmlparser import node_types, XmlElement
from .styles_xml import Styles
@@ -317,7 +318,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f
return _find_linked_image(link_relationship_id)
def _find_embedded_image(relationship_id):
- image_path = "word/" + relationships[relationship_id].target.lstrip("/")
+ image_path = uri_to_zip_entry_name("word", relationships[relationship_id].target)
def open_image():
image_file = docx_file.open(image_path)
diff --git a/mammoth/zips.py b/mammoth/zips.py
index 432be5d..8e09924 100644
--- a/mammoth/zips.py
+++ b/mammoth/zips.py
@@ -54,3 +54,10 @@ def update_zip(fileobj, files):
fileobj.seek(0)
destination_fileobj.seek(0)
shutil.copyfileobj(destination_fileobj, fileobj)
+
+
+def uri_to_zip_entry_name(base, uri):
+ if uri.startswith("/"):
+ return uri[1:]
+ else:
+ return base + "/" + uri
diff --git a/tests/mammoth_tests.py b/tests/mammoth_tests.py
index 322ebf7..8789855 100644
--- a/tests/mammoth_tests.py
+++ b/tests/mammoth_tests.py
@@ -108,7 +108,7 @@ p => h1"""
@istest
-def inline_images_are_included_in_output():
+def inline_images_referenced_by_path_relative_to_part_are_included_in_output():
with open(test_path("tiny-picture.docx"), "rb") as fileobj:
result = mammoth.convert_to_html(fileobj=fileobj)
assert_equal("""<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>""", result.value)
@@ -116,6 +116,14 @@ def inline_images_are_included_in_output():
@istest
+def inline_images_referenced_by_path_relative_to_base_are_included_in_output():
+ with open(test_path("tiny-picture-target-base-relative.docx"), "rb") as fileobj:
+ result = mammoth.convert_to_html(fileobj=fileobj)
+ assert_equal("""<p><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAIAAAACUFjqAAAAAXNSR0IArs4c6QAAAAlwSFlzAAAOvgAADr4B6kKxwAAAABNJREFUKFNj/M+ADzDhlWUYqdIAQSwBE8U+X40AAAAASUVORK5CYII=" /></p>""", result.value)
+ assert_equal([], result.messages)
+
+
+@istest
def images_stored_outside_of_document_are_included_in_output():
with open(test_path("external-picture.docx"), "rb") as fileobj:
result = mammoth.convert_to_html(fileobj=fileobj)
diff --git a/tests/test-data/tiny-picture-target-base-relative.docx b/tests/test-data/tiny-picture-target-base-relative.docx
new file mode 100644
index 0000000..1d61988
--- /dev/null
+++ b/tests/test-data/tiny-picture-target-base-relative.docx
Binary files differ
diff --git a/tests/zips_tests.py b/tests/zips_tests.py
new file mode 100644
index 0000000..bef9895
--- /dev/null
+++ b/tests/zips_tests.py
@@ -0,0 +1,19 @@
+from nose.tools import istest, assert_equal
+
+from mammoth.zips import uri_to_zip_entry_name
+
+
+@istest
+def when_path_does_not_have_leading_slash_then_path_is_resolved_relative_to_base():
+ assert_equal(
+ "one/two/three/four",
+ uri_to_zip_entry_name("one/two", "three/four"),
+ )
+
+
+@istest
+def when_path_has_leading_slash_then_base_is_ignored():
+ assert_equal(
+ "three/four",
+ uri_to_zip_entry_name("one/two", "/three/four"),
+ )