Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Strip full line bold/italics in hyperlinks #9

Merged
merged 5 commits into from

2 participants

@jlward
Owner

No description provided.

@jlward jlward was assigned
@winhamwr winhamwr commented on the diff
docx2html/tests/test_xml.py
((13 lines not shown))
))
xml = DOCUMENT_XML_TEMPLATE % {
'body': table,
}
return etree.fromstring(xml)
+
+
+class HyperlinkTestCase(_TranslationTestCase):
+ relationship_dict = {
+ 'rId0': 'www.google.com',
+ }
+
+ expected_output = '''
+ <html>
+ <p><a href="www.google.com">link</a></p>
+ </html>
+ '''
+
+ def get_xml(self):
@winhamwr Owner

I would prefer two tests. One for the vanilla route and one that makes it explicitly that we're stripping bold.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jlward jlward merged commit 56d2a64 into master
@jlward jlward referenced this pull request from a commit
@jlward jlward refs #9: added a vanilla test case f1fbee6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
3  docx2html/core.py
@@ -1112,7 +1112,8 @@ def handle_t_tag(
# The relationship_id is the href
if hyperlink_id in meta_data.relationship_dict:
href = meta_data.relationship_dict[hyperlink_id]
- text = '<a href="%s">%s</a>' % (href, text)
+ # Do not do any styling on hyperlinks
+ return '<a href="%s">%s</a>' % (href, text)
# Wrap the text with any modifiers it might have (bold, italics or
# underline)
el_is_bold = not remove_bold and (
View
23 docx2html/tests/__init__.py
@@ -17,20 +17,31 @@
<w:t>%(text)s</w:t>
"""
+
+DOCUMENT_R_TEMPLATE = """
+<w:r>
+ <w:rPr>
+ %(bold)s
+ </w:rPr>
+ %(text)s
+</w:r>
+"""
+
DOCUMENT_P_TEMPLATE = """
<w:p>
<w:pPr>
<w:pStyle w:val="style0"/>
</w:pPr>
- <w:r>
- <w:rPr>
- %(bold)s
- </w:rPr>
- %(text)s
- </w:r>
+ %(r_tags)s
</w:p>
""".strip()
+DOCUMENT_HYPERLINK_TEMPLATE = """
+<w:hyperlink r:id="%(r_id)s">
+ %(r_tag)s
+</w:hyperlink>
+"""
+
DOCUMENT_LI_TEMPLATE = """
<w:p>
<w:pPr>
View
103 docx2html/tests/test_xml.py
@@ -12,18 +12,20 @@
is_last_li,
)
from docx2html.tests import (
- assert_html_equal,
- _TranslationTestCase,
DOCUMENT_DRAWING_TEMPLATE,
+ DOCUMENT_HYPERLINK_TEMPLATE,
DOCUMENT_LI_TEMPLATE,
- DOCUMENT_PICT_TEMPLATE,
DOCUMENT_PICT_NO_IMAGEID_TEMPLATE,
+ DOCUMENT_PICT_TEMPLATE,
DOCUMENT_P_TEMPLATE,
- DOCUMENT_T_TEMPLATE,
+ DOCUMENT_R_TEMPLATE,
DOCUMENT_TBL_TEMPLATE,
DOCUMENT_TC_TEMPLATE,
DOCUMENT_TR_TEMPLATE,
+ DOCUMENT_T_TEMPLATE,
DOCUMENT_XML_TEMPLATE,
+ _TranslationTestCase,
+ assert_html_equal,
)
@@ -39,11 +41,27 @@ def _bold(is_bold):
return ''
-def _create_p_tag(text, bold=False):
- t_tag = _create_t_tag(text)
+def _create_p_tag(r_tags):
+ """
+ r_tags is a list of r tags or hyperlinks (or some combination)
+ """
return DOCUMENT_P_TEMPLATE % {
+ 'r_tags': ''.join(r_tags)
+ }
+
+
+def _create_r_tag(text, is_bold=False):
+ t_tag = _create_t_tag(text)
+ return DOCUMENT_R_TEMPLATE % {
'text': t_tag,
- 'bold': _bold(is_bold=bold),
+ 'bold': _bold(is_bold=is_bold),
+ }
+
+
+def _create_hyperlink_tag(r_id, r_tag):
+ return DOCUMENT_HYPERLINK_TEMPLATE % {
+ 'r_id': r_id,
+ 'r_tag': r_tag,
}
@@ -149,16 +167,16 @@ class TableInListTestCase(_TranslationTestCase):
def get_xml(self):
table = _create_table(num_rows=2, num_columns=2, text=chain(
- [_create_p_tag('BBB')],
- [_create_p_tag('CCC')],
- [_create_p_tag('DDD')],
- [_create_p_tag('EEE')],
+ [_create_p_tag([_create_r_tag('BBB')])],
+ [_create_p_tag([_create_r_tag('CCC')])],
+ [_create_p_tag([_create_r_tag('DDD')])],
+ [_create_p_tag([_create_r_tag('EEE')])],
))
# Nest that table in a list.
first_li = _create_li(text='AAA', ilvl=0, numId=1)
second = _create_li(text='FFF', ilvl=0, numId=1)
- p_tag = _create_p_tag('GGG')
+ p_tag = _create_p_tag([_create_r_tag('GGG')])
body = ''
for el in [first_li, table, second, p_tag]:
body += el
@@ -403,17 +421,14 @@ class ListWithContinuationTestCase(_TranslationTestCase):
def get_xml(self):
table = _create_table(num_rows=2, num_columns=2, text=chain(
- [_create_p_tag('DDD')],
- [_create_p_tag('EEE')],
- [_create_p_tag('FFF')],
- [_create_p_tag('GGG')],
+ [_create_p_tag([_create_r_tag('DDD')])],
+ [_create_p_tag([_create_r_tag('EEE')])],
+ [_create_p_tag([_create_r_tag('FFF')])],
+ [_create_p_tag([_create_r_tag('GGG')])],
))
tags = [
_create_li(text='AAA', ilvl=0, numId=1),
- DOCUMENT_P_TEMPLATE % {
- 'text': _create_t_tag('BBB'),
- 'bold': _bold(is_bold=False),
- },
+ _create_p_tag([_create_r_tag('BBB')]),
_create_li(text='CCC', ilvl=0, numId=1),
table,
_create_li(text='HHH', ilvl=0, numId=1),
@@ -514,14 +529,56 @@ class TableWithInvalidTag(_TranslationTestCase):
def get_xml(self):
table = _create_table(num_rows=2, num_columns=2, text=chain(
- [_create_p_tag('AAA')],
- [_create_p_tag('BBB')],
+ [_create_p_tag([_create_r_tag('AAA')])],
+ [_create_p_tag([_create_r_tag('BBB')])],
# This tag may have CCC in it, however this tag has no meaning
# pertaining to content.
['<w:invalidTag>CCC</w:invalidTag>'],
- [_create_p_tag('DDD')],
+ [_create_p_tag([_create_r_tag('DDD')])],
))
xml = DOCUMENT_XML_TEMPLATE % {
'body': table,
}
return etree.fromstring(xml)
+
+
+class HyperlinkStyledTestCase(_TranslationTestCase):
+ relationship_dict = {
+ 'rId0': 'www.google.com',
+ }
+
+ expected_output = '''
+ <html>
+ <p><a href="www.google.com">link</a></p>
+ </html>
+ '''
+
+ def get_xml(self):
@winhamwr Owner

I would prefer two tests. One for the vanilla route and one that makes it explicitly that we're stripping bold.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ r_tag = _create_r_tag('link', is_bold=True)
+ hyperlink = _create_hyperlink_tag(r_id='rId0', r_tag=r_tag)
+ p_tag = _create_p_tag([hyperlink])
+ xml = DOCUMENT_XML_TEMPLATE % {
+ 'body': p_tag,
+ }
+ return etree.fromstring(xml)
+
+
+class HyperlinkVanillaTestCase(_TranslationTestCase):
+ relationship_dict = {
+ 'rId0': 'www.google.com',
+ }
+
+ expected_output = '''
+ <html>
+ <p><a href="www.google.com">link</a></p>
+ </html>
+ '''
+
+ def get_xml(self):
+ r_tag = _create_r_tag('link', is_bold=False)
+ hyperlink = _create_hyperlink_tag(r_id='rId0', r_tag=r_tag)
+ p_tag = _create_p_tag([hyperlink])
+ xml = DOCUMENT_XML_TEMPLATE % {
+ 'body': p_tag,
+ }
+ return etree.fromstring(xml)
Something went wrong with that request. Please try again.