Compare commits
42 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
02bb914ef3 | ||
|
|
0fee4b0a80 | ||
|
|
10e1ff3e6e | ||
|
|
73800ced36 | ||
|
|
1538cacb94 | ||
|
|
21c0d034d0 | ||
|
|
f59f9f9a54 | ||
|
|
bd22a16c9e | ||
|
|
55fb96e3c0 | ||
|
|
5f102d5223 | ||
|
|
e3ddc789a2 | ||
|
|
651d5f00e8 | ||
|
|
3cf324d03d | ||
|
|
96f7e7d307 | ||
|
|
e1dbbfad42 | ||
|
|
2d0cd97323 | ||
|
|
d4882b86b9 | ||
|
|
b47d5f11c8 | ||
|
|
29c794e17d | ||
|
|
e877602a5e | ||
|
|
5580b0b51d | ||
|
|
650f377b64 | ||
|
|
7ee87b1d32 | ||
|
|
16dbc471b9 | ||
|
|
c04ec855dd | ||
|
|
8da0bdf998 | ||
|
|
a79ed44ec3 | ||
|
|
29a4e551f7 | ||
|
|
b3ac4606a6 | ||
|
|
f093843f40 | ||
|
|
de6f91af0e | ||
|
|
8c28ade348 | ||
|
|
a152c5b706 | ||
|
|
292d64bbf4 | ||
|
|
db96eeb785 | ||
|
|
73f7644c0d | ||
|
|
2c7e4a0100 | ||
|
|
4f00d638d2 | ||
|
|
d23596706d | ||
|
|
6a0e5d8176 | ||
|
|
7b788bafd4 | ||
|
|
146104b41f |
12
README.rst
12
README.rst
@@ -75,6 +75,18 @@ bullets
|
|||||||
lists are nested. Otherwise, the bullet will alternate based on nesting
|
lists are nested. Otherwise, the bullet will alternate based on nesting
|
||||||
level. Defaults to ``'*+-'``.
|
level. Defaults to ``'*+-'``.
|
||||||
|
|
||||||
|
strong_em_symbol
|
||||||
|
In markdown, both ``*`` and ``_`` are used to encode **strong** or
|
||||||
|
*emphasized* texts. Either of these symbols can be chosen by the options
|
||||||
|
``ASTERISK`` (default) or ``UNDERSCORE`` respectively.
|
||||||
|
|
||||||
|
newline_style
|
||||||
|
Defines the style of marking linebreaks (``<br>``) in markdown. The default
|
||||||
|
value ``SPACES`` of this option will adopt the usual two spaces and a newline,
|
||||||
|
while ``BACKSLASH`` will convert a linebreak to ``\\n`` (a backslash an a
|
||||||
|
newline). While the latter convention is non-standard, it is commonly
|
||||||
|
preferred and supported by a lot of interpreters.
|
||||||
|
|
||||||
Options may be specified as kwargs to the ``markdownify`` function, or as a
|
Options may be specified as kwargs to the ``markdownify`` function, or as a
|
||||||
nested ``Options`` class in ``MarkdownConverter`` subclasses.
|
nested ``Options`` class in ``MarkdownConverter`` subclasses.
|
||||||
|
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ 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'[\t ]+')
|
whitespace_re = re.compile(r'[\t ]+')
|
||||||
|
all_whitespace_re = re.compile(r'[\s]+')
|
||||||
html_heading_re = re.compile(r'h[1-6]')
|
html_heading_re = re.compile(r'h[1-6]')
|
||||||
|
|
||||||
|
|
||||||
@@ -15,6 +16,14 @@ ATX_CLOSED = 'atx_closed'
|
|||||||
UNDERLINED = 'underlined'
|
UNDERLINED = 'underlined'
|
||||||
SETEXT = UNDERLINED
|
SETEXT = UNDERLINED
|
||||||
|
|
||||||
|
# Newline style
|
||||||
|
SPACES = 'spaces'
|
||||||
|
BACKSLASH = 'backslash'
|
||||||
|
|
||||||
|
# Strong and emphasis style
|
||||||
|
ASTERISK = '*'
|
||||||
|
UNDERSCORE = '_'
|
||||||
|
|
||||||
|
|
||||||
def escape(text):
|
def escape(text):
|
||||||
if not text:
|
if not text:
|
||||||
@@ -46,6 +55,8 @@ class MarkdownConverter(object):
|
|||||||
autolinks = True
|
autolinks = True
|
||||||
heading_style = UNDERLINED
|
heading_style = UNDERLINED
|
||||||
bullets = '*+-' # An iterable of bullet types.
|
bullets = '*+-' # An iterable of bullet types.
|
||||||
|
strong_em_symbol = ASTERISK
|
||||||
|
newline_style = SPACES
|
||||||
|
|
||||||
class Options(DefaultOptions):
|
class Options(DefaultOptions):
|
||||||
pass
|
pass
|
||||||
@@ -73,12 +84,18 @@ class MarkdownConverter(object):
|
|||||||
if not children_only and isHeading:
|
if not children_only and isHeading:
|
||||||
convert_children_as_inline = True
|
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
|
# Convert the children first
|
||||||
for el in node.children:
|
for el in node.children:
|
||||||
if isinstance(el, Comment):
|
if isinstance(el, Comment):
|
||||||
continue
|
continue
|
||||||
elif isinstance(el, NavigableString):
|
elif isinstance(el, NavigableString):
|
||||||
text += self.process_text(six.text_type(el))
|
text += self.process_text(el)
|
||||||
else:
|
else:
|
||||||
text += self.process_tag(el, convert_children_as_inline)
|
text += self.process_tag(el, convert_children_as_inline)
|
||||||
|
|
||||||
@@ -89,7 +106,10 @@ class MarkdownConverter(object):
|
|||||||
|
|
||||||
return text
|
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 ''))
|
return escape(whitespace_re.sub(' ', text or ''))
|
||||||
|
|
||||||
def __getattr__(self, attr):
|
def __getattr__(self, attr):
|
||||||
@@ -154,19 +174,23 @@ class MarkdownConverter(object):
|
|||||||
if convert_as_inline:
|
if convert_as_inline:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
return ' \n'
|
if self.options['newline_style'].lower() == BACKSLASH:
|
||||||
|
return '\\\n'
|
||||||
|
else:
|
||||||
|
return ' \n'
|
||||||
|
|
||||||
def convert_em(self, el, text, convert_as_inline):
|
def convert_em(self, el, text, convert_as_inline):
|
||||||
|
em_tag = self.options['strong_em_symbol']
|
||||||
prefix, suffix, text = chomp(text)
|
prefix, suffix, text = chomp(text)
|
||||||
if not text:
|
if not text:
|
||||||
return ''
|
return ''
|
||||||
return '%s*%s*%s' % (prefix, text, suffix)
|
return '%s%s%s%s%s' % (prefix, em_tag, text, em_tag, suffix)
|
||||||
|
|
||||||
def convert_hn(self, n, el, text, convert_as_inline):
|
def convert_hn(self, n, el, text, convert_as_inline):
|
||||||
if convert_as_inline:
|
if convert_as_inline:
|
||||||
return text
|
return text
|
||||||
|
|
||||||
style = self.options['heading_style']
|
style = self.options['heading_style'].lower()
|
||||||
text = text.rstrip()
|
text = text.rstrip()
|
||||||
if style == UNDERLINED and n <= 2:
|
if style == UNDERLINED and n <= 2:
|
||||||
line = '=' if n == 1 else '-'
|
line = '=' if n == 1 else '-'
|
||||||
@@ -185,6 +209,9 @@ class MarkdownConverter(object):
|
|||||||
# Ignoring convert_to_inline for list.
|
# Ignoring convert_to_inline for list.
|
||||||
|
|
||||||
nested = False
|
nested = False
|
||||||
|
before_paragraph = False
|
||||||
|
if el.next_sibling and el.next_sibling.name not in ['ul', 'ol']:
|
||||||
|
before_paragraph = True
|
||||||
while el:
|
while el:
|
||||||
if el.name == 'li':
|
if el.name == 'li':
|
||||||
nested = True
|
nested = True
|
||||||
@@ -193,7 +220,7 @@ class MarkdownConverter(object):
|
|||||||
if nested:
|
if nested:
|
||||||
# remove trailing newline if nested
|
# remove trailing newline if nested
|
||||||
return '\n' + self.indent(text, 1).rstrip()
|
return '\n' + self.indent(text, 1).rstrip()
|
||||||
return '\n' + text + '\n'
|
return text + ('\n' if before_paragraph else '')
|
||||||
|
|
||||||
convert_ul = convert_list
|
convert_ul = convert_list
|
||||||
convert_ol = convert_list
|
convert_ol = convert_list
|
||||||
@@ -222,10 +249,11 @@ class MarkdownConverter(object):
|
|||||||
return '%s\n\n' % text if text else ''
|
return '%s\n\n' % text if text else ''
|
||||||
|
|
||||||
def convert_strong(self, el, text, convert_as_inline):
|
def convert_strong(self, el, text, convert_as_inline):
|
||||||
|
strong_tag = 2 * self.options['strong_em_symbol']
|
||||||
prefix, suffix, text = chomp(text)
|
prefix, suffix, text = chomp(text)
|
||||||
if not text:
|
if not text:
|
||||||
return ''
|
return ''
|
||||||
return '%s**%s**%s' % (prefix, text, suffix)
|
return '%s%s%s%s%s' % (prefix, strong_tag, text, strong_tag, suffix)
|
||||||
|
|
||||||
def convert_img(self, el, text, convert_as_inline):
|
def convert_img(self, el, text, convert_as_inline):
|
||||||
alt = el.attrs.get('alt', None) or ''
|
alt = el.attrs.get('alt', None) or ''
|
||||||
@@ -237,6 +265,26 @@ class MarkdownConverter(object):
|
|||||||
|
|
||||||
return '' % (alt, src, title_part)
|
return '' % (alt, src, title_part)
|
||||||
|
|
||||||
|
def convert_table(self, el, text, convert_as_inline):
|
||||||
|
rows = el.find_all('tr')
|
||||||
|
text_data = []
|
||||||
|
for row in rows:
|
||||||
|
headers = row.find_all('th')
|
||||||
|
columns = row.find_all('td')
|
||||||
|
if len(headers) > 0:
|
||||||
|
headers = [head.text.strip() for head in headers]
|
||||||
|
text_data.append('| ' + ' | '.join(headers) + ' |')
|
||||||
|
text_data.append('| ' + ' | '.join(['---'] * len(headers)) + ' |')
|
||||||
|
elif len(columns) > 0:
|
||||||
|
columns = [colm.text.strip() for colm in columns]
|
||||||
|
text_data.append('| ' + ' | '.join(columns) + ' |')
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
return '\n'.join(text_data)
|
||||||
|
|
||||||
|
def convert_hr(self, el, text, convert_as_inline):
|
||||||
|
return '\n\n---\n\n'
|
||||||
|
|
||||||
|
|
||||||
def markdownify(html, **options):
|
def markdownify(html, **options):
|
||||||
return MarkdownConverter(**options).convert(html)
|
return MarkdownConverter(**options).convert(html)
|
||||||
|
|||||||
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.5',
|
'__version__': '0.7.2',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
from markdownify import markdownify as md, ATX, ATX_CLOSED
|
from markdownify import markdownify as md, ATX, ATX_CLOSED, BACKSLASH, UNDERSCORE
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
|
||||||
nested_uls = re.sub(r'\s+', '', """
|
nested_uls = """
|
||||||
<ul>
|
<ul>
|
||||||
<li>1
|
<li>1
|
||||||
<ul>
|
<ul>
|
||||||
@@ -19,7 +19,96 @@ nested_uls = re.sub(r'\s+', '', """
|
|||||||
</li>
|
</li>
|
||||||
<li>2</li>
|
<li>2</li>
|
||||||
<li>3</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+', '', """
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<th>Firstname</th>
|
||||||
|
<th>Lastname</th>
|
||||||
|
<th>Age</th>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Jill</td>
|
||||||
|
<td>Smith</td>
|
||||||
|
<td>50</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Eve</td>
|
||||||
|
<td>Jackson</td>
|
||||||
|
<td>94</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
""")
|
||||||
|
|
||||||
|
|
||||||
|
table_head_body = re.sub(r'\s+', '', """
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Firstname</th>
|
||||||
|
<th>Lastname</th>
|
||||||
|
<th>Age</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td>Jill</td>
|
||||||
|
<td>Smith</td>
|
||||||
|
<td>50</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Eve</td>
|
||||||
|
<td>Jackson</td>
|
||||||
|
<td>94</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
""")
|
||||||
|
|
||||||
|
table_missing_text = re.sub(r'\s+', '', """
|
||||||
|
<table>
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th></th>
|
||||||
|
<th>Lastname</th>
|
||||||
|
<th>Age</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
<tr>
|
||||||
|
<td>Jill</td>
|
||||||
|
<td></td>
|
||||||
|
<td>50</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Eve</td>
|
||||||
|
<td>Jackson</td>
|
||||||
|
<td>94</td>
|
||||||
|
</tr>
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
""")
|
||||||
|
|
||||||
|
|
||||||
def test_chomp():
|
def test_chomp():
|
||||||
@@ -159,7 +248,9 @@ def test_hn_nested_img():
|
|||||||
|
|
||||||
|
|
||||||
def test_hr():
|
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():
|
def test_head():
|
||||||
@@ -181,8 +272,8 @@ def test_i():
|
|||||||
|
|
||||||
|
|
||||||
def test_ol():
|
def test_ol():
|
||||||
assert md('<ol><li>a</li><li>b</li></ol>') == '\n1. a\n2. 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>') == '\n3. a\n4. b\n\n'
|
assert md('<ol start="3"><li>a</li><li>b</li></ol>') == '3. a\n4. b\n'
|
||||||
|
|
||||||
|
|
||||||
def test_p():
|
def test_p():
|
||||||
@@ -194,11 +285,15 @@ def test_strong():
|
|||||||
|
|
||||||
|
|
||||||
def test_ul():
|
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():
|
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():
|
def test_nested_uls():
|
||||||
@@ -206,11 +301,11 @@ def test_nested_uls():
|
|||||||
Nested ULs should alternate bullet characters.
|
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():
|
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():
|
def test_img():
|
||||||
@@ -220,3 +315,20 @@ def test_img():
|
|||||||
|
|
||||||
def test_div():
|
def test_div():
|
||||||
assert md('Hello</div> World') == 'Hello World'
|
assert md('Hello</div> World') == 'Hello World'
|
||||||
|
|
||||||
|
|
||||||
|
def test_table():
|
||||||
|
assert md(table) == '| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |'
|
||||||
|
assert md(table_head_body) == '| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |'
|
||||||
|
assert md(table_missing_text) == '| | Lastname | Age |\n| --- | --- | --- |\n| Jill | | 50 |\n| Eve | Jackson | 94 |'
|
||||||
|
|
||||||
|
|
||||||
|
def test_strong_em_symbol():
|
||||||
|
assert md('<strong>Hello</strong>', strong_em_symbol=UNDERSCORE) == '__Hello__'
|
||||||
|
assert md('<b>Hello</b>', strong_em_symbol=UNDERSCORE) == '__Hello__'
|
||||||
|
assert md('<em>Hello</em>', strong_em_symbol=UNDERSCORE) == '_Hello_'
|
||||||
|
assert md('<i>Hello</i>', strong_em_symbol=UNDERSCORE) == '_Hello_'
|
||||||
|
|
||||||
|
|
||||||
|
def test_newline_style():
|
||||||
|
assert md('a<br />b<br />c', newline_style=BACKSLASH) == 'a\\\nb\\\nc'
|
||||||
|
|||||||
Reference in New Issue
Block a user