Pass code block's language to the specified Pygments formatter in CodeHilite by ccwang002 · Pull Request #1258 · Python-Markdown/markdown (original) (raw)

As per the discussions in PR #1255, I add a new option lang_str that passes the language of a code block to the specified Pygments formatter. lang_str has the value of {lang_prefix}{lang}, so it respects the existing language prefix option.

While lang_str has no effect the builtin Pygments formatters, users can leverage this information in their custom formatter to annotate the generated output.

An example I added to the documentation annotates the language as a class of the <code> tag. I copied it below:

from pygments.formatters import HtmlFormatter

class CustomHtmlFormatter(HtmlFormatter): def init(self, lang_str='', **options): super().init(**options) # lang_str has the value {lang_prefix}{lang} # specified by the CodeHilite's options self.lang_str = lang_str

def _wrap_code(self, source):
    yield 0, f'<code class="{self.lang_str}">'
    yield from source
    yield 0, '</code>'

some_text = '''
:::python print('hellow world') '''

markdown.markdown( some_text, extensions=['markdown.extensions.codehilite'], extension_configs={'markdown.extensions.codehilite': { 'pygments_formatter': CustomHtmlFormatter }} )

The formatter above will output the following HTML structure for the code block:

        
        ...