From 0a1343a538c16d185eecaefcb76287a184570367 Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Sun, 23 Jan 2022 11:00:19 +0100 Subject: [PATCH] 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**'