Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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,12 @@
|
||||
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
|
||||
|
||||
|
||||
def escape(text):
|
||||
@@ -17,26 +19,32 @@ 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:
|
||||
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):
|
||||
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
|
||||
|
||||
@@ -102,8 +110,8 @@ class MarkdownConverter(object):
|
||||
return self.convert_em(el, text)
|
||||
|
||||
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 = '*'
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
pkgmeta = dict(
|
||||
__title__='markdownify',
|
||||
__author__='Matthew Tretter',
|
||||
__version__='0.2.0',
|
||||
__version__='0.3.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,
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user