Compare commits
47 Commits
Inzaniak-d
...
0.11.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d375116807 | ||
|
|
87b9f6c88e | ||
|
|
bda367dad9 | ||
|
|
61e8940486 | ||
|
|
35479d2d3b | ||
|
|
b589863715 | ||
|
|
423b7e948c | ||
|
|
0ea95de4d0 | ||
|
|
ed3eee78d2 | ||
|
|
eb0330bfc6 | ||
|
|
ddda696396 | ||
|
|
0a1343a538 | ||
|
|
9d0b839b73 | ||
|
|
28793ac0b3 | ||
|
|
d3eff11617 | ||
|
|
bd6b581122 | ||
|
|
9231704988 | ||
|
|
c8f7cf63e3 | ||
|
|
12a68a7d14 | ||
|
|
1613c302bc | ||
|
|
478b1c7e13 | ||
|
|
ffcf6cbcb2 | ||
|
|
0ab0452414 | ||
|
|
b62b067cbd | ||
|
|
55c9e84f38 | ||
|
|
99875683ac | ||
|
|
eaeb0603eb | ||
|
|
cb73590623 | ||
|
|
59417ab115 | ||
|
|
917b01e548 | ||
|
|
652714859d | ||
|
|
ea5b22824b | ||
|
|
ec5858e42f | ||
|
|
02bb914ef3 | ||
|
|
21c0d034d0 | ||
|
|
e3ddc789a2 | ||
|
|
2d0cd97323 | ||
|
|
ec185e2e9c | ||
|
|
079d1721aa | ||
|
|
bf24df3e2e | ||
|
|
15329588b1 | ||
|
|
34ad8485fa | ||
|
|
f0ce934bf8 | ||
|
|
99cd237f27 | ||
|
|
2bde8d3e8e | ||
|
|
8c9b029756 | ||
|
|
ae50065872 |
54
README.rst
54
README.rst
@@ -32,14 +32,14 @@ Convert some HTML to Markdown:
|
||||
from markdownify import markdownify as md
|
||||
md('<b>Yay</b> <a href="http://github.com">GitHub</a>') # > '**Yay** [GitHub](http://github.com)'
|
||||
|
||||
Specify tags to exclude (blacklist):
|
||||
Specify tags to exclude:
|
||||
|
||||
.. code:: python
|
||||
|
||||
from markdownify import markdownify as md
|
||||
md('<b>Yay</b> <a href="http://github.com">GitHub</a>', strip=['a']) # > '**Yay** GitHub'
|
||||
|
||||
\...or specify the tags you want to include (whitelist):
|
||||
\...or specify the tags you want to include:
|
||||
|
||||
.. code:: python
|
||||
|
||||
@@ -53,11 +53,11 @@ Options
|
||||
Markdownify supports the following options:
|
||||
|
||||
strip
|
||||
A list of tags to strip (blacklist). This option can't be used with the
|
||||
A list of tags to strip. 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
|
||||
A list of tags to convert. This option can't be used with the
|
||||
``strip`` option.
|
||||
|
||||
autolinks
|
||||
@@ -96,10 +96,56 @@ newline_style
|
||||
newline). While the latter convention is non-standard, it is commonly
|
||||
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.
|
||||
|
||||
code_language_callback
|
||||
When the HTML code contains ``pre`` tags that in some way provide the code
|
||||
language, for example as class, this callback can be used to extract the
|
||||
language from the tag and prefix it to the converted ``pre`` tag.
|
||||
The callback gets one single argument, an BeautifylSoup object, and returns
|
||||
a string containing the code language, or ``None``.
|
||||
An example to use the class name as code language could be::
|
||||
|
||||
def callback(el):
|
||||
return el['class'][0] if el.has_attr('class') else None
|
||||
|
||||
Defaults to ``None``.
|
||||
|
||||
escape_asterisks
|
||||
If set to ``False``, do not escape ``*`` to ``\*`` in text.
|
||||
Defaults to ``True``.
|
||||
|
||||
escape_underscores
|
||||
If set to ``False``, do not escape ``_`` to ``\_`` in text.
|
||||
Defaults to ``True``.
|
||||
|
||||
keep_inline_images_in
|
||||
Images are converted to their alt-text when the images are located inside
|
||||
headlines or table cells. If some inline images should be converted to
|
||||
markdown images instead, this option can be set to a list of parent tags
|
||||
that should be allowed to contain inline images, for example ``['td']``.
|
||||
Defaults to an empty list.
|
||||
|
||||
Options may be specified as kwargs to the ``markdownify`` function, or as a
|
||||
nested ``Options`` class in ``MarkdownConverter`` subclasses.
|
||||
|
||||
|
||||
Converting BeautifulSoup objects
|
||||
================================
|
||||
|
||||
.. code:: python
|
||||
|
||||
from markdownify import MarkdownConverter
|
||||
|
||||
# Create shorthand method for conversion
|
||||
def md(soup, **options):
|
||||
return MarkdownConverter(**options).convert_soup(soup)
|
||||
|
||||
|
||||
Creating Custom Converters
|
||||
==========================
|
||||
|
||||
|
||||
@@ -25,12 +25,6 @@ ASTERISK = '*'
|
||||
UNDERSCORE = '_'
|
||||
|
||||
|
||||
def escape(text):
|
||||
if not text:
|
||||
return ''
|
||||
return text.replace('_', r'\_')
|
||||
|
||||
|
||||
def chomp(text):
|
||||
"""
|
||||
If the text in an inline tag like b, a, or em contains a leading or trailing
|
||||
@@ -68,15 +62,19 @@ class MarkdownConverter(object):
|
||||
class DefaultOptions:
|
||||
autolinks = True
|
||||
bullets = '*+-' # An iterable of bullet types.
|
||||
code_language = ''
|
||||
code_language_callback = None
|
||||
convert = None
|
||||
default_title = False
|
||||
escape_asterisks = True
|
||||
escape_underscores = True
|
||||
heading_style = UNDERLINED
|
||||
keep_inline_images_in = []
|
||||
newline_style = SPACES
|
||||
strip = None
|
||||
strong_em_symbol = ASTERISK
|
||||
sub_symbol = ''
|
||||
sup_symbol = ''
|
||||
code_language = ''
|
||||
|
||||
class Options(DefaultOptions):
|
||||
pass
|
||||
@@ -93,6 +91,9 @@ class MarkdownConverter(object):
|
||||
|
||||
def convert(self, html):
|
||||
soup = BeautifulSoup(html, 'html.parser')
|
||||
return self.convert_soup(soup)
|
||||
|
||||
def convert_soup(self, soup):
|
||||
return self.process_tag(soup, convert_as_inline=False, children_only=True)
|
||||
|
||||
def process_tag(self, node, convert_as_inline, children_only=False):
|
||||
@@ -155,7 +156,7 @@ class MarkdownConverter(object):
|
||||
text = whitespace_re.sub(' ', text)
|
||||
|
||||
if el.parent.name != 'code':
|
||||
text = escape(text)
|
||||
text = self.escape(text)
|
||||
|
||||
# remove trailing whitespaces if any of the following condition is true:
|
||||
# - current text node is the last node in li
|
||||
@@ -193,6 +194,15 @@ class MarkdownConverter(object):
|
||||
else:
|
||||
return True
|
||||
|
||||
def escape(self, text):
|
||||
if not text:
|
||||
return ''
|
||||
if self.options['escape_asterisks']:
|
||||
text = text.replace('*', r'\*')
|
||||
if self.options['escape_underscores']:
|
||||
text = text.replace('_', r'\_')
|
||||
return text
|
||||
|
||||
def indent(self, text, level):
|
||||
return line_beginning_re.sub('\t' * level, text) if text else ''
|
||||
|
||||
@@ -272,7 +282,8 @@ class MarkdownConverter(object):
|
||||
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:
|
||||
if (convert_as_inline
|
||||
and el.parent.name not in self.options['keep_inline_images_in']):
|
||||
return alt
|
||||
|
||||
return '' % (alt, src, title_part)
|
||||
@@ -325,7 +336,12 @@ class MarkdownConverter(object):
|
||||
def convert_pre(self, el, text, convert_as_inline):
|
||||
if not text:
|
||||
return ''
|
||||
return '\n```%s\n%s\n```\n' % (self.options['code_language'], text)
|
||||
code_language = self.options['code_language']
|
||||
|
||||
if self.options['code_language_callback']:
|
||||
code_language = self.options['code_language_callback'](el) or code_language
|
||||
|
||||
return '\n```%s\n%s\n```\n' % (code_language, text)
|
||||
|
||||
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 = {
|
||||
'__title__': 'markdownify',
|
||||
'__author__': 'Matthew Tretter',
|
||||
'__version__': '0.9.4',
|
||||
'__version__': '0.11.0',
|
||||
}
|
||||
|
||||
|
||||
@@ -70,7 +70,7 @@ setup(
|
||||
zip_safe=False,
|
||||
include_package_data=True,
|
||||
setup_requires=[
|
||||
'flake8>=3.8,<4',
|
||||
'flake8>=3.8,<5',
|
||||
],
|
||||
tests_require=[
|
||||
'pytest>=6.2,<7',
|
||||
|
||||
@@ -133,12 +133,13 @@ def test_hn_nested_simple_tag():
|
||||
|
||||
def test_hn_nested_img():
|
||||
image_attributes_to_markdown = [
|
||||
("", ""),
|
||||
("alt='Alt Text'", "Alt Text"),
|
||||
("alt='Alt Text' title='Optional title'", "Alt Text"),
|
||||
("", "", ""),
|
||||
("alt='Alt Text'", "Alt Text", ""),
|
||||
("alt='Alt Text' title='Optional title'", "Alt Text", " \"Optional title\""),
|
||||
]
|
||||
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'
|
||||
for image_attributes, markdown, title in image_attributes_to_markdown:
|
||||
assert md('<h3>A <img src="/path/to/img.jpg" ' + image_attributes + '/> B</h3>') == '### A ' + markdown + ' B\n\n'
|
||||
assert md('<h3>A <img src="/path/to/img.jpg" ' + image_attributes + '/> B</h3>', keep_inline_images_in=['h3']) == '### A  B\n\n'
|
||||
|
||||
|
||||
def test_hn_atx_headings():
|
||||
@@ -215,3 +216,12 @@ def test_sup():
|
||||
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'
|
||||
|
||||
|
||||
def test_lang_callback():
|
||||
def callback(el):
|
||||
return el['class'][0] if el.has_attr('class') else None
|
||||
|
||||
assert md('<pre class="python">test\n foo\nbar</pre>', code_language_callback=callback) == '\n```python\ntest\n foo\nbar\n```\n'
|
||||
assert md('<pre class="javascript"><code>test\n foo\nbar</code></pre>', code_language_callback=callback) == '\n```javascript\ntest\n foo\nbar\n```\n'
|
||||
assert md('<pre class="javascript"><code class="javascript">test\n foo\nbar</code></pre>', code_language_callback=callback) == '\n```javascript\ntest\n foo\nbar\n```\n'
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from markdownify import MarkdownConverter
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
|
||||
class ImageBlockConverter(MarkdownConverter):
|
||||
@@ -16,3 +17,9 @@ def test_img():
|
||||
|
||||
assert md('<img src="/path/to/img.jpg" alt="Alt text" title="Optional title" />') == '\n\n'
|
||||
assert md('<img src="/path/to/img.jpg" alt="Alt text" />') == '\n\n'
|
||||
|
||||
|
||||
def test_soup():
|
||||
html = '<b>test</b>'
|
||||
soup = BeautifulSoup(html, 'html.parser')
|
||||
assert MarkdownConverter().convert_soup(soup) == '**test**'
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
from markdownify import markdownify as md
|
||||
|
||||
|
||||
def test_asterisks():
|
||||
assert md('*hey*dude*') == r'\*hey\*dude\*'
|
||||
assert md('*hey*dude*', escape_asterisks=False) == r'*hey*dude*'
|
||||
|
||||
|
||||
def test_underscore():
|
||||
assert md('_hey_dude_') == r'\_hey\_dude\_'
|
||||
assert md('_hey_dude_', escape_underscores=False) == r'_hey_dude_'
|
||||
|
||||
|
||||
def test_xml_entities():
|
||||
|
||||
Reference in New Issue
Block a user