Skip to content

Commit

Permalink
Implement convertion of definition lists.
Browse files Browse the repository at this point in the history
  • Loading branch information
jone committed Jan 18, 2012
1 parent d04463a commit 6dac472
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 18 deletions.
3 changes: 3 additions & 0 deletions docs/HISTORY.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ Changelog
1.0dev (unreleased)
-------------------

- Implement convertion of definition lists.
[jone]

- Rename as_pdf view to export_pdf.
[jone]
73 changes: 55 additions & 18 deletions ftw/pdfgenerator/html2latex/subconverters/listing.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ class ListConverter(subconverter.SubConverter):
into latex' itemize- and enumerate-environments.
"""

pattern = r'<(ul|ol)(.*)</\1>'
pattern = r'<(ul|ol|dl)(.*)</\1>'
listing_tag_mapping = {
'ul': 'itemize',
'ol': 'enumerate',
'dl': 'description',
}

def __call__(self):
Expand All @@ -35,26 +36,13 @@ def __call__(self):

# iterate, because there may be multiple lists
begin_env, end_env = self._create_environ(node)

latex.append(begin_env)
for elm in node.childNodes:

if elm.nodeType == 3:
content_html = elm.toxml().strip()

else:
content_html = ''.join(
[e.toxml() for e in elm.childNodes])

if elm.nodeType == 1 and elm.tagName.lower() == 'li':
latex.append(
(r'\item %s' %
self.converter.convert(content_html)).strip())
if node.tagName.lower() in ('ol', 'ul'):
latex.append(self._convert_listing_items(node))

else:
data = self.converter.convert(content_html).strip()
if len(data) > 0:
latex.append(data)
else:
latex.append(self._convert_description_items(node))

latex.append(end_env)

Expand All @@ -64,6 +52,55 @@ def __call__(self):
latex.append('')
self.replace_and_lock('\n'.join(latex))

def _convert_listing_items(self, list_node):
latex = []
for elm in list_node.childNodes:
if elm.nodeType == 1 and elm.tagName.lower() == 'li':
latex.append(r'\item %s' % self._get_node_content(elm).strip())

else:
content_latex = self._get_node_content(elm)
if content_latex is not None:
latex.append(content_latex)

return '\n'.join(latex)

def _convert_description_items(self, list_node):
latex = []

dt_node = None
for elm in list_node.childNodes:
if elm.nodeType == 1 and elm.tagName.lower() == 'dt':
dt_node = elm

elif elm.nodeType == 1 and elm.tagName.lower() == 'dd' and \
dt_node is not None:
latex.append(r'\item[%s] %s' % (
self._get_node_content(dt_node).strip(),
self._get_node_content(elm).strip()))
dt_node = None

else:
content_latex = self._get_node_content(elm)
if content_latex is not None:
latex.append(content_latex)

return '\n'.join(latex)

def _get_node_content(self, elm):
if elm.nodeType == 3: # text node
content_html = elm.toxml().strip()

else: # tag node
content_html = ''.join(
[e.toxml() for e in elm.childNodes])

if len(content_html) == 0:
return None

else:
return self.converter.convert(content_html)

def _create_environ(self, list_):
name = list_.tagName.lower()
env = self.listing_tag_mapping[name]
Expand Down
10 changes: 10 additions & 0 deletions ftw/pdfgenerator/tests/test_html2latex_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -372,3 +372,13 @@ def test_repeat_bug(self):

self.assertEqual(len(result), len(latex))
self.assertEqual(result, latex)

def XXXtest_blubb(self):
html = """<dl><dt>Ein Begriff</dt><dd>Mit einer Beschreibung</dd></dl>
<p>Hier ist ein <a class="external-link" href="http://www.4teamwork.ch">Link auf www.4teamwork.ch</a>.</p>
<blockquote class="pullquote">Und noch ein Zitat. Mit welchem man etwas sagen kann.</blockquote>
<p class="callout">Hier noch ein Text der betont sein muss.</p>"""

result = self.convert(html)

self.assertEqual(result, 'xxx')
41 changes: 41 additions & 0 deletions ftw/pdfgenerator/tests/test_html2latex_subconverters.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,44 @@ def test_bad_html_fallback(self):
r''))

self.assertEqual(self.convert(html), latex)

def test_definition_list(self):
html = '\n'.join((
'<dl>',
'<dt>definition term</dt>',
'<dd>THE definition</dd>',
'<dt>another term</dt>',
'<dd>another definition</dd>',
'</dl>'))

latex = '\n'.join((
r'\begin{description}',
r'\item[definition term] THE definition',
r'\item[another term] another definition',
r'\end{description}',
r''))

self.assertEqual(self.convert(html), latex)

def test_nested_definition_list(self):
html = '\n'.join((
'<dl>',
'<dt>definition term</dt>',
'<dd>THE definition</dd>',
'<dt>nested</dt>',
'<dd><dl><dt>subterm</dt><dd>subdef</dd></dl></dd>',
'<dt>another term</dt>',
'<dd>another definition</dd>',
'</dl>'))

latex = '\n'.join((
r'\begin{description}',
r'\item[definition term] THE definition',
r'\item[nested] \begin{description}',
r'\item[subterm] subdef',
r'\end{description}',
r'\item[another term] another definition',
r'\end{description}',
r''))

self.assertEqual(self.convert(html), latex)

0 comments on commit 6dac472

Please sign in to comment.