Compare commits

..

16 Commits
0.7.0 ... 0.7.2

Author SHA1 Message Date
AlexVonB
02bb914ef3 Merge branch 'develop' 2021-05-02 13:49:30 +02:00
AlexVonB
0fee4b0a80 bump to v0.7.2 2021-05-02 13:49:14 +02:00
AlexVonB
10e1ff3e6e Merge pull request #23 from SimonIT/ordere-list-update
Ordered list update
2021-05-02 13:47:43 +02:00
AlexVonB
73800ced36 fixed whitespace issues at nested lists 2021-05-02 13:44:09 +02:00
AlexVonB
1538cacb94 Merge branch 'develop' into ordere-list-update 2021-05-02 10:58:13 +02:00
AlexVonB
21c0d034d0 Merge branch 'develop' 2021-05-02 10:51:00 +02:00
AlexVonB
f59f9f9a54 bump to v0.7.1 2021-05-02 10:50:49 +02:00
AlexVonB
bd22a16c9e Merge pull request #40 from jiulongw/jiulongw/hr
Add conversion for hr element
2021-05-02 10:47:32 +02:00
AlexVonB
55fb96e3c0 fix hr tests 2021-05-02 10:45:52 +02:00
Jiulong Wang
5f102d5223 Add conversion for hr element 2021-04-29 13:41:28 -07:00
SimonIT
2c7e4a0100 Fix tests 2020-08-26 19:47:11 +02:00
SimonIT
4f00d638d2 Merge remote-tracking branch 'upstream/develop' into ordered-list
# Conflicts:
#	markdownify/__init__.py
#	tests/test_conversions.py
2020-08-26 19:41:43 +02:00
Rémi
d23596706d Remove debug prints 2019-11-22 11:49:22 +01:00
Rémi
6a0e5d8176 Correct inline UL test as paragraphs are followed by two newlines 2019-11-21 09:46:22 +01:00
Rémi
7b788bafd4 Add nested OL test (for newlines) and correct lists nesting 2019-11-21 09:35:34 +01:00
Rémi
146104b41f Remove newline-only textnodes outside <pre> 2019-11-20 10:37:39 +01:00
3 changed files with 54 additions and 13 deletions

View File

@@ -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,12 +84,18 @@ class MarkdownConverter(object):
if not children_only and isHeading:
convert_children_as_inline = True
# 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)
@@ -99,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):
@@ -199,6 +209,9 @@ class MarkdownConverter(object):
# Ignoring convert_to_inline for list.
nested = False
before_paragraph = False
if el.next_sibling and el.next_sibling.name not in ['ul', 'ol']:
before_paragraph = True
while el:
if el.name == 'li':
nested = True
@@ -207,7 +220,7 @@ class MarkdownConverter(object):
if nested:
# remove trailing newline if nested
return '\n' + self.indent(text, 1).rstrip()
return '\n' + text + '\n'
return text + ('\n' if before_paragraph else '')
convert_ul = convert_list
convert_ol = convert_list
@@ -269,6 +282,9 @@ class MarkdownConverter(object):
continue
return '\n'.join(text_data)
def convert_hr(self, el, text, convert_as_inline):
return '\n\n---\n\n'
def markdownify(html, **options):
return MarkdownConverter(**options).convert(html)

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.0',
'__version__': '0.7.2',
}

View File

@@ -2,7 +2,7 @@ from markdownify import markdownify as md, ATX, ATX_CLOSED, BACKSLASH, UNDERSCOR
import re
nested_uls = re.sub(r'\s+', '', """
nested_uls = """
<ul>
<li>1
<ul>
@@ -19,7 +19,26 @@ nested_uls = re.sub(r'\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>"""
table = re.sub(r'\s+', '', """
@@ -229,7 +248,9 @@ def test_hn_nested_img():
def test_hr():
assert md('<hr>hr</hr>') == 'hr'
assert md('Hello<hr>World') == 'Hello\n\n---\n\nWorld'
assert md('Hello<hr />World') == 'Hello\n\n---\n\nWorld'
assert md('<p>Hello</p>\n<hr>\n<p>World</p>') == 'Hello\n\n\n\n\n---\n\n\nWorld\n\n'
def test_head():
@@ -251,8 +272,8 @@ def test_i():
def test_ol():
assert md('<ol><li>a</li><li>b</li></ol>') == '\n1. a\n2. b\n\n'
assert md('<ol start="3"><li>a</li><li>b</li></ol>') == '\n3. a\n4. b\n\n'
assert md('<ol><li>a</li><li>b</li></ol>') == '1. a\n2. b\n'
assert md('<ol start="3"><li>a</li><li>b</li></ol>') == '3. a\n4. b\n'
def test_p():
@@ -264,11 +285,15 @@ def test_strong():
def test_ul():
assert md('<ul><li>a</li><li>b</li></ul>') == '\n* a\n* b\n\n'
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\n* a\n* b\n\nbar\n\n'
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'
def test_nested_uls():
@@ -276,11 +301,11 @@ def test_nested_uls():
Nested ULs should alternate bullet characters.
"""
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\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='-') == '\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\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():