Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
eb0330bfc6 | ||
|
|
ddda696396 | ||
|
|
0a1343a538 | ||
|
|
9d0b839b73 | ||
|
|
28793ac0b3 | ||
|
|
d3eff11617 | ||
|
|
bd6b581122 | ||
|
|
9231704988 | ||
|
|
c8f7cf63e3 | ||
|
|
12a68a7d14 |
24
README.rst
24
README.rst
@@ -32,14 +32,14 @@ Convert some HTML to Markdown:
|
|||||||
from markdownify import markdownify as md
|
from markdownify import markdownify as md
|
||||||
md('<b>Yay</b> <a href="http://github.com">GitHub</a>') # > '**Yay** [GitHub](http://github.com)'
|
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
|
.. code:: python
|
||||||
|
|
||||||
from markdownify import markdownify as md
|
from markdownify import markdownify as md
|
||||||
md('<b>Yay</b> <a href="http://github.com">GitHub</a>', strip=['a']) # > '**Yay** GitHub'
|
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
|
.. code:: python
|
||||||
|
|
||||||
@@ -53,11 +53,11 @@ Options
|
|||||||
Markdownify supports the following options:
|
Markdownify supports the following options:
|
||||||
|
|
||||||
strip
|
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`` option.
|
||||||
|
|
||||||
convert
|
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.
|
``strip`` option.
|
||||||
|
|
||||||
autolinks
|
autolinks
|
||||||
@@ -102,10 +102,26 @@ code_language
|
|||||||
should be annotated with `````python`` or similar.
|
should be annotated with `````python`` or similar.
|
||||||
Defaults to ``''`` (empty string) and can be any string.
|
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.
|
||||||
|
|
||||||
|
|
||||||
|
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
|
Creating Custom Converters
|
||||||
==========================
|
==========================
|
||||||
|
|
||||||
|
|||||||
@@ -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,15 +70,16 @@ 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
|
||||||
strong_em_symbol = ASTERISK
|
strong_em_symbol = ASTERISK
|
||||||
sub_symbol = ''
|
sub_symbol = ''
|
||||||
sup_symbol = ''
|
sup_symbol = ''
|
||||||
code_language = ''
|
|
||||||
|
|
||||||
class Options(DefaultOptions):
|
class Options(DefaultOptions):
|
||||||
pass
|
pass
|
||||||
@@ -93,6 +96,9 @@ class MarkdownConverter(object):
|
|||||||
|
|
||||||
def convert(self, html):
|
def convert(self, html):
|
||||||
soup = BeautifulSoup(html, 'html.parser')
|
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)
|
return self.process_tag(soup, convert_as_inline=False, children_only=True)
|
||||||
|
|
||||||
def process_tag(self, node, convert_as_inline, children_only=False):
|
def process_tag(self, node, convert_as_inline, children_only=False):
|
||||||
@@ -155,7 +161,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
|
||||||
|
|||||||
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.10.0',
|
'__version__': '0.10.3',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -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',
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from markdownify import MarkdownConverter
|
from markdownify import MarkdownConverter
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
|
|
||||||
class ImageBlockConverter(MarkdownConverter):
|
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" title="Optional title" />') == '\n\n'
|
||||||
assert md('<img src="/path/to/img.jpg" alt="Alt text" />') == '\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():
|
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():
|
||||||
|
|||||||
Reference in New Issue
Block a user