Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
28793ac0b3 | ||
|
|
d3eff11617 | ||
|
|
bd6b581122 | ||
|
|
9231704988 | ||
|
|
c8f7cf63e3 | ||
|
|
12a68a7d14 | ||
|
|
1613c302bc | ||
|
|
478b1c7e13 | ||
|
|
ffcf6cbcb2 | ||
|
|
0ab0452414 | ||
|
|
b62b067cbd | ||
|
|
cb2646cd93 | ||
|
|
9692b5e714 | ||
|
|
ac68c53a7d | ||
|
|
55c9e84f38 | ||
|
|
40dd30419c | ||
|
|
da56f7f56a | ||
|
|
8400b39dd9 | ||
|
|
5fc1441fe7 | ||
|
|
044615eff1 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -8,3 +8,4 @@
|
|||||||
/MANIFEST
|
/MANIFEST
|
||||||
/venv
|
/venv
|
||||||
build/
|
build/
|
||||||
|
.vscode/settings.json
|
||||||
|
|||||||
10
README.rst
10
README.rst
@@ -96,6 +96,16 @@ newline_style
|
|||||||
newline). While the latter convention is non-standard, it is commonly
|
newline). While the latter convention is non-standard, it is commonly
|
||||||
preferred and supported by a lot of interpreters.
|
preferred and supported by a lot of interpreters.
|
||||||
|
|
||||||
|
code_language
|
||||||
|
Defines the language that should be assumed for all ``<pre>`` sections.
|
||||||
|
Useful, if all code on a page is in the same programming language and
|
||||||
|
should be annotated with `````python`` or similar.
|
||||||
|
Defaults to ``''`` (empty string) and can be any string.
|
||||||
|
|
||||||
|
escape_underscores
|
||||||
|
If set to ``False``, do not escape ``_`` to ``\_`` in text.
|
||||||
|
Defaults to ``True``.
|
||||||
|
|
||||||
Options may be specified as kwargs to the ``markdownify`` function, or as a
|
Options may be specified as kwargs to the ``markdownify`` function, or as a
|
||||||
nested ``Options`` class in ``MarkdownConverter`` subclasses.
|
nested ``Options`` class in ``MarkdownConverter`` subclasses.
|
||||||
|
|
||||||
|
|||||||
@@ -25,10 +25,12 @@ ASTERISK = '*'
|
|||||||
UNDERSCORE = '_'
|
UNDERSCORE = '_'
|
||||||
|
|
||||||
|
|
||||||
def escape(text):
|
def escape(text, escape_underscores):
|
||||||
if not text:
|
if not text:
|
||||||
return ''
|
return ''
|
||||||
return text.replace('_', r'\_')
|
if escape_underscores:
|
||||||
|
return text.replace('_', r'\_')
|
||||||
|
return text
|
||||||
|
|
||||||
|
|
||||||
def chomp(text):
|
def chomp(text):
|
||||||
@@ -68,8 +70,10 @@ class MarkdownConverter(object):
|
|||||||
class DefaultOptions:
|
class DefaultOptions:
|
||||||
autolinks = True
|
autolinks = True
|
||||||
bullets = '*+-' # An iterable of bullet types.
|
bullets = '*+-' # An iterable of bullet types.
|
||||||
|
code_language = ''
|
||||||
convert = None
|
convert = None
|
||||||
default_title = False
|
default_title = False
|
||||||
|
escape_underscores = True
|
||||||
heading_style = UNDERLINED
|
heading_style = UNDERLINED
|
||||||
newline_style = SPACES
|
newline_style = SPACES
|
||||||
strip = None
|
strip = None
|
||||||
@@ -154,7 +158,7 @@ class MarkdownConverter(object):
|
|||||||
text = whitespace_re.sub(' ', text)
|
text = whitespace_re.sub(' ', text)
|
||||||
|
|
||||||
if el.parent.name != 'code':
|
if el.parent.name != 'code':
|
||||||
text = escape(text)
|
text = escape(text, self.options['escape_underscores'])
|
||||||
|
|
||||||
# remove trailing whitespaces if any of the following condition is true:
|
# remove trailing whitespaces if any of the following condition is true:
|
||||||
# - current text node is the last node in li
|
# - current text node is the last node in li
|
||||||
@@ -314,7 +318,7 @@ class MarkdownConverter(object):
|
|||||||
el = el.parent
|
el = el.parent
|
||||||
bullets = self.options['bullets']
|
bullets = self.options['bullets']
|
||||||
bullet = bullets[depth % len(bullets)]
|
bullet = bullets[depth % len(bullets)]
|
||||||
return '%s %s\n' % (bullet, text or '')
|
return '%s %s\n' % (bullet, (text or '').strip())
|
||||||
|
|
||||||
def convert_p(self, el, text, convert_as_inline):
|
def convert_p(self, el, text, convert_as_inline):
|
||||||
if convert_as_inline:
|
if convert_as_inline:
|
||||||
@@ -324,7 +328,7 @@ class MarkdownConverter(object):
|
|||||||
def convert_pre(self, el, text, convert_as_inline):
|
def convert_pre(self, el, text, convert_as_inline):
|
||||||
if not text:
|
if not text:
|
||||||
return ''
|
return ''
|
||||||
return '\n```\n%s\n```\n' % text
|
return '\n```%s\n%s\n```\n' % (self.options['code_language'], text)
|
||||||
|
|
||||||
convert_s = convert_del
|
convert_s = convert_del
|
||||||
|
|
||||||
|
|||||||
4
setup.py
4
setup.py
@@ -10,7 +10,7 @@ read = lambda filepath: codecs.open(filepath, 'r', 'utf-8').read()
|
|||||||
pkgmeta = {
|
pkgmeta = {
|
||||||
'__title__': 'markdownify',
|
'__title__': 'markdownify',
|
||||||
'__author__': 'Matthew Tretter',
|
'__author__': 'Matthew Tretter',
|
||||||
'__version__': '0.9.3',
|
'__version__': '0.10.2',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -70,7 +70,7 @@ setup(
|
|||||||
zip_safe=False,
|
zip_safe=False,
|
||||||
include_package_data=True,
|
include_package_data=True,
|
||||||
setup_requires=[
|
setup_requires=[
|
||||||
'flake8>=3.8,<4',
|
'flake8>=3.8,<5',
|
||||||
],
|
],
|
||||||
tests_require=[
|
tests_require=[
|
||||||
'pytest>=6.2,<7',
|
'pytest>=6.2,<7',
|
||||||
|
|||||||
@@ -210,3 +210,8 @@ def test_sub():
|
|||||||
def test_sup():
|
def test_sup():
|
||||||
assert md('<sup>foo</sup>') == 'foo'
|
assert md('<sup>foo</sup>') == 'foo'
|
||||||
assert md('<sup>foo</sup>', sup_symbol='^') == '^foo^'
|
assert md('<sup>foo</sup>', sup_symbol='^') == '^foo^'
|
||||||
|
|
||||||
|
|
||||||
|
def test_lang():
|
||||||
|
assert md('<pre>test\n foo\nbar</pre>', code_language='python') == '\n```python\ntest\n foo\nbar\n```\n'
|
||||||
|
assert md('<pre><code>test\n foo\nbar</code></pre>', code_language='javascript') == '\n```javascript\ntest\n foo\nbar\n```\n'
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ from markdownify import markdownify as md
|
|||||||
|
|
||||||
def test_underscore():
|
def test_underscore():
|
||||||
assert md('_hey_dude_') == r'\_hey\_dude\_'
|
assert md('_hey_dude_') == r'\_hey\_dude\_'
|
||||||
|
assert md('_hey_dude_', escape_underscores=False) == r'_hey_dude_'
|
||||||
|
|
||||||
|
|
||||||
def test_xml_entities():
|
def test_xml_entities():
|
||||||
|
|||||||
@@ -51,6 +51,14 @@ def test_nested_ols():
|
|||||||
|
|
||||||
def test_ul():
|
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>') == '* a\n* b\n'
|
||||||
|
assert md("""<ul>
|
||||||
|
<li>
|
||||||
|
a
|
||||||
|
</li>
|
||||||
|
<li> b </li>
|
||||||
|
<li> c
|
||||||
|
</li>
|
||||||
|
</ul>""") == '* a\n* b\n* c\n'
|
||||||
|
|
||||||
|
|
||||||
def test_inline_ul():
|
def test_inline_ul():
|
||||||
|
|||||||
Reference in New Issue
Block a user