Compare commits
38 Commits
Inzaniak-d
...
0.10.3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
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 |
30
README.rst
30
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,32 @@ 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.
|
||||
|
||||
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
|
||||
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 ImageBlockConverter(**options).convert_soup(soup)
|
||||
|
||||
|
||||
Creating Custom Converters
|
||||
==========================
|
||||
|
||||
|
||||
@@ -25,10 +25,12 @@ ASTERISK = '*'
|
||||
UNDERSCORE = '_'
|
||||
|
||||
|
||||
def escape(text):
|
||||
def escape(text, escape_underscores):
|
||||
if not text:
|
||||
return ''
|
||||
return text.replace('_', r'\_')
|
||||
if escape_underscores:
|
||||
return text.replace('_', r'\_')
|
||||
return text
|
||||
|
||||
|
||||
def chomp(text):
|
||||
@@ -68,15 +70,16 @@ class MarkdownConverter(object):
|
||||
class DefaultOptions:
|
||||
autolinks = True
|
||||
bullets = '*+-' # An iterable of bullet types.
|
||||
code_language = ''
|
||||
convert = None
|
||||
default_title = False
|
||||
escape_underscores = True
|
||||
heading_style = UNDERLINED
|
||||
newline_style = SPACES
|
||||
strip = None
|
||||
strong_em_symbol = ASTERISK
|
||||
sub_symbol = ''
|
||||
sup_symbol = ''
|
||||
code_language = ''
|
||||
|
||||
class Options(DefaultOptions):
|
||||
pass
|
||||
@@ -93,6 +96,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 +161,7 @@ class MarkdownConverter(object):
|
||||
text = whitespace_re.sub(' ', text)
|
||||
|
||||
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:
|
||||
# - current text node is the last node in li
|
||||
|
||||
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.10.3',
|
||||
}
|
||||
|
||||
|
||||
@@ -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',
|
||||
|
||||
@@ -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**'
|
||||
|
||||
@@ -3,6 +3,7 @@ from markdownify import markdownify as md
|
||||
|
||||
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