diff options
| author | Michael Williamson <mike@zwobble.org> | 2013-12-01 21:33:53 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2013-12-01 21:33:53 +0000 |
| commit | 8717dd906667c5bf7a6c11f202ab475b14456990 (patch) | |
| tree | 572c51bb63c80db953445ecbc31d45405020a550 | |
| parent | 4fd08468ad3823d09041a0ffa33aba2dd509e192 (diff) | |
Add support for reading hyperlinks
| -rw-r--r-- | mammoth/documents.py | 3 | ||||
| -rw-r--r-- | mammoth/docx/document_xml.py | 14 | ||||
| -rw-r--r-- | tests/docx/document_xml_tests.py | 22 |
3 files changed, 37 insertions, 2 deletions
diff --git a/mammoth/documents.py b/mammoth/documents.py index 851755e..6ce4582 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -4,7 +4,7 @@ Document = collections.namedtuple("Document", ["children"]) Paragraph = collections.namedtuple("Paragraph", ["children", "style_name", "numbering"]) Run = collections.namedtuple("Run", ["children", "style_name", "is_bold", "is_italic"]) Text = collections.namedtuple("Text", ["value"]) - +Hyperlink = collections.namedtuple("Hyperlink", ["href", "children"]) class Tab(object): pass @@ -34,3 +34,4 @@ def tab(): image = Image +hyperlink = Hyperlink diff --git a/mammoth/docx/document_xml.py b/mammoth/docx/document_xml.py index 9c895d4..fad4f87 100644 --- a/mammoth/docx/document_xml.py +++ b/mammoth/docx/document_xml.py @@ -96,12 +96,24 @@ def _create_reader(numbering, content_types, relationships, docx_file): @handler("w:tab") def tab(element): return results.success(documents.tab()) - + + @handler("w:ins") def ins(element): return _read_xml_elements(element.children) + @handler("w:hyperlink") + def hyperlink(element): + relationship_id = element.attributes.get("r:id") + children_result = _read_xml_elements(element.children) + if relationship_id is None: + return children_result + else: + href = relationships[relationship_id].target + return children_result.map(lambda children: documents.hyperlink(href, children)) + + @handler("w:drawing") def drawing(element): return _read_xml_elements(element.children) diff --git a/tests/docx/document_xml_tests.py b/tests/docx/document_xml_tests.py index 9badbac..30580d0 100644 --- a/tests/docx/document_xml_tests.py +++ b/tests/docx/document_xml_tests.py @@ -130,6 +130,28 @@ class ReadXmlElementTests(object): ) @istest + def hyperlink_is_read_if_it_has_a_relationship_id(self): + relationships = Relationships({ + "r42": Relationship(target="http://example.com") + }) + 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([])]), + _read_and_get_document_xml_element(element, relationships=relationships) + ) + + @istest + def hyperlink_is_ignored_if_it_does_not_have_a_relationship_id(self): + run_element = xml_element("w:r") + element = xml_element("w:hyperlink", {}, [run_element]) + assert_equal( + [documents.run([])], + _read_and_get_document_xml_element(element) + ) + + + @istest @funk.with_context def can_read_inline_pictures(self, context): drawing_element = _create_inline_image( |
