Compare commits
22 Commits
0.8.0
...
Inzaniak-d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb2646cd93 | ||
|
|
9692b5e714 | ||
|
|
ac68c53a7d | ||
|
|
40dd30419c | ||
|
|
da56f7f56a | ||
|
|
8400b39dd9 | ||
|
|
5fc1441fe7 | ||
|
|
044615eff1 | ||
|
|
dbd9f3f3d2 | ||
|
|
0fdeb1ff6e | ||
|
|
6a2f3a4b42 | ||
|
|
22180a166d | ||
|
|
16d8a0e1f7 | ||
|
|
4aa6cf2a24 | ||
|
|
828e116530 | ||
|
|
62e9f0de02 | ||
|
|
cec570fc49 | ||
|
|
a6a31624ad | ||
|
|
6f3732307d | ||
|
|
8f6d7e500d | ||
|
|
e96351b666 | ||
|
|
129c4ef060 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -8,3 +8,4 @@
|
||||
/MANIFEST
|
||||
/venv
|
||||
build/
|
||||
.vscode/settings.json
|
||||
|
||||
34
README.rst
34
README.rst
@@ -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``,
|
||||
@@ -80,6 +84,11 @@ strong_em_symbol
|
||||
*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,
|
||||
@@ -91,6 +100,29 @@ Options may be specified as kwargs to the ``markdownify`` function, or as a
|
||||
nested ``Options`` class in ``MarkdownConverter`` subclasses.
|
||||
|
||||
|
||||
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:
|
||||
|
||||
.. code:: python
|
||||
|
||||
from markdownify import MarkdownConverter
|
||||
|
||||
class ImageBlockConverter(MarkdownConverter):
|
||||
"""
|
||||
Create a custom MarkdownConverter that adds two newlines after an image
|
||||
"""
|
||||
def convert_img(self, el, text, convert_as_inline):
|
||||
return super().convert_img(el, text, convert_as_inline) + '\n\n'
|
||||
|
||||
# Create shorthand method for conversion
|
||||
def md(html, **options):
|
||||
return ImageBlockConverter(**options).convert(html)
|
||||
|
||||
|
||||
Development
|
||||
===========
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from bs4 import BeautifulSoup, NavigableString, Comment
|
||||
from bs4 import BeautifulSoup, NavigableString, Comment, Doctype
|
||||
import re
|
||||
import six
|
||||
|
||||
@@ -66,13 +66,17 @@ def _todict(obj):
|
||||
|
||||
class MarkdownConverter(object):
|
||||
class DefaultOptions:
|
||||
strip = None
|
||||
convert = None
|
||||
autolinks = True
|
||||
heading_style = UNDERLINED
|
||||
bullets = '*+-' # An iterable of bullet types.
|
||||
strong_em_symbol = ASTERISK
|
||||
convert = None
|
||||
default_title = False
|
||||
heading_style = UNDERLINED
|
||||
newline_style = SPACES
|
||||
strip = None
|
||||
strong_em_symbol = ASTERISK
|
||||
sub_symbol = ''
|
||||
sup_symbol = ''
|
||||
code_language = ''
|
||||
|
||||
class Options(DefaultOptions):
|
||||
pass
|
||||
@@ -93,11 +97,14 @@ class MarkdownConverter(object):
|
||||
|
||||
def process_tag(self, node, convert_as_inline, children_only=False):
|
||||
text = ''
|
||||
# markdown headings can't include block elements (elements w/newlines)
|
||||
|
||||
# markdown headings or cells can't include
|
||||
# block elements (elements w/newlines)
|
||||
isHeading = html_heading_re.match(node.name) is not None
|
||||
isCell = node.name in ['td', 'th']
|
||||
convert_children_as_inline = convert_as_inline
|
||||
|
||||
if not children_only and isHeading:
|
||||
if not children_only and (isHeading or isCell):
|
||||
convert_children_as_inline = True
|
||||
|
||||
# Remove whitespace-only textnodes in purely nested nodes
|
||||
@@ -124,7 +131,7 @@ class MarkdownConverter(object):
|
||||
|
||||
# Convert the children first
|
||||
for el in node.children:
|
||||
if isinstance(el, Comment):
|
||||
if isinstance(el, Comment) or isinstance(el, Doctype):
|
||||
continue
|
||||
elif isinstance(el, NavigableString):
|
||||
text += self.process_text(el)
|
||||
@@ -139,22 +146,26 @@ class MarkdownConverter(object):
|
||||
return text
|
||||
|
||||
def process_text(self, el):
|
||||
text = six.text_type(el)
|
||||
text = six.text_type(el) or ''
|
||||
|
||||
# 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 '')
|
||||
if not (el.parent.name == 'pre'
|
||||
or (el.parent.name == 'code'
|
||||
and el.parent.parent.name == 'pre')):
|
||||
text = whitespace_re.sub(' ', text)
|
||||
|
||||
cleaned_text = escape(whitespace_re.sub(' ', text or ''))
|
||||
if el.parent.name != 'code':
|
||||
text = escape(text)
|
||||
|
||||
# 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()
|
||||
if (el.parent.name == 'li'
|
||||
and (not el.next_sibling
|
||||
or el.next_sibling.name in ['ul', 'ol'])):
|
||||
text = text.rstrip()
|
||||
|
||||
return cleaned_text
|
||||
return text
|
||||
|
||||
def __getattr__(self, attr):
|
||||
# Handle headings
|
||||
@@ -193,14 +204,17 @@ class MarkdownConverter(object):
|
||||
prefix, suffix, text = chomp(text)
|
||||
if not text:
|
||||
return ''
|
||||
if convert_as_inline:
|
||||
return text
|
||||
href = el.get('href')
|
||||
title = el.get('title')
|
||||
# For the replacement see #29: text nodes underscores are escaped
|
||||
if self.options['autolinks'] and text.replace(r'\_', '_') == href and not title:
|
||||
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
|
||||
|
||||
@@ -301,7 +315,7 @@ class MarkdownConverter(object):
|
||||
el = el.parent
|
||||
bullets = self.options['bullets']
|
||||
bullet = bullets[depth % len(bullets)]
|
||||
return '%s %s\n' % (bullet, text or '')
|
||||
return '%s %s\n' % (bullet, (text or '').strip())
|
||||
|
||||
def convert_p(self, el, text, convert_as_inline):
|
||||
if convert_as_inline:
|
||||
@@ -311,7 +325,7 @@ class MarkdownConverter(object):
|
||||
def convert_pre(self, el, text, convert_as_inline):
|
||||
if not text:
|
||||
return ''
|
||||
return '\n```\n%s\n```\n' % text
|
||||
return '\n```%s\n%s\n```\n' % (self.options['code_language'], text)
|
||||
|
||||
convert_s = convert_del
|
||||
|
||||
@@ -319,6 +333,10 @@ class MarkdownConverter(object):
|
||||
|
||||
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'
|
||||
|
||||
|
||||
2
setup.py
2
setup.py
@@ -10,7 +10,7 @@ read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
|
||||
pkgmeta = {
|
||||
'__title__': 'markdownify',
|
||||
'__author__': 'Matthew Tretter',
|
||||
'__version__': '0.8.0',
|
||||
'__version__': '0.9.4',
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,17 @@
|
||||
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'
|
||||
@@ -21,3 +32,8 @@ def test_code_with_tricky_content():
|
||||
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'
|
||||
|
||||
@@ -1,179 +1,17 @@
|
||||
from markdownify import markdownify as md, ATX, ATX_CLOSED, BACKSLASH, UNDERSCORE
|
||||
|
||||
|
||||
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>"""
|
||||
|
||||
|
||||
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_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="https://google.com">Google</a>') == '[Google](https://google.com)'
|
||||
assert md('<a href="https://google.com">https://google.com</a>', autolinks=False) == '[https://google.com](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)'
|
||||
@@ -189,6 +27,7 @@ def test_a_spaces():
|
||||
def test_a_with_title():
|
||||
text = md('<a href="http://google.com" title="The "Goog"">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():
|
||||
@@ -197,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():
|
||||
@@ -220,58 +58,31 @@ def test_blockquote_with_paragraph():
|
||||
assert md('<blockquote>Hello</blockquote><p>handsome</p>') == '\n> Hello\n\nhandsome\n\n'
|
||||
|
||||
|
||||
def test_nested_blockquote():
|
||||
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'
|
||||
|
||||
|
||||
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'
|
||||
|
||||
|
||||
def inline_tests(tag, markup):
|
||||
# Basically re-use test_em() and test_em_spaces(),
|
||||
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
|
||||
assert md('a<br />b<br />c', newline_style=BACKSLASH) == 'a\\\nb\\\nc'
|
||||
|
||||
|
||||
def test_code():
|
||||
inline_tests('code', '`')
|
||||
|
||||
|
||||
def test_samp():
|
||||
inline_tests('samp', '`')
|
||||
|
||||
|
||||
def test_kbd():
|
||||
inline_tests('kbd', '`')
|
||||
|
||||
|
||||
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('<code>this_should_not_escape</code>') == '`this_should_not_escape`'
|
||||
|
||||
|
||||
def test_del():
|
||||
inline_tests('del', '~~')
|
||||
|
||||
|
||||
def test_s():
|
||||
inline_tests('s', '~~')
|
||||
def test_div():
|
||||
assert md('Hello</div> World') == 'Hello World'
|
||||
|
||||
|
||||
def test_em():
|
||||
inline_tests('em', '*')
|
||||
|
||||
|
||||
def test_h1():
|
||||
@@ -284,6 +95,8 @@ 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'
|
||||
|
||||
|
||||
@@ -319,8 +132,6 @@ def test_hn_nested_simple_tag():
|
||||
|
||||
|
||||
def test_hn_nested_img():
|
||||
assert md('<img src="/path/to/img.jpg" alt="Alt text" title="Optional title" />') == ''
|
||||
assert md('<img src="/path/to/img.jpg" alt="Alt text" />') == ''
|
||||
image_attributes_to_markdown = [
|
||||
("", ""),
|
||||
("alt='Alt Text'", "Alt Text"),
|
||||
@@ -330,87 +141,58 @@ 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('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_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>') == '1. a\n2. b\n'
|
||||
assert md('<ol start="3"><li>a</li><li>b</li></ol>') == '3. a\n4. b\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>') == '* a\n* 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_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'
|
||||
|
||||
|
||||
def test_img():
|
||||
assert md('<img src="/path/to/img.jpg" alt="Alt text" title="Optional title" />') == ''
|
||||
assert md('<img src="/path/to/img.jpg" alt="Alt text" />') == ''
|
||||
|
||||
|
||||
def test_div():
|
||||
assert md('Hello</div> World') == 'Hello World'
|
||||
def test_kbd():
|
||||
inline_tests('kbd', '`')
|
||||
|
||||
|
||||
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'
|
||||
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():
|
||||
@@ -420,5 +202,16 @@ def test_strong_em_symbol():
|
||||
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'
|
||||
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^'
|
||||
|
||||
|
||||
def test_lang():
|
||||
assert md('<pre>test\n foo\nbar</pre>', code_language='python') == '\n```python\ntest\n foo\nbar\n```\n'
|
||||
assert md('<pre><code>test\n foo\nbar</code></pre>', code_language='javascript') == '\n```javascript\ntest\n foo\nbar\n```\n'
|
||||
|
||||
18
tests/test_custom_converter.py
Normal file
18
tests/test_custom_converter.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from markdownify import MarkdownConverter
|
||||
|
||||
|
||||
class ImageBlockConverter(MarkdownConverter):
|
||||
"""
|
||||
Create a custom MarkdownConverter that adds two newlines after an image
|
||||
"""
|
||||
def convert_img(self, el, text, convert_as_inline):
|
||||
return super().convert_img(el, text, convert_as_inline) + '\n\n'
|
||||
|
||||
|
||||
def test_img():
|
||||
# Create shorthand method for conversion
|
||||
def md(html, **options):
|
||||
return ImageBlockConverter(**options).convert(html)
|
||||
|
||||
assert md('<img src="/path/to/img.jpg" alt="Alt text" title="Optional title" />') == '\n\n'
|
||||
assert md('<img src="/path/to/img.jpg" alt="Alt text" />') == '\n\n'
|
||||
81
tests/test_lists.py
Normal file
81
tests/test_lists.py
Normal file
@@ -0,0 +1,81 @@
|
||||
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'
|
||||
assert md("""<ul>
|
||||
<li>
|
||||
a
|
||||
</li>
|
||||
<li> b </li>
|
||||
<li> c
|
||||
</li>
|
||||
</ul>""") == '* a\n* b\n* c\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'
|
||||
150
tests/test_tables.py
Normal file
150
tests/test_tables.py
Normal file
@@ -0,0 +1,150 @@
|
||||
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_paragraphs = """<table>
|
||||
<tr>
|
||||
<th>Firstname</th>
|
||||
<th><p>Lastname</p></th>
|
||||
<th>Age</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><p>Jill</p></td>
|
||||
<td><p>Smith</p></td>
|
||||
<td><p>50</p></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_paragraphs) == '\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'
|
||||
Reference in New Issue
Block a user