Compare commits

...

39 Commits
0.6.0 ... 0.6.6

Author SHA1 Message Date
AlexVonB
2d0cd97323 Merge branch 'develop' 2021-04-22 12:13:03 +02:00
AlexVonB
d4882b86b9 bump to v0.6.6 2021-04-22 12:12:51 +02:00
AlexVonB
b47d5f11c8 Merge pull request #37 from andredelft/develop
Add `strong_em_symbol` and `newline` options to the converter
2021-04-18 21:35:16 +02:00
André van Delft
29c794e17d Introduce OPTIONs for strong_em_symbol 2021-04-18 18:13:29 +02:00
André van Delft
e877602a5e Separate the strong_em_symbol and newline style tests 2021-04-05 11:28:42 +02:00
André van Delft
5580b0b51d Update README.rst 2021-04-05 11:13:52 +02:00
André van Delft
650f377b64 Fix linting 2021-04-05 11:13:19 +02:00
André van Delft
7ee87b1d32 Use .lower() on _style option fetching 2021-04-05 10:50:23 +02:00
André van Delft
16dbc471b9 Test newline_style 2021-04-05 10:47:55 +02:00
André van Delft
c04ec855dd Change option to newline_style and use variables like heading_style does 2021-04-05 10:44:20 +02:00
André van Delft
8da0bdf998 Test strong_em_symbol 2021-04-05 10:28:46 +02:00
AlexVonB
ec185e2e9c Merge branch 'develop' 2021-02-21 23:09:55 +01:00
AlexVonB
a59e4b9f48 bump to v0.6.5 2021-02-21 23:09:44 +01:00
AlexVonB
fd293a9714 use python 3.8 instead of 3.6 2021-02-21 23:08:49 +01:00
AlexVonB
99365de669 upgrading code for python 3.x
closes #38
2021-02-21 23:06:21 +01:00
AlexVonB
079d1721aa Merge branch 'develop' 2021-02-21 20:58:34 +01:00
AlexVonB
ed406d3206 bump to v0.6.4 2021-02-21 20:57:57 +01:00
AlexVonB
f320cf87ff closing #25 and #18
Adds newlines after blockquotes, allowing for paragraphs after a
blockquote.

Due to merging problems with @lucafrance 's code I had to quickly copy
and paste their code. Thanks for the contribution!
2021-02-21 20:53:44 +01:00
André van Delft
a79ed44ec3 Fix code ticks in README 2021-02-15 16:51:20 +01:00
André van Delft
29a4e551f7 Update README with the two new options 2021-02-15 16:37:13 +01:00
André van Delft
b3ac4606a6 Allow for the use of backslash for newlines 2021-02-15 16:29:14 +01:00
André van Delft
f093843f40 Allow for a custom strong or emphasis symbol 2021-02-15 16:19:19 +01:00
AlexVonB
a4d134df97 Merge pull request #34 from BrunoMiguens/add-ignore-comment-tags
Add ignore comment tags
2021-02-07 19:46:49 +01:00
Bruno Miguens
457454c713 Add new line at the end of file 2021-02-05 19:49:57 +00:00
Bruno Miguens
321e9eb5f6 Add ignore comment tags 2021-02-05 19:40:43 +00:00
AlexVonB
bf24df3e2e bump to v0.6.3 2021-01-12 22:43:18 +01:00
AlexVonB
15329588b1 Merge branch 'develop' 2021-01-12 22:42:58 +01:00
AlexVonB
77d1e99bd5 satisfy linter 2021-01-12 22:42:06 +01:00
AlexVonB
34ad8485fa bump to v0.6.2 2021-01-12 22:40:03 +01:00
AlexVonB
f0ce934bf8 Merge branch 'develop' 2021-01-12 22:39:47 +01:00
AlexVonB
97c78ef55b Merge branch 'fix-extra-headline-whitespace' into develop 2021-01-12 22:38:59 +01:00
AlexVonB
99cd237f27 Merge branch 'develop' 2021-01-04 10:22:02 +01:00
AlexVonB
b7e1ab889d bump to v0.6.1 2021-01-04 10:21:27 +01:00
AlexVonB
29e86aec55 Merge branch 'fix-link-underscores' into develop 2021-01-04 10:18:05 +01:00
AlexVonB
453b604096 Fixing autolinks
When checking a links href and text for
equality, first un-escape the underscores
in the text -- because six escapes them.
This should fix #29.
2021-01-02 17:22:36 +01:00
AlexVonB
2bde8d3e8e Merge branch 'develop' 2021-01-02 16:49:28 +01:00
AlexVonB
4f8937810b dont replace newlines and tabs with spaces
this should fix #17, as all leading new lines
were replaced with a single space, which in turn
was rendered before the # of a headline
2020-12-29 10:28:50 +01:00
AlexVonB
8c9b029756 Merge branch 'develop' 2020-09-01 18:10:07 +02:00
AlexVonB
ae50065872 Merge branch 'develop' 2020-08-18 18:53:10 +02:00
9 changed files with 92 additions and 25 deletions

View File

@@ -16,14 +16,14 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Set up Python 3.6
- name: Set up Python 3.8
uses: actions/setup-python@v2
with:
python-version: 3.6
python-version: 3.8
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install flake8==2.5.4 pytest
pip install flake8==3.8.4 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |

View File

@@ -17,7 +17,7 @@ jobs:
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: '3.x'
python-version: '3.8'
- name: Install dependencies
run: |
python -m pip install --upgrade pip

View File

@@ -75,6 +75,18 @@ bullets
lists are nested. Otherwise, the bullet will alternate based on nesting
level. Defaults to ``'*+-'``.
strong_em_symbol
In markdown, both ``*`` and ``_`` are used to encode **strong** or
*emphasized* texts. Either of these symbols can be chosen by the options
``ASTERISK`` (default) or ``UNDERSCORE`` respectively.
newline_style
Defines the style of marking linebreaks (``<br>``) in markdown. The default
value ``SPACES`` of this option will adopt the usual two spaces and a newline,
while ``BACKSLASH`` will convert a linebreak to ``\\n`` (a backslash an a
newline). While the latter convention is non-standard, it is commonly
preferred and supported by a lot of interpreters.
Options may be specified as kwargs to the ``markdownify`` function, or as a
nested ``Options`` class in ``MarkdownConverter`` subclasses.

View File

@@ -1,11 +1,11 @@
from bs4 import BeautifulSoup, NavigableString
from bs4 import BeautifulSoup, NavigableString, Comment
import re
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 ]+')
whitespace_re = re.compile(r'[\t ]+')
html_heading_re = re.compile(r'h[1-6]')
@@ -15,6 +15,14 @@ ATX_CLOSED = 'atx_closed'
UNDERLINED = 'underlined'
SETEXT = UNDERLINED
# Newline style
SPACES = 'spaces'
BACKSLASH = 'backslash'
# Strong and emphasis style
ASTERISK = '*'
UNDERSCORE = '_'
def escape(text):
if not text:
@@ -46,6 +54,8 @@ class MarkdownConverter(object):
autolinks = True
heading_style = UNDERLINED
bullets = '*+-' # An iterable of bullet types.
strong_em_symbol = ASTERISK
newline_style = SPACES
class Options(DefaultOptions):
pass
@@ -75,7 +85,9 @@ class MarkdownConverter(object):
# Convert the children first
for el in node.children:
if isinstance(el, NavigableString):
if isinstance(el, Comment):
continue
elif isinstance(el, NavigableString):
text += self.process_text(six.text_type(el))
else:
text += self.process_tag(el, convert_children_as_inline)
@@ -131,7 +143,8 @@ class MarkdownConverter(object):
return text
href = el.get('href')
title = el.get('title')
if self.options['autolinks'] and text == href and not title:
# For the replacement see #29: text nodes underscores are escaped
if self.options['autolinks'] and text.replace(r'\_', '_') == href and not title:
# Shortcut syntax
return '<%s>' % href
title_part = ' "%s"' % title.replace('"', r'\"') if title else ''
@@ -145,25 +158,29 @@ class MarkdownConverter(object):
if convert_as_inline:
return text
return '\n' + line_beginning_re.sub('> ', text) if text else ''
return '\n' + (line_beginning_re.sub('> ', text) + '\n\n') if text else ''
def convert_br(self, el, text, convert_as_inline):
if convert_as_inline:
return ""
return ' \n'
if self.options['newline_style'].lower() == BACKSLASH:
return '\\\n'
else:
return ' \n'
def convert_em(self, el, text, convert_as_inline):
em_tag = self.options['strong_em_symbol']
prefix, suffix, text = chomp(text)
if not text:
return ''
return '%s*%s*%s' % (prefix, text, suffix)
return '%s%s%s%s%s' % (prefix, em_tag, text, em_tag, suffix)
def convert_hn(self, n, el, text, convert_as_inline):
if convert_as_inline:
return text
style = self.options['heading_style']
style = self.options['heading_style'].lower()
text = text.rstrip()
if style == UNDERLINED and n <= 2:
line = '=' if n == 1 else '-'
@@ -219,10 +236,11 @@ class MarkdownConverter(object):
return '%s\n\n' % text if text else ''
def convert_strong(self, el, text, convert_as_inline):
strong_tag = 2 * self.options['strong_em_symbol']
prefix, suffix, text = chomp(text)
if not text:
return ''
return '%s**%s**%s' % (prefix, text, suffix)
return '%s%s%s%s%s' % (prefix, strong_tag, text, strong_tag, suffix)
def convert_img(self, el, text, convert_as_inline):
alt = el.attrs.get('alt', None) or ''

View File

@@ -10,7 +10,7 @@ read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
pkgmeta = {
'__title__': 'markdownify',
'__author__': 'Matthew Tretter',
'__version__': '0.6.0',
'__version__': '0.6.6',
}
@@ -50,7 +50,7 @@ class LintCommand(Command):
yield "%s.py" % filename
def run(self):
from flake8.engine import get_style_guide
from flake8.api.legacy import get_style_guide
flake8_style = get_style_guide(config_file='setup.cfg')
paths = self.distribution_files()
report = flake8_style.check_files(paths)
@@ -70,13 +70,13 @@ setup(
zip_safe=False,
include_package_data=True,
setup_requires=[
'flake8',
'flake8>=3.8,<4',
],
tests_require=[
'pytest',
'pytest>=6.2,<7',
],
install_requires=[
'beautifulsoup4', 'six'
'beautifulsoup4>=4.9,<5', 'six>=1.15,<2'
],
classifiers=[
'Environment :: Web Environment',
@@ -87,6 +87,9 @@ setup(
'Programming Language :: Python :: 2.5',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3.6',
'Programming Language :: Python :: 3.7',
'Programming Language :: Python :: 3.8',
'Topic :: Utilities'
],
cmdclass={

View File

@@ -4,3 +4,13 @@ from markdownify import markdownify as md
def test_nested():
text = md('<p>This is an <a href="http://example.com/">example link</a>.</p>')
assert text == 'This is an [example link](http://example.com/).\n\n'
def test_ignore_comments():
text = md("<!-- This is a comment -->")
assert text == ""
def test_ignore_comments_with_other_tags():
text = md("<!-- This is a comment --><a href='http://example.com/'>example link</a>")
assert text == "[example link](http://example.com/)"

View File

@@ -10,4 +10,4 @@ def test_soup():
def test_whitespace():
assert md(' a b \n\n c ') == ' a b c '
assert md(' a b \t\t c ') == ' a b c '

View File

@@ -1,4 +1,4 @@
from markdownify import markdownify as md, ATX, ATX_CLOSED
from markdownify import markdownify as md, ATX, ATX_CLOSED, BACKSLASH, UNDERSCORE
import re
@@ -34,7 +34,11 @@ def test_chomp():
def test_a():
assert md('<a href="http://google.com">Google</a>') == '[Google](http://google.com)'
assert md('<a href="https://google.com">Google</a>') == '[Google](https://google.com)'
assert md('<a href="https://google.com">https://google.com</a>', autolinks=False) == '[https://google.com](https://google.com)'
assert md('<a href="https://google.com">https://google.com</a>') == '<https://google.com>'
assert md('<a href="https://community.kde.org/Get_Involved">https://community.kde.org/Get_Involved</a>') == '<https://community.kde.org/Get_Involved>'
assert md('<a href="https://community.kde.org/Get_Involved">https://community.kde.org/Get_Involved</a>', autolinks=False) == '[https://community.kde.org/Get\\_Involved](https://community.kde.org/Get_Involved)'
def test_a_spaces():
@@ -71,12 +75,16 @@ def test_b_spaces():
def test_blockquote():
assert md('<blockquote>Hello</blockquote>').strip() == '> Hello'
assert md('<blockquote>Hello</blockquote>') == '\n> Hello\n\n'
def test_blockquote_with_paragraph():
assert md('<blockquote>Hello</blockquote><p>handsome</p>') == '\n> Hello\n\nhandsome\n\n'
def test_nested_blockquote():
text = md('<blockquote>And she was like <blockquote>Hello</blockquote></blockquote>').strip()
assert text == '> And she was like \n> > Hello'
text = md('<blockquote>And she was like <blockquote>Hello</blockquote></blockquote>')
assert text == '\n> And she was like \n> > Hello\n> \n> \n\n'
def test_br():
@@ -107,6 +115,11 @@ def test_hn():
assert md('<h6>Hello</h6>') == '###### Hello\n\n'
def test_hn_chained():
assert md('<h1>First</h1>\n<h2>Second</h2>\n<h3>Third</h3>', heading_style=ATX) == '# First\n\n\n## Second\n\n\n### Third\n\n'
assert md('X<h1>First</h1>', heading_style=ATX) == 'X# First\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'
@@ -207,3 +220,14 @@ def test_img():
def test_div():
assert md('Hello</div> World') == 'Hello World'
def test_strong_em_symbol():
assert md('<strong>Hello</strong>', strong_em_symbol=UNDERSCORE) == '__Hello__'
assert md('<b>Hello</b>', strong_em_symbol=UNDERSCORE) == '__Hello__'
assert md('<em>Hello</em>', strong_em_symbol=UNDERSCORE) == '_Hello_'
assert md('<i>Hello</i>', strong_em_symbol=UNDERSCORE) == '_Hello_'
def test_newline_style():
assert md('a<br />b<br />c', newline_style=BACKSLASH) == 'a\\\nb\\\nc'

View File

@@ -2,7 +2,7 @@ from markdownify import markdownify as md
def test_underscore():
assert md('_hey_dude_') == '\_hey\_dude\_'
assert md('_hey_dude_') == r'\_hey\_dude\_'
def test_xml_entities():