From 73800ced360d907f262b4d548926cfff9c9f42f5 Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 2 May 2021 13:44:09 +0200 Subject: [PATCH] fixed whitespace issues at nested lists --- markdownify/__init__.py | 17 +++++++++++------ tests/test_conversions.py | 12 ++++++------ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 1322ac0..da04ebf 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -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
-        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):
diff --git a/tests/test_conversions.py b/tests/test_conversions.py
index 68bb81e..caac0fd 100644
--- a/tests/test_conversions.py
+++ b/tests/test_conversions.py
@@ -276,10 +276,6 @@ def test_ol():
     assert md('
  1. a
  2. b
') == '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('

hello

') == 'hello\n\n' @@ -292,6 +288,10 @@ def test_ul(): assert md('') == '* 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('

foo

bar

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