Compare commits

...

27 Commits
0.5.0 ... 0.6.0

Author SHA1 Message Date
AlexVonB
3544322ed2 Bump Version 0.6.0 2020-12-13 23:41:56 +01:00
AlexVonB
c4d0a14ce5 Merge pull request #26 from idvorkin/develop
Add support for headings that include nested divs
2020-12-13 23:39:34 +01:00
Igor Dvorkin
05ea8dc58a Add many tests and support image tag 2020-12-13 17:40:53 +00:00
Igor Dvorkin
7780f82c30 Using a regexp to determine if a tag is a heading. 2020-12-11 16:54:14 -08:00
Igor Dvorkin
d558617cd7 Add support for headings that include nested block elements 2020-11-20 06:03:51 -08:00
AlexVonB
25d68b4265 Bump version 0.5.3 2020-09-01 18:09:24 +02:00
AlexVonB
5561106991 Merge pull request #24 from SimonIT/fix-corrupt-html
Fix parsing corrupt html
2020-09-01 18:04:17 +02:00
SimonIT
1b3136ad04 Fix parsing corrupt html 2020-08-31 13:15:10 +02:00
AlexVonB
987a2a9cae Merge pull request #20 from SimonIT/badges
Add some fancy badges
2020-08-19 10:32:30 +02:00
SimonIT
a4461161bc Make badges inline 2020-08-19 10:06:21 +02:00
AlexVonB
19e2c3db0d Bump version 0.5.2 2020-08-18 18:52:53 +02:00
AlexVonB
ba51bbee12 Merge pull request #22 from SimonIT/ol-start-attribute
Support the start attribute for ordered lists
2020-08-18 18:44:59 +02:00
AlexVonB
9f3d497053 use python3.6 for linting 2020-08-18 18:41:46 +02:00
AlexVonB
d2fc689b66 set max flake8 version again3 2020-08-18 18:39:20 +02:00
AlexVonB
ab78385b56 set max flake8 version again2 2020-08-18 18:38:17 +02:00
AlexVonB
9ebf726e78 set max flake8 version again 2020-08-18 18:37:39 +02:00
AlexVonB
3f8403aa7a set max flake8 version 2020-08-18 18:35:31 +02:00
AlexVonB
5b6e76f984 Create python-app.yml 2020-08-18 18:30:55 +02:00
SimonIT
04711027e6 Replace downloads badge 2020-08-13 20:11:18 +02:00
SimonIT
ca98892953 Support the start attribute for ordered lists 2020-08-11 11:43:02 +02:00
AlexVonB
0dc281e6ea Bump version 0.5.1 2020-08-11 09:51:04 +02:00
AlexVonB
4e6e20e756 Merge pull request #21 from matthewwithanm/python-publish
Create python-publish.yml
2020-08-11 09:49:29 +02:00
Matthew Dapena-Tretter
9358522c73 Create python-publish.yml
Add workflow for publishing to PyPI.
2020-08-10 19:42:48 -07:00
SimonIT
28d7a22da3 Remove alt because it makes some trouble 2020-08-10 17:42:18 +02:00
SimonIT
8b882ca3c9 Add some fancy badges 2020-08-10 16:24:00 +02:00
AlexVonB
1078610066 ignore build folder 2020-08-10 13:03:12 +02:00
AlexVonB
d23dbc77e4 Merge branch 'master' into develop 2020-08-10 13:01:34 +02:00
7 changed files with 188 additions and 28 deletions

33
.github/workflows/python-app.yml vendored Normal file
View 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
View 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/*

1
.gitignore vendored
View File

@@ -7,3 +7,4 @@
/dist
/MANIFEST
/venv
build/

View File

@@ -1,3 +1,21 @@
|build| |version| |license| |downloads|
.. |build| image:: https://img.shields.io/github/workflow/status/matthewwithanm/python-markdownify/Python%20application/develop
:alt: GitHub Workflow Status
:target: https://github.com/matthewwithanm/python-markdownify/actions?query=workflow%3A%22Python+application%22
.. |version| image:: https://img.shields.io/pypi/v/markdownify
:alt: Pypi version
:target: https://pypi.org/project/markdownify/
.. |license| image:: https://img.shields.io/pypi/l/markdownify
:alt: License
:target: https://github.com/matthewwithanm/python-markdownify/blob/develop/LICENSE
.. |downloads| image:: https://pepy.tech/badge/markdownify
:alt: Pypi Downloads
:target: https://pepy.tech/project/markdownify
Installation
============

View File

@@ -6,8 +6,7 @@ import six
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
html_heading_re = re.compile(r'h[1-6]')
# Heading styles
@@ -62,27 +61,29 @@ class MarkdownConverter(object):
' convert, but not both.')
def convert(self, html):
# 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, 'html.parser')
return self.process_tag(soup.find(id=FRAGMENT_ID), children_only=True)
return self.process_tag(soup, convert_as_inline=False, children_only=True)
def process_tag(self, node, children_only=False):
def process_tag(self, node, convert_as_inline, children_only=False):
text = ''
# markdown headings can't include block elements (elements w/newlines)
isHeading = html_heading_re.match(node.name) is not None
convert_children_as_inline = convert_as_inline
if not children_only and isHeading:
convert_children_as_inline = True
# Convert the children first
for el in node.children:
if isinstance(el, NavigableString):
text += self.process_text(six.text_type(el))
else:
text += self.process_tag(el)
text += self.process_tag(el, convert_children_as_inline)
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)
text = convert_fn(node, text, convert_as_inline)
return text
@@ -95,8 +96,8 @@ class MarkdownConverter(object):
if m:
n = int(m.group(1))
def convert_tag(el, text):
return self.convert_hn(n, el, text)
def convert_tag(el, text, convert_as_inline):
return self.convert_hn(n, el, text, convert_as_inline)
convert_tag.__name__ = 'convert_h%s' % n
setattr(self, convert_tag.__name__, convert_tag)
@@ -122,10 +123,12 @@ class MarkdownConverter(object):
text = (text or '').rstrip()
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, convert_as_inline):
prefix, suffix, text = chomp(text)
if not text:
return ''
if convert_as_inline:
return text
href = el.get('href')
title = el.get('title')
if self.options['autolinks'] and text == href and not title:
@@ -134,22 +137,32 @@ class MarkdownConverter(object):
title_part = ' "%s"' % title.replace('"', r'\"') if title else ''
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)
def convert_b(self, el, text, convert_as_inline):
return self.convert_strong(el, text, convert_as_inline)
def convert_blockquote(self, el, text, convert_as_inline):
if convert_as_inline:
return text
def convert_blockquote(self, el, text):
return '\n' + line_beginning_re.sub('> ', text) if text else ''
def convert_br(self, el, text):
def convert_br(self, el, text, convert_as_inline):
if convert_as_inline:
return ""
return ' \n'
def convert_em(self, el, text):
def convert_em(self, el, text, convert_as_inline):
prefix, suffix, text = chomp(text)
if not text:
return ''
return '%s*%s*%s' % (prefix, text, suffix)
def convert_hn(self, n, el, text):
def convert_hn(self, n, el, text, convert_as_inline):
if convert_as_inline:
return text
style = self.options['heading_style']
text = text.rstrip()
if style == UNDERLINED and n <= 2:
@@ -160,10 +173,14 @@ class MarkdownConverter(object):
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_i(self, el, text, convert_as_inline):
return self.convert_em(el, text, convert_as_inline)
def convert_list(self, el, text, convert_as_inline):
# Converting a list to inline is undefined.
# Ignoring convert_to_inline for list.
def convert_list(self, el, text):
nested = False
while el:
if el.name == 'li':
@@ -178,10 +195,14 @@ class MarkdownConverter(object):
convert_ul = convert_list
convert_ol = convert_list
def convert_li(self, el, text):
def convert_li(self, el, text, convert_as_inline):
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:
@@ -192,20 +213,25 @@ class MarkdownConverter(object):
bullet = bullets[depth % len(bullets)]
return '%s %s\n' % (bullet, text or '')
def convert_p(self, el, text):
def convert_p(self, el, text, convert_as_inline):
if convert_as_inline:
return text
return '%s\n\n' % text if text else ''
def convert_strong(self, el, text):
def convert_strong(self, el, text, convert_as_inline):
prefix, suffix, text = chomp(text)
if not text:
return ''
return '%s**%s**%s' % (prefix, text, suffix)
def convert_img(self, el, text):
def convert_img(self, el, text, convert_as_inline):
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 ''
if convert_as_inline:
return alt
return '![%s](%s%s)' % (alt, src, title_part)

View File

@@ -10,7 +10,7 @@ read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
pkgmeta = {
'__title__': 'markdownify',
'__author__': 'Matthew Tretter',
'__version__': '0.5.0',
'__version__': '0.6.0',
}

View File

@@ -107,6 +107,52 @@ def test_hn():
assert md('<h6>Hello</h6>') == '###### Hello\n\n'
def test_hn_nested_tag_heading_style():
assert md('<h1>A <p>P</p> C </h1>', heading_style=ATX_CLOSED) == '# A P C #\n\n'
assert md('<h1>A <p>P</p> C </h1>', heading_style=ATX) == '# A P C\n\n'
def test_hn_nested_simple_tag():
tag_to_markdown = [
("strong", "**strong**"),
("b", "**b**"),
("em", "*em*"),
("i", "*i*"),
("p", "p"),
("a", "a"),
("div", "div"),
("blockquote", "blockquote"),
]
for tag, markdown in tag_to_markdown:
assert md('<h3>A <' + tag + '>' + tag + '</' + tag + '> B</h3>') == '### A ' + markdown + ' B\n\n'
assert md('<h3>A <br>B</h3>', heading_style=ATX) == '### A B\n\n'
# Nested lists not supported
# assert md('<h3>A <ul><li>li1</i><li>l2</li></ul></h3>', heading_style=ATX) == '### A li1 li2 B\n\n'
def test_hn_nested_img():
assert md('<img src="/path/to/img.jpg" alt="Alt text" title="Optional title" />') == '![Alt text](/path/to/img.jpg "Optional title")'
assert md('<img src="/path/to/img.jpg" alt="Alt text" />') == '![Alt text](/path/to/img.jpg)'
image_attributes_to_markdown = [
("", ""),
("alt='Alt Text'", "Alt Text"),
("alt='Alt Text' title='Optional title'", "Alt Text"),
]
for image_attributes, markdown in image_attributes_to_markdown:
assert md('<h3>A <img src="/path/to/img.jpg " ' + image_attributes + '/> B</h3>') == '### A ' + markdown + ' B\n\n'
def test_hr():
assert md('<hr>hr</hr>') == 'hr'
def test_head():
assert md('<head>head</head>') == 'head'
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'
@@ -123,6 +169,7 @@ def test_i():
def test_ol():
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():
@@ -156,3 +203,7 @@ def test_bullets():
def test_img():
assert md('<img src="/path/to/img.jpg" alt="Alt text" title="Optional title" />') == '![Alt text](/path/to/img.jpg "Optional title")'
assert md('<img src="/path/to/img.jpg" alt="Alt text" />') == '![Alt text](/path/to/img.jpg)'
def test_div():
assert md('Hello</div> World') == 'Hello World'