From 1b3333073a7139938a25b65631beac62804c805f Mon Sep 17 00:00:00 2001 From: Fess-AKA-DeadMonk Date: Sun, 19 Jan 2025 17:48:08 +0300 Subject: [PATCH] for convert_* functions, allow for tags with special characters in their name (like "subtag-name") (#136) support custom conversion functions for tags with `:` and `-` characters in their names by mapping them to underscores in the function name --- markdownify/__init__.py | 3 ++- tests/test_custom_converter.py | 15 +++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 2360210..8e90a61 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -172,7 +172,8 @@ class MarkdownConverter(object): text = text_strip + newlines + next_text_strip if not children_only: - convert_fn = getattr(self, 'convert_%s' % node.name, None) + fn_name = 'convert_%s' % node.name.translate(''.maketrans(':-', '__')) + convert_fn = getattr(self, fn_name, None) if convert_fn and self.should_convert_tag(node.name): text = convert_fn(node, text, convert_as_inline) diff --git a/tests/test_custom_converter.py b/tests/test_custom_converter.py index a3e33ac..adc83f7 100644 --- a/tests/test_custom_converter.py +++ b/tests/test_custom_converter.py @@ -2,22 +2,29 @@ from markdownify import MarkdownConverter from bs4 import BeautifulSoup -class ImageBlockConverter(MarkdownConverter): +class UnitTestConverter(MarkdownConverter): """ - Create a custom MarkdownConverter that adds two newlines after an image + Create a custom MarkdownConverter for unit tests """ def convert_img(self, el, text, convert_as_inline): + """Add two newlines after an image""" return super().convert_img(el, text, convert_as_inline) + '\n\n' + def convert_custom_tag(self, el, text, convert_as_inline): + """Ensure conversion function is found for tags with special characters in name""" + return "FUNCTION USED: %s" % text -def test_img(): + +def test_custom_conversion_functions(): # Create shorthand method for conversion def md(html, **options): - return ImageBlockConverter(**options).convert(html) + return UnitTestConverter(**options).convert(html) assert md('Alt text') == '![Alt text](/path/to/img.jpg "Optional title")\n\n' assert md('Alt text') == '![Alt text](/path/to/img.jpg)\n\n' + assert md("text") == "FUNCTION USED: text" + def test_soup(): html = 'test'