Compare commits
28 Commits
feature/to
...
0.11.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d375116807 | ||
|
|
eb0330bfc6 | ||
|
|
28793ac0b3 | ||
|
|
9231704988 | ||
|
|
1613c302bc | ||
|
|
55c9e84f38 | ||
|
|
99875683ac | ||
|
|
eaeb0603eb | ||
|
|
cb73590623 | ||
|
|
59417ab115 | ||
|
|
917b01e548 | ||
|
|
652714859d | ||
|
|
ea5b22824b | ||
|
|
ec5858e42f | ||
|
|
02bb914ef3 | ||
|
|
21c0d034d0 | ||
|
|
e3ddc789a2 | ||
|
|
2d0cd97323 | ||
|
|
ec185e2e9c | ||
|
|
079d1721aa | ||
|
|
bf24df3e2e | ||
|
|
15329588b1 | ||
|
|
34ad8485fa | ||
|
|
f0ce934bf8 | ||
|
|
99cd237f27 | ||
|
|
2bde8d3e8e | ||
|
|
8c9b029756 | ||
|
|
ae50065872 |
10
.github/workflows/python-app.yml
vendored
10
.github/workflows/python-app.yml
vendored
@@ -23,7 +23,11 @@ jobs:
|
|||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
pip install tox
|
pip install flake8==3.8.4 pytest
|
||||||
- name: Lint and test
|
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
|
||||||
|
- name: Lint with flake8
|
||||||
run: |
|
run: |
|
||||||
tox
|
python setup.py lint
|
||||||
|
- name: Test with pytest
|
||||||
|
run: |
|
||||||
|
python setup.py test
|
||||||
|
|||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -9,4 +9,3 @@
|
|||||||
/venv
|
/venv
|
||||||
build/
|
build/
|
||||||
.vscode/settings.json
|
.vscode/settings.json
|
||||||
.tox/
|
|
||||||
|
|||||||
24
README.rst
24
README.rst
@@ -92,7 +92,7 @@ sub_symbol, sup_symbol
|
|||||||
newline_style
|
newline_style
|
||||||
Defines the style of marking linebreaks (``<br>``) in markdown. The default
|
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,
|
value ``SPACES`` of this option will adopt the usual two spaces and a newline,
|
||||||
while ``BACKSLASH`` will convert a linebreak to ``\\n`` (a backslash and a
|
while ``BACKSLASH`` will convert a linebreak to ``\\n`` (a backslash an a
|
||||||
newline). While the latter convention is non-standard, it is commonly
|
newline). While the latter convention is non-standard, it is commonly
|
||||||
preferred and supported by a lot of interpreters.
|
preferred and supported by a lot of interpreters.
|
||||||
|
|
||||||
@@ -130,11 +130,6 @@ keep_inline_images_in
|
|||||||
that should be allowed to contain inline images, for example ``['td']``.
|
that should be allowed to contain inline images, for example ``['td']``.
|
||||||
Defaults to an empty list.
|
Defaults to an empty list.
|
||||||
|
|
||||||
wrap, wrap_width
|
|
||||||
If ``wrap`` is set to ``True``, all text paragraphs are wrapped at
|
|
||||||
``wrap_width`` characters. Defaults to ``False`` and ``80``.
|
|
||||||
Use with ``newline_style=BACKSLASH`` to keep line breaks in paragraphs.
|
|
||||||
|
|
||||||
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.
|
||||||
|
|
||||||
@@ -174,16 +169,13 @@ change:
|
|||||||
return ImageBlockConverter(**options).convert(html)
|
return ImageBlockConverter(**options).convert(html)
|
||||||
|
|
||||||
|
|
||||||
Command Line Interface
|
|
||||||
=====================
|
|
||||||
|
|
||||||
Use ``markdownify example.html > example.md`` or pipe input from stdin
|
|
||||||
(``cat example.html | markdownify > example.md``).
|
|
||||||
Call ``markdownify -h`` to see all available options.
|
|
||||||
They are the same as listed above and take the same arguments.
|
|
||||||
|
|
||||||
|
|
||||||
Development
|
Development
|
||||||
===========
|
===========
|
||||||
|
|
||||||
To run tests and the linter run ``pip install tox`` once, then ``tox``.
|
To run tests:
|
||||||
|
|
||||||
|
``python setup.py test``
|
||||||
|
|
||||||
|
To lint:
|
||||||
|
|
||||||
|
``python setup.py lint``
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
from bs4 import BeautifulSoup, NavigableString, Comment, Doctype
|
from bs4 import BeautifulSoup, NavigableString, Comment, Doctype
|
||||||
from textwrap import fill
|
|
||||||
import re
|
import re
|
||||||
import six
|
import six
|
||||||
|
|
||||||
@@ -76,8 +75,6 @@ class MarkdownConverter(object):
|
|||||||
strong_em_symbol = ASTERISK
|
strong_em_symbol = ASTERISK
|
||||||
sub_symbol = ''
|
sub_symbol = ''
|
||||||
sup_symbol = ''
|
sup_symbol = ''
|
||||||
wrap = False
|
|
||||||
wrap_width = 80
|
|
||||||
|
|
||||||
class Options(DefaultOptions):
|
class Options(DefaultOptions):
|
||||||
pass
|
pass
|
||||||
@@ -158,7 +155,7 @@ class MarkdownConverter(object):
|
|||||||
and el.parent.parent.name == 'pre')):
|
and el.parent.parent.name == 'pre')):
|
||||||
text = whitespace_re.sub(' ', text)
|
text = whitespace_re.sub(' ', text)
|
||||||
|
|
||||||
if el.parent.name != 'code' and el.parent.name != 'pre':
|
if el.parent.name != 'code':
|
||||||
text = self.escape(text)
|
text = self.escape(text)
|
||||||
|
|
||||||
# remove trailing whitespaces if any of the following condition is true:
|
# remove trailing whitespaces if any of the following condition is true:
|
||||||
@@ -334,11 +331,6 @@ class MarkdownConverter(object):
|
|||||||
def convert_p(self, el, text, convert_as_inline):
|
def convert_p(self, el, text, convert_as_inline):
|
||||||
if convert_as_inline:
|
if convert_as_inline:
|
||||||
return text
|
return text
|
||||||
if self.options['wrap']:
|
|
||||||
text = fill(text,
|
|
||||||
width=self.options['wrap_width'],
|
|
||||||
break_long_words=False,
|
|
||||||
break_on_hyphens=False)
|
|
||||||
return '%s\n\n' % text if text else ''
|
return '%s\n\n' % text if text else ''
|
||||||
|
|
||||||
def convert_pre(self, el, text, convert_as_inline):
|
def convert_pre(self, el, text, convert_as_inline):
|
||||||
@@ -378,13 +370,8 @@ class MarkdownConverter(object):
|
|||||||
if is_headrow and not el.previous_sibling:
|
if is_headrow and not el.previous_sibling:
|
||||||
# first row and is headline: print headline underline
|
# first row and is headline: print headline underline
|
||||||
underline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'
|
underline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'
|
||||||
elif (not el.previous_sibling
|
elif not el.previous_sibling and not el.parent.name != 'table':
|
||||||
and (el.parent.name == 'table'
|
# first row, not headline, and the parent is sth. like tbody:
|
||||||
or (el.parent.name == 'tbody'
|
|
||||||
and not el.parent.previous_sibling))):
|
|
||||||
# first row, not headline, and:
|
|
||||||
# - the parent is table or
|
|
||||||
# - the parent is tbody at the beginning of a table.
|
|
||||||
# print empty headline above this row
|
# print empty headline above this row
|
||||||
overline += '| ' + ' | '.join([''] * len(cells)) + ' |' + '\n'
|
overline += '| ' + ' | '.join([''] * len(cells)) + ' |' + '\n'
|
||||||
overline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'
|
overline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'
|
||||||
|
|||||||
@@ -1,65 +0,0 @@
|
|||||||
#!/usr/bin/env python
|
|
||||||
|
|
||||||
import argparse
|
|
||||||
import sys
|
|
||||||
|
|
||||||
from markdownify import markdownify
|
|
||||||
|
|
||||||
|
|
||||||
def main(argv=sys.argv[1:]):
|
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
prog='markdownify',
|
|
||||||
description='Converts html to markdown.',
|
|
||||||
)
|
|
||||||
|
|
||||||
parser.add_argument('html', nargs='?', type=argparse.FileType('r'),
|
|
||||||
default=sys.stdin,
|
|
||||||
help="The html file to convert. Defaults to STDIN if not "
|
|
||||||
"provided.")
|
|
||||||
parser.add_argument('-s', '--strip', nargs='*',
|
|
||||||
help="A list of tags to strip. This option can't be used with "
|
|
||||||
"the --convert option.")
|
|
||||||
parser.add_argument('-c', '--convert', nargs='*',
|
|
||||||
help="A list of tags to convert. This option can't be used with "
|
|
||||||
"the --strip option.")
|
|
||||||
parser.add_argument('-a', '--autolinks', action='store_true',
|
|
||||||
help="A boolean indicating whether the 'automatic link' style "
|
|
||||||
"should be used when a 'a' tag's contents match its href.")
|
|
||||||
parser.add_argument('--default-title', action='store_false',
|
|
||||||
help="A boolean to enable setting the title of a link to its "
|
|
||||||
"href, if no title is given.")
|
|
||||||
parser.add_argument('--heading-style',
|
|
||||||
choices=('ATX', 'ATX_CLOSED', 'SETEXT', 'UNDERLINED'),
|
|
||||||
help="Defines how headings should be converted.")
|
|
||||||
parser.add_argument('-b', '--bullets', default='*+-',
|
|
||||||
help="A string of bullet styles to use; the bullet will "
|
|
||||||
"alternate based on nesting level.")
|
|
||||||
parser.add_argument('--sub-symbol', default='',
|
|
||||||
help="Define the chars that surround '<sub>'.")
|
|
||||||
parser.add_argument('--sup-symbol', default='',
|
|
||||||
help="Define the chars that surround '<sup>'.")
|
|
||||||
parser.add_argument('--code-language', default='',
|
|
||||||
help="Defines the language that should be assumed for all "
|
|
||||||
"'<pre>' sections.")
|
|
||||||
parser.add_argument('--no-escape-asterisks', dest='escape_asterisks',
|
|
||||||
action='store_false',
|
|
||||||
help="Do not escape '*' to '\\*' in text.")
|
|
||||||
parser.add_argument('--no-escape-underscores', dest='escape_underscores',
|
|
||||||
action='store_false',
|
|
||||||
help="Do not escape '_' to '\\_' in text.")
|
|
||||||
parser.add_argument('-i', '--keep-inline-images-in', nargs='*',
|
|
||||||
help="Images are converted to their alt-text when the images are "
|
|
||||||
"located inside headlines or table cells. If some inline images "
|
|
||||||
"should be converted to markdown images instead, this option can "
|
|
||||||
"be set to a list of parent tags that should be allowed to "
|
|
||||||
"contain inline images.")
|
|
||||||
parser.add_argument('-w', '--wrap', action='store_true',
|
|
||||||
help="Wrap all text paragraphs at --wrap-width characters.")
|
|
||||||
parser.add_argument('--wrap-width', type=int, default=80)
|
|
||||||
|
|
||||||
args = parser.parse_args(argv)
|
|
||||||
print(markdownify(**vars(args)))
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
main()
|
|
||||||
65
setup.py
65
setup.py
@@ -2,6 +2,7 @@
|
|||||||
import codecs
|
import codecs
|
||||||
import os
|
import os
|
||||||
from setuptools import setup, find_packages
|
from setuptools import setup, find_packages
|
||||||
|
from setuptools.command.test import test as TestCommand, Command
|
||||||
|
|
||||||
|
|
||||||
read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
|
read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
|
||||||
@@ -9,10 +10,52 @@ read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
|
|||||||
pkgmeta = {
|
pkgmeta = {
|
||||||
'__title__': 'markdownify',
|
'__title__': 'markdownify',
|
||||||
'__author__': 'Matthew Tretter',
|
'__author__': 'Matthew Tretter',
|
||||||
'__version__': '0.11.2',
|
'__version__': '0.11.0',
|
||||||
}
|
}
|
||||||
|
|
||||||
read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
|
|
||||||
|
class PyTest(TestCommand):
|
||||||
|
def finalize_options(self):
|
||||||
|
TestCommand.finalize_options(self)
|
||||||
|
self.test_args = ['tests', '-s']
|
||||||
|
self.test_suite = True
|
||||||
|
|
||||||
|
def run_tests(self):
|
||||||
|
import pytest
|
||||||
|
errno = pytest.main(self.test_args)
|
||||||
|
raise SystemExit(errno)
|
||||||
|
|
||||||
|
|
||||||
|
class LintCommand(Command):
|
||||||
|
"""
|
||||||
|
A copy of flake8's Flake8Command
|
||||||
|
|
||||||
|
"""
|
||||||
|
description = "Run flake8 on modules registered in setuptools"
|
||||||
|
user_options = []
|
||||||
|
|
||||||
|
def initialize_options(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def finalize_options(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def distribution_files(self):
|
||||||
|
if self.distribution.packages:
|
||||||
|
for package in self.distribution.packages:
|
||||||
|
yield package.replace(".", os.path.sep)
|
||||||
|
|
||||||
|
if self.distribution.py_modules:
|
||||||
|
for filename in self.distribution.py_modules:
|
||||||
|
yield "%s.py" % filename
|
||||||
|
|
||||||
|
def run(self):
|
||||||
|
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)
|
||||||
|
raise SystemExit(report.total_errors > 0)
|
||||||
|
|
||||||
|
|
||||||
setup(
|
setup(
|
||||||
name='markdownify',
|
name='markdownify',
|
||||||
@@ -26,9 +69,14 @@ setup(
|
|||||||
packages=find_packages(),
|
packages=find_packages(),
|
||||||
zip_safe=False,
|
zip_safe=False,
|
||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
|
setup_requires=[
|
||||||
|
'flake8>=3.8,<5',
|
||||||
|
],
|
||||||
|
tests_require=[
|
||||||
|
'pytest>=6.2,<7',
|
||||||
|
],
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'beautifulsoup4>=4.9,<5',
|
'beautifulsoup4>=4.9,<5', 'six>=1.15,<2'
|
||||||
'six>=1.15,<2',
|
|
||||||
],
|
],
|
||||||
classifiers=[
|
classifiers=[
|
||||||
'Environment :: Web Environment',
|
'Environment :: Web Environment',
|
||||||
@@ -44,9 +92,8 @@ setup(
|
|||||||
'Programming Language :: Python :: 3.8',
|
'Programming Language :: Python :: 3.8',
|
||||||
'Topic :: Utilities'
|
'Topic :: Utilities'
|
||||||
],
|
],
|
||||||
entry_points={
|
cmdclass={
|
||||||
'console_scripts': [
|
'test': PyTest,
|
||||||
'markdownify = markdownify.main:main'
|
'lint': LintCommand,
|
||||||
]
|
},
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -177,17 +177,11 @@ def test_kbd():
|
|||||||
|
|
||||||
def test_p():
|
def test_p():
|
||||||
assert md('<p>hello</p>') == 'hello\n\n'
|
assert md('<p>hello</p>') == 'hello\n\n'
|
||||||
assert md('<p>123456789 123456789</p>') == '123456789 123456789\n\n'
|
|
||||||
assert md('<p>123456789 123456789</p>', wrap=True, wrap_width=10) == '123456789\n123456789\n\n'
|
|
||||||
assert md('<p><a href="https://example.com">Some long link</a></p>', wrap=True, wrap_width=10) == '[Some long\nlink](https://example.com)\n\n'
|
|
||||||
assert md('<p>12345<br />67890</p>', wrap=True, wrap_width=10, newline_style=BACKSLASH) == '12345\\\n67890\n\n'
|
|
||||||
assert md('<p>12345678901<br />12345</p>', wrap=True, wrap_width=10, newline_style=BACKSLASH) == '12345678901\\\n12345\n\n'
|
|
||||||
|
|
||||||
|
|
||||||
def test_pre():
|
def test_pre():
|
||||||
assert md('<pre>test\n foo\nbar</pre>') == '\n```\ntest\n foo\nbar\n```\n'
|
assert md('<pre>test\n foo\nbar</pre>') == '\n```\ntest\n foo\nbar\n```\n'
|
||||||
assert md('<pre><code>test\n foo\nbar</code></pre>') == '\n```\ntest\n foo\nbar\n```\n'
|
assert md('<pre><code>test\n foo\nbar</code></pre>') == '\n```\ntest\n foo\nbar\n```\n'
|
||||||
assert md('<pre>this_should_not_escape</pre>') == '\n```\nthis_should_not_escape\n```\n'
|
|
||||||
|
|
||||||
|
|
||||||
def test_s():
|
def test_s():
|
||||||
|
|||||||
@@ -139,26 +139,6 @@ table_missing_head = """<table>
|
|||||||
</tr>
|
</tr>
|
||||||
</table>"""
|
</table>"""
|
||||||
|
|
||||||
table_body = """<table>
|
|
||||||
<tbody>
|
|
||||||
<tr>
|
|
||||||
<td>Firstname</td>
|
|
||||||
<td>Lastname</td>
|
|
||||||
<td>Age</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td>Jill</td>
|
|
||||||
<td>Smith</td>
|
|
||||||
<td>50</td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td>Eve</td>
|
|
||||||
<td>Jackson</td>
|
|
||||||
<td>94</td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
|
||||||
</table>"""
|
|
||||||
|
|
||||||
|
|
||||||
def test_table():
|
def test_table():
|
||||||
assert md(table) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
assert md(table) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||||
@@ -168,4 +148,3 @@ def test_table():
|
|||||||
assert md(table_head_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
assert md(table_head_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||||
assert md(table_missing_text) == '\n\n| | Lastname | Age |\n| --- | --- | --- |\n| Jill | | 50 |\n| Eve | Jackson | 94 |\n\n'
|
assert md(table_missing_text) == '\n\n| | Lastname | Age |\n| --- | --- | --- |\n| Jill | | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||||
assert md(table_missing_head) == '\n\n| | | |\n| --- | --- | --- |\n| Firstname | Lastname | Age |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
assert md(table_missing_head) == '\n\n| | | |\n| --- | --- | --- |\n| Firstname | Lastname | Age |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||||
assert md(table_body) == '\n\n| | | |\n| --- | --- | --- |\n| Firstname | Lastname | Age |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
|
||||||
|
|||||||
Reference in New Issue
Block a user