fixed whitespace issues at nested lists
This commit is contained in:
@@ -6,6 +6,7 @@ import six
|
||||
convert_heading_re = re.compile(r'convert_h(\d+)')
|
||||
line_beginning_re = re.compile(r'^', re.MULTILINE)
|
||||
whitespace_re = re.compile(r'[\t ]+')
|
||||
all_whitespace_re = re.compile(r'[\s]+')
|
||||
html_heading_re = re.compile(r'h[1-6]')
|
||||
|
||||
|
||||
@@ -83,17 +84,18 @@ class MarkdownConverter(object):
|
||||
if not children_only and isHeading:
|
||||
convert_children_as_inline = True
|
||||
|
||||
# Clean newline-only textnodes outside <pre>
|
||||
for el in node.children:
|
||||
if node.name != 'pre' and isinstance(el, NavigableString) and six.text_type(el) == '\n':
|
||||
el.extract()
|
||||
# Remove whitespace-only textnodes in lists
|
||||
if node.name in ['ol', 'ul', 'li']:
|
||||
for el in node.children:
|
||||
if isinstance(el, NavigableString) and six.text_type(el).strip() == '':
|
||||
el.extract()
|
||||
|
||||
# Convert the children first
|
||||
for el in node.children:
|
||||
if isinstance(el, Comment):
|
||||
continue
|
||||
elif isinstance(el, NavigableString):
|
||||
text += self.process_text(six.text_type(el))
|
||||
text += self.process_text(el)
|
||||
else:
|
||||
text += self.process_tag(el, convert_children_as_inline)
|
||||
|
||||
@@ -104,7 +106,10 @@ class MarkdownConverter(object):
|
||||
|
||||
return text
|
||||
|
||||
def process_text(self, text):
|
||||
def process_text(self, el):
|
||||
text = six.text_type(el)
|
||||
if el.parent.name == 'li':
|
||||
return escape(all_whitespace_re.sub(' ', text or '')).rstrip()
|
||||
return escape(whitespace_re.sub(' ', text or ''))
|
||||
|
||||
def __getattr__(self, attr):
|
||||
|
||||
@@ -276,10 +276,6 @@ def test_ol():
|
||||
assert md('<ol start="3"><li>a</li><li>b</li></ol>') == '3. a\n4. b\n'
|
||||
|
||||
|
||||
def test_nested_ols():
|
||||
assert md(nested_ols) == '1. 1 \n\t1. a \n\t\t1. I\n\t\t2. II\n\t\t3. III\n\t2. b\n\t3. c\n2. 2\n3. 3\n'
|
||||
|
||||
|
||||
def test_p():
|
||||
assert md('<p>hello</p>') == 'hello\n\n'
|
||||
|
||||
@@ -292,6 +288,10 @@ def test_ul():
|
||||
assert md('<ul><li>a</li><li>b</li></ul>') == '* a\n* b\n'
|
||||
|
||||
|
||||
def test_nested_ols():
|
||||
assert md(nested_ols) == '\n1. 1\n\t1. a\n\t\t1. I\n\t\t2. II\n\t\t3. III\n\t2. b\n\t3. c\n2. 2\n3. 3\n'
|
||||
|
||||
|
||||
def test_inline_ul():
|
||||
assert md('<p>foo</p><ul><li>a</li><li>b</li></ul><p>bar</p>') == 'foo\n\n* a\n* b\n\nbar\n\n'
|
||||
|
||||
@@ -301,11 +301,11 @@ def test_nested_uls():
|
||||
Nested ULs should alternate bullet characters.
|
||||
|
||||
"""
|
||||
assert md(nested_uls) == '* 1 \n\t+ a \n\t\t- I\n\t\t- II\n\t\t- III\n\t+ b\n\t+ c\n* 2\n* 3\n'
|
||||
assert md(nested_uls) == '\n* 1\n\t+ a\n\t\t- I\n\t\t- II\n\t\t- III\n\t+ b\n\t+ c\n* 2\n* 3\n'
|
||||
|
||||
|
||||
def test_bullets():
|
||||
assert md(nested_uls, bullets='-') == '- 1 \n\t- a \n\t\t- I\n\t\t- II\n\t\t- III\n\t- b\n\t- c\n- 2\n- 3\n'
|
||||
assert md(nested_uls, bullets='-') == '\n- 1\n\t- a\n\t\t- I\n\t\t- II\n\t\t- III\n\t- b\n\t- c\n- 2\n- 3\n'
|
||||
|
||||
|
||||
def test_img():
|
||||
|
||||
Reference in New Issue
Block a user