Compare commits

...

96 Commits
0.6.0 ... 0.9.0

Author SHA1 Message Date
AlexVonB
59417ab115 Merge branch 'develop' 2021-05-30 19:10:49 +02:00
AlexVonB
cec570fc49 bump to v0.9.0 2021-05-30 19:10:31 +02:00
AlexVonB
a6a31624ad add options for sub and sup tags
fixes #44
2021-05-30 19:07:43 +02:00
AlexVonB
6f3732307d restructured test files 2021-05-30 19:06:52 +02:00
AlexVonB
8f6d7e500d add option 'default_title' to links
fixes #39
2021-05-30 18:40:40 +02:00
AlexVonB
917b01e548 Merge branch 'develop' 2021-05-30 11:20:32 +02:00
AlexVonB
e96351b666 bump to v0.8.1 2021-05-30 11:20:16 +02:00
AlexVonB
129c4ef060 ignore doctype tag, test cdata tag
fixes #45
2021-05-30 11:18:18 +02:00
AlexVonB
652714859d Merge branch 'develop' 2021-05-21 14:18:14 +02:00
AlexVonB
9cb940cbc0 bump to v0.8.0 2021-05-21 14:17:51 +02:00
AlexVonB
70ef9b6e48 added pre tag
closes #15
2021-05-21 14:15:41 +02:00
AlexVonB
91d53ddd5a refactor simple inline conversions 2021-05-21 13:53:00 +02:00
AlexVonB
079f32f6cd added del and s tags 2021-05-21 12:27:49 +02:00
AlexVonB
89b577e91e ordering functions alphabetically 2021-05-21 12:21:21 +02:00
AlexVonB
4bf2ea44fc Merge branch 'AndrewCRichards-andrewcrichards/add_code_samp_kbd_tags' into develop 2021-05-21 12:13:48 +02:00
AlexVonB
77797ebb79 Merge branch 'andrewcrichards/add_code_samp_kbd_tags' of https://github.com/AndrewCRichards/python-markdownify into AndrewCRichards-andrewcrichards/add_code_samp_kbd_tags 2021-05-21 12:11:59 +02:00
AlexVonB
ea5b22824b Merge branch 'develop' 2021-05-18 10:42:27 +02:00
AlexVonB
9f3c4c9fa0 bump to v0.7.4 2021-05-18 10:42:16 +02:00
AlexVonB
967db26b3a Merge branch 'fix-headless-tables' into develop 2021-05-18 10:41:42 +02:00
AlexVonB
ea81407b87 implemented table parsing correctly
instead of manually walking down the dom tree
in a table, we now rely on the main descent loop
and just implement conversion for rows and cells
correctly. this enables the use of html inside a
table cell.
2021-05-17 14:00:00 +02:00
AlexVonB
e6da15c173 allow tables with headers in first (or any) column 2021-05-17 12:36:48 +02:00
AlexVonB
7dac92e85e Allow for tables without header row
fixes #42
2021-05-16 19:02:04 +02:00
AlexVonB
ec5858e42f Merge branch 'develop' 2021-05-16 18:41:24 +02:00
AlexVonB
fc29483899 bump to v0.7.3 2021-05-16 18:41:08 +02:00
AlexVonB
bd7a8d6990 Merge pull request #43 from jiulongw/develop
Fix missing whitespaces in <li> node
2021-05-16 18:39:58 +02:00
Jiulong Wang
ddfbf6a364 Keep important spaces in <li> element 2021-05-10 16:07:54 -07:00
Jiulong Wang
91a64e3cd4 Fix missing whitespaces in <li> node 2021-05-10 14:42:05 -07:00
AlexVonB
02bb914ef3 Merge branch 'develop' 2021-05-02 13:49:30 +02:00
AlexVonB
0fee4b0a80 bump to v0.7.2 2021-05-02 13:49:14 +02:00
AlexVonB
10e1ff3e6e Merge pull request #23 from SimonIT/ordere-list-update
Ordered list update
2021-05-02 13:47:43 +02:00
AlexVonB
73800ced36 fixed whitespace issues at nested lists 2021-05-02 13:44:09 +02:00
AlexVonB
1538cacb94 Merge branch 'develop' into ordere-list-update 2021-05-02 10:58:13 +02:00
AlexVonB
21c0d034d0 Merge branch 'develop' 2021-05-02 10:51:00 +02:00
AlexVonB
f59f9f9a54 bump to v0.7.1 2021-05-02 10:50:49 +02:00
AlexVonB
bd22a16c9e Merge pull request #40 from jiulongw/jiulongw/hr
Add conversion for hr element
2021-05-02 10:47:32 +02:00
AlexVonB
55fb96e3c0 fix hr tests 2021-05-02 10:45:52 +02:00
Jiulong Wang
5f102d5223 Add conversion for hr element 2021-04-29 13:41:28 -07:00
AlexVonB
e3ddc789a2 Merge branch 'develop' 2021-04-22 12:43:27 +02:00
AlexVonB
651d5f00e8 bump to v0.7.0 2021-04-22 12:43:17 +02:00
AlexVonB
3cf324d03d Merge pull request #36 from BrunoMiguens/add-basic-support-for-tables
Add basic support for tables
2021-04-22 12:41:54 +02:00
AlexVonB
96f7e7d307 Merge branch 'develop' into add-basic-support-for-tables 2021-04-22 12:40:16 +02:00
AlexVonB
e1dbbfad42 guard table lines with pipes, resolves the empty header problem 2021-04-22 12:36:11 +02:00
AlexVonB
2d0cd97323 Merge branch 'develop' 2021-04-22 12:13:03 +02:00
AlexVonB
d4882b86b9 bump to v0.6.6 2021-04-22 12:12:51 +02:00
AlexVonB
b47d5f11c8 Merge pull request #37 from andredelft/develop
Add `strong_em_symbol` and `newline` options to the converter
2021-04-18 21:35:16 +02:00
André van Delft
29c794e17d Introduce OPTIONs for strong_em_symbol 2021-04-18 18:13:29 +02:00
André van Delft
e877602a5e Separate the strong_em_symbol and newline style tests 2021-04-05 11:28:42 +02:00
André van Delft
5580b0b51d Update README.rst 2021-04-05 11:13:52 +02:00
André van Delft
650f377b64 Fix linting 2021-04-05 11:13:19 +02:00
André van Delft
7ee87b1d32 Use .lower() on _style option fetching 2021-04-05 10:50:23 +02:00
André van Delft
16dbc471b9 Test newline_style 2021-04-05 10:47:55 +02:00
André van Delft
c04ec855dd Change option to newline_style and use variables like heading_style does 2021-04-05 10:44:20 +02:00
André van Delft
8da0bdf998 Test strong_em_symbol 2021-04-05 10:28:46 +02:00
AlexVonB
ec185e2e9c Merge branch 'develop' 2021-02-21 23:09:55 +01:00
AlexVonB
a59e4b9f48 bump to v0.6.5 2021-02-21 23:09:44 +01:00
AlexVonB
fd293a9714 use python 3.8 instead of 3.6 2021-02-21 23:08:49 +01:00
AlexVonB
99365de669 upgrading code for python 3.x
closes #38
2021-02-21 23:06:21 +01:00
AlexVonB
079d1721aa Merge branch 'develop' 2021-02-21 20:58:34 +01:00
AlexVonB
ed406d3206 bump to v0.6.4 2021-02-21 20:57:57 +01:00
AlexVonB
f320cf87ff closing #25 and #18
Adds newlines after blockquotes, allowing for paragraphs after a
blockquote.

Due to merging problems with @lucafrance 's code I had to quickly copy
and paste their code. Thanks for the contribution!
2021-02-21 20:53:44 +01:00
André van Delft
a79ed44ec3 Fix code ticks in README 2021-02-15 16:51:20 +01:00
André van Delft
29a4e551f7 Update README with the two new options 2021-02-15 16:37:13 +01:00
André van Delft
b3ac4606a6 Allow for the use of backslash for newlines 2021-02-15 16:29:14 +01:00
André van Delft
f093843f40 Allow for a custom strong or emphasis symbol 2021-02-15 16:19:19 +01:00
Bruno Miguens
de6f91af0e Revert header validation and leave possibility to empty column 2021-02-08 20:56:18 +00:00
Bruno Miguens
8c28ade348 Remove empty header validation to allow empty header 2021-02-08 20:50:15 +00:00
Bruno Miguens
a152c5b706 Fix lint 2021-02-08 19:32:35 +00:00
Bruno Miguens
292d64bbf4 Remove unnecessary tests 2021-02-08 19:26:27 +00:00
Bruno Miguens
db96eeb785 Add tests for basic and thead/tbody tables 2021-02-08 17:00:09 +00:00
Bruno Miguens
73f7644c0d Add basic support for HTML tables 2021-02-08 17:00:09 +00:00
AlexVonB
a4d134df97 Merge pull request #34 from BrunoMiguens/add-ignore-comment-tags
Add ignore comment tags
2021-02-07 19:46:49 +01:00
Bruno Miguens
457454c713 Add new line at the end of file 2021-02-05 19:49:57 +00:00
Bruno Miguens
321e9eb5f6 Add ignore comment tags 2021-02-05 19:40:43 +00:00
AlexVonB
bf24df3e2e bump to v0.6.3 2021-01-12 22:43:18 +01:00
AlexVonB
15329588b1 Merge branch 'develop' 2021-01-12 22:42:58 +01:00
AlexVonB
77d1e99bd5 satisfy linter 2021-01-12 22:42:06 +01:00
AlexVonB
34ad8485fa bump to v0.6.2 2021-01-12 22:40:03 +01:00
AlexVonB
f0ce934bf8 Merge branch 'develop' 2021-01-12 22:39:47 +01:00
AlexVonB
97c78ef55b Merge branch 'fix-extra-headline-whitespace' into develop 2021-01-12 22:38:59 +01:00
AlexVonB
99cd237f27 Merge branch 'develop' 2021-01-04 10:22:02 +01:00
AlexVonB
b7e1ab889d bump to v0.6.1 2021-01-04 10:21:27 +01:00
AlexVonB
29e86aec55 Merge branch 'fix-link-underscores' into develop 2021-01-04 10:18:05 +01:00
AlexVonB
453b604096 Fixing autolinks
When checking a links href and text for
equality, first un-escape the underscores
in the text -- because six escapes them.
This should fix #29.
2021-01-02 17:22:36 +01:00
AlexVonB
2bde8d3e8e Merge branch 'develop' 2021-01-02 16:49:28 +01:00
AlexVonB
4f8937810b dont replace newlines and tabs with spaces
this should fix #17, as all leading new lines
were replaced with a single space, which in turn
was rendered before the # of a headline
2020-12-29 10:28:50 +01:00
Andrew Richards
7685738344 Formatting tweak
Change indent of continuation line; squashes a flake8 warning.
2020-11-27 14:18:08 +00:00
Andrew Richards
92a73c8dfe Correct test_code_with_tricky_content()
Result of previous test didn't check for the trailing '  ' that
convert_br() adds: This is needed to ensure that the resulting
markdown not only has \n for the <br> but also renders it as
a newline.
2020-11-26 22:20:29 +00:00
Andrew Richards
3354f143d8 Add method for <code> tag
Add method and tests for inline tag <code>.
2020-11-23 17:28:23 +00:00
AlexVonB
8c9b029756 Merge branch 'develop' 2020-09-01 18:10:07 +02:00
SimonIT
2c7e4a0100 Fix tests 2020-08-26 19:47:11 +02:00
SimonIT
4f00d638d2 Merge remote-tracking branch 'upstream/develop' into ordered-list
# Conflicts:
#	markdownify/__init__.py
#	tests/test_conversions.py
2020-08-26 19:41:43 +02:00
AlexVonB
ae50065872 Merge branch 'develop' 2020-08-18 18:53:10 +02:00
Rémi
d23596706d Remove debug prints 2019-11-22 11:49:22 +01:00
Rémi
6a0e5d8176 Correct inline UL test as paragraphs are followed by two newlines 2019-11-21 09:46:22 +01:00
Rémi
7b788bafd4 Add nested OL test (for newlines) and correct lists nesting 2019-11-21 09:35:34 +01:00
Rémi
146104b41f Remove newline-only textnodes outside <pre> 2019-11-20 10:37:39 +01:00
12 changed files with 523 additions and 138 deletions

View File

@@ -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: |

View File

@@ -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

View File

@@ -62,7 +62,11 @@ convert
autolinks
A boolean indicating whether the "automatic link" style should be used when
a ``a`` tag's contents match its href. Defaults to ``True``
a ``a`` tag's contents match its href. Defaults to ``True``.
default_title
A boolean to enable setting the title of a link to its href, if no title is
given. Defaults to ``False``.
heading_style
Defines how headings should be converted. Accepted values are ``ATX``,
@@ -75,6 +79,23 @@ 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.
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^``.
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.

View File

@@ -1,11 +1,12 @@
from bs4 import BeautifulSoup, NavigableString
from bs4 import BeautifulSoup, NavigableString, Comment, Doctype
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 ]+')
all_whitespace_re = re.compile(r'[\s]+')
html_heading_re = re.compile(r'h[1-6]')
@@ -15,6 +16,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:
@@ -35,17 +44,38 @@ def chomp(text):
return (prefix, suffix, text)
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
references to self.strong_em_symbol etc.
"""
def implementation(self, el, text, convert_as_inline):
markup = markup_fn(self)
prefix, suffix, text = chomp(text)
if not text:
return ''
return '%s%s%s%s%s' % (prefix, markup, text, markup, suffix)
return implementation
def _todict(obj):
return dict((k, getattr(obj, k)) for k in dir(obj) if not k.startswith('_'))
class MarkdownConverter(object):
class DefaultOptions:
strip = None
convert = None
autolinks = True
heading_style = UNDERLINED
bullets = '*+-' # An iterable of bullet types.
convert = None
default_title = False
heading_style = UNDERLINED
newline_style = SPACES
strip = None
strong_em_symbol = ASTERISK
sub_symbol = ''
sup_symbol = ''
class Options(DefaultOptions):
pass
@@ -73,10 +103,34 @@ class MarkdownConverter(object):
if not children_only and isHeading:
convert_children_as_inline = True
# Remove whitespace-only textnodes in purely nested nodes
def is_nested_node(el):
return el and el.name in ['ol', 'ul', 'li',
'table', 'thead', 'tbody', 'tfoot',
'tr', 'td', 'th']
if is_nested_node(node):
for el in node.children:
# Only extract (remove) whitespace-only text node if any of the
# conditions is true:
# - el is the first element in its parent
# - el is the last element in its parent
# - el is adjacent to an nested node
can_extract = (not el.previous_sibling
or not el.next_sibling
or is_nested_node(el.previous_sibling)
or is_nested_node(el.next_sibling))
if (isinstance(el, NavigableString)
and six.text_type(el).strip() == ''
and can_extract):
el.extract()
# Convert the children first
for el in node.children:
if isinstance(el, NavigableString):
text += self.process_text(six.text_type(el))
if isinstance(el, Comment) or isinstance(el, Doctype):
continue
elif isinstance(el, NavigableString):
text += self.process_text(el)
else:
text += self.process_tag(el, convert_children_as_inline)
@@ -87,8 +141,23 @@ class MarkdownConverter(object):
return text
def process_text(self, text):
return escape(whitespace_re.sub(' ', text or ''))
def process_text(self, el):
text = six.text_type(el)
# dont remove any whitespace when handling pre or code in pre
if (el.parent.name == 'pre'
or (el.parent.name == 'code' and el.parent.parent.name == 'pre')):
return escape(text or '')
cleaned_text = escape(whitespace_re.sub(' ', text or ''))
# remove trailing whitespaces if any of the following condition is true:
# - current text node is the last node in li
# - current text node is followed by an embedded list
if el.parent.name == 'li' and (not el.next_sibling or el.next_sibling.name in ['ul', 'ol']):
return cleaned_text.rstrip()
return cleaned_text
def __getattr__(self, attr):
# Handle headings
@@ -131,39 +200,53 @@ class MarkdownConverter(object):
return text
href = el.get('href')
title = el.get('title')
if self.options['autolinks'] and text == href and not title:
# For the replacement see #29: text nodes underscores are escaped
if (self.options['autolinks']
and text.replace(r'\_', '_') == href
and not title
and not self.options['default_title']):
# Shortcut syntax
return '<%s>' % href
if self.options['default_title'] and not title:
title = href
title_part = ' "%s"' % title.replace('"', r'\"') if title else ''
return '%s[%s](%s%s)%s' % (prefix, text, href, title_part, suffix) if href else text
def convert_b(self, el, text, convert_as_inline):
return self.convert_strong(el, text, convert_as_inline)
convert_b = abstract_inline_conversion(lambda self: 2 * self.options['strong_em_symbol'])
def convert_blockquote(self, el, text, convert_as_inline):
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):
prefix, suffix, text = chomp(text)
if not text:
return ''
return '%s*%s*%s' % (prefix, text, suffix)
def convert_code(self, el, text, convert_as_inline):
if el.parent.name == 'pre':
return text
converter = abstract_inline_conversion(lambda self: '`')
return converter(self, el, text, convert_as_inline)
convert_del = abstract_inline_conversion(lambda self: '~~')
convert_em = abstract_inline_conversion(lambda self: self.options['strong_em_symbol'])
convert_kbd = convert_code
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 '-'
@@ -173,8 +256,20 @@ class MarkdownConverter(object):
return '%s %s %s\n\n' % (hashes, text, hashes)
return '%s %s\n\n' % (hashes, text)
def convert_i(self, el, text, convert_as_inline):
return self.convert_em(el, text, convert_as_inline)
def convert_hr(self, el, text, convert_as_inline):
return '\n\n---\n\n'
convert_i = convert_em
def convert_img(self, el, text, convert_as_inline):
alt = el.attrs.get('alt', None) or ''
src = el.attrs.get('src', None) or ''
title = el.attrs.get('title', None) or ''
title_part = ' "%s"' % title.replace('"', r'\"') if title else ''
if convert_as_inline:
return alt
return '![%s](%s%s)' % (alt, src, title_part)
def convert_list(self, el, text, convert_as_inline):
@@ -182,6 +277,9 @@ class MarkdownConverter(object):
# Ignoring convert_to_inline for list.
nested = False
before_paragraph = False
if el.next_sibling and el.next_sibling.name not in ['ul', 'ol']:
before_paragraph = True
while el:
if el.name == 'li':
nested = True
@@ -190,7 +288,7 @@ class MarkdownConverter(object):
if nested:
# remove trailing newline if nested
return '\n' + self.indent(text, 1).rstrip()
return '\n' + text + '\n'
return text + ('\n' if before_paragraph else '')
convert_ul = convert_list
convert_ol = convert_list
@@ -218,21 +316,44 @@ class MarkdownConverter(object):
return text
return '%s\n\n' % text if text else ''
def convert_strong(self, el, text, convert_as_inline):
prefix, suffix, text = chomp(text)
def convert_pre(self, el, text, convert_as_inline):
if not text:
return ''
return '%s**%s**%s' % (prefix, text, suffix)
return '\n```\n%s\n```\n' % text
def convert_img(self, el, text, convert_as_inline):
alt = el.attrs.get('alt', None) or ''
src = el.attrs.get('src', None) or ''
title = el.attrs.get('title', None) or ''
title_part = ' "%s"' % title.replace('"', r'\"') if title else ''
if convert_as_inline:
return alt
convert_s = convert_del
return '![%s](%s%s)' % (alt, src, title_part)
convert_strong = convert_b
convert_samp = convert_code
convert_sub = abstract_inline_conversion(lambda self: self.options['sub_symbol'])
convert_sup = abstract_inline_conversion(lambda self: self.options['sup_symbol'])
def convert_table(self, el, text, convert_as_inline):
return '\n\n' + text + '\n'
def convert_td(self, el, text, convert_as_inline):
return ' ' + text + ' |'
def convert_th(self, el, text, convert_as_inline):
return ' ' + text + ' |'
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])
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:
# print empty headline above this row
overline += '| ' + ' | '.join([''] * len(cells)) + ' |' + '\n'
overline += '| ' + ' | '.join(['---'] * len(cells)) + ' |' + '\n'
return overline + '|' + text + '\n' + underline
def markdownify(html, **options):

View File

@@ -1,2 +1,2 @@
[flake8]
ignore = E501
ignore = E501 W503

View File

@@ -10,7 +10,7 @@ read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
pkgmeta = {
'__title__': 'markdownify',
'__author__': 'Matthew Tretter',
'__version__': '0.6.0',
'__version__': '0.9.0',
}
@@ -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={

View File

@@ -1,6 +1,39 @@
from markdownify import markdownify as md
def test_chomp():
assert md(' <b></b> ') == ' '
assert md(' <b> </b> ') == ' '
assert md(' <b> </b> ') == ' '
assert md(' <b> </b> ') == ' '
assert md(' <b>s </b> ') == ' **s** '
assert md(' <b> s</b> ') == ' **s** '
assert md(' <b> s </b> ') == ' **s** '
assert md(' <b> s </b> ') == ' **s** '
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/)"
def test_code_with_tricky_content():
assert md('<code>></code>') == "`>`"
assert md('<code>/home/</code><b>username</b>') == "`/home/`**username**"
assert md('First line <code>blah blah<br />blah blah</code> second line') \
== "First line `blah blah \nblah blah` second line"
def test_special_tags():
assert md('<!DOCTYPE html>') == ''
assert md('<![CDATA[foobar]]>') == 'foobar'

View File

@@ -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 '

View File

@@ -1,40 +1,20 @@
from markdownify import markdownify as md, ATX, ATX_CLOSED
import re
from markdownify import markdownify as md, ATX, ATX_CLOSED, BACKSLASH, UNDERSCORE
nested_uls = re.sub(r'\s+', '', """
<ul>
<li>1
<ul>
<li>a
<ul>
<li>I</li>
<li>II</li>
<li>III</li>
</ul>
</li>
<li>b</li>
<li>c</li>
</ul>
</li>
<li>2</li>
<li>3</li>
</ul>""")
def test_chomp():
assert md(' <b></b> ') == ' '
assert md(' <b> </b> ') == ' '
assert md(' <b> </b> ') == ' '
assert md(' <b> </b> ') == ' '
assert md(' <b>s </b> ') == ' **s** '
assert md(' <b> s</b> ') == ' **s** '
assert md(' <b> s </b> ') == ' **s** '
assert md(' <b> s </b> ') == ' **s** '
def inline_tests(tag, markup):
# test template for different inline tags
assert md(f'<{tag}>Hello</{tag}>') == f'{markup}Hello{markup}'
assert md(f'foo <{tag}>Hello</{tag}> bar') == f'foo {markup}Hello{markup} bar'
assert md(f'foo<{tag}> Hello</{tag}> bar') == f'foo {markup}Hello{markup} bar'
assert md(f'foo <{tag}>Hello </{tag}>bar') == f'foo {markup}Hello{markup} bar'
assert md(f'foo <{tag}></{tag}> bar') in ['foo bar', 'foo bar'] # Either is OK
def test_a():
assert md('<a href="http://google.com">Google</a>') == '[Google](http://google.com)'
assert md('<a href="https://google.com">Google</a>') == '[Google](https://google.com)'
assert md('<a href="https://google.com">https://google.com</a>') == '<https://google.com>'
assert md('<a href="https://community.kde.org/Get_Involved">https://community.kde.org/Get_Involved</a>') == '<https://community.kde.org/Get_Involved>'
assert md('<a href="https://community.kde.org/Get_Involved">https://community.kde.org/Get_Involved</a>', autolinks=False) == '[https://community.kde.org/Get\\_Involved](https://community.kde.org/Get_Involved)'
def test_a_spaces():
@@ -47,6 +27,7 @@ def test_a_spaces():
def test_a_with_title():
text = md('<a href="http://google.com" title="The &quot;Goog&quot;">Google</a>')
assert text == r'[Google](http://google.com "The \"Goog\"")'
assert md('<a href="https://google.com">https://google.com</a>', default_title=True) == '[https://google.com](https://google.com "https://google.com")'
def test_a_shortcut():
@@ -55,8 +36,7 @@ def test_a_shortcut():
def test_a_no_autolinks():
text = md('<a href="http://google.com">http://google.com</a>', autolinks=False)
assert text == '[http://google.com](http://google.com)'
assert md('<a href="https://google.com">https://google.com</a>', autolinks=False) == '[https://google.com](https://google.com)'
def test_b():
@@ -71,27 +51,37 @@ 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_nested_blockquote():
text = md('<blockquote>And she was like <blockquote>Hello</blockquote></blockquote>').strip()
assert text == '> And she was like \n> > Hello'
def test_blockquote_with_paragraph():
assert md('<blockquote>Hello</blockquote><p>handsome</p>') == '\n> Hello\n\nhandsome\n\n'
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'
def test_br():
assert md('a<br />b<br />c') == 'a \nb \nc'
assert md('a<br />b<br />c', newline_style=BACKSLASH) == 'a\\\nb\\\nc'
def test_code():
inline_tests('code', '`')
def test_del():
inline_tests('del', '~~')
def test_div():
assert md('Hello</div> World') == 'Hello World'
def test_em():
assert md('<em>Hello</em>') == '*Hello*'
def test_em_spaces():
assert md('foo <em>Hello</em> bar') == 'foo *Hello* bar'
assert md('foo<em> Hello</em> bar') == 'foo *Hello* bar'
assert md('foo <em>Hello </em>bar') == 'foo *Hello* bar'
assert md('foo <em></em> bar') == 'foo bar'
inline_tests('em', '*')
def test_h1():
@@ -104,9 +94,16 @@ def test_h2():
def test_hn():
assert md('<h3>Hello</h3>') == '### Hello\n\n'
assert md('<h4>Hello</h4>') == '#### Hello\n\n'
assert md('<h5>Hello</h5>') == '##### Hello\n\n'
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'
@@ -145,65 +142,72 @@ def test_hn_nested_img():
assert md('<h3>A <img src="/path/to/img.jpg " ' + image_attributes + '/> B</h3>') == '### A ' + markdown + ' B\n\n'
def test_hr():
assert md('<hr>hr</hr>') == 'hr'
def test_hn_atx_headings():
assert md('<h1>Hello</h1>', heading_style=ATX) == '# Hello\n\n'
assert md('<h2>Hello</h2>', heading_style=ATX) == '## Hello\n\n'
def test_hn_atx_closed_headings():
assert md('<h1>Hello</h1>', heading_style=ATX_CLOSED) == '# Hello #\n\n'
assert md('<h2>Hello</h2>', heading_style=ATX_CLOSED) == '## Hello ##\n\n'
def test_head():
assert md('<head>head</head>') == 'head'
def test_atx_headings():
assert md('<h1>Hello</h1>', heading_style=ATX) == '# Hello\n\n'
assert md('<h2>Hello</h2>', heading_style=ATX) == '## Hello\n\n'
def test_atx_closed_headings():
assert md('<h1>Hello</h1>', heading_style=ATX_CLOSED) == '# Hello #\n\n'
assert md('<h2>Hello</h2>', heading_style=ATX_CLOSED) == '## Hello ##\n\n'
def test_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_i():
assert md('<i>Hello</i>') == '*Hello*'
def test_ol():
assert md('<ol><li>a</li><li>b</li></ol>') == '\n1. a\n2. b\n\n'
assert md('<ol start="3"><li>a</li><li>b</li></ol>') == '\n3. a\n4. b\n\n'
def test_p():
assert md('<p>hello</p>') == 'hello\n\n'
def test_strong():
assert md('<strong>Hello</strong>') == '**Hello**'
def test_ul():
assert md('<ul><li>a</li><li>b</li></ul>') == '\n* a\n* b\n\n'
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'
def test_nested_uls():
"""
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'
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'
def test_img():
assert md('<img src="/path/to/img.jpg" alt="Alt text" title="Optional title" />') == '![Alt text](/path/to/img.jpg "Optional title")'
assert md('<img src="/path/to/img.jpg" alt="Alt text" />') == '![Alt text](/path/to/img.jpg)'
def test_div():
assert md('Hello</div> World') == 'Hello World'
def test_kbd():
inline_tests('kbd', '`')
def test_p():
assert md('<p>hello</p>') == 'hello\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'
def test_s():
inline_tests('s', '~~')
def test_samp():
inline_tests('samp', '`')
def test_strong():
assert md('<strong>Hello</strong>') == '**Hello**'
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_sub():
assert md('<sub>foo</sub>') == 'foo'
assert md('<sub>foo</sub>', sub_symbol='~') == '~foo~'
def test_sup():
assert md('<sup>foo</sup>') == 'foo'
assert md('<sup>foo</sup>', sup_symbol='^') == '^foo^'

View File

@@ -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():

73
tests/test_lists.py Normal file
View File

@@ -0,0 +1,73 @@
from markdownify import markdownify as md
nested_uls = """
<ul>
<li>1
<ul>
<li>a
<ul>
<li>I</li>
<li>II</li>
<li>III</li>
</ul>
</li>
<li>b</li>
<li>c</li>
</ul>
</li>
<li>2</li>
<li>3</li>
</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>"""
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'
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_ul():
assert md('<ul><li>a</li><li>b</li></ul>') == '* a\n* b\n'
def test_inline_ul():
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():
"""
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'
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'
def test_li_text():
assert md('<ul><li>foo <a href="#">bar</a></li><li>foo bar </li><li>foo <b>bar</b> <i>space</i>.</ul>') == '* foo [bar](#)\n* foo bar\n* foo **bar** *space*.\n'

130
tests/test_tables.py Normal file
View File

@@ -0,0 +1,130 @@
from markdownify import markdownify as md
table = """<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_with_html_content = """<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<td><b>Jill</b></td>
<td><i>Smith</i></td>
<td><a href="#">50</a></td>
</tr>
<tr>
<td>Eve</td>
<td>Jackson</td>
<td>94</td>
</tr>
</table>"""
table_with_header_column = """<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>
<tr>
<th>Jill</th>
<td>Smith</td>
<td>50</td>
</tr>
<tr>
<th>Eve</th>
<td>Jackson</td>
<td>94</td>
</tr>
</table>"""
table_head_body = """<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 = """<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>"""
table_missing_head = """<table>
<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>
</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_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_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'