Compare commits
48 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
21c0d034d0 | ||
|
|
f59f9f9a54 | ||
|
|
bd22a16c9e | ||
|
|
55fb96e3c0 | ||
|
|
5f102d5223 | ||
|
|
e3ddc789a2 | ||
|
|
651d5f00e8 | ||
|
|
3cf324d03d | ||
|
|
96f7e7d307 | ||
|
|
e1dbbfad42 | ||
|
|
2d0cd97323 | ||
|
|
d4882b86b9 | ||
|
|
b47d5f11c8 | ||
|
|
29c794e17d | ||
|
|
e877602a5e | ||
|
|
5580b0b51d | ||
|
|
650f377b64 | ||
|
|
7ee87b1d32 | ||
|
|
16dbc471b9 | ||
|
|
c04ec855dd | ||
|
|
8da0bdf998 | ||
|
|
ec185e2e9c | ||
|
|
a59e4b9f48 | ||
|
|
fd293a9714 | ||
|
|
99365de669 | ||
|
|
079d1721aa | ||
|
|
ed406d3206 | ||
|
|
f320cf87ff | ||
|
|
a79ed44ec3 | ||
|
|
29a4e551f7 | ||
|
|
b3ac4606a6 | ||
|
|
f093843f40 | ||
|
|
de6f91af0e | ||
|
|
8c28ade348 | ||
|
|
a152c5b706 | ||
|
|
292d64bbf4 | ||
|
|
db96eeb785 | ||
|
|
73f7644c0d | ||
|
|
a4d134df97 | ||
|
|
457454c713 | ||
|
|
321e9eb5f6 | ||
|
|
bf24df3e2e | ||
|
|
15329588b1 | ||
|
|
77d1e99bd5 | ||
|
|
34ad8485fa | ||
|
|
f0ce934bf8 | ||
|
|
97c78ef55b | ||
|
|
4f8937810b |
6
.github/workflows/python-app.yml
vendored
6
.github/workflows/python-app.yml
vendored
@@ -16,14 +16,14 @@ jobs:
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Set up Python 3.6
|
||||
- name: Set up Python 3.8
|
||||
uses: actions/setup-python@v2
|
||||
with:
|
||||
python-version: 3.6
|
||||
python-version: 3.8
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install flake8==2.5.4 pytest
|
||||
pip install flake8==3.8.4 pytest
|
||||
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
|
||||
- name: Lint with flake8
|
||||
run: |
|
||||
|
||||
2
.github/workflows/python-publish.yml
vendored
2
.github/workflows/python-publish.yml
vendored
@@ -17,7 +17,7 @@ jobs:
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v2
|
||||
with:
|
||||
python-version: '3.x'
|
||||
python-version: '3.8'
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
|
||||
12
README.rst
12
README.rst
@@ -75,6 +75,18 @@ bullets
|
||||
lists are nested. Otherwise, the bullet will alternate based on nesting
|
||||
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
|
||||
nested ``Options`` class in ``MarkdownConverter`` subclasses.
|
||||
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
from bs4 import BeautifulSoup, NavigableString
|
||||
from bs4 import BeautifulSoup, NavigableString, Comment
|
||||
import re
|
||||
import six
|
||||
|
||||
|
||||
convert_heading_re = re.compile(r'convert_h(\d+)')
|
||||
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]')
|
||||
|
||||
|
||||
@@ -15,6 +15,14 @@ ATX_CLOSED = 'atx_closed'
|
||||
UNDERLINED = 'underlined'
|
||||
SETEXT = UNDERLINED
|
||||
|
||||
# Newline style
|
||||
SPACES = 'spaces'
|
||||
BACKSLASH = 'backslash'
|
||||
|
||||
# Strong and emphasis style
|
||||
ASTERISK = '*'
|
||||
UNDERSCORE = '_'
|
||||
|
||||
|
||||
def escape(text):
|
||||
if not text:
|
||||
@@ -46,6 +54,8 @@ class MarkdownConverter(object):
|
||||
autolinks = True
|
||||
heading_style = UNDERLINED
|
||||
bullets = '*+-' # An iterable of bullet types.
|
||||
strong_em_symbol = ASTERISK
|
||||
newline_style = SPACES
|
||||
|
||||
class Options(DefaultOptions):
|
||||
pass
|
||||
@@ -75,7 +85,9 @@ class MarkdownConverter(object):
|
||||
|
||||
# Convert the children first
|
||||
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))
|
||||
else:
|
||||
text += self.process_tag(el, convert_children_as_inline)
|
||||
@@ -146,25 +158,29 @@ class MarkdownConverter(object):
|
||||
if convert_as_inline:
|
||||
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):
|
||||
if convert_as_inline:
|
||||
return ""
|
||||
|
||||
return ' \n'
|
||||
if self.options['newline_style'].lower() == BACKSLASH:
|
||||
return '\\\n'
|
||||
else:
|
||||
return ' \n'
|
||||
|
||||
def convert_em(self, el, text, convert_as_inline):
|
||||
em_tag = self.options['strong_em_symbol']
|
||||
prefix, suffix, text = chomp(text)
|
||||
if not text:
|
||||
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):
|
||||
if convert_as_inline:
|
||||
return text
|
||||
|
||||
style = self.options['heading_style']
|
||||
style = self.options['heading_style'].lower()
|
||||
text = text.rstrip()
|
||||
if style == UNDERLINED and n <= 2:
|
||||
line = '=' if n == 1 else '-'
|
||||
@@ -220,10 +236,11 @@ class MarkdownConverter(object):
|
||||
return '%s\n\n' % text if text else ''
|
||||
|
||||
def convert_strong(self, el, text, convert_as_inline):
|
||||
strong_tag = 2 * self.options['strong_em_symbol']
|
||||
prefix, suffix, text = chomp(text)
|
||||
if not text:
|
||||
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):
|
||||
alt = el.attrs.get('alt', None) or ''
|
||||
@@ -235,6 +252,26 @@ class MarkdownConverter(object):
|
||||
|
||||
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):
|
||||
return MarkdownConverter(**options).convert(html)
|
||||
|
||||
13
setup.py
13
setup.py
@@ -10,7 +10,7 @@ read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
|
||||
pkgmeta = {
|
||||
'__title__': 'markdownify',
|
||||
'__author__': 'Matthew Tretter',
|
||||
'__version__': '0.6.1',
|
||||
'__version__': '0.7.1',
|
||||
}
|
||||
|
||||
|
||||
@@ -50,7 +50,7 @@ class LintCommand(Command):
|
||||
yield "%s.py" % filename
|
||||
|
||||
def run(self):
|
||||
from flake8.engine import get_style_guide
|
||||
from flake8.api.legacy import get_style_guide
|
||||
flake8_style = get_style_guide(config_file='setup.cfg')
|
||||
paths = self.distribution_files()
|
||||
report = flake8_style.check_files(paths)
|
||||
@@ -70,13 +70,13 @@ setup(
|
||||
zip_safe=False,
|
||||
include_package_data=True,
|
||||
setup_requires=[
|
||||
'flake8',
|
||||
'flake8>=3.8,<4',
|
||||
],
|
||||
tests_require=[
|
||||
'pytest',
|
||||
'pytest>=6.2,<7',
|
||||
],
|
||||
install_requires=[
|
||||
'beautifulsoup4', 'six'
|
||||
'beautifulsoup4>=4.9,<5', 'six>=1.15,<2'
|
||||
],
|
||||
classifiers=[
|
||||
'Environment :: Web Environment',
|
||||
@@ -87,6 +87,9 @@ setup(
|
||||
'Programming Language :: Python :: 2.5',
|
||||
'Programming Language :: Python :: 2.6',
|
||||
'Programming Language :: Python :: 2.7',
|
||||
'Programming Language :: Python :: 3.6',
|
||||
'Programming Language :: Python :: 3.7',
|
||||
'Programming Language :: Python :: 3.8',
|
||||
'Topic :: Utilities'
|
||||
],
|
||||
cmdclass={
|
||||
|
||||
@@ -4,3 +4,13 @@ from markdownify import markdownify as md
|
||||
def test_nested():
|
||||
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'
|
||||
|
||||
|
||||
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():
|
||||
assert md(' a b \n\n c ') == ' a b c '
|
||||
assert md(' a b \t\t c ') == ' a b c '
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from markdownify import markdownify as md, ATX, ATX_CLOSED
|
||||
from markdownify import markdownify as md, ATX, ATX_CLOSED, BACKSLASH, UNDERSCORE
|
||||
import re
|
||||
|
||||
|
||||
@@ -22,6 +22,76 @@ nested_uls = re.sub(r'\s+', '', """
|
||||
</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():
|
||||
assert md(' <b></b> ') == ' '
|
||||
assert md(' <b> </b> ') == ' '
|
||||
@@ -75,12 +145,16 @@ def test_b_spaces():
|
||||
|
||||
|
||||
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():
|
||||
text = md('<blockquote>And she was like <blockquote>Hello</blockquote></blockquote>').strip()
|
||||
assert text == '> And she was like \n> > Hello'
|
||||
text = md('<blockquote>And she was like <blockquote>Hello</blockquote></blockquote>')
|
||||
assert text == '\n> And she was like \n> > Hello\n> \n> \n\n'
|
||||
|
||||
|
||||
def test_br():
|
||||
@@ -111,6 +185,11 @@ def test_hn():
|
||||
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():
|
||||
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'
|
||||
@@ -150,7 +229,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():
|
||||
@@ -211,3 +292,20 @@ def test_img():
|
||||
|
||||
def test_div():
|
||||
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'
|
||||
|
||||
@@ -2,7 +2,7 @@ from markdownify import markdownify as md
|
||||
|
||||
|
||||
def test_underscore():
|
||||
assert md('_hey_dude_') == '\_hey\_dude\_'
|
||||
assert md('_hey_dude_') == r'\_hey\_dude\_'
|
||||
|
||||
|
||||
def test_xml_entities():
|
||||
|
||||
Reference in New Issue
Block a user