Compare commits
83 Commits
tdgroot-co
...
0.13.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8c810eb8a8 | ||
|
|
f6c8daf8a5 | ||
|
|
75a678dab9 | ||
|
|
0a5c89aa49 | ||
|
|
51390d7389 | ||
|
|
50b4640db2 | ||
|
|
7861b330cd | ||
|
|
2ec33384de | ||
|
|
c1672aee44 | ||
|
|
43dbe20aaf | ||
|
|
46af45bb3c | ||
|
|
2bd0772685 | ||
|
|
383847ee86 | ||
|
|
74ddc408cc | ||
|
|
be3a7f4672 | ||
|
|
3b4a014f25 | ||
|
|
57d4f37923 | ||
|
|
d5fb0fbb85 | ||
|
|
e4df41225d | ||
|
|
804a3f8f07 | ||
|
|
7d0bf46057 | ||
|
|
2f9a42d3b8 | ||
|
|
96a25cfbf3 | ||
|
|
0477a0c8a0 | ||
|
|
f33ccd7c1a | ||
|
|
a2f82678f7 | ||
|
|
60967c1c95 | ||
|
|
c7718b6d81 | ||
|
|
2b22d239ad | ||
|
|
8219d2a673 | ||
|
|
e6e23fd512 | ||
|
|
433fad2dec | ||
|
|
4fb451ffa6 | ||
|
|
0c8ac578c9 | ||
|
|
e8d041c251 | ||
|
|
f729c3ba43 | ||
|
|
eddfdae4ca | ||
|
|
8f047753ae | ||
|
|
50b3b73a8f | ||
|
|
0310216877 | ||
|
|
194c646a20 | ||
|
|
9914474828 | ||
|
|
6263f0e5f0 | ||
|
|
17d8586843 | ||
|
|
59eb069700 | ||
|
|
e79971a7eb | ||
|
|
2c533339cf | ||
|
|
5adda130b8 | ||
|
|
5f1b98e25d | ||
|
|
16acd2b763 | ||
|
|
2b8cf444f1 | ||
|
|
207d0f4ec6 | ||
|
|
ebb9ea713d | ||
|
|
d375116807 | ||
|
|
87b9f6c88e | ||
|
|
bda367dad9 | ||
|
|
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,11 +23,7 @@ jobs:
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install flake8==3.8.4 pytest
|
||||
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
|
||||
- name: Lint with flake8
|
||||
pip install tox
|
||||
- name: Lint and test
|
||||
run: |
|
||||
python setup.py lint
|
||||
- name: Test with pytest
|
||||
run: |
|
||||
python setup.py test
|
||||
tox
|
||||
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -9,3 +9,4 @@
|
||||
/venv
|
||||
build/
|
||||
.vscode/settings.json
|
||||
.tox/
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
include README.rst
|
||||
prune tests
|
||||
|
||||
61
README.rst
61
README.rst
@@ -1,8 +1,8 @@
|
||||
|build| |version| |license| |downloads|
|
||||
|
||||
.. |build| image:: https://img.shields.io/github/workflow/status/matthewwithanm/python-markdownify/Python%20application/develop
|
||||
.. |build| image:: https://img.shields.io/github/actions/workflow/status/matthewwithanm/python-markdownify/python-app.yml?branch=develop
|
||||
:alt: GitHub Workflow Status
|
||||
:target: https://github.com/matthewwithanm/python-markdownify/actions?query=workflow%3A%22Python+application%22
|
||||
:target: https://github.com/matthewwithanm/python-markdownify/actions/workflows/python-app.yml?query=workflow%3A%22Python+application%22
|
||||
|
||||
.. |version| image:: https://img.shields.io/pypi/v/markdownify
|
||||
:alt: Pypi version
|
||||
@@ -87,12 +87,16 @@ strong_em_symbol
|
||||
sub_symbol, sup_symbol
|
||||
Define the chars that surround ``<sub>`` and ``<sup>`` text. Defaults to an
|
||||
empty string, because this is non-standard behavior. Could be something like
|
||||
``~`` and ``^`` to result in ``~sub~`` and ``^sup^``.
|
||||
``~`` and ``^`` to result in ``~sub~`` and ``^sup^``. If the value starts
|
||||
with ``<`` and ends with ``>``, it is treated as an HTML tag and a ``/`` is
|
||||
inserted after the ``<`` in the string used after the text; this allows
|
||||
specifying ``<sub>`` to use raw HTML in the output for subscripts, for
|
||||
example.
|
||||
|
||||
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
|
||||
while ``BACKSLASH`` will convert a linebreak to ``\\n`` (a backslash and a
|
||||
newline). While the latter convention is non-standard, it is commonly
|
||||
preferred and supported by a lot of interpreters.
|
||||
|
||||
@@ -123,6 +127,11 @@ escape_underscores
|
||||
If set to ``False``, do not escape ``_`` to ``\_`` in text.
|
||||
Defaults to ``True``.
|
||||
|
||||
escape_misc
|
||||
If set to ``False``, do not escape miscellaneous punctuation characters
|
||||
that sometimes have Markdown significance in text.
|
||||
Defaults to ``True``.
|
||||
|
||||
keep_inline_images_in
|
||||
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
|
||||
@@ -130,6 +139,11 @@ keep_inline_images_in
|
||||
that should be allowed to contain inline images, for example ``['td']``.
|
||||
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
|
||||
nested ``Options`` class in ``MarkdownConverter`` subclasses.
|
||||
|
||||
@@ -151,7 +165,12 @@ Creating Custom Converters
|
||||
|
||||
If you have a special usecase that calls for a special conversion, you can
|
||||
always inherit from ``MarkdownConverter`` and override the method you want to
|
||||
change:
|
||||
change.
|
||||
The function that handles a HTML tag named ``abc`` is called
|
||||
``convert_abc(self, el, text, convert_as_inline)`` and returns a string
|
||||
containing the converted HTML tag.
|
||||
The ``MarkdownConverter`` object will handle the conversion based on the
|
||||
function names:
|
||||
|
||||
.. code:: python
|
||||
|
||||
@@ -168,14 +187,32 @@ change:
|
||||
def md(html, **options):
|
||||
return ImageBlockConverter(**options).convert(html)
|
||||
|
||||
.. code:: python
|
||||
|
||||
from markdownify import MarkdownConverter
|
||||
|
||||
class IgnoreParagraphsConverter(MarkdownConverter):
|
||||
"""
|
||||
Create a custom MarkdownConverter that ignores paragraphs
|
||||
"""
|
||||
def convert_p(self, el, text, convert_as_inline):
|
||||
return ''
|
||||
|
||||
# Create shorthand method for conversion
|
||||
def md(html, **options):
|
||||
return IgnoreParagraphsConverter(**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
|
||||
===========
|
||||
|
||||
To run tests:
|
||||
|
||||
``python setup.py test``
|
||||
|
||||
To lint:
|
||||
|
||||
``python setup.py lint``
|
||||
To run tests and the linter run ``pip install tox`` once, then ``tox``.
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from bs4 import BeautifulSoup, NavigableString, Comment, Doctype
|
||||
from textwrap import fill
|
||||
import re
|
||||
import six
|
||||
|
||||
@@ -42,15 +43,22 @@ def abstract_inline_conversion(markup_fn):
|
||||
"""
|
||||
This abstracts all simple inline tags like b, em, del, ...
|
||||
Returns a function that wraps the chomped text in a pair of the string
|
||||
that is returned by markup_fn. markup_fn is necessary to allow for
|
||||
that is returned by markup_fn, with '/' inserted in the string used after
|
||||
the text if it looks like an HTML tag. markup_fn is necessary to allow for
|
||||
references to self.strong_em_symbol etc.
|
||||
"""
|
||||
def implementation(self, el, text, convert_as_inline):
|
||||
markup = markup_fn(self)
|
||||
markup_prefix = markup_fn(self)
|
||||
if markup_prefix.startswith('<') and markup_prefix.endswith('>'):
|
||||
markup_suffix = '</' + markup_prefix[1:]
|
||||
else:
|
||||
markup_suffix = markup_prefix
|
||||
if el.find_parent(['pre', 'code', 'kbd', 'samp']):
|
||||
return text
|
||||
prefix, suffix, text = chomp(text)
|
||||
if not text:
|
||||
return ''
|
||||
return '%s%s%s%s%s' % (prefix, markup, text, markup, suffix)
|
||||
return '%s%s%s%s%s' % (prefix, markup_prefix, text, markup_suffix, suffix)
|
||||
return implementation
|
||||
|
||||
|
||||
@@ -68,6 +76,7 @@ class MarkdownConverter(object):
|
||||
default_title = False
|
||||
escape_asterisks = True
|
||||
escape_underscores = True
|
||||
escape_misc = True
|
||||
heading_style = UNDERLINED
|
||||
keep_inline_images_in = []
|
||||
newline_style = SPACES
|
||||
@@ -75,6 +84,8 @@ class MarkdownConverter(object):
|
||||
strong_em_symbol = ASTERISK
|
||||
sub_symbol = ''
|
||||
sup_symbol = ''
|
||||
wrap = False
|
||||
wrap_width = 80
|
||||
|
||||
class Options(DefaultOptions):
|
||||
pass
|
||||
@@ -149,13 +160,12 @@ class MarkdownConverter(object):
|
||||
def process_text(self, el):
|
||||
text = six.text_type(el) or ''
|
||||
|
||||
# dont remove any whitespace when handling pre or code in pre
|
||||
if not (el.parent.name == 'pre'
|
||||
or (el.parent.name == 'code'
|
||||
and el.parent.parent.name == 'pre')):
|
||||
# normalize whitespace if we're not inside a preformatted element
|
||||
if not el.find_parent('pre'):
|
||||
text = whitespace_re.sub(' ', text)
|
||||
|
||||
if el.parent.name != 'code':
|
||||
# escape special characters if we're not inside a preformatted or code element
|
||||
if not el.find_parent(['pre', 'code', 'kbd', 'samp']):
|
||||
text = self.escape(text)
|
||||
|
||||
# remove trailing whitespaces if any of the following condition is true:
|
||||
@@ -197,6 +207,9 @@ class MarkdownConverter(object):
|
||||
def escape(self, text):
|
||||
if not text:
|
||||
return ''
|
||||
if self.options['escape_misc']:
|
||||
text = re.sub(r'([\\&<`[>~#=+|-])', r'\\\1', text)
|
||||
text = re.sub(r'([0-9])([.)])', r'\1\\\2', text)
|
||||
if self.options['escape_asterisks']:
|
||||
text = text.replace('*', r'\*')
|
||||
if self.options['escape_underscores']:
|
||||
@@ -235,7 +248,7 @@ class MarkdownConverter(object):
|
||||
if convert_as_inline:
|
||||
return text
|
||||
|
||||
return '\n' + (line_beginning_re.sub('> ', text) + '\n\n') if text else ''
|
||||
return '\n' + (line_beginning_re.sub('> ', text.strip()) + '\n\n') if text else ''
|
||||
|
||||
def convert_br(self, el, text, convert_as_inline):
|
||||
if convert_as_inline:
|
||||
@@ -263,7 +276,7 @@ class MarkdownConverter(object):
|
||||
return text
|
||||
|
||||
style = self.options['heading_style'].lower()
|
||||
text = text.rstrip()
|
||||
text = text.strip()
|
||||
if style == UNDERLINED and n <= 2:
|
||||
line = '=' if n == 1 else '-'
|
||||
return self.underline(text, line)
|
||||
@@ -313,7 +326,7 @@ class MarkdownConverter(object):
|
||||
def convert_li(self, el, text, convert_as_inline):
|
||||
parent = el.parent
|
||||
if parent is not None and parent.name == 'ol':
|
||||
if parent.get("start"):
|
||||
if parent.get("start") and str(parent.get("start")).isnumeric():
|
||||
start = int(parent.get("start"))
|
||||
else:
|
||||
start = 1
|
||||
@@ -331,6 +344,11 @@ class MarkdownConverter(object):
|
||||
def convert_p(self, el, text, convert_as_inline):
|
||||
if convert_as_inline:
|
||||
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 ''
|
||||
|
||||
def convert_pre(self, el, text, convert_as_inline):
|
||||
@@ -343,6 +361,12 @@ class MarkdownConverter(object):
|
||||
|
||||
return '\n```%s\n%s\n```\n' % (code_language, text)
|
||||
|
||||
def convert_script(self, el, text, convert_as_inline):
|
||||
return ''
|
||||
|
||||
def convert_style(self, el, text, convert_as_inline):
|
||||
return ''
|
||||
|
||||
convert_s = convert_del
|
||||
|
||||
convert_strong = convert_b
|
||||
@@ -356,22 +380,49 @@ class MarkdownConverter(object):
|
||||
def convert_table(self, el, text, convert_as_inline):
|
||||
return '\n\n' + text + '\n'
|
||||
|
||||
def convert_caption(self, el, text, convert_as_inline):
|
||||
return text + '\n'
|
||||
|
||||
def convert_figcaption(self, el, text, convert_as_inline):
|
||||
return '\n\n' + text + '\n\n'
|
||||
|
||||
def convert_td(self, el, text, convert_as_inline):
|
||||
return ' ' + text + ' |'
|
||||
colspan = 1
|
||||
if 'colspan' in el.attrs and el['colspan'].isdigit():
|
||||
colspan = int(el['colspan'])
|
||||
return ' ' + text.strip().replace("\n", " ") + ' |' * colspan
|
||||
|
||||
def convert_th(self, el, text, convert_as_inline):
|
||||
return ' ' + text + ' |'
|
||||
colspan = 1
|
||||
if 'colspan' in el.attrs and el['colspan'].isdigit():
|
||||
colspan = int(el['colspan'])
|
||||
return ' ' + text.strip().replace("\n", " ") + ' |' * colspan
|
||||
|
||||
def convert_tr(self, el, text, convert_as_inline):
|
||||
cells = el.find_all(['td', 'th'])
|
||||
is_headrow = all([cell.name == 'th' for cell in cells])
|
||||
is_headrow = (
|
||||
all([cell.name == 'th' for cell in cells])
|
||||
or (not el.previous_sibling and not el.parent.name == 'tbody')
|
||||
or (not el.previous_sibling and el.parent.name == 'tbody' and len(el.parent.parent.find_all(['thead'])) < 1)
|
||||
)
|
||||
overline = ''
|
||||
underline = ''
|
||||
if is_headrow and not el.previous_sibling:
|
||||
# first row and is headline: print headline underline
|
||||
underline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'
|
||||
elif not el.previous_sibling and not el.parent.name != 'table':
|
||||
# first row, not headline, and the parent is sth. like tbody:
|
||||
full_colspan = 0
|
||||
for cell in cells:
|
||||
if 'colspan' in cell.attrs and cell['colspan'].isdigit():
|
||||
full_colspan += int(cell["colspan"])
|
||||
else:
|
||||
full_colspan += 1
|
||||
underline += '| ' + ' | '.join(['---'] * full_colspan) + ' |' + '\n'
|
||||
elif (not el.previous_sibling
|
||||
and (el.parent.name == 'table'
|
||||
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
|
||||
overline += '| ' + ' | '.join([''] * len(cells)) + ' |' + '\n'
|
||||
overline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'
|
||||
|
||||
73
markdownify/main.py
Normal file
73
markdownify/main.py
Normal file
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import argparse
|
||||
import sys
|
||||
|
||||
from markdownify import markdownify, ATX, ATX_CLOSED, UNDERLINED, \
|
||||
SPACES, BACKSLASH, ASTERISK, UNDERSCORE
|
||||
|
||||
|
||||
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', default=UNDERLINED,
|
||||
choices=(ATX, ATX_CLOSED, 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('--strong-em-symbol', default=ASTERISK,
|
||||
choices=(ASTERISK, UNDERSCORE),
|
||||
help="Use * or _ to convert strong and italics text"),
|
||||
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('--newline-style', default=SPACES,
|
||||
choices=(SPACES, BACKSLASH),
|
||||
help="Defines the style of <br> conversions: two spaces "
|
||||
"or backslash at the and of the line thet should break.")
|
||||
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,7 +2,6 @@
|
||||
import codecs
|
||||
import os
|
||||
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()
|
||||
@@ -10,52 +9,10 @@ read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
|
||||
pkgmeta = {
|
||||
'__title__': 'markdownify',
|
||||
'__author__': 'Matthew Tretter',
|
||||
'__version__': '0.10.3',
|
||||
'__version__': '0.13.0',
|
||||
}
|
||||
|
||||
|
||||
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)
|
||||
|
||||
read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
|
||||
|
||||
setup(
|
||||
name='markdownify',
|
||||
@@ -69,14 +26,9 @@ setup(
|
||||
packages=find_packages(),
|
||||
zip_safe=False,
|
||||
include_package_data=True,
|
||||
setup_requires=[
|
||||
'flake8>=3.8,<5',
|
||||
],
|
||||
tests_require=[
|
||||
'pytest>=6.2,<7',
|
||||
],
|
||||
install_requires=[
|
||||
'beautifulsoup4>=4.9,<5', 'six>=1.15,<2'
|
||||
'beautifulsoup4>=4.9,<5',
|
||||
'six>=1.15,<2',
|
||||
],
|
||||
classifiers=[
|
||||
'Environment :: Web Environment',
|
||||
@@ -92,8 +44,9 @@ setup(
|
||||
'Programming Language :: Python :: 3.8',
|
||||
'Topic :: Utilities'
|
||||
],
|
||||
cmdclass={
|
||||
'test': PyTest,
|
||||
'lint': LintCommand,
|
||||
},
|
||||
entry_points={
|
||||
'console_scripts': [
|
||||
'markdownify = markdownify.main:main'
|
||||
]
|
||||
}
|
||||
)
|
||||
|
||||
10
shell.nix
Normal file
10
shell.nix
Normal file
@@ -0,0 +1,10 @@
|
||||
{ pkgs ? import <nixpkgs> {} }:
|
||||
pkgs.mkShell {
|
||||
name = "python-shell";
|
||||
buildInputs = with pkgs; [
|
||||
python38
|
||||
python38Packages.tox
|
||||
python38Packages.setuptools
|
||||
python38Packages.virtualenv
|
||||
];
|
||||
}
|
||||
@@ -52,6 +52,12 @@ def test_b_spaces():
|
||||
|
||||
def test_blockquote():
|
||||
assert md('<blockquote>Hello</blockquote>') == '\n> Hello\n\n'
|
||||
assert md('<blockquote>\nHello\n</blockquote>') == '\n> Hello\n\n'
|
||||
|
||||
|
||||
def test_blockquote_with_nested_paragraph():
|
||||
assert md('<blockquote><p>Hello</p></blockquote>') == '\n> Hello\n\n'
|
||||
assert md('<blockquote><p>Hello</p><p>Hello again</p></blockquote>') == '\n> Hello\n> \n> Hello again\n\n'
|
||||
|
||||
|
||||
def test_blockquote_with_paragraph():
|
||||
@@ -60,7 +66,7 @@ def test_blockquote_with_paragraph():
|
||||
|
||||
def test_blockquote_nested():
|
||||
text = md('<blockquote>And she was like <blockquote>Hello</blockquote></blockquote>')
|
||||
assert text == '\n> And she was like \n> > Hello\n> \n> \n\n'
|
||||
assert text == '\n> And she was like \n> > Hello\n\n'
|
||||
|
||||
|
||||
def test_br():
|
||||
@@ -68,9 +74,29 @@ def test_br():
|
||||
assert md('a<br />b<br />c', newline_style=BACKSLASH) == 'a\\\nb\\\nc'
|
||||
|
||||
|
||||
def test_caption():
|
||||
assert md('TEXT<figure><figcaption>Caption</figcaption><span>SPAN</span></figure>') == 'TEXT\n\nCaption\n\nSPAN'
|
||||
assert md('<figure><span>SPAN</span><figcaption>Caption</figcaption></figure>TEXT') == 'SPAN\n\nCaption\n\nTEXT'
|
||||
|
||||
|
||||
def test_code():
|
||||
inline_tests('code', '`')
|
||||
assert md('<code>this_should_not_escape</code>') == '`this_should_not_escape`'
|
||||
assert md('<code>*this_should_not_escape*</code>') == '`*this_should_not_escape*`'
|
||||
assert md('<kbd>*this_should_not_escape*</kbd>') == '`*this_should_not_escape*`'
|
||||
assert md('<samp>*this_should_not_escape*</samp>') == '`*this_should_not_escape*`'
|
||||
assert md('<code><span>*this_should_not_escape*</span></code>') == '`*this_should_not_escape*`'
|
||||
assert md('<code>this should\t\tnormalize</code>') == '`this should normalize`'
|
||||
assert md('<code><span>this should\t\tnormalize</span></code>') == '`this should normalize`'
|
||||
assert md('<code>foo<b>bar</b>baz</code>') == '`foobarbaz`'
|
||||
assert md('<kbd>foo<i>bar</i>baz</kbd>') == '`foobarbaz`'
|
||||
assert md('<samp>foo<del> bar </del>baz</samp>') == '`foo bar baz`'
|
||||
assert md('<samp>foo <del>bar</del> baz</samp>') == '`foo bar baz`'
|
||||
assert md('<code>foo<em> bar </em>baz</code>') == '`foo bar baz`'
|
||||
assert md('<code>foo<code> bar </code>baz</code>') == '`foo bar baz`'
|
||||
assert md('<code>foo<strong> bar </strong>baz</code>') == '`foo bar baz`'
|
||||
assert md('<code>foo<s> bar </s>baz</code>') == '`foo bar baz`'
|
||||
assert md('<code>foo<sup>bar</sup>baz</code>', sup_symbol='^') == '`foobarbaz`'
|
||||
assert md('<code>foo<sub>bar</sub>baz</code>', sub_symbol='^') == '`foobarbaz`'
|
||||
|
||||
|
||||
def test_del():
|
||||
@@ -85,6 +111,14 @@ def test_em():
|
||||
inline_tests('em', '*')
|
||||
|
||||
|
||||
def test_header_with_space():
|
||||
assert md('<h3>\n\nHello</h3>') == '### Hello\n\n'
|
||||
assert md('<h4>\n\nHello</h4>') == '#### Hello\n\n'
|
||||
assert md('<h5>\n\nHello</h5>') == '##### Hello\n\n'
|
||||
assert md('<h5>\n\nHello\n\n</h5>') == '##### Hello\n\n'
|
||||
assert md('<h5>\n\nHello \n\n</h5>') == '##### Hello\n\n'
|
||||
|
||||
|
||||
def test_h1():
|
||||
assert md('<h1>Hello</h1>') == 'Hello\n=====\n\n'
|
||||
|
||||
@@ -177,11 +211,39 @@ def test_kbd():
|
||||
|
||||
def test_p():
|
||||
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():
|
||||
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>*this_should_not_escape*</pre>') == '\n```\n*this_should_not_escape*\n```\n'
|
||||
assert md('<pre><span>*this_should_not_escape*</span></pre>') == '\n```\n*this_should_not_escape*\n```\n'
|
||||
assert md('<pre>\t\tthis should\t\tnot normalize</pre>') == '\n```\n\t\tthis should\t\tnot normalize\n```\n'
|
||||
assert md('<pre><span>\t\tthis should\t\tnot normalize</span></pre>') == '\n```\n\t\tthis should\t\tnot normalize\n```\n'
|
||||
assert md('<pre>foo<b>\nbar\n</b>baz</pre>') == '\n```\nfoo\nbar\nbaz\n```\n'
|
||||
assert md('<pre>foo<i>\nbar\n</i>baz</pre>') == '\n```\nfoo\nbar\nbaz\n```\n'
|
||||
assert md('<pre>foo\n<i>bar</i>\nbaz</pre>') == '\n```\nfoo\nbar\nbaz\n```\n'
|
||||
assert md('<pre>foo<i>\n</i>baz</pre>') == '\n```\nfoo\nbaz\n```\n'
|
||||
assert md('<pre>foo<del>\nbar\n</del>baz</pre>') == '\n```\nfoo\nbar\nbaz\n```\n'
|
||||
assert md('<pre>foo<em>\nbar\n</em>baz</pre>') == '\n```\nfoo\nbar\nbaz\n```\n'
|
||||
assert md('<pre>foo<code>\nbar\n</code>baz</pre>') == '\n```\nfoo\nbar\nbaz\n```\n'
|
||||
assert md('<pre>foo<strong>\nbar\n</strong>baz</pre>') == '\n```\nfoo\nbar\nbaz\n```\n'
|
||||
assert md('<pre>foo<s>\nbar\n</s>baz</pre>') == '\n```\nfoo\nbar\nbaz\n```\n'
|
||||
assert md('<pre>foo<sup>\nbar\n</sup>baz</pre>', sup_symbol='^') == '\n```\nfoo\nbar\nbaz\n```\n'
|
||||
assert md('<pre>foo<sub>\nbar\n</sub>baz</pre>', sub_symbol='^') == '\n```\nfoo\nbar\nbaz\n```\n'
|
||||
|
||||
|
||||
def test_script():
|
||||
assert md('foo <script>var foo=42;</script> bar') == 'foo bar'
|
||||
|
||||
|
||||
def test_style():
|
||||
assert md('foo <style>h1 { font-size: larger }</style> bar') == 'foo bar'
|
||||
|
||||
|
||||
def test_s():
|
||||
@@ -206,11 +268,13 @@ def test_strong_em_symbol():
|
||||
def test_sub():
|
||||
assert md('<sub>foo</sub>') == 'foo'
|
||||
assert md('<sub>foo</sub>', sub_symbol='~') == '~foo~'
|
||||
assert md('<sub>foo</sub>', sub_symbol='<sub>') == '<sub>foo</sub>'
|
||||
|
||||
|
||||
def test_sup():
|
||||
assert md('<sup>foo</sup>') == 'foo'
|
||||
assert md('<sup>foo</sup>', sup_symbol='^') == '^foo^'
|
||||
assert md('<sup>foo</sup>', sup_symbol='<sup>') == '<sup>foo</sup>'
|
||||
|
||||
|
||||
def test_lang():
|
||||
|
||||
@@ -12,7 +12,7 @@ def test_underscore():
|
||||
|
||||
|
||||
def test_xml_entities():
|
||||
assert md('&') == '&'
|
||||
assert md('&') == r'\&'
|
||||
|
||||
|
||||
def test_named_entities():
|
||||
@@ -25,4 +25,23 @@ def test_hexadecimal_entities():
|
||||
|
||||
|
||||
def test_single_escaping_entities():
|
||||
assert md('&amp;') == '&'
|
||||
assert md('&amp;') == r'\&'
|
||||
|
||||
|
||||
def text_misc():
|
||||
assert md('\\*') == r'\\\*'
|
||||
assert md('<foo>') == r'\<foo\>'
|
||||
assert md('# foo') == r'\# foo'
|
||||
assert md('> foo') == r'\> foo'
|
||||
assert md('~~foo~~') == r'\~\~foo\~\~'
|
||||
assert md('foo\n===\n') == 'foo\n\\=\\=\\=\n'
|
||||
assert md('---\n') == '\\-\\-\\-\n'
|
||||
assert md('+ x\n+ y\n') == '\\+ x\n\\+ y\n'
|
||||
assert md('`x`') == r'\`x\`'
|
||||
assert md('[text](link)') == r'\[text](link)'
|
||||
assert md('1. x') == r'1\. x'
|
||||
assert md('not a number. x') == r'not a number. x'
|
||||
assert md('1) x') == r'1\) x'
|
||||
assert md('not a number) x') == r'not a number) x'
|
||||
assert md('|not table|') == r'\|not table\|'
|
||||
assert md(r'\ <foo> &amp; | ` `', escape_misc=False) == r'\ <foo> & | ` `'
|
||||
|
||||
@@ -43,6 +43,9 @@ nested_ols = """
|
||||
def test_ol():
|
||||
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>') == '3. a\n4. b\n'
|
||||
assert md('<ol start="-1"><li>a</li><li>b</li></ol>') == '1. a\n2. b\n'
|
||||
assert md('<ol start="foo"><li>a</li><li>b</li></ol>') == '1. a\n2. b\n'
|
||||
assert md('<ol start="1.5"><li>a</li><li>b</li></ol>') == '1. a\n2. b\n'
|
||||
|
||||
|
||||
def test_nested_ols():
|
||||
|
||||
@@ -57,6 +57,26 @@ table_with_paragraphs = """<table>
|
||||
</tr>
|
||||
</table>"""
|
||||
|
||||
table_with_linebreaks = """<table>
|
||||
<tr>
|
||||
<th>Firstname</th>
|
||||
<th>Lastname</th>
|
||||
<th>Age</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Jill</td>
|
||||
<td>Smith
|
||||
Jackson</td>
|
||||
<td>50</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Eve</td>
|
||||
<td>Jackson
|
||||
Smith</td>
|
||||
<td>94</td>
|
||||
</tr>
|
||||
</table>"""
|
||||
|
||||
|
||||
table_with_header_column = """<table>
|
||||
<tr>
|
||||
@@ -99,6 +119,28 @@ table_head_body = """<table>
|
||||
</tbody>
|
||||
</table>"""
|
||||
|
||||
table_head_body_missing_head = """<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<td>Firstname</td>
|
||||
<td>Lastname</td>
|
||||
<td>Age</td>
|
||||
</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 = """<table>
|
||||
<thead>
|
||||
<tr>
|
||||
@@ -139,12 +181,74 @@ table_missing_head = """<table>
|
||||
</tr>
|
||||
</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>"""
|
||||
|
||||
table_with_caption = """TEXT<table><caption>Caption</caption>
|
||||
<tbody><tr><td>Firstname</td>
|
||||
<td>Lastname</td>
|
||||
<td>Age</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>"""
|
||||
|
||||
table_with_colspan = """<table>
|
||||
<tr>
|
||||
<th colspan="2">Name</th>
|
||||
<th>Age</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="1">Jill</td>
|
||||
<td>Smith</td>
|
||||
<td>50</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Eve</td>
|
||||
<td>Jackson</td>
|
||||
<td>94</td>
|
||||
</tr>
|
||||
</table>"""
|
||||
|
||||
table_with_undefined_colspan = """<table>
|
||||
<tr>
|
||||
<th colspan="undefined">Name</th>
|
||||
<th>Age</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td colspan="-1">Jill</td>
|
||||
<td>Smith</td>
|
||||
</tr>
|
||||
</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_with_html_content) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| **Jill** | *Smith* | [50](#) |\n| Eve | Jackson | 94 |\n\n'
|
||||
assert md(table_with_paragraphs) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||
assert md(table_with_linebreaks) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith Jackson | 50 |\n| Eve | Jackson Smith | 94 |\n\n'
|
||||
assert md(table_with_header_column) == '\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_head_body_missing_head) == '\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_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| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||
assert md(table_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||
assert md(table_with_caption) == 'TEXT\n\nCaption\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n\n'
|
||||
assert md(table_with_colspan) == '\n\n| Name | | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||
assert md(table_with_undefined_colspan) == '\n\n| Name | Age |\n| --- | --- |\n| Jill | Smith |\n\n'
|
||||
|
||||
Reference in New Issue
Block a user