Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Improved translation of dl, dt, and dd

b translator now returns nothing if the b element is empty, so as to prevent a false hr
  • Loading branch information...
commit 005cdb5962eddc772e658b40a1d2eae7d18d3bed 1 parent 528b957
Bill Bushey authored
Showing with 13 additions and 5 deletions.
  1. +13 −5 Markdownipy.py
18 Markdownipy.py
View
@@ -9,7 +9,7 @@ class Markdownipy(object):
# Define which elements are block level, cause they're special
# Maybe poor name - this set refers to HTML elements that will cause newlines in MD.
- block_level = {'blockquote','br','dd','div','dl','h1','h2','h3','h4','h5','h6','hr','li','ol','p','pre','td','ul'}
+ block_level = {'blockquote','br','dd','div','dl','dt','h1','h2','h3','h4','h5','h6','hr','li','ol','p','pre','td','ul'}
# Build the dictionary of element translators
@@ -28,7 +28,8 @@ def translate_li(self, el):
return el.text_content().strip() + "\n"
translator['li'] = translate_li
- translator['dd'] = translator['li']
+ translator['dd'] = lambda self,el: el.text_content().strip() + "\n\n"
+ translator['dt'] = translator['dd']
def translate_ul(self, el):
if el.getparent() is not None and el.getparent().tag == 'li':
@@ -89,7 +90,7 @@ def translate_p(self,el):
# ------Bold and Strong------
# MD breaks when there is spacing between the markup and the content
# MD Bold doesn't work across line breaks, so wrap seperate paragraphs in their own bolds
- translator['b'] = lambda self,el : "**" + re.sub(r'\n{2,}', '**\n\n' + self.indent_list(False) + '**', el.text_content().strip()) + "**"
+ translator['b'] = lambda self,el : "**" + re.sub(r'\n{2,}', '**\n\n' + self.indent_list(False) + '**', el.text_content().strip()) + "**" if el.text_content().strip() != '' else ''
translator['strong'] = translator['b']
# ------Italics and Emphasis------
@@ -108,7 +109,7 @@ def translate_p(self,el):
#translator['u'] = lambda self,el : el.text_content()
# ------Images------
- translator['img'] = lambda self,el : "![" + el.attrib['alt'] + "](" + el.attrib['src'] + ")"
+ translator['img'] = lambda self,el : "![" + (el.attrib['alt'] if 'alt' in el.attrib else '') + "](" + el.attrib['src'] + ")"
# ------Headings------
def pre_heading(self,el):
@@ -212,8 +213,15 @@ def recursiveTranslate(self, el):
translated = self.recursiveTranslate(child)
child.text = self.indent_list(True) + `i` + ". " + translated
i += 1
+ elif el.tag == 'dl' and (child.tag == 'dt' or child.tag == 'dd') and self.tl:
+ translated = self.recursiveTranslate(child)
+ # DTs should be indented as a ul or ol, while a dd should be indented as content of a ul or ol.
+ if child.tag == 'dt':
+ child.text = self.indent_list(True) + translated
+ elif child.tag == 'dd':
+ child.text = self.indent_list(False) + translated
else:
- if child.tag in {'ol','ul'}:
+ if child.tag in {'ol','ul','dl'}:
self.list_level += 1
child.text = self.recursiveTranslate(child)
self.list_level -= 1
Please sign in to comment.
Something went wrong with that request. Please try again.