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 "convert_custom_tag(): %s" % text def convert_h1(self, el, text, parent_tags): """Ensure explicit heading conversion function is used""" return "convert_h1: %s" % (text) def convert_hN(self, n, el, text, parent_tags): """Ensure general heading conversion function is used""" return "convert_hN(%d): %s" % (n, text) def test_custom_conversion_functions(): # Create shorthand method for conversion def md(html, **options): return UnitTestConverter(**options).convert(html) assert md('Alt texttext') == '![Alt text](/path/to/img.jpg "Optional title")\n\ntext' assert md('Alt texttext') == '![Alt text](/path/to/img.jpg)\n\ntext' assert md("text") == "convert_custom_tag(): text" assert md("

text

") == "convert_h1: text" assert md("

text

") == "convert_hN(3): text" def test_soup(): html = 'test' soup = BeautifulSoup(html, 'html.parser') assert MarkdownConverter().convert_soup(soup) == '**test**'