Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
Merge branch 'master' into issue_6
Browse files Browse the repository at this point in the history
Conflicts:
	docx2html/tests/__init__.py
	docx2html/tests/test_xml.py
  • Loading branch information
Jason Ward committed Jan 23, 2013
2 parents 59dd98d + 56d2a64 commit b5ae650
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 12 deletions.
3 changes: 2 additions & 1 deletion docx2html/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
40 changes: 37 additions & 3 deletions docx2html/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,55 @@ def create_xml(body):


def create_p_tag(text, bold=False):
if isinstance(text, str):
# Use create a single r tag based on the text and the bold
run_tag = create_r_tag(text, bold)
run_tags = [run_tag]
elif isinstance(text, list):
run_tags = text
else:
raise AssertionError('text must be a string or a list')
template = env.get_template('p.xml')

kwargs = {
'run_tags': run_tags,
}
return template.render(**kwargs)


def create_r_tag(text, is_bold=False):
template = env.get_template('r.xml')
kwargs = {
'text': text,
'is_bold': bold,
'is_bold': is_bold,
}
return template.render(**kwargs)


def create_hyperlink_tag(r_id, run_tags):
template = env.get_template('hyperlink.xml')
kwargs = {
'r_id': r_id,
'run_tags': run_tags,
}
return template.render(**kwargs)


def create_li(text, ilvl, numId, bold=False):
if isinstance(text, str):
# Use create a single r tag based on the text and the bold
run_tag = create_r_tag(text, bold)
run_tags = [run_tag]
elif isinstance(text, list):
run_tags = []
for run_text, run_bold in text:
run_tags.append(create_r_tag(run_tags, run_bold))
else:
raise AssertionError('text must be a string or a list')
template = env.get_template('p.xml')

kwargs = {
'text': text,
'is_bold': bold,
'run_tags': run_tags,
'is_list': True,
'ilvl': ilvl,
'numId': numId,
Expand Down
5 changes: 5 additions & 0 deletions docx2html/tests/templates/hyperlink.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<w:hyperlink r:id="{{ r_id }}">
{% for run_tag in run_tags %}
{{ run_tag }}
{% endfor %}
</w:hyperlink>
11 changes: 3 additions & 8 deletions docx2html/tests/templates/p.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@
</w:numPr>
{% endif %}
</w:pPr>
<w:r>
<w:rPr>
{% if is_bold %}
<w:b/>
{% endif %}
</w:rPr>
{% include 't.xml' %}
</w:r>
{% for run_tag in run_tags %}
{{ run_tag }}
{% endfor %}
</w:p>
8 changes: 8 additions & 0 deletions docx2html/tests/templates/r.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<w:r>
<w:rPr>
{% if is_bold %}
<w:b/>
{% endif %}
</w:rPr>
{% include 't.xml' %}
</w:r>
45 changes: 45 additions & 0 deletions docx2html/tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
_TranslationTestCase,
assert_html_equal,
create_drawing,
create_hyperlink_tag,
create_li,
create_p_tag,
create_pict,
create_r_tag,
create_table,
create_xml,
)
Expand Down Expand Up @@ -101,6 +103,7 @@ def get_xml(self):
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')

body = ''
for el in [first_li, table, second, p_tag]:
body += el
Expand Down Expand Up @@ -443,3 +446,45 @@ def get_xml(self):
body = table
xml = create_xml(body)
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):
run_tags = []
run_tags.append(create_r_tag('link', is_bold=True))
run_tags = [create_hyperlink_tag(r_id='rId0', run_tags=run_tags)]
run_tags.append(create_r_tag('.', is_bold=False))
body = create_p_tag(run_tags)
xml = create_xml(body)
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):
run_tags = []
run_tags.append(create_r_tag('link', is_bold=False))
run_tags = [create_hyperlink_tag(r_id='rId0', run_tags=run_tags)]
run_tags.append(create_r_tag('.', is_bold=False))
body = create_p_tag(run_tags)
xml = create_xml(body)
return etree.fromstring(xml)

0 comments on commit b5ae650

Please sign in to comment.