forked from matthewwithanm/python-markdownify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_custom_converter.py
More file actions
32 lines (23 loc) · 1.17 KB
/
test_custom_converter.py
File metadata and controls
32 lines (23 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from markdownify import MarkdownConverter
from bs4 import BeautifulSoup
class UnitTestConverter(MarkdownConverter):
"""
Create a custom MarkdownConverter for unit tests
"""
def convert_img(self, el, text, parent_tags):
"""Add two newlines after an image"""
return super().convert_img(el, text, parent_tags) + '\n\n'
def convert_custom_tag(self, el, text, parent_tags):
"""Ensure conversion function is found for tags with special characters in name"""
return "FUNCTION USED: %s" % text
def test_custom_conversion_functions():
# Create shorthand method for conversion
def md(html, **options):
return UnitTestConverter(**options).convert(html)
assert md('<img src="/path/to/img.jpg" alt="Alt text" title="Optional title" />text') == '\n\ntext'
assert md('<img src="/path/to/img.jpg" alt="Alt text" />text') == '\n\ntext'
assert md("<custom-tag>text</custom-tag>") == "FUNCTION USED: text"
def test_soup():
html = '<b>test</b>'
soup = BeautifulSoup(html, 'html.parser')
assert MarkdownConverter().convert_soup(soup) == '**test**'