Basic conversion skeleton

This commit is contained in:
Matthew Tretter
2012-06-29 12:19:30 -04:00
commit 36cb7f6ac1
6 changed files with 103 additions and 0 deletions

0
README.rst Normal file
View File

42
markdownify/__init__.py Normal file
View File

@@ -0,0 +1,42 @@
from lxml.etree import tostring
from lxml.html.soupparser import fromstring
class MarkdownConverter(object):
def __init__(self, strip=None, keep=None):
if strip is not None and keep is not None:
raise ValueError('You may specify either tags to strip or tags to'
' keep, but not both.')
self.strip = strip
self.keep = keep
def convert(self, html):
soup = fromstring(html)
self.convert_tag(soup)
return soup.text
def convert_tag(self, node):
text = node.text or ''
# Convert the children first
for el in node.findall('*'):
self.convert_tag(el)
convert_fn = getattr(self, 'convert_%s' % el.tag, None)
tail = el.tail or ''
el.tail = ''
if convert_fn:
text += convert_fn(el)
else:
text += el.text or ''
text += tail
node.clear()
node.text = text
def markdownify(html, strip=None, keep=None):
converter = MarkdownConverter(strip, keep)
return converter.convert(html)

1
markdownify/version.py Normal file
View File

@@ -0,0 +1 @@
__version__ = '0.1.0'

5
runtests.py Normal file
View File

@@ -0,0 +1,5 @@
#!/usr/bin/env python
from nose.core import run, collector
if __name__ == '__main__':
run()

44
setup.py Normal file
View File

@@ -0,0 +1,44 @@
#/usr/bin/env python
import codecs
import os
from setuptools import setup, find_packages
read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
execfile(os.path.join(os.path.dirname(__file__), 'markdownify', 'version.py'))
setup(
name='markdownify',
description='Convert HTML to markdown.',
long_description=read(os.path.join(os.path.dirname(__file__), 'README.rst')),
version=__version__,
author='Matthew Tretter',
author_email='matthew@exanimo.com',
url='http://github.com/matthewwithanm/markdownify',
download_url='http://github.com/matthewwithanm/markdownify/tarball/master',
packages=find_packages(),
zip_safe=False,
include_package_data=True,
tests_require=[
'nose',
'unittest2',
],
install_requires=[
'lxml',
'BeautifulSoup',
],
classifiers=[
'Environment :: Web Environment',
'Framework :: Django',
'Intended Audience :: Developers',
'License :: OSI Approved :: BSD License',
'Operating System :: OS Independent',
'Programming Language :: Python :: 2.5',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Topic :: Utilities'
],
setup_requires=[],
test_suite='runtests.collector',
)

11
tests.py Normal file
View File

@@ -0,0 +1,11 @@
import unittest
from markdownify import markdownify as md
class BasicTests(unittest.TestCase):
def test_single_tag(self):
self.assertEqual(md('<span>Hello</span>'), 'Hello')
def test_soup(self):
self.assertEqual(md('<div><span>Hello</div></span>'), 'Hello')