Compare commits
31 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0c4b856b9c | ||
|
|
e9cc01938a | ||
|
|
aceced68eb | ||
|
|
3b049cdb9c | ||
|
|
b747378b52 | ||
|
|
ee73d89879 | ||
|
|
5563161c86 | ||
|
|
28e447d9ae | ||
|
|
89d14f4487 | ||
|
|
5f9243d91d | ||
|
|
d0f688d2e4 | ||
|
|
5ac08522be | ||
|
|
78afcc173e | ||
|
|
b132a6f5b3 | ||
|
|
0abe0a29e8 | ||
|
|
4932df631f | ||
|
|
8696e2bde1 | ||
|
|
ee53d85c41 | ||
|
|
53ba0daa77 | ||
|
|
fb98e9878f | ||
|
|
aa10053fbb | ||
|
|
253a34c2d7 | ||
|
|
3ea09609e6 | ||
|
|
1cd8e56c47 | ||
|
|
891a4a8d08 | ||
|
|
e5a1784f30 | ||
|
|
f60d910335 | ||
|
|
d707d107f6 | ||
|
|
1ef4dd1468 | ||
|
|
934c97b342 | ||
|
|
8a1e2d9403 |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -1,5 +1,7 @@
|
|||||||
*.pyc
|
*.pyc
|
||||||
*.egg
|
*.egg
|
||||||
|
.eggs/
|
||||||
|
*.egg-info/
|
||||||
.DS_Store
|
.DS_Store
|
||||||
/.env
|
/.env
|
||||||
/dist
|
/dist
|
||||||
|
|||||||
21
LICENSE
Normal file
21
LICENSE
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright 2012-2018 Matthew Tretter
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
32
README.rst
32
README.rst
@@ -29,6 +29,38 @@ Specify tags to exclude (blacklist):
|
|||||||
md('<b>Yay</b> <a href="http://github.com">GitHub</a>', convert=['b']) # > '**Yay** GitHub'
|
md('<b>Yay</b> <a href="http://github.com">GitHub</a>', convert=['b']) # > '**Yay** GitHub'
|
||||||
|
|
||||||
|
|
||||||
|
Options
|
||||||
|
=======
|
||||||
|
|
||||||
|
Markdownify supports the following options:
|
||||||
|
|
||||||
|
strip
|
||||||
|
A list of tags to strip (blacklist). This option can't be used with the
|
||||||
|
``convert`` option.
|
||||||
|
|
||||||
|
convert
|
||||||
|
A list of tags to convert (whitelist). This option can't be used with the
|
||||||
|
``strip`` option.
|
||||||
|
|
||||||
|
autolinks
|
||||||
|
A boolean indicating whether the "automatic link" style should be used when
|
||||||
|
a ``a`` tag's contents match its href. Defaults to ``True``
|
||||||
|
|
||||||
|
heading_style
|
||||||
|
Defines how headings should be converted. Accepted values are ``ATX``,
|
||||||
|
``ATX_CLOSED``, ``SETEXT``, and ``UNDERLINED`` (which is an alias for
|
||||||
|
``SETEXT``). Defaults to ``UNDERLINED``.
|
||||||
|
|
||||||
|
bullets
|
||||||
|
An iterable (string, list, or tuple) of bullet styles to be used. If the
|
||||||
|
iterable only contains one item, it will be used regardless of how deeply
|
||||||
|
lists are nested. Otherwise, the bullet will alternate based on nesting
|
||||||
|
level. Defaults to ``'*+-'``.
|
||||||
|
|
||||||
|
Options may be specified as kwargs to the ``markdownify`` function, or as a
|
||||||
|
nested ``Options`` class in ``MarkdownConverter`` subclasses.
|
||||||
|
|
||||||
|
|
||||||
Development
|
Development
|
||||||
===========
|
===========
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
from bs4 import BeautifulSoup, NavigableString
|
from bs4 import BeautifulSoup, NavigableString
|
||||||
import re
|
import re
|
||||||
|
import six
|
||||||
|
|
||||||
|
|
||||||
convert_heading_re = re.compile(r'convert_h(\d+)')
|
convert_heading_re = re.compile(r'convert_h(\d+)')
|
||||||
@@ -9,26 +10,63 @@ FRAGMENT_ID = '__MARKDOWNIFY_WRAPPER__'
|
|||||||
wrapped = '<div id="%s">%%s</div>' % FRAGMENT_ID
|
wrapped = '<div id="%s">%%s</div>' % FRAGMENT_ID
|
||||||
|
|
||||||
|
|
||||||
|
# Heading styles
|
||||||
|
ATX = 'atx'
|
||||||
|
ATX_CLOSED = 'atx_closed'
|
||||||
|
UNDERLINED = 'underlined'
|
||||||
|
SETEXT = UNDERLINED
|
||||||
|
|
||||||
|
|
||||||
def escape(text):
|
def escape(text):
|
||||||
if not text:
|
if not text:
|
||||||
return ''
|
return ''
|
||||||
return text.replace('_', r'\_')
|
return text.replace('_', r'\_')
|
||||||
|
|
||||||
|
|
||||||
|
def chomp(text):
|
||||||
|
"""
|
||||||
|
If the text in an inline tag like b, a, or em contains a leading or trailing
|
||||||
|
space, strip the string and return a space as suffix of prefix, if needed.
|
||||||
|
This function is used to prevent conversions like
|
||||||
|
<b> foo</b> => ** foo**
|
||||||
|
"""
|
||||||
|
prefix = ' ' if text and text[0] == ' ' else ''
|
||||||
|
suffix = ' ' if text and text[-1] == ' ' else ''
|
||||||
|
text = text.strip()
|
||||||
|
return (prefix, suffix, text)
|
||||||
|
|
||||||
|
|
||||||
|
def _todict(obj):
|
||||||
|
return dict((k, getattr(obj, k)) for k in dir(obj) if not k.startswith('_'))
|
||||||
|
|
||||||
|
|
||||||
class MarkdownConverter(object):
|
class MarkdownConverter(object):
|
||||||
def __init__(self, tags_to_strip=None, tags_to_convert=None):
|
class DefaultOptions:
|
||||||
if tags_to_strip is not None and tags_to_convert is not None:
|
strip = None
|
||||||
|
convert = None
|
||||||
|
autolinks = True
|
||||||
|
heading_style = UNDERLINED
|
||||||
|
bullets = '*+-' # An iterable of bullet types.
|
||||||
|
|
||||||
|
class Options(DefaultOptions):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def __init__(self, **options):
|
||||||
|
# Create an options dictionary. Use DefaultOptions as a base so that
|
||||||
|
# it doesn't have to be extended.
|
||||||
|
self.options = _todict(self.DefaultOptions)
|
||||||
|
self.options.update(_todict(self.Options))
|
||||||
|
self.options.update(options)
|
||||||
|
if self.options['strip'] is not None and self.options['convert'] is not None:
|
||||||
raise ValueError('You may specify either tags to strip or tags to'
|
raise ValueError('You may specify either tags to strip or tags to'
|
||||||
' convert, but not both.')
|
' convert, but not both.')
|
||||||
self.tags_to_strip = tags_to_strip
|
|
||||||
self.tags_to_convert = tags_to_convert
|
|
||||||
|
|
||||||
def convert(self, html):
|
def convert(self, html):
|
||||||
# We want to take advantage of the html5 parsing, but we don't actually
|
# We want to take advantage of the html5 parsing, but we don't actually
|
||||||
# want a full document. Therefore, we'll mark our fragment with an id,
|
# want a full document. Therefore, we'll mark our fragment with an id,
|
||||||
# create the document, and extract the element with the id.
|
# create the document, and extract the element with the id.
|
||||||
html = wrapped % html
|
html = wrapped % html
|
||||||
soup = BeautifulSoup(html)
|
soup = BeautifulSoup(html, 'html.parser')
|
||||||
return self.process_tag(soup.find(id=FRAGMENT_ID), children_only=True)
|
return self.process_tag(soup.find(id=FRAGMENT_ID), children_only=True)
|
||||||
|
|
||||||
def process_tag(self, node, children_only=False):
|
def process_tag(self, node, children_only=False):
|
||||||
@@ -37,7 +75,7 @@ class MarkdownConverter(object):
|
|||||||
# Convert the children first
|
# Convert the children first
|
||||||
for el in node.children:
|
for el in node.children:
|
||||||
if isinstance(el, NavigableString):
|
if isinstance(el, NavigableString):
|
||||||
text += self.process_text(unicode(el))
|
text += self.process_text(six.text_type(el))
|
||||||
else:
|
else:
|
||||||
text += self.process_tag(el)
|
text += self.process_tag(el)
|
||||||
|
|
||||||
@@ -52,7 +90,7 @@ class MarkdownConverter(object):
|
|||||||
return escape(whitespace_re.sub(' ', text or ''))
|
return escape(whitespace_re.sub(' ', text or ''))
|
||||||
|
|
||||||
def __getattr__(self, attr):
|
def __getattr__(self, attr):
|
||||||
# Handle heading levels > 2
|
# Handle headings
|
||||||
m = convert_heading_re.match(attr)
|
m = convert_heading_re.match(attr)
|
||||||
if m:
|
if m:
|
||||||
n = int(m.group(1))
|
n = int(m.group(1))
|
||||||
@@ -68,22 +106,33 @@ class MarkdownConverter(object):
|
|||||||
|
|
||||||
def should_convert_tag(self, tag):
|
def should_convert_tag(self, tag):
|
||||||
tag = tag.lower()
|
tag = tag.lower()
|
||||||
if self.tags_to_strip is not None:
|
strip = self.options['strip']
|
||||||
return tag not in self.tags_to_strip
|
convert = self.options['convert']
|
||||||
elif self.tags_to_convert is not None:
|
if strip is not None:
|
||||||
return tag in self.tags_to_convert
|
return tag not in strip
|
||||||
|
elif convert is not None:
|
||||||
|
return tag in convert
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def indent(self, text, level):
|
||||||
|
return line_beginning_re.sub('\t' * level, text) if text else ''
|
||||||
|
|
||||||
def underline(self, text, pad_char):
|
def underline(self, text, pad_char):
|
||||||
text = (text or '').rstrip()
|
text = (text or '').rstrip()
|
||||||
return '%s\n%s\n\n' % (text, pad_char * len(text)) if text else ''
|
return '%s\n%s\n\n' % (text, pad_char * len(text)) if text else ''
|
||||||
|
|
||||||
def convert_a(self, el, text):
|
def convert_a(self, el, text):
|
||||||
|
prefix, suffix, text = chomp(text)
|
||||||
|
if not text:
|
||||||
|
return ''
|
||||||
href = el.get('href')
|
href = el.get('href')
|
||||||
title = el.get('title')
|
title = el.get('title')
|
||||||
|
if self.options['autolinks'] and text == href and not title:
|
||||||
|
# Shortcut syntax
|
||||||
|
return '<%s>' % href
|
||||||
title_part = ' "%s"' % title.replace('"', r'\"') if title else ''
|
title_part = ' "%s"' % title.replace('"', r'\"') if title else ''
|
||||||
return '[%s](%s%s)' % (text or '', href, title_part) if href else text or ''
|
return '%s[%s](%s%s)%s' % (prefix, text, href, title_part, suffix) if href else text
|
||||||
|
|
||||||
def convert_b(self, el, text):
|
def convert_b(self, el, text):
|
||||||
return self.convert_strong(el, text)
|
return self.convert_strong(el, text)
|
||||||
@@ -95,35 +144,70 @@ class MarkdownConverter(object):
|
|||||||
return ' \n'
|
return ' \n'
|
||||||
|
|
||||||
def convert_em(self, el, text):
|
def convert_em(self, el, text):
|
||||||
return '*%s*' % text if text else ''
|
prefix, suffix, text = chomp(text)
|
||||||
|
if not text:
|
||||||
def convert_h1(self, el, text):
|
return ''
|
||||||
return self.underline(text, '=')
|
return '%s*%s*%s' % (prefix, text, suffix)
|
||||||
|
|
||||||
def convert_h2(self, el, text):
|
|
||||||
return self.underline(text, '-')
|
|
||||||
|
|
||||||
def convert_hn(self, n, el, text):
|
def convert_hn(self, n, el, text):
|
||||||
return '%s %s\n\n' % ('#' * n, text.rstrip()) if text else ''
|
style = self.options['heading_style']
|
||||||
|
text = text.rstrip()
|
||||||
|
if style == UNDERLINED and n <= 2:
|
||||||
|
line = '=' if n == 1 else '-'
|
||||||
|
return self.underline(text, line)
|
||||||
|
hashes = '#' * n
|
||||||
|
if style == ATX_CLOSED:
|
||||||
|
return '%s %s %s\n\n' % (hashes, text, hashes)
|
||||||
|
return '%s %s\n\n' % (hashes, text)
|
||||||
|
|
||||||
def convert_i(self, el, text):
|
def convert_i(self, el, text):
|
||||||
return self.convert_em(el, text)
|
return self.convert_em(el, text)
|
||||||
|
|
||||||
|
def convert_list(self, el, text):
|
||||||
|
nested = False
|
||||||
|
while el:
|
||||||
|
if el.name == 'li':
|
||||||
|
nested = True
|
||||||
|
break
|
||||||
|
el = el.parent
|
||||||
|
if nested:
|
||||||
|
# remove trailing newline if nested
|
||||||
|
return '\n' + self.indent(text, 1).rstrip()
|
||||||
|
return '\n' + text + '\n'
|
||||||
|
|
||||||
|
convert_ul = convert_list
|
||||||
|
convert_ol = convert_list
|
||||||
|
|
||||||
def convert_li(self, el, text):
|
def convert_li(self, el, text):
|
||||||
parent = el.parent
|
parent = el.parent
|
||||||
if parent is not None and parent.name == 'ol':
|
if parent is not None and parent.name == 'ol':
|
||||||
bullet = '%s.' % (parent.index(el) + 1)
|
bullet = '%s.' % (parent.index(el) + 1)
|
||||||
else:
|
else:
|
||||||
bullet = '*'
|
depth = -1
|
||||||
|
while el:
|
||||||
|
if el.name == 'ul':
|
||||||
|
depth += 1
|
||||||
|
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 '')
|
||||||
|
|
||||||
def convert_p(self, el, text):
|
def convert_p(self, el, text):
|
||||||
return '%s\n\n' % text if text else ''
|
return '%s\n\n' % text if text else ''
|
||||||
|
|
||||||
def convert_strong(self, el, text):
|
def convert_strong(self, el, text):
|
||||||
return '**%s**' % text if text else ''
|
prefix, suffix, text = chomp(text)
|
||||||
|
if not text:
|
||||||
|
return ''
|
||||||
|
return '%s**%s**%s' % (prefix, text, suffix)
|
||||||
|
|
||||||
|
def convert_img(self, el, text):
|
||||||
|
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 ''
|
||||||
|
return '' % (alt, src, title_part)
|
||||||
|
|
||||||
|
|
||||||
def markdownify(html, strip=None, convert=None):
|
def markdownify(html, **options):
|
||||||
converter = MarkdownConverter(strip, convert)
|
return MarkdownConverter(**options).convert(html)
|
||||||
return converter.convert(html)
|
|
||||||
|
|||||||
@@ -1,8 +0,0 @@
|
|||||||
pkgmeta = dict(
|
|
||||||
__title__='markdownify',
|
|
||||||
__author__='Matthew Tretter',
|
|
||||||
__version__='0.3.0',
|
|
||||||
)
|
|
||||||
|
|
||||||
globals().update(pkgmeta)
|
|
||||||
__all__ = pkgmeta.keys()
|
|
||||||
13
setup.py
13
setup.py
@@ -7,10 +7,11 @@ from setuptools.command.test import test as TestCommand, Command
|
|||||||
|
|
||||||
read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
|
read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
|
||||||
|
|
||||||
|
pkgmeta = {
|
||||||
pkgmeta = {}
|
'__title__': 'markdownify',
|
||||||
execfile(os.path.join(os.path.dirname(__file__), 'markdownify', 'pkgmeta.py'),
|
'__author__': 'Matthew Tretter',
|
||||||
pkgmeta)
|
'__version__': '0.5.0',
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
class PyTest(TestCommand):
|
class PyTest(TestCommand):
|
||||||
@@ -75,13 +76,13 @@ setup(
|
|||||||
'pytest',
|
'pytest',
|
||||||
],
|
],
|
||||||
install_requires=[
|
install_requires=[
|
||||||
'beautifulsoup4',
|
'beautifulsoup4', 'six'
|
||||||
],
|
],
|
||||||
classifiers=[
|
classifiers=[
|
||||||
'Environment :: Web Environment',
|
'Environment :: Web Environment',
|
||||||
'Framework :: Django',
|
'Framework :: Django',
|
||||||
'Intended Audience :: Developers',
|
'Intended Audience :: Developers',
|
||||||
'License :: OSI Approved :: BSD License',
|
'License :: OSI Approved :: MIT License',
|
||||||
'Operating System :: OS Independent',
|
'Operating System :: OS Independent',
|
||||||
'Programming Language :: Python :: 2.5',
|
'Programming Language :: Python :: 2.5',
|
||||||
'Programming Language :: Python :: 2.6',
|
'Programming Language :: Python :: 2.6',
|
||||||
|
|||||||
@@ -1,19 +1,75 @@
|
|||||||
from markdownify import markdownify as md
|
from markdownify import markdownify as md, ATX, ATX_CLOSED
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
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 test_a():
|
def test_a():
|
||||||
assert md('<a href="http://google.com">Google</a>') == '[Google](http://google.com)'
|
assert md('<a href="http://google.com">Google</a>') == '[Google](http://google.com)'
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_spaces():
|
||||||
|
assert md('foo <a href="http://google.com">Google</a> bar') == 'foo [Google](http://google.com) bar'
|
||||||
|
assert md('foo<a href="http://google.com"> Google</a> bar') == 'foo [Google](http://google.com) bar'
|
||||||
|
assert md('foo <a href="http://google.com">Google </a>bar') == 'foo [Google](http://google.com) bar'
|
||||||
|
assert md('foo <a href="http://google.com"></a> bar') == 'foo bar'
|
||||||
|
|
||||||
|
|
||||||
def test_a_with_title():
|
def test_a_with_title():
|
||||||
text = md('<a href="http://google.com" title="The "Goog"">Google</a>')
|
text = md('<a href="http://google.com" title="The "Goog"">Google</a>')
|
||||||
assert text == r'[Google](http://google.com "The \"Goog\"")'
|
assert text == r'[Google](http://google.com "The \"Goog\"")'
|
||||||
|
|
||||||
|
|
||||||
|
def test_a_shortcut():
|
||||||
|
text = md('<a href="http://google.com">http://google.com</a>')
|
||||||
|
assert text == '<http://google.com>'
|
||||||
|
|
||||||
|
|
||||||
|
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)'
|
||||||
|
|
||||||
|
|
||||||
def test_b():
|
def test_b():
|
||||||
assert md('<b>Hello</b>') == '**Hello**'
|
assert md('<b>Hello</b>') == '**Hello**'
|
||||||
|
|
||||||
|
|
||||||
|
def test_b_spaces():
|
||||||
|
assert md('foo <b>Hello</b> bar') == 'foo **Hello** bar'
|
||||||
|
assert md('foo<b> Hello</b> bar') == 'foo **Hello** bar'
|
||||||
|
assert md('foo <b>Hello </b>bar') == 'foo **Hello** bar'
|
||||||
|
assert md('foo <b></b> bar') == 'foo bar'
|
||||||
|
|
||||||
|
|
||||||
def test_blockquote():
|
def test_blockquote():
|
||||||
assert md('<blockquote>Hello</blockquote>').strip() == '> Hello'
|
assert md('<blockquote>Hello</blockquote>').strip() == '> Hello'
|
||||||
|
|
||||||
@@ -31,6 +87,13 @@ def test_em():
|
|||||||
assert md('<em>Hello</em>') == '*Hello*'
|
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 test_h1():
|
def test_h1():
|
||||||
assert md('<h1>Hello</h1>') == 'Hello\n=====\n\n'
|
assert md('<h1>Hello</h1>') == 'Hello\n=====\n\n'
|
||||||
|
|
||||||
@@ -44,12 +107,22 @@ def test_hn():
|
|||||||
assert md('<h6>Hello</h6>') == '###### Hello\n\n'
|
assert md('<h6>Hello</h6>') == '###### Hello\n\n'
|
||||||
|
|
||||||
|
|
||||||
|
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_i():
|
def test_i():
|
||||||
assert md('<i>Hello</i>') == '*Hello*'
|
assert md('<i>Hello</i>') == '*Hello*'
|
||||||
|
|
||||||
|
|
||||||
def test_ol():
|
def test_ol():
|
||||||
assert md('<ol><li>a</li><li>b</li></ol>') == '1. a\n2. b\n'
|
assert md('<ol><li>a</li><li>b</li></ol>') == '\n1. a\n2. b\n\n'
|
||||||
|
|
||||||
|
|
||||||
def test_p():
|
def test_p():
|
||||||
@@ -61,4 +134,25 @@ def test_strong():
|
|||||||
|
|
||||||
|
|
||||||
def test_ul():
|
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></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" />') == ''
|
||||||
|
assert md('<img src="/path/to/img.jpg" alt="Alt text" />') == ''
|
||||||
|
|||||||
Reference in New Issue
Block a user