Compare commits
18 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb98e9878f | ||
|
|
aa10053fbb | ||
|
|
253a34c2d7 | ||
|
|
3ea09609e6 | ||
|
|
1cd8e56c47 | ||
|
|
891a4a8d08 | ||
|
|
e5a1784f30 | ||
|
|
f60d910335 | ||
|
|
d707d107f6 | ||
|
|
1ef4dd1468 | ||
|
|
934c97b342 | ||
|
|
8a1e2d9403 | ||
|
|
5563723cbc | ||
|
|
a9c13a56da | ||
|
|
7bdeb15b18 | ||
|
|
87c8f3bd5e | ||
|
|
0211ac6619 | ||
|
|
2515e9e107 |
12
README.rst
12
README.rst
@@ -27,3 +27,15 @@ Specify tags to exclude (blacklist):
|
||||
|
||||
from markdownify import markdownify as md
|
||||
md('<b>Yay</b> <a href="http://github.com">GitHub</a>', convert=['b']) # > '**Yay** GitHub'
|
||||
|
||||
|
||||
Development
|
||||
===========
|
||||
|
||||
To run tests:
|
||||
|
||||
``python setup.py test``
|
||||
|
||||
To lint:
|
||||
|
||||
``python setup.py lint``
|
||||
|
||||
@@ -1,10 +1,19 @@
|
||||
from lxml.html.soupparser import fromstring
|
||||
from bs4 import BeautifulSoup, NavigableString
|
||||
import re
|
||||
|
||||
|
||||
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 ]+')
|
||||
FRAGMENT_ID = '__MARKDOWNIFY_WRAPPER__'
|
||||
wrapped = '<div id="%s">%%s</div>' % FRAGMENT_ID
|
||||
|
||||
|
||||
# Heading styles
|
||||
ATX = 'atx'
|
||||
ATX_CLOSED = 'atx_closed'
|
||||
UNDERLINED = 'underlined'
|
||||
SETEXT = UNDERLINED
|
||||
|
||||
|
||||
def escape(text):
|
||||
@@ -13,30 +22,53 @@ def escape(text):
|
||||
return text.replace('_', r'\_')
|
||||
|
||||
|
||||
def _todict(obj):
|
||||
return dict((k, getattr(obj, k)) for k in dir(obj) if not k.startswith('_'))
|
||||
|
||||
|
||||
class MarkdownConverter(object):
|
||||
def __init__(self, tags_to_strip=None, tags_to_convert=None):
|
||||
if tags_to_strip is not None and tags_to_convert is not None:
|
||||
class DefaultOptions:
|
||||
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'
|
||||
' convert, but not both.')
|
||||
self.tags_to_strip = tags_to_strip
|
||||
self.tags_to_convert = tags_to_convert
|
||||
' convert, but not both.')
|
||||
|
||||
def convert(self, html):
|
||||
soup = fromstring(html)
|
||||
return self.process_tag(soup)
|
||||
# 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,
|
||||
# create the document, and extract the element with the id.
|
||||
html = wrapped % html
|
||||
soup = BeautifulSoup(html)
|
||||
return self.process_tag(soup.find(id=FRAGMENT_ID), children_only=True)
|
||||
|
||||
def process_tag(self, node):
|
||||
text = self.process_text(node.text)
|
||||
def process_tag(self, node, children_only=False):
|
||||
text = ''
|
||||
|
||||
# Convert the children first
|
||||
for el in node.findall('*'):
|
||||
text += self.process_tag(el)
|
||||
for el in node.children:
|
||||
if isinstance(el, NavigableString):
|
||||
text += self.process_text(unicode(el))
|
||||
else:
|
||||
text += self.process_tag(el)
|
||||
|
||||
convert_fn = getattr(self, 'convert_%s' % node.tag, None)
|
||||
if convert_fn and self.should_convert_tag(node.tag):
|
||||
text = convert_fn(node, text)
|
||||
|
||||
text += self.process_text(node.tail)
|
||||
if not children_only:
|
||||
convert_fn = getattr(self, 'convert_%s' % node.name, None)
|
||||
if convert_fn and self.should_convert_tag(node.name):
|
||||
text = convert_fn(node, text)
|
||||
|
||||
return text
|
||||
|
||||
@@ -44,7 +76,7 @@ class MarkdownConverter(object):
|
||||
return escape(whitespace_re.sub(' ', text or ''))
|
||||
|
||||
def __getattr__(self, attr):
|
||||
# Handle heading levels > 2
|
||||
# Handle headings
|
||||
m = convert_heading_re.match(attr)
|
||||
if m:
|
||||
n = int(m.group(1))
|
||||
@@ -60,13 +92,18 @@ class MarkdownConverter(object):
|
||||
|
||||
def should_convert_tag(self, tag):
|
||||
tag = tag.lower()
|
||||
if self.tags_to_strip is not None:
|
||||
return tag not in self.tags_to_strip
|
||||
elif self.tags_to_convert is not None:
|
||||
return tag in self.tags_to_convert
|
||||
strip = self.options['strip']
|
||||
convert = self.options['convert']
|
||||
if strip is not None:
|
||||
return tag not in strip
|
||||
elif convert is not None:
|
||||
return tag in convert
|
||||
else:
|
||||
return True
|
||||
|
||||
def indent(self, text, level):
|
||||
return line_beginning_re.sub('\t' * level, text) if text else ''
|
||||
|
||||
def underline(self, text, pad_char):
|
||||
text = (text or '').rstrip()
|
||||
return '%s\n%s\n\n' % (text, pad_char * len(text)) if text else ''
|
||||
@@ -74,6 +111,9 @@ class MarkdownConverter(object):
|
||||
def convert_a(self, el, text):
|
||||
href = el.get('href')
|
||||
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 ''
|
||||
return '[%s](%s%s)' % (text or '', href, title_part) if href else text or ''
|
||||
|
||||
@@ -89,24 +129,46 @@ class MarkdownConverter(object):
|
||||
def convert_em(self, el, text):
|
||||
return '*%s*' % text if text else ''
|
||||
|
||||
def convert_h1(self, el, text):
|
||||
return self.underline(text, '=')
|
||||
|
||||
def convert_h2(self, el, text):
|
||||
return self.underline(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):
|
||||
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:
|
||||
text = '\n' + self.indent(text, 1)
|
||||
return text
|
||||
|
||||
convert_ul = convert_list
|
||||
convert_ol = convert_list
|
||||
|
||||
def convert_li(self, el, text):
|
||||
parent = el.getparent()
|
||||
if parent is not None and parent.tag == 'ol':
|
||||
parent = el.parent
|
||||
if parent is not None and parent.name == 'ol':
|
||||
bullet = '%s.' % (parent.index(el) + 1)
|
||||
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 '')
|
||||
|
||||
def convert_p(self, el, text):
|
||||
@@ -115,7 +177,13 @@ class MarkdownConverter(object):
|
||||
def convert_strong(self, el, text):
|
||||
return '**%s**' % text if text else ''
|
||||
|
||||
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):
|
||||
converter = MarkdownConverter(strip, convert)
|
||||
return converter.convert(html)
|
||||
|
||||
def markdownify(html, **options):
|
||||
return MarkdownConverter(**options).convert(html)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
pkgmeta = dict(
|
||||
__title__='markdownify',
|
||||
__author__='Matthew Tretter',
|
||||
__version__='0.2.0',
|
||||
__version__='0.4.0',
|
||||
)
|
||||
|
||||
globals().update(pkgmeta)
|
||||
|
||||
41
setup.py
41
setup.py
@@ -2,7 +2,7 @@
|
||||
import codecs
|
||||
import os
|
||||
from setuptools import setup, find_packages
|
||||
from setuptools.command.test import test as TestCommand
|
||||
from setuptools.command.test import test as TestCommand, Command
|
||||
|
||||
|
||||
read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
|
||||
@@ -25,6 +25,37 @@ class PyTest(TestCommand):
|
||||
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.engine import get_style_guide
|
||||
flake8_style = get_style_guide(config_file='setup.cfg')
|
||||
paths = self.distribution_files()
|
||||
report = flake8_style.check_files(paths)
|
||||
raise SystemExit(report.total_errors > 0)
|
||||
|
||||
|
||||
setup(
|
||||
name='markdownify',
|
||||
description='Convert HTML to markdown.',
|
||||
@@ -37,12 +68,14 @@ setup(
|
||||
packages=find_packages(),
|
||||
zip_safe=False,
|
||||
include_package_data=True,
|
||||
setup_requires=[
|
||||
'flake8',
|
||||
],
|
||||
tests_require=[
|
||||
'pytest',
|
||||
],
|
||||
install_requires=[
|
||||
'lxml',
|
||||
'BeautifulSoup',
|
||||
'beautifulsoup4',
|
||||
],
|
||||
classifiers=[
|
||||
'Environment :: Web Environment',
|
||||
@@ -55,8 +88,8 @@ setup(
|
||||
'Programming Language :: Python :: 2.7',
|
||||
'Topic :: Utilities'
|
||||
],
|
||||
setup_requires=[],
|
||||
cmdclass={
|
||||
'test': PyTest,
|
||||
'lint': LintCommand,
|
||||
},
|
||||
)
|
||||
|
||||
@@ -1,4 +1,25 @@
|
||||
from markdownify import markdownify as md
|
||||
from markdownify import markdownify as md, ATX, ATX_CLOSED
|
||||
import re
|
||||
|
||||
|
||||
nested_uls = re.sub('\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_a():
|
||||
@@ -10,6 +31,16 @@ def test_a_with_title():
|
||||
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():
|
||||
assert md('<b>Hello</b>') == '**Hello**'
|
||||
|
||||
@@ -44,6 +75,16 @@ def test_hn():
|
||||
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():
|
||||
assert md('<i>Hello</i>') == '*Hello*'
|
||||
|
||||
@@ -62,3 +103,20 @@ def test_strong():
|
||||
|
||||
def test_ul():
|
||||
assert md('<ul><li>a</li><li>b</li></ul>') == '* a\n* b\n'
|
||||
|
||||
|
||||
def test_nested_uls():
|
||||
"""
|
||||
Nested ULs should alternate bullet characters.
|
||||
|
||||
"""
|
||||
assert md(nested_uls) == '* 1\n\t+ a\n\t\t- I\n\t\t- II\n\t\t- III\n\t\t\n\t+ b\n\t+ c\n\t\n* 2\n* 3\n'
|
||||
|
||||
|
||||
def test_bullets():
|
||||
assert md(nested_uls, bullets='-') == '- 1\n\t- a\n\t\t- I\n\t\t- II\n\t\t- III\n\t\t\n\t- b\n\t- c\n\t\n- 2\n- 3\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