From 7ba9cc0ecdca695fec3cb90c98dd383e0edb2ba9 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Wed, 11 Feb 2015 20:22:54 +0000 Subject: Read anchor from hyperlinks --- mammoth/documents.py | 8 ++++++-- mammoth/docx/document_xml.py | 11 +++++++---- tests/docx/document_xml_tests.py | 13 +++++++++++-- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/mammoth/documents.py b/mammoth/documents.py index 1adcc0d..9028b03 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -8,7 +8,7 @@ Run = dodge.data_class("Run", [ "is_underline", "vertical_alignment", ]) Text = dodge.data_class("Text", ["value"]) -Hyperlink = dodge.data_class("Hyperlink", ["href", "children"]) +Hyperlink = dodge.data_class("Hyperlink", ["href", "anchor", "children"]) Table = dodge.data_class("Table", ["children"]) TableRow = dodge.data_class("TableRow", ["children"]) TableCell = dodge.data_class("TableCell", ["children"]) @@ -55,7 +55,11 @@ def tab(): image = Image -hyperlink = Hyperlink + +def hyperlink(children, href=None, anchor=None): + return Hyperlink(href, anchor, children) + + table = Table table_row = TableRow table_cell = TableCell diff --git a/mammoth/docx/document_xml.py b/mammoth/docx/document_xml.py index 2004948..56298f3 100644 --- a/mammoth/docx/document_xml.py +++ b/mammoth/docx/document_xml.py @@ -176,12 +176,15 @@ def _create_reader(numbering, content_types, relationships, styles, note_element @handler("w:hyperlink") def hyperlink(element): relationship_id = element.attributes.get("r:id") + anchor = element.attributes.get("w:anchor") children_result = _read_xml_elements(element.children) - if relationship_id is None: - return children_result - else: + if relationship_id is not None: href = relationships[relationship_id].target - return children_result.map(lambda children: documents.hyperlink(href, children)) + 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)) + else: + return children_result @handler("w:br") def br(element): diff --git a/tests/docx/document_xml_tests.py b/tests/docx/document_xml_tests.py index ffa5fbe..12fcef2 100644 --- a/tests/docx/document_xml_tests.py +++ b/tests/docx/document_xml_tests.py @@ -218,12 +218,21 @@ class ReadXmlElementTests(object): run_element = xml_element("w:r") element = xml_element("w:hyperlink", {"r:id": "r42"}, [run_element]) assert_equal( - documents.hyperlink("http://example.com", [documents.run([])]), + documents.hyperlink(href="http://example.com", children=[documents.run([])]), _read_and_get_document_xml_element(element, relationships=relationships) ) @istest - def hyperlink_is_ignored_if_it_does_not_have_a_relationship_id(self): + def hyperlink_is_read_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( + documents.hyperlink(anchor="start", children=[documents.run([])]), + _read_and_get_document_xml_element(element) + ) + + @istest + def hyperlink_is_ignored_if_it_does_not_have_a_relationship_id_nor_anchor(self): run_element = xml_element("w:r") element = xml_element("w:hyperlink", {}, [run_element]) assert_equal( -- cgit v1.2.3