From 62e9f0de0258088412dc41f251d07c10bab0d0b8 Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 27 Jun 2021 15:53:23 +0200 Subject: [PATCH 1/5] add examples for custom converters closes #46 --- README.rst | 23 +++++++++++++++++++++++ tests/test_conversions.py | 2 -- tests/test_custom_converter.py | 18 ++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 tests/test_custom_converter.py diff --git a/README.rst b/README.rst index 6e914b7..6e071f2 100644 --- a/README.rst +++ b/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 =========== diff --git a/tests/test_conversions.py b/tests/test_conversions.py index b88d887..86d8b5d 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -131,8 +131,6 @@ def test_hn_nested_simple_tag(): def test_hn_nested_img(): - assert md('Alt text') == '![Alt text](/path/to/img.jpg "Optional title")' - assert md('Alt text') == '![Alt text](/path/to/img.jpg)' image_attributes_to_markdown = [ ("", ""), ("alt='Alt Text'", "Alt Text"), diff --git a/tests/test_custom_converter.py b/tests/test_custom_converter.py new file mode 100644 index 0000000..c7944ab --- /dev/null +++ b/tests/test_custom_converter.py @@ -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('Alt text') == '![Alt text](/path/to/img.jpg "Optional title")\n\n' + assert md('Alt text') == '![Alt text](/path/to/img.jpg)\n\n' From 828e11653092b49f9dc2d3aa93f337fd73f76bde Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Wed, 30 Jun 2021 13:02:36 +0200 Subject: [PATCH 2/5] add figure/figcaption for #46 --- markdownify/__init__.py | 6 ++++++ tests/test_conversions.py | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index ffde006..5bd492e 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -240,6 +240,12 @@ class MarkdownConverter(object): convert_em = abstract_inline_conversion(lambda self: self.options['strong_em_symbol']) + def convert_figcaption(self, el, text, convert_as_inline): + return "
%s
" % text + + def convert_figure(self, el, text, convert_as_inline): + return "
%s
" % text + convert_kbd = convert_code def convert_hn(self, n, el, text, convert_as_inline): diff --git a/tests/test_conversions.py b/tests/test_conversions.py index 86d8b5d..8afd589 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -39,6 +39,10 @@ def test_a_no_autolinks(): assert md('https://google.com', autolinks=False) == '[https://google.com](https://google.com)' +def test_a_containing_images(): + assert md('') == '[![](/path/to/img.jpg)](#)' + + def test_b(): assert md('Hello') == '**Hello**' @@ -84,6 +88,10 @@ def test_em(): inline_tests('em', '*') +def test_figure(): + assert md('
A
') == '
![](#)
A
' + + def test_h1(): assert md('

Hello

') == 'Hello\n=====\n\n' From 4aa6cf2a246796823ee3183d969343ecd4a40591 Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 11 Jul 2021 13:10:59 +0200 Subject: [PATCH 3/5] rewrote text processing to not escape _ in code fixes #47 --- markdownify/__init__.py | 20 ++++++++++++-------- tests/test_conversions.py | 1 + 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 5bd492e..2b5d145 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -142,22 +142,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 diff --git a/tests/test_conversions.py b/tests/test_conversions.py index 8afd589..e741607 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -74,6 +74,7 @@ def test_br(): def test_code(): inline_tests('code', '`') + assert md('this_should_not_escape') == '`this_should_not_escape`' def test_del(): From 16d8a0e1f78536113ab1f93ce9bfa1374a43efdd Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 11 Jul 2021 13:12:16 +0200 Subject: [PATCH 4/5] Revert "add figure/figcaption" This reverts commit 828e11653092b49f9dc2d3aa93f337fd73f76bde. --- markdownify/__init__.py | 6 ------ tests/test_conversions.py | 8 -------- 2 files changed, 14 deletions(-) diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 2b5d145..47485f5 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -244,12 +244,6 @@ class MarkdownConverter(object): convert_em = abstract_inline_conversion(lambda self: self.options['strong_em_symbol']) - def convert_figcaption(self, el, text, convert_as_inline): - return "
%s
" % text - - def convert_figure(self, el, text, convert_as_inline): - return "
%s
" % text - convert_kbd = convert_code def convert_hn(self, n, el, text, convert_as_inline): diff --git a/tests/test_conversions.py b/tests/test_conversions.py index e741607..0a5fba8 100644 --- a/tests/test_conversions.py +++ b/tests/test_conversions.py @@ -39,10 +39,6 @@ def test_a_no_autolinks(): assert md('https://google.com', autolinks=False) == '[https://google.com](https://google.com)' -def test_a_containing_images(): - assert md('') == '[![](/path/to/img.jpg)](#)' - - def test_b(): assert md('Hello') == '**Hello**' @@ -89,10 +85,6 @@ def test_em(): inline_tests('em', '*') -def test_figure(): - assert md('
A
') == '
![](#)
A
' - - def test_h1(): assert md('

Hello

') == 'Hello\n=====\n\n' From 22180a166ddc6b4588e786f5090eb8ca406253e1 Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 11 Jul 2021 13:13:31 +0200 Subject: [PATCH 5/5] bump to v0.9.1 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 126526e..b5310cc 100644 --- a/setup.py +++ b/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.1', }