diff options
| author | Michael Williamson <mike@zwobble.org> | 2017-05-18 19:52:04 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2017-05-18 19:53:14 +0100 |
| commit | 85f99611c84cf0853200fb358f135160c063b690 (patch) | |
| tree | c266a0c36e41653b43f10b00054eb5d27924b0b4 | |
| parent | 54980f619dd055b5797db131f47a3805f4fe4050 (diff) | |
Add anchor on hyperlinks as fragment if present
| -rw-r--r-- | NEWS | 2 | ||||
| -rw-r--r-- | mammoth/compat.py | 9 | ||||
| -rw-r--r-- | mammoth/docx/body_xml.py | 4 | ||||
| -rw-r--r-- | tests/docx/body_xml_tests.py | 16 |
4 files changed, 29 insertions, 2 deletions
@@ -2,6 +2,8 @@ * Extract the default data URI image converter to the images module. +* Add anchor on hyperlinks as fragment if present. + # 1.3.5 * Handle complex fields that do not have a "separate" fldChar. diff --git a/mammoth/compat.py b/mammoth/compat.py new file mode 100644 index 0000000..5fb9acf --- /dev/null +++ b/mammoth/compat.py @@ -0,0 +1,9 @@ +import sys + +if sys.version_info < (3, ): + from urlparse import urldefrag +else: + from urllib.parse import urldefrag + + +__all__ = ["urldefrag"] diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index b8d94cf..0724ac8 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -4,6 +4,7 @@ import re from .. import documents from .. import results from .. import lists +from ..compat import urldefrag from . import complex_fields from .xmlparser import node_types, XmlElement from .styles_xml import Styles @@ -294,6 +295,9 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f children_result = _read_xml_elements(element.children) if relationship_id is not None: href = relationships[relationship_id].target + if anchor is not None: + href = urldefrag(href)[0] + "#" + anchor + return children_result.map(lambda children: documents.hyperlink(href=href, children=children)) elif anchor is not None: return children_result.map(lambda children: documents.hyperlink(anchor=anchor, children=children)) diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py index d112349..1277238 100644 --- a/tests/docx/body_xml_tests.py +++ b/tests/docx/body_xml_tests.py @@ -600,7 +600,7 @@ def _assert_children_are_converted_normally(tag_name): @istest class HyperlinkTests(object): @istest - def hyperlink_is_read_if_it_has_a_relationship_id(self): + def hyperlink_is_read_as_external_hyperlink_if_it_has_a_relationship_id(self): relationships = Relationships({ "r42": Relationship(target="http://example.com") }) @@ -612,7 +612,19 @@ class HyperlinkTests(object): ) @istest - def hyperlink_is_read_if_it_has_an_anchor_attribute(self): + def hyperlink_is_read_as_external_hyperlink_if_it_has_a_relationship_id_and_an_anchor(self): + relationships = Relationships({ + "r42": Relationship(target="http://example.com/") + }) + run_element = xml_element("w:r") + element = xml_element("w:hyperlink", {"r:id": "r42", "w:anchor": "fragment"}, [run_element]) + assert_equal( + documents.hyperlink(href="http://example.com/#fragment", children=[documents.run([])]), + _read_and_get_document_xml_element(element, relationships=relationships) + ) + + @istest + def hyperlink_is_read_as_internal_hyperlink_if_it_has_an_anchor_attribute(self): run_element = xml_element("w:r") element = xml_element("w:hyperlink", {"w:anchor": "start"}, [run_element]) assert_equal( |
