Compare commits
34 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ae50065872 | ||
|
|
19e2c3db0d | ||
|
|
ba51bbee12 | ||
|
|
9f3d497053 | ||
|
|
d2fc689b66 | ||
|
|
ab78385b56 | ||
|
|
9ebf726e78 | ||
|
|
3f8403aa7a | ||
|
|
5b6e76f984 | ||
|
|
ca98892953 | ||
|
|
0dc281e6ea | ||
|
|
4e6e20e756 | ||
|
|
9358522c73 | ||
|
|
1078610066 | ||
|
|
d23dbc77e4 | ||
|
|
0c4b856b9c | ||
|
|
e9cc01938a | ||
|
|
aceced68eb | ||
|
|
3b049cdb9c | ||
|
|
b747378b52 | ||
|
|
ee73d89879 | ||
|
|
5563161c86 | ||
|
|
28e447d9ae | ||
|
|
89d14f4487 | ||
|
|
5f9243d91d | ||
|
|
d0f688d2e4 | ||
|
|
5ac08522be | ||
|
|
78afcc173e | ||
|
|
b132a6f5b3 | ||
|
|
0abe0a29e8 | ||
|
|
4932df631f | ||
|
|
8696e2bde1 | ||
|
|
ee53d85c41 | ||
|
|
53ba0daa77 |
33
.github/workflows/python-app.yml
vendored
Normal file
33
.github/workflows/python-app.yml
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
# This workflow will install Python dependencies, run tests and lint with a single version of Python
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
|
||||
|
||||
name: Python application
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ develop ]
|
||||
pull_request:
|
||||
branches: [ develop ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Set up Python 3.6
|
||||
uses: actions/setup-python@v2
|
||||
with:
|
||||
python-version: 3.6
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install flake8==2.5.4 pytest
|
||||
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
|
||||
- name: Lint with flake8
|
||||
run: |
|
||||
python setup.py lint
|
||||
- name: Test with pytest
|
||||
run: |
|
||||
python setup.py test
|
||||
31
.github/workflows/python-publish.yml
vendored
Normal file
31
.github/workflows/python-publish.yml
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
# This workflow will upload a Python Package using Twine when a release is created
|
||||
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries
|
||||
|
||||
name: Upload Python Package
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [created]
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Set up Python
|
||||
uses: actions/setup-python@v2
|
||||
with:
|
||||
python-version: '3.x'
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
python -m pip install --upgrade pip
|
||||
pip install setuptools wheel twine
|
||||
- name: Build and publish
|
||||
env:
|
||||
TWINE_USERNAME: ${{ secrets.PYPI_USERNAME }}
|
||||
TWINE_PASSWORD: ${{ secrets.PYPI_PASSWORD }}
|
||||
run: |
|
||||
python setup.py sdist bdist_wheel
|
||||
twine upload dist/*
|
||||
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,7 +1,10 @@
|
||||
*.pyc
|
||||
*.egg
|
||||
.eggs/
|
||||
*.egg-info/
|
||||
.DS_Store
|
||||
/.env
|
||||
/dist
|
||||
/MANIFEST
|
||||
/venv
|
||||
build/
|
||||
|
||||
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'
|
||||
|
||||
|
||||
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
|
||||
===========
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from bs4 import BeautifulSoup, NavigableString
|
||||
import re
|
||||
import six
|
||||
|
||||
|
||||
convert_heading_re = re.compile(r'convert_h(\d+)')
|
||||
@@ -22,6 +23,19 @@ def escape(text):
|
||||
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('_'))
|
||||
|
||||
@@ -52,7 +66,7 @@ class MarkdownConverter(object):
|
||||
# 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)
|
||||
soup = BeautifulSoup(html, 'html.parser')
|
||||
return self.process_tag(soup.find(id=FRAGMENT_ID), children_only=True)
|
||||
|
||||
def process_tag(self, node, children_only=False):
|
||||
@@ -61,7 +75,7 @@ class MarkdownConverter(object):
|
||||
# Convert the children first
|
||||
for el in node.children:
|
||||
if isinstance(el, NavigableString):
|
||||
text += self.process_text(unicode(el))
|
||||
text += self.process_text(six.text_type(el))
|
||||
else:
|
||||
text += self.process_tag(el)
|
||||
|
||||
@@ -109,13 +123,16 @@ class MarkdownConverter(object):
|
||||
return '%s\n%s\n\n' % (text, pad_char * len(text)) if text else ''
|
||||
|
||||
def convert_a(self, el, text):
|
||||
prefix, suffix, text = chomp(text)
|
||||
if not text:
|
||||
return ''
|
||||
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 ''
|
||||
return '%s[%s](%s%s)%s' % (prefix, text, href, title_part, suffix) if href else text
|
||||
|
||||
def convert_b(self, el, text):
|
||||
return self.convert_strong(el, text)
|
||||
@@ -127,7 +144,10 @@ class MarkdownConverter(object):
|
||||
return ' \n'
|
||||
|
||||
def convert_em(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_hn(self, n, el, text):
|
||||
style = self.options['heading_style']
|
||||
@@ -151,8 +171,9 @@ class MarkdownConverter(object):
|
||||
break
|
||||
el = el.parent
|
||||
if nested:
|
||||
text = '\n' + self.indent(text, 1)
|
||||
return text
|
||||
# remove trailing newline if nested
|
||||
return '\n' + self.indent(text, 1).rstrip()
|
||||
return '\n' + text + '\n'
|
||||
|
||||
convert_ul = convert_list
|
||||
convert_ol = convert_list
|
||||
@@ -160,7 +181,11 @@ class MarkdownConverter(object):
|
||||
def convert_li(self, el, text):
|
||||
parent = el.parent
|
||||
if parent is not None and parent.name == 'ol':
|
||||
bullet = '%s.' % (parent.index(el) + 1)
|
||||
if parent.get("start"):
|
||||
start = int(parent.get("start"))
|
||||
else:
|
||||
start = 1
|
||||
bullet = '%s.' % (start + parent.index(el))
|
||||
else:
|
||||
depth = -1
|
||||
while el:
|
||||
@@ -175,7 +200,10 @@ class MarkdownConverter(object):
|
||||
return '%s\n\n' % text if text else ''
|
||||
|
||||
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 ''
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
pkgmeta = dict(
|
||||
__title__='markdownify',
|
||||
__author__='Matthew Tretter',
|
||||
__version__='0.4.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()
|
||||
|
||||
|
||||
pkgmeta = {}
|
||||
execfile(os.path.join(os.path.dirname(__file__), 'markdownify', 'pkgmeta.py'),
|
||||
pkgmeta)
|
||||
pkgmeta = {
|
||||
'__title__': 'markdownify',
|
||||
'__author__': 'Matthew Tretter',
|
||||
'__version__': '0.5.2',
|
||||
}
|
||||
|
||||
|
||||
class PyTest(TestCommand):
|
||||
@@ -75,13 +76,13 @@ setup(
|
||||
'pytest',
|
||||
],
|
||||
install_requires=[
|
||||
'beautifulsoup4',
|
||||
'beautifulsoup4', 'six'
|
||||
],
|
||||
classifiers=[
|
||||
'Environment :: Web Environment',
|
||||
'Framework :: Django',
|
||||
'Intended Audience :: Developers',
|
||||
'License :: OSI Approved :: BSD License',
|
||||
'License :: OSI Approved :: MIT License',
|
||||
'Operating System :: OS Independent',
|
||||
'Programming Language :: Python :: 2.5',
|
||||
'Programming Language :: Python :: 2.6',
|
||||
|
||||
@@ -2,7 +2,7 @@ from markdownify import markdownify as md, ATX, ATX_CLOSED
|
||||
import re
|
||||
|
||||
|
||||
nested_uls = re.sub('\s+', '', """
|
||||
nested_uls = re.sub(r'\s+', '', """
|
||||
<ul>
|
||||
<li>1
|
||||
<ul>
|
||||
@@ -22,10 +22,28 @@ nested_uls = re.sub('\s+', '', """
|
||||
</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():
|
||||
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():
|
||||
text = md('<a href="http://google.com" title="The "Goog"">Google</a>')
|
||||
assert text == r'[Google](http://google.com "The \"Goog\"")'
|
||||
@@ -45,6 +63,13 @@ def test_b():
|
||||
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():
|
||||
assert md('<blockquote>Hello</blockquote>').strip() == '> Hello'
|
||||
|
||||
@@ -62,6 +87,13 @@ 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 test_h1():
|
||||
assert md('<h1>Hello</h1>') == 'Hello\n=====\n\n'
|
||||
|
||||
@@ -90,7 +122,8 @@ def test_i():
|
||||
|
||||
|
||||
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'
|
||||
assert md('<ol start="3"><li>a</li><li>b</li></ol>') == '\n3. a\n4. b\n\n'
|
||||
|
||||
|
||||
def test_p():
|
||||
@@ -102,7 +135,11 @@ def test_strong():
|
||||
|
||||
|
||||
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():
|
||||
@@ -110,11 +147,11 @@ 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'
|
||||
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='-') == '- 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'
|
||||
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():
|
||||
|
||||
Reference in New Issue
Block a user