From 804a3f8f079e1f9018030d80414d9981b531fccb Mon Sep 17 00:00:00 2001 From: AlexVonB Date: Tue, 26 Mar 2024 21:21:45 +0100 Subject: [PATCH] added further readme for custom converters --- README.rst | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 06f319b..51888ea 100644 --- a/README.rst +++ b/README.rst @@ -156,7 +156,12 @@ 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: +change. +The function that handles a HTML tag named ``abc`` is called +``convert_abc(self, el, text, convert_as_inline)`` and returns a string +containing the converted HTML tag. +The ``MarkdownConverter`` object will handle the conversion based on the +function names: .. code:: python @@ -173,6 +178,21 @@ change: def md(html, **options): return ImageBlockConverter(**options).convert(html) +.. code:: python + + from markdownify import MarkdownConverter + + class IgnoreParagraphsConverter(MarkdownConverter): + """ + Create a custom MarkdownConverter that ignores paragraphs + """ + def convert_p(self, el, text, convert_as_inline): + return '' + + # Create shorthand method for conversion + def md(html, **options): + return IgnoreParagraphsConverter(**options).convert(html) + Command Line Interface ======================