From 9d0b839b738d7c015a2ced6513ca5c6ae636cfa8 Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 23 Jan 2022 10:59:24 +0100 Subject: [PATCH 1/3] wording --- README.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.rst b/README.rst index 9a06912..4d6db8e 100644 --- a/README.rst +++ b/README.rst @@ -32,14 +32,14 @@ Convert some HTML to Markdown: from markdownify import markdownify as md md('Yay GitHub') # > '**Yay** [GitHub](http://github.com)' -Specify tags to exclude (blacklist): +Specify tags to exclude: .. code:: python from markdownify import markdownify as md md('Yay GitHub', strip=['a']) # > '**Yay** GitHub' -\...or specify the tags you want to include (whitelist): +\...or specify the tags you want to include: .. code:: python @@ -53,11 +53,11 @@ Options Markdownify supports the following options: strip - A list of tags to strip (blacklist). This option can't be used with the + A list of tags to strip. This option can't be used with the ``convert`` option. convert - A list of tags to convert (whitelist). This option can't be used with the + A list of tags to convert. This option can't be used with the ``strip`` option. autolinks From 0a1343a538c16d185eecaefcb76287a184570367 Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 23 Jan 2022 11:00:19 +0100 Subject: [PATCH 2/3] allow BeautifulSoup objects to be converted --- README.rst | 12 ++++++++++++ markdownify/__init__.py | 3 +++ tests/test_custom_converter.py | 7 +++++++ 3 files changed, 22 insertions(+) diff --git a/README.rst b/README.rst index 4d6db8e..7fa0b19 100644 --- a/README.rst +++ b/README.rst @@ -110,6 +110,18 @@ Options may be specified as kwargs to the ``markdownify`` function, or as a nested ``Options`` class in ``MarkdownConverter`` subclasses. +Converting BeautifulSoup objects +================================ + +.. code:: python + + from markdownify import MarkdownConverter + + # Create shorthand method for conversion + def md(soup, **options): + return ImageBlockConverter(**options).convert_soup(soup) + + Creating Custom Converters ========================== diff --git a/markdownify/__init__.py b/markdownify/__init__.py index 098c784..82262d4 100644 --- a/markdownify/__init__.py +++ b/markdownify/__init__.py @@ -96,6 +96,9 @@ class MarkdownConverter(object): def convert(self, html): soup = BeautifulSoup(html, 'html.parser') + return self.convert_soup(soup) + + def convert_soup(self, soup): return self.process_tag(soup, convert_as_inline=False, children_only=True) def process_tag(self, node, convert_as_inline, children_only=False): diff --git a/tests/test_custom_converter.py b/tests/test_custom_converter.py index c7944ab..a3e33ac 100644 --- a/tests/test_custom_converter.py +++ b/tests/test_custom_converter.py @@ -1,4 +1,5 @@ from markdownify import MarkdownConverter +from bs4 import BeautifulSoup class ImageBlockConverter(MarkdownConverter): @@ -16,3 +17,9 @@ def test_img(): 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' + + +def test_soup(): + html = 'test' + soup = BeautifulSoup(html, 'html.parser') + assert MarkdownConverter().convert_soup(soup) == '**test**' From ddda69639656f25da3579bd191275935e99d2dcc Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 23 Jan 2022 11:01:26 +0100 Subject: [PATCH 3/3] bump to v0.10.3 --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 895ed62..3e5d27b 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.10.2', + '__version__': '0.10.3', }