Compare commits
35 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 | ||
|
|
a79ed44ec3 | ||
|
|
29a4e551f7 | ||
|
|
b3ac4606a6 | ||
|
|
f093843f40 | ||
|
|
de6f91af0e | ||
|
|
8c28ade348 | ||
|
|
a152c5b706 | ||
|
|
292d64bbf4 | ||
|
|
db96eeb785 | ||
|
|
73f7644c0d |
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.
|
||||
|
||||
|
||||
@@ -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
|
||||
@@ -154,19 +164,23 @@ class MarkdownConverter(object):
|
||||
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 '-'
|
||||
@@ -222,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 ''
|
||||
@@ -237,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.4',
|
||||
'__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={
|
||||
|
||||
@@ -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> ') == ' '
|
||||
@@ -159,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():
|
||||
@@ -220,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