Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
``<pre>`` 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``.

Expand Down
5 changes: 3 additions & 2 deletions markdownify/__init__.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from _typeshed import Incomplete
from typing import Callable, Union
from bs4 import Tag

ATX: str
ATX_CLOSED: str
Expand All @@ -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 = ...,
Expand Down Expand Up @@ -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 = ...,
Expand Down
4 changes: 2 additions & 2 deletions tests/types.py
Original file line number Diff line number Diff line change
@@ -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("<p>Hello</p>") == "Hello" # test default of STRIP
Expand Down Expand Up @@ -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


Expand Down