diff --git a/markdownify/__init__.py b/markdownify/__init__.py index f97bcbf..b7c9545 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -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 diff --git a/tests/test_conversions.py b/tests/test_conversions.py index dfc8d3c..98065bb 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -2,7 +2,7 @@ from markdownify import markdownify as md, ATX, ATX_CLOSED import re -nested_uls = re.sub('\s+', '', """ +nested_uls = """ """ + +nested_ols = """ +
    +
  1. 1 +
      +
    1. a +
        +
      1. I
      2. +
      3. II
      4. +
      5. III
      6. +
      +
    2. +
    3. b
    4. +
    5. c
    6. +
    +
  2. +
  3. 2
  4. +
  5. 3
  6. + """ def test_a(): @@ -92,6 +111,8 @@ def test_i(): def test_ol(): assert md('
    1. a
    2. b
    ') == '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('

    hello

    ') == '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():