Compare commits
16 Commits
0.9.0
...
Inzaniak-d
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb2646cd93 | ||
|
|
9692b5e714 | ||
|
|
ac68c53a7d | ||
|
|
40dd30419c | ||
|
|
da56f7f56a | ||
|
|
8400b39dd9 | ||
|
|
5fc1441fe7 | ||
|
|
044615eff1 | ||
|
|
dbd9f3f3d2 | ||
|
|
0fdeb1ff6e | ||
|
|
6a2f3a4b42 | ||
|
|
22180a166d | ||
|
|
16d8a0e1f7 | ||
|
|
4aa6cf2a24 | ||
|
|
828e116530 | ||
|
|
62e9f0de02 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -8,3 +8,4 @@
|
||||
/MANIFEST
|
||||
/venv
|
||||
build/
|
||||
.vscode/settings.json
|
||||
|
||||
23
README.rst
23
README.rst
@@ -100,6 +100,29 @@ Options may be specified as kwargs to the ``markdownify`` function, or as a
|
||||
nested ``Options`` class in ``MarkdownConverter`` subclasses.
|
||||
|
||||
|
||||
Creating Custom Converters
|
||||
==========================
|
||||
|
||||
If you have a special usecase that calls for a special conversion, you can
|
||||
always inherit from ``MarkdownConverter`` and override the method you want to
|
||||
change:
|
||||
|
||||
.. code:: python
|
||||
|
||||
from markdownify import MarkdownConverter
|
||||
|
||||
class ImageBlockConverter(MarkdownConverter):
|
||||
"""
|
||||
Create a custom MarkdownConverter that adds two newlines after an image
|
||||
"""
|
||||
def convert_img(self, el, text, convert_as_inline):
|
||||
return super().convert_img(el, text, convert_as_inline) + '\n\n'
|
||||
|
||||
# Create shorthand method for conversion
|
||||
def md(html, **options):
|
||||
return ImageBlockConverter(**options).convert(html)
|
||||
|
||||
|
||||
Development
|
||||
===========
|
||||
|
||||
|
||||
@@ -76,6 +76,7 @@ class MarkdownConverter(object):
|
||||
strong_em_symbol = ASTERISK
|
||||
sub_symbol = ''
|
||||
sup_symbol = ''
|
||||
code_language = ''
|
||||
|
||||
class Options(DefaultOptions):
|
||||
pass
|
||||
@@ -96,11 +97,14 @@ class MarkdownConverter(object):
|
||||
|
||||
def process_tag(self, node, convert_as_inline, children_only=False):
|
||||
text = ''
|
||||
# markdown headings can't include block elements (elements w/newlines)
|
||||
|
||||
# markdown headings or cells can't include
|
||||
# block elements (elements w/newlines)
|
||||
isHeading = html_heading_re.match(node.name) is not None
|
||||
isCell = node.name in ['td', 'th']
|
||||
convert_children_as_inline = convert_as_inline
|
||||
|
||||
if not children_only and isHeading:
|
||||
if not children_only and (isHeading or isCell):
|
||||
convert_children_as_inline = True
|
||||
|
||||
# Remove whitespace-only textnodes in purely nested nodes
|
||||
@@ -142,22 +146,26 @@ class MarkdownConverter(object):
|
||||
return text
|
||||
|
||||
def process_text(self, el):
|
||||
text = six.text_type(el)
|
||||
text = six.text_type(el) or ''
|
||||
|
||||
# dont remove any whitespace when handling pre or code in pre
|
||||
if (el.parent.name == 'pre'
|
||||
or (el.parent.name == 'code' and el.parent.parent.name == 'pre')):
|
||||
return escape(text or '')
|
||||
if not (el.parent.name == 'pre'
|
||||
or (el.parent.name == 'code'
|
||||
and el.parent.parent.name == 'pre')):
|
||||
text = whitespace_re.sub(' ', text)
|
||||
|
||||
cleaned_text = escape(whitespace_re.sub(' ', text or ''))
|
||||
if el.parent.name != 'code':
|
||||
text = escape(text)
|
||||
|
||||
# remove trailing whitespaces if any of the following condition is true:
|
||||
# - current text node is the last node in li
|
||||
# - current text node is followed by an embedded list
|
||||
if el.parent.name == 'li' and (not el.next_sibling or el.next_sibling.name in ['ul', 'ol']):
|
||||
return cleaned_text.rstrip()
|
||||
if (el.parent.name == 'li'
|
||||
and (not el.next_sibling
|
||||
or el.next_sibling.name in ['ul', 'ol'])):
|
||||
text = text.rstrip()
|
||||
|
||||
return cleaned_text
|
||||
return text
|
||||
|
||||
def __getattr__(self, attr):
|
||||
# Handle headings
|
||||
@@ -196,8 +204,6 @@ class MarkdownConverter(object):
|
||||
prefix, suffix, text = chomp(text)
|
||||
if not text:
|
||||
return ''
|
||||
if convert_as_inline:
|
||||
return text
|
||||
href = el.get('href')
|
||||
title = el.get('title')
|
||||
# For the replacement see #29: text nodes underscores are escaped
|
||||
@@ -309,7 +315,7 @@ class MarkdownConverter(object):
|
||||
el = el.parent
|
||||
bullets = self.options['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):
|
||||
if convert_as_inline:
|
||||
@@ -319,7 +325,7 @@ class MarkdownConverter(object):
|
||||
def convert_pre(self, el, text, convert_as_inline):
|
||||
if not text:
|
||||
return ''
|
||||
return '\n```\n%s\n```\n' % text
|
||||
return '\n```%s\n%s\n```\n' % (self.options['code_language'], text)
|
||||
|
||||
convert_s = convert_del
|
||||
|
||||
|
||||
2
setup.py
2
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.0',
|
||||
'__version__': '0.9.4',
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -70,6 +70,7 @@ def test_br():
|
||||
|
||||
def test_code():
|
||||
inline_tests('code', '`')
|
||||
assert md('<code>this_should_not_escape</code>') == '`this_should_not_escape`'
|
||||
|
||||
|
||||
def test_del():
|
||||
@@ -131,8 +132,6 @@ def test_hn_nested_simple_tag():
|
||||
|
||||
|
||||
def test_hn_nested_img():
|
||||
assert md('<img src="/path/to/img.jpg" alt="Alt text" title="Optional title" />') == ''
|
||||
assert md('<img src="/path/to/img.jpg" alt="Alt text" />') == ''
|
||||
image_attributes_to_markdown = [
|
||||
("", ""),
|
||||
("alt='Alt Text'", "Alt Text"),
|
||||
@@ -211,3 +210,8 @@ def test_sub():
|
||||
def test_sup():
|
||||
assert md('<sup>foo</sup>') == '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'
|
||||
|
||||
18
tests/test_custom_converter.py
Normal file
18
tests/test_custom_converter.py
Normal file
@@ -0,0 +1,18 @@
|
||||
from markdownify import MarkdownConverter
|
||||
|
||||
|
||||
class ImageBlockConverter(MarkdownConverter):
|
||||
"""
|
||||
Create a custom MarkdownConverter that adds two newlines after an image
|
||||
"""
|
||||
def convert_img(self, el, text, convert_as_inline):
|
||||
return super().convert_img(el, text, convert_as_inline) + '\n\n'
|
||||
|
||||
|
||||
def test_img():
|
||||
# Create shorthand method for conversion
|
||||
def md(html, **options):
|
||||
return ImageBlockConverter(**options).convert(html)
|
||||
|
||||
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'
|
||||
@@ -51,6 +51,14 @@ def test_nested_ols():
|
||||
|
||||
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>
|
||||
<li> c
|
||||
</li>
|
||||
</ul>""") == '* a\n* b\n* c\n'
|
||||
|
||||
|
||||
def test_inline_ul():
|
||||
|
||||
@@ -39,6 +39,25 @@ table_with_html_content = """<table>
|
||||
</table>"""
|
||||
|
||||
|
||||
table_with_paragraphs = """<table>
|
||||
<tr>
|
||||
<th>Firstname</th>
|
||||
<th><p>Lastname</p></th>
|
||||
<th>Age</th>
|
||||
</tr>
|
||||
<tr>
|
||||
<td><p>Jill</p></td>
|
||||
<td><p>Smith</p></td>
|
||||
<td><p>50</p></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>Eve</td>
|
||||
<td>Jackson</td>
|
||||
<td>94</td>
|
||||
</tr>
|
||||
</table>"""
|
||||
|
||||
|
||||
table_with_header_column = """<table>
|
||||
<tr>
|
||||
<th>Firstname</th>
|
||||
@@ -124,6 +143,7 @@ table_missing_head = """<table>
|
||||
def test_table():
|
||||
assert md(table) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||
assert md(table_with_html_content) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| **Jill** | *Smith* | [50](#) |\n| Eve | Jackson | 94 |\n\n'
|
||||
assert md(table_with_paragraphs) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||
assert md(table_with_header_column) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||
assert md(table_head_body) == '\n\n| Firstname | Lastname | Age |\n| --- | --- | --- |\n| Jill | Smith | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||
assert md(table_missing_text) == '\n\n| | Lastname | Age |\n| --- | --- | --- |\n| Jill | | 50 |\n| Eve | Jackson | 94 |\n\n'
|
||||
|
||||
Reference in New Issue
Block a user