228 lines
8.1 KiB
Python
228 lines
8.1 KiB
Python
from markdownify import markdownify as md, ATX, ATX_CLOSED, BACKSLASH, UNDERSCORE
|
|
|
|
|
|
def inline_tests(tag, markup):
|
|
# test template for different inline tags
|
|
assert md(f'<{tag}>Hello</{tag}>') == f'{markup}Hello{markup}'
|
|
assert md(f'foo <{tag}>Hello</{tag}> bar') == f'foo {markup}Hello{markup} bar'
|
|
assert md(f'foo<{tag}> Hello</{tag}> bar') == f'foo {markup}Hello{markup} bar'
|
|
assert md(f'foo <{tag}>Hello </{tag}>bar') == f'foo {markup}Hello{markup} bar'
|
|
assert md(f'foo <{tag}></{tag}> bar') in ['foo bar', 'foo bar'] # Either is OK
|
|
|
|
|
|
def test_a():
|
|
assert md('<a href="https://google.com">Google</a>') == '[Google](https://google.com)'
|
|
assert md('<a href="https://google.com">https://google.com</a>') == '<https://google.com>'
|
|
assert md('<a href="https://community.kde.org/Get_Involved">https://community.kde.org/Get_Involved</a>') == '<https://community.kde.org/Get_Involved>'
|
|
assert md('<a href="https://community.kde.org/Get_Involved">https://community.kde.org/Get_Involved</a>', autolinks=False) == '[https://community.kde.org/Get\\_Involved](https://community.kde.org/Get_Involved)'
|
|
|
|
|
|
def test_a_spaces():
|
|
assert md('foo <a href="http://google.com">Google</a> bar') == 'foo [Google](http://google.com) bar'
|
|
assert md('foo<a href="http://google.com"> Google</a> bar') == 'foo [Google](http://google.com) bar'
|
|
assert md('foo <a href="http://google.com">Google </a>bar') == 'foo [Google](http://google.com) bar'
|
|
assert md('foo <a href="http://google.com"></a> bar') == 'foo bar'
|
|
|
|
|
|
def test_a_with_title():
|
|
text = md('<a href="http://google.com" title="The "Goog"">Google</a>')
|
|
assert text == r'[Google](http://google.com "The \"Goog\"")'
|
|
assert md('<a href="https://google.com">https://google.com</a>', default_title=True) == '[https://google.com](https://google.com "https://google.com")'
|
|
|
|
|
|
def test_a_shortcut():
|
|
text = md('<a href="http://google.com">http://google.com</a>')
|
|
assert text == '<http://google.com>'
|
|
|
|
|
|
def test_a_no_autolinks():
|
|
assert md('<a href="https://google.com">https://google.com</a>', autolinks=False) == '[https://google.com](https://google.com)'
|
|
|
|
|
|
def test_b():
|
|
assert md('<b>Hello</b>') == '**Hello**'
|
|
|
|
|
|
def test_b_spaces():
|
|
assert md('foo <b>Hello</b> bar') == 'foo **Hello** bar'
|
|
assert md('foo<b> Hello</b> bar') == 'foo **Hello** bar'
|
|
assert md('foo <b>Hello </b>bar') == 'foo **Hello** bar'
|
|
assert md('foo <b></b> bar') == 'foo bar'
|
|
|
|
|
|
def test_blockquote():
|
|
assert md('<blockquote>Hello</blockquote>') == '\n> Hello\n\n'
|
|
|
|
|
|
def test_blockquote_with_paragraph():
|
|
assert md('<blockquote>Hello</blockquote><p>handsome</p>') == '\n> Hello\n\nhandsome\n\n'
|
|
|
|
|
|
def test_blockquote_nested():
|
|
text = md('<blockquote>And she was like <blockquote>Hello</blockquote></blockquote>')
|
|
assert text == '\n> And she was like \n> > Hello\n> \n> \n\n'
|
|
|
|
|
|
def test_br():
|
|
assert md('a<br />b<br />c') == 'a \nb \nc'
|
|
assert md('a<br />b<br />c', newline_style=BACKSLASH) == 'a\\\nb\\\nc'
|
|
|
|
|
|
def test_code():
|
|
inline_tests('code', '`')
|
|
assert md('<code>this_should_not_escape</code>') == '`this_should_not_escape`'
|
|
|
|
|
|
def test_del():
|
|
inline_tests('del', '~~')
|
|
|
|
|
|
def test_div():
|
|
assert md('Hello</div> World') == 'Hello World'
|
|
|
|
|
|
def test_em():
|
|
inline_tests('em', '*')
|
|
|
|
|
|
def test_h1():
|
|
assert md('<h1>Hello</h1>') == 'Hello\n=====\n\n'
|
|
|
|
|
|
def test_h2():
|
|
assert md('<h2>Hello</h2>') == 'Hello\n-----\n\n'
|
|
|
|
|
|
def test_hn():
|
|
assert md('<h3>Hello</h3>') == '### Hello\n\n'
|
|
assert md('<h4>Hello</h4>') == '#### Hello\n\n'
|
|
assert md('<h5>Hello</h5>') == '##### Hello\n\n'
|
|
assert md('<h6>Hello</h6>') == '###### Hello\n\n'
|
|
|
|
|
|
def test_hn_chained():
|
|
assert md('<h1>First</h1>\n<h2>Second</h2>\n<h3>Third</h3>', heading_style=ATX) == '# First\n\n\n## Second\n\n\n### Third\n\n'
|
|
assert md('X<h1>First</h1>', heading_style=ATX) == 'X# First\n\n'
|
|
|
|
|
|
def test_hn_nested_tag_heading_style():
|
|
assert md('<h1>A <p>P</p> C </h1>', heading_style=ATX_CLOSED) == '# A P C #\n\n'
|
|
assert md('<h1>A <p>P</p> C </h1>', heading_style=ATX) == '# A P C\n\n'
|
|
|
|
|
|
def test_hn_nested_simple_tag():
|
|
tag_to_markdown = [
|
|
("strong", "**strong**"),
|
|
("b", "**b**"),
|
|
("em", "*em*"),
|
|
("i", "*i*"),
|
|
("p", "p"),
|
|
("a", "a"),
|
|
("div", "div"),
|
|
("blockquote", "blockquote"),
|
|
]
|
|
|
|
for tag, markdown in tag_to_markdown:
|
|
assert md('<h3>A <' + tag + '>' + tag + '</' + tag + '> B</h3>') == '### A ' + markdown + ' B\n\n'
|
|
|
|
assert md('<h3>A <br>B</h3>', heading_style=ATX) == '### A B\n\n'
|
|
|
|
# Nested lists not supported
|
|
# assert md('<h3>A <ul><li>li1</i><li>l2</li></ul></h3>', heading_style=ATX) == '### A li1 li2 B\n\n'
|
|
|
|
|
|
def test_hn_nested_img():
|
|
image_attributes_to_markdown = [
|
|
("", "", ""),
|
|
("alt='Alt Text'", "Alt Text", ""),
|
|
("alt='Alt Text' title='Optional title'", "Alt Text", " \"Optional title\""),
|
|
]
|
|
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():
|
|
assert md('<h1>Hello</h1>', heading_style=ATX) == '# Hello\n\n'
|
|
assert md('<h2>Hello</h2>', heading_style=ATX) == '## Hello\n\n'
|
|
|
|
|
|
def test_hn_atx_closed_headings():
|
|
assert md('<h1>Hello</h1>', heading_style=ATX_CLOSED) == '# Hello #\n\n'
|
|
assert md('<h2>Hello</h2>', heading_style=ATX_CLOSED) == '## Hello ##\n\n'
|
|
|
|
|
|
def test_head():
|
|
assert md('<head>head</head>') == 'head'
|
|
|
|
|
|
def test_hr():
|
|
assert md('Hello<hr>World') == 'Hello\n\n---\n\nWorld'
|
|
assert md('Hello<hr />World') == 'Hello\n\n---\n\nWorld'
|
|
assert md('<p>Hello</p>\n<hr>\n<p>World</p>') == 'Hello\n\n\n\n\n---\n\n\nWorld\n\n'
|
|
|
|
|
|
def test_i():
|
|
assert md('<i>Hello</i>') == '*Hello*'
|
|
|
|
|
|
def test_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" />') == ''
|
|
|
|
|
|
def test_kbd():
|
|
inline_tests('kbd', '`')
|
|
|
|
|
|
def test_p():
|
|
assert md('<p>hello</p>') == 'hello\n\n'
|
|
|
|
|
|
def test_pre():
|
|
assert md('<pre>test\n foo\nbar</pre>') == '\n```\ntest\n foo\nbar\n```\n'
|
|
assert md('<pre><code>test\n foo\nbar</code></pre>') == '\n```\ntest\n foo\nbar\n```\n'
|
|
|
|
|
|
def test_s():
|
|
inline_tests('s', '~~')
|
|
|
|
|
|
def test_samp():
|
|
inline_tests('samp', '`')
|
|
|
|
|
|
def test_strong():
|
|
assert md('<strong>Hello</strong>') == '**Hello**'
|
|
|
|
|
|
def test_strong_em_symbol():
|
|
assert md('<strong>Hello</strong>', strong_em_symbol=UNDERSCORE) == '__Hello__'
|
|
assert md('<b>Hello</b>', strong_em_symbol=UNDERSCORE) == '__Hello__'
|
|
assert md('<em>Hello</em>', strong_em_symbol=UNDERSCORE) == '_Hello_'
|
|
assert md('<i>Hello</i>', strong_em_symbol=UNDERSCORE) == '_Hello_'
|
|
|
|
|
|
def test_sub():
|
|
assert md('<sub>foo</sub>') == 'foo'
|
|
assert md('<sub>foo</sub>', sub_symbol='~') == '~foo~'
|
|
|
|
|
|
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'
|
|
|
|
|
|
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'
|