Compare commits

...

5 Commits
0.7.2 ... 0.7.3

Author SHA1 Message Date
AlexVonB
ec5858e42f Merge branch 'develop' 2021-05-16 18:41:24 +02:00
AlexVonB
fc29483899 bump to v0.7.3 2021-05-16 18:41:08 +02:00
AlexVonB
bd7a8d6990 Merge pull request #43 from jiulongw/develop
Fix missing whitespaces in <li> node
2021-05-16 18:39:58 +02:00
Jiulong Wang
ddfbf6a364 Keep important spaces in <li> element 2021-05-10 16:07:54 -07:00
Jiulong Wang
91a64e3cd4 Fix missing whitespaces in <li> node 2021-05-10 14:42:05 -07:00
3 changed files with 19 additions and 4 deletions

View File

@@ -85,9 +85,17 @@ class MarkdownConverter(object):
convert_children_as_inline = True
# Remove whitespace-only textnodes in lists
if node.name in ['ol', 'ul', 'li']:
def is_list_node(el):
return el and el.name in ['ol', 'ul', 'li']
if is_list_node(node):
for el in node.children:
if isinstance(el, NavigableString) and six.text_type(el).strip() == '':
# Only extract (remove) whitespace-only text node if any of the conditions is true:
# - el is the first element in its parent
# - el is the last element in its parent
# - el is adjacent to an list node
can_extract = not el.previous_sibling or not el.next_sibling or is_list_node(el.previous_sibling) or is_list_node(el.next_sibling)
if isinstance(el, NavigableString) and six.text_type(el).strip() == '' and can_extract:
el.extract()
# Convert the children first
@@ -108,7 +116,10 @@ class MarkdownConverter(object):
def process_text(self, el):
text = six.text_type(el)
if el.parent.name == 'li':
# remove trailing whitespaces if any of the following condition is true:
# - current text node is the last node in li
# - current text node is followed by an embedded list
if el.parent.name == 'li' and (not el.next_sibling or el.next_sibling.name in ['ul', 'ol']):
return escape(all_whitespace_re.sub(' ', text or '')).rstrip()
return escape(whitespace_re.sub(' ', text or ''))

View File

@@ -10,7 +10,7 @@ read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
pkgmeta = {
'__title__': 'markdownify',
'__author__': 'Matthew Tretter',
'__version__': '0.7.2',
'__version__': '0.7.3',
}

View File

@@ -308,6 +308,10 @@ def test_bullets():
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_li_text():
assert md('<ul><li>foo <a href="#">bar</a></li><li>foo bar </li><li>foo <b>bar</b> <i>space</i>.</ul>') == '* foo [bar](#)\n* foo bar\n* foo **bar** *space*.\n'
def test_img():
assert md('<img src="/path/to/img.jpg" alt="Alt text" title="Optional title" />') == '![Alt text](/path/to/img.jpg "Optional title")'
assert md('<img src="/path/to/img.jpg" alt="Alt text" />') == '![Alt text](/path/to/img.jpg)'