Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
079d1721aa | ||
|
|
ed406d3206 | ||
|
|
f320cf87ff | ||
|
|
a4d134df97 | ||
|
|
457454c713 | ||
|
|
321e9eb5f6 | ||
|
|
bf24df3e2e | ||
|
|
15329588b1 | ||
|
|
77d1e99bd5 | ||
|
|
34ad8485fa | ||
|
|
f0ce934bf8 | ||
|
|
97c78ef55b | ||
|
|
99cd237f27 | ||
|
|
b7e1ab889d | ||
|
|
29e86aec55 | ||
|
|
453b604096 | ||
|
|
2bde8d3e8e | ||
|
|
4f8937810b | ||
|
|
8c9b029756 | ||
|
|
ae50065872 |
@@ -1,11 +1,11 @@
|
|||||||
from bs4 import BeautifulSoup, NavigableString
|
from bs4 import BeautifulSoup, NavigableString, Comment
|
||||||
import re
|
import re
|
||||||
import six
|
import six
|
||||||
|
|
||||||
|
|
||||||
convert_heading_re = re.compile(r'convert_h(\d+)')
|
convert_heading_re = re.compile(r'convert_h(\d+)')
|
||||||
line_beginning_re = re.compile(r'^', re.MULTILINE)
|
line_beginning_re = re.compile(r'^', re.MULTILINE)
|
||||||
whitespace_re = re.compile(r'[\r\n\s\t ]+')
|
whitespace_re = re.compile(r'[\t ]+')
|
||||||
html_heading_re = re.compile(r'h[1-6]')
|
html_heading_re = re.compile(r'h[1-6]')
|
||||||
|
|
||||||
|
|
||||||
@@ -75,7 +75,9 @@ class MarkdownConverter(object):
|
|||||||
|
|
||||||
# Convert the children first
|
# Convert the children first
|
||||||
for el in node.children:
|
for el in node.children:
|
||||||
if isinstance(el, NavigableString):
|
if isinstance(el, Comment):
|
||||||
|
continue
|
||||||
|
elif isinstance(el, NavigableString):
|
||||||
text += self.process_text(six.text_type(el))
|
text += self.process_text(six.text_type(el))
|
||||||
else:
|
else:
|
||||||
text += self.process_tag(el, convert_children_as_inline)
|
text += self.process_tag(el, convert_children_as_inline)
|
||||||
@@ -131,7 +133,8 @@ class MarkdownConverter(object):
|
|||||||
return text
|
return text
|
||||||
href = el.get('href')
|
href = el.get('href')
|
||||||
title = el.get('title')
|
title = el.get('title')
|
||||||
if self.options['autolinks'] and text == href and not title:
|
# For the replacement see #29: text nodes underscores are escaped
|
||||||
|
if self.options['autolinks'] and text.replace(r'\_', '_') == href and not title:
|
||||||
# Shortcut syntax
|
# Shortcut syntax
|
||||||
return '<%s>' % href
|
return '<%s>' % href
|
||||||
title_part = ' "%s"' % title.replace('"', r'\"') if title else ''
|
title_part = ' "%s"' % title.replace('"', r'\"') if title else ''
|
||||||
@@ -145,7 +148,7 @@ class MarkdownConverter(object):
|
|||||||
if convert_as_inline:
|
if convert_as_inline:
|
||||||
return text
|
return text
|
||||||
|
|
||||||
return '\n' + line_beginning_re.sub('> ', text) if text else ''
|
return '\n' + (line_beginning_re.sub('> ', text) + '\n\n') if text else ''
|
||||||
|
|
||||||
def convert_br(self, el, text, convert_as_inline):
|
def convert_br(self, el, text, convert_as_inline):
|
||||||
if convert_as_inline:
|
if convert_as_inline:
|
||||||
|
|||||||
2
setup.py
2
setup.py
@@ -10,7 +10,7 @@ read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
|
|||||||
pkgmeta = {
|
pkgmeta = {
|
||||||
'__title__': 'markdownify',
|
'__title__': 'markdownify',
|
||||||
'__author__': 'Matthew Tretter',
|
'__author__': 'Matthew Tretter',
|
||||||
'__version__': '0.6.0',
|
'__version__': '0.6.4',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4,3 +4,13 @@ from markdownify import markdownify as md
|
|||||||
def test_nested():
|
def test_nested():
|
||||||
text = md('<p>This is an <a href="http://example.com/">example link</a>.</p>')
|
text = md('<p>This is an <a href="http://example.com/">example link</a>.</p>')
|
||||||
assert text == 'This is an [example link](http://example.com/).\n\n'
|
assert text == 'This is an [example link](http://example.com/).\n\n'
|
||||||
|
|
||||||
|
|
||||||
|
def test_ignore_comments():
|
||||||
|
text = md("<!-- This is a comment -->")
|
||||||
|
assert text == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_ignore_comments_with_other_tags():
|
||||||
|
text = md("<!-- This is a comment --><a href='http://example.com/'>example link</a>")
|
||||||
|
assert text == "[example link](http://example.com/)"
|
||||||
|
|||||||
@@ -10,4 +10,4 @@ def test_soup():
|
|||||||
|
|
||||||
|
|
||||||
def test_whitespace():
|
def test_whitespace():
|
||||||
assert md(' a b \n\n c ') == ' a b c '
|
assert md(' a b \t\t c ') == ' a b c '
|
||||||
|
|||||||
@@ -34,7 +34,11 @@ def test_chomp():
|
|||||||
|
|
||||||
|
|
||||||
def test_a():
|
def test_a():
|
||||||
assert md('<a href="http://google.com">Google</a>') == '[Google](http://google.com)'
|
assert md('<a href="https://google.com">Google</a>') == '[Google](https://google.com)'
|
||||||
|
assert md('<a href="https://google.com">https://google.com</a>', autolinks=False) == '[https://google.com](https://google.com)'
|
||||||
|
assert md('<a href="https://google.com">https://google.com</a>') == '<https://google.com>'
|
||||||
|
assert md('<a href="https://community.kde.org/Get_Involved">https://community.kde.org/Get_Involved</a>') == '<https://community.kde.org/Get_Involved>'
|
||||||
|
assert md('<a href="https://community.kde.org/Get_Involved">https://community.kde.org/Get_Involved</a>', autolinks=False) == '[https://community.kde.org/Get\\_Involved](https://community.kde.org/Get_Involved)'
|
||||||
|
|
||||||
|
|
||||||
def test_a_spaces():
|
def test_a_spaces():
|
||||||
@@ -71,12 +75,16 @@ def test_b_spaces():
|
|||||||
|
|
||||||
|
|
||||||
def test_blockquote():
|
def test_blockquote():
|
||||||
assert md('<blockquote>Hello</blockquote>').strip() == '> Hello'
|
assert md('<blockquote>Hello</blockquote>') == '\n> Hello\n\n'
|
||||||
|
|
||||||
|
|
||||||
|
def test_blockquote_with_paragraph():
|
||||||
|
assert md('<blockquote>Hello</blockquote><p>handsome</p>') == '\n> Hello\n\nhandsome\n\n'
|
||||||
|
|
||||||
|
|
||||||
def test_nested_blockquote():
|
def test_nested_blockquote():
|
||||||
text = md('<blockquote>And she was like <blockquote>Hello</blockquote></blockquote>').strip()
|
text = md('<blockquote>And she was like <blockquote>Hello</blockquote></blockquote>')
|
||||||
assert text == '> And she was like \n> > Hello'
|
assert text == '\n> And she was like \n> > Hello\n> \n> \n\n'
|
||||||
|
|
||||||
|
|
||||||
def test_br():
|
def test_br():
|
||||||
@@ -107,6 +115,11 @@ def test_hn():
|
|||||||
assert md('<h6>Hello</h6>') == '###### Hello\n\n'
|
assert md('<h6>Hello</h6>') == '###### Hello\n\n'
|
||||||
|
|
||||||
|
|
||||||
|
def test_hn_chained():
|
||||||
|
assert md('<h1>First</h1>\n<h2>Second</h2>\n<h3>Third</h3>', heading_style=ATX) == '# First\n\n\n## Second\n\n\n### Third\n\n'
|
||||||
|
assert md('X<h1>First</h1>', heading_style=ATX) == 'X# First\n\n'
|
||||||
|
|
||||||
|
|
||||||
def test_hn_nested_tag_heading_style():
|
def test_hn_nested_tag_heading_style():
|
||||||
assert md('<h1>A <p>P</p> C </h1>', heading_style=ATX_CLOSED) == '# A P C #\n\n'
|
assert md('<h1>A <p>P</p> C </h1>', heading_style=ATX_CLOSED) == '# A P C #\n\n'
|
||||||
assert md('<h1>A <p>P</p> C </h1>', heading_style=ATX) == '# A P C\n\n'
|
assert md('<h1>A <p>P</p> C </h1>', heading_style=ATX) == '# A P C\n\n'
|
||||||
|
|||||||
Reference in New Issue
Block a user