diff --git a/README.rst b/README.rst index 059a68f..e4848d6 100644 --- a/README.rst +++ b/README.rst @@ -109,13 +109,15 @@ code_language code_language_callback When the HTML code contains ``pre`` tags that in some way provide the code language, for example as class, this callback can be used to extract the - language from the tag and prefix it to the converted ``pre`` tag. - The callback gets one single argument, a BeautifulSoup object, and returns - a string containing the code language, or ``None``. + language from the tag and use it in the opening Markdown code fence. + The callback receives the current Beautiful Soup ``Tag`` for the + ``
`` element, not the enclosing document. Return a language string or
+ ``None`` to fall back to ``code_language``.
An example to use the class name as code language could be::
def callback(el):
- return el['class'][0] if el.has_attr('class') else None
+ classes = el.get_attribute_list('class')
+ return classes[0] if classes else None
Defaults to ``None``.
diff --git a/markdownify/__init__.pyi b/markdownify/__init__.pyi
index 2494011..401ed1c 100644
--- a/markdownify/__init__.pyi
+++ b/markdownify/__init__.pyi
@@ -1,5 +1,6 @@
from _typeshed import Incomplete
from typing import Callable, Union
+from bs4 import Tag
ATX: str
ATX_CLOSED: str
@@ -21,7 +22,7 @@ def markdownify(
bs4_options: str = ...,
bullets: str = ...,
code_language: str = ...,
- code_language_callback: Union[Callable[[Incomplete], Union[str, None]], None] = ...,
+ code_language_callback: Union[Callable[[Tag], Union[str, None]], None] = ...,
convert: Union[list[str], None] = ...,
default_title: bool = ...,
escape_asterisks: bool = ...,
@@ -49,7 +50,7 @@ class MarkdownConverter:
bs4_options: str = ...,
bullets: str = ...,
code_language: str = ...,
- code_language_callback: Union[Callable[[Incomplete], Union[str, None]], None] = ...,
+ code_language_callback: Union[Callable[[Tag], Union[str, None]], None] = ...,
convert: Union[list[str], None] = ...,
default_title: bool = ...,
escape_asterisks: bool = ...,
diff --git a/tests/types.py b/tests/types.py
index 7424978..638af90 100644
--- a/tests/types.py
+++ b/tests/types.py
@@ -1,5 +1,5 @@
from markdownify import markdownify, ASTERISK, BACKSLASH, LSTRIP, RSTRIP, SPACES, STRIP, UNDERLINED, UNDERSCORE, MarkdownConverter
-from bs4 import BeautifulSoup
+from bs4 import BeautifulSoup, Tag
from typing import Union
markdownify("Hello
") == "Hello" # test default of STRIP
@@ -59,7 +59,7 @@
MarkdownConverter().convert_soup(soup) == '**test**'
-def callback(el: BeautifulSoup) -> Union[str, None]:
+def callback(el: Tag) -> Union[str, None]:
return el['class'][0] if el.has_attr('class') else None