Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pydocx/export/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ def get_heading_tag(self, paragraph):
heading_style.name.lower(),
self.default_heading_level,
)
if paragraph.bookmark_name:
return HtmlTag(tag, id=paragraph.bookmark_name)
return HtmlTag(tag)

def export_paragraph(self, paragraph):
Expand Down Expand Up @@ -507,7 +509,10 @@ def get_hyperlink_tag(self, target_uri):

def export_hyperlink(self, hyperlink):
results = super(PyDocXHTMLExporter, self).export_hyperlink(hyperlink)
tag = self.get_hyperlink_tag(target_uri=hyperlink.target_uri)
if not hyperlink.target_uri and hyperlink.anchor:
tag = self.get_hyperlink_tag(target_uri='#' + hyperlink.anchor)
else:
tag = self.get_hyperlink_tag(target_uri=hyperlink.target_uri)
if tag:
results = tag.apply(results, allow_empty=False)

Expand Down
2 changes: 2 additions & 0 deletions pydocx/openxml/wordprocessing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# coding: utf-8
from pydocx.openxml.wordprocessing.abstract_num import AbstractNum
from pydocx.openxml.wordprocessing.body import Body
from pydocx.openxml.wordprocessing.bookmark import Bookmark
from pydocx.openxml.wordprocessing.br import Break
from pydocx.openxml.wordprocessing.deleted_run import DeletedRun
from pydocx.openxml.wordprocessing.deleted_text import DeletedText
Expand Down Expand Up @@ -47,6 +48,7 @@
__all__ = [
'AbstractNum',
'Body',
'Bookmark',
'Break',
'DeletedRun',
'DeletedText',
Expand Down
14 changes: 14 additions & 0 deletions pydocx/openxml/wordprocessing/bookmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# coding: utf-8
from __future__ import (
absolute_import,
print_function,
unicode_literals,
)

from pydocx.models import XmlModel, XmlAttribute


class Bookmark(XmlModel):
XML_TAG = 'bookmarkStart'

name = XmlAttribute(name='name')
8 changes: 8 additions & 0 deletions pydocx/openxml/wordprocessing/paragraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from pydocx.openxml.wordprocessing.deleted_run import DeletedRun
from pydocx.openxml.wordprocessing.sdt_run import SdtRun
from pydocx.openxml.wordprocessing.simple_field import SimpleField
from pydocx.openxml.wordprocessing.bookmark import Bookmark


class Paragraph(XmlModel):
Expand All @@ -31,6 +32,7 @@ class Paragraph(XmlModel):
DeletedRun,
SdtRun,
SimpleField,
Bookmark
)

def __init__(self, **kwargs):
Expand Down Expand Up @@ -121,6 +123,12 @@ def runs(self):
if isinstance(p_child, Run):
yield p_child

@property
def bookmark_name(self):
for p_child in self.children:
if isinstance(p_child, Bookmark):
return p_child.name

def get_text(self, tab_char=None):
'''
Return a string of all of the contained Text nodes concatenated
Expand Down
27 changes: 27 additions & 0 deletions tests/export/html/test_heading.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,3 +744,30 @@ def test_single_lvl_list_has_precedence_over_headings(self):
</ol>
'''
self.assert_document_generates_html(document, expected_html)

def test_heading_with_bookmark(self):
document_xml = '''
<p>
<pPr>
<pStyle val="heading1"/>
</pPr>
<bookmarkStart name="testing"/>
<bookmarkEnd/>
<r>
<t>aaa</t>
</r>
</p>
'''

style_xml = '''
<style styleId="heading1" type="paragraph">
<name val="Heading 1"/>
</style>
'''

document = WordprocessingDocumentFactory()
document.add(StyleDefinitionsPart, style_xml)
document.add(MainDocumentPart, document_xml)

expected_html = '<h1 id="testing">aaa</h1>'
self.assert_document_generates_html(document, expected_html)
18 changes: 18 additions & 0 deletions tests/export/html/test_hyperlink.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,21 @@ def test_with_anchor(self):

expected_html = '<p><a href="http://google.com#testing">link</a>.</p>'
self.assert_document_generates_html(document, expected_html)

def test_internal_link(self):
document_xml = '''
<p>
<hyperlink anchor="testing">
<r>
<t>link</t>
</r>
</hyperlink>
</p>
'''

document = WordprocessingDocumentFactory()

document.add(MainDocumentPart, document_xml)

expected_html = '<p><a href="#testing">link</a></p>'
self.assert_document_generates_html(document, expected_html)