diff options
| author | Michael Williamson <mike@zwobble.org> | 2015-02-11 20:30:33 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2015-02-11 20:30:33 +0000 |
| commit | 123907f68de3b4d802bd305f37b8651551be3f71 (patch) | |
| tree | 5c801ce84daa9004578bb65c0812210531ecf085 | |
| parent | 519dfddb18eea15c26d0d8d8da68e1cebd865c39 (diff) | |
Convert hyperlinks with anchors to HTML
| -rw-r--r-- | mammoth/conversion.py | 6 | ||||
| -rw-r--r-- | tests/conversion_tests.py | 11 |
2 files changed, 15 insertions, 2 deletions
diff --git a/mammoth/conversion.py b/mammoth/conversion.py index 2932351..c17ef9f 100644 --- a/mammoth/conversion.py +++ b/mammoth/conversion.py @@ -103,7 +103,11 @@ class DocumentConverter(object): def _convert_hyperlink(self, hyperlink, html_generator): - html_generator.start("a", {"href": hyperlink.href}) + if hyperlink.anchor is None: + href = hyperlink.href + else: + href = "#{0}-{1}".format(self._id_prefix, hyperlink.anchor) + html_generator.start("a", {"href": href}) self._convert_elements_to_html(hyperlink.children, html_generator) html_generator.end() diff --git a/tests/conversion_tests.py b/tests/conversion_tests.py index 78fbce5..ced1d9e 100644 --- a/tests/conversion_tests.py +++ b/tests/conversion_tests.py @@ -201,7 +201,7 @@ def runs_are_converted_by_satisfying_matching_paths(): @istest -def docx_hyperlink_is_converted_to_anchor_tag(): +def docx_hyperlink_with_href_is_converted_to_anchor_tag(): result = convert_document_element_to_html( documents.hyperlink(href="http://example.com", children=[documents.Text("Hello")]), ) @@ -209,6 +209,15 @@ def docx_hyperlink_is_converted_to_anchor_tag(): @istest +def docx_hyperlink_with_internal_anchor_reference_is_converted_to_anchor_tag(): + result = convert_document_element_to_html( + documents.hyperlink(anchor="start", children=[documents.Text("Hello")]), + id_prefix="doc-42", + ) + assert_equal('<a href="#doc-42-start">Hello</a>', result.value) + + +@istest def docx_tab_is_converted_to_tab_in_html(): result = convert_document_element_to_html(documents.tab()) assert_equal('\t', result.value) |
