Add nested OL test (for newlines) and correct lists nesting

This commit is contained in:
Rémi
2019-11-21 09:35:34 +01:00
parent 146104b41f
commit 7b788bafd4
2 changed files with 32 additions and 6 deletions

View File

@@ -151,14 +151,19 @@ class MarkdownConverter(object):
def convert_list(self, el, text):
nested = False
before_paragraph = False
print(el.name, repr(el.next_sibling), repr(text))
if el.next_sibling and el.next_sibling.name not in ['ul', 'ol']:
print(el.name, repr(el.next_sibling))
before_paragraph = True
while el:
if el.name == 'li':
nested = True
break
el = el.parent
if nested:
text = '\n' + self.indent(text, 1)
return '\n' + text + '\n'
text = '\n' + self.indent(text, 1).rstrip()
return text + ('\n' if before_paragraph else '')
convert_ul = convert_list
convert_ol = convert_list

View File

@@ -2,7 +2,7 @@ from markdownify import markdownify as md, ATX, ATX_CLOSED
import re
nested_uls = re.sub('\s+', '', """
nested_uls = """
<ul>
<li>1
<ul>
@@ -19,7 +19,26 @@ nested_uls = re.sub('\s+', '', """
</li>
<li>2</li>
<li>3</li>
</ul>""")
</ul>"""
nested_ols = """
<ol>
<li>1
<ol>
<li>a
<ol>
<li>I</li>
<li>II</li>
<li>III</li>
</ol>
</li>
<li>b</li>
<li>c</li>
</ol>
</li>
<li>2</li>
<li>3</li>
</ul>"""
def test_a():
@@ -92,6 +111,8 @@ def test_i():
def test_ol():
assert md('<ol><li>a</li><li>b</li></ol>') == '1. a\n2. 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'
@@ -113,11 +134,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\t\n\t+ b\n\t+ c\n\t\n* 2\n* 3\n'
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'
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\t\n\t- b\n\t- c\n\t\n- 2\n- 3\n'
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'
def test_img():