codehilite — Python-Markdown 3.10.2 documentation
Determine language of source code, and pass it on to the Pygments highlighter.
Usage:
code = CodeHilite(src=some_code, lang='python')
html = code.hilite()
Parameters:
-
src(str) –Source string or any object with a
.readlineattribute.
Other Parameters:
-
lang(str) –String name of Pygments lexer to use for highlighting. Default:
None. -
guess_lang(bool) –Auto-detect which lexer to use. Ignored if
langis set to a valid value. Default:True. -
use_pygments(bool) –Pass code to Pygments for code highlighting. If
False, the code is instead wrapped for highlighting by a JavaScript library. Default:True. -
pygments_formatter(str) –The name of a Pygments formatter or a formatter class used for highlighting the code blocks. Default:
html. -
linenums(bool) –An alias to Pygments
linenosformatter option. Default:None. -
css_class(str) –An alias to Pygments
cssclassformatter option. Default: ‘codehilite’. -
lang_prefix(str) –Prefix prepended to the language. Default: “language-“.
Other Options:
Any other options are accepted and passed on to the lexer and formatter. Therefore,
valid options include any options which are accepted by the html formatter or
whichever lexer the code’s language uses. Note that most lexers do not have any
options. However, a few have very useful options, such as PHP’s startinline option.
Any invalid options are ignored without error.
- Formatter options: https://pygments.org/docs/formatters/#HtmlFormatter
- Lexer Options: https://pygments.org/docs/lexers/
Additionally, when Pygments is enabled, the code’s language is passed to the
formatter as an extra option lang_str, whose value being {lang_prefix}{lang}.
This option has no effect to the Pygments’ builtin formatters.
Advanced Usage:
code = CodeHilite(
src = some_code,
lang = 'php',
startinline = True, # Lexer option. Snippet does not start with `<?php`.
linenostart = 42, # Formatter option. Snippet starts on line 42.
hl_lines = [45, 49, 50], # Formatter option. Highlight lines 45, 49, and 50.
linenos = 'inline' # Formatter option. Avoid alignment problems.
)
html = code.hilite()
Methods:
‹›
markdown.extensions.codehilite.CodeHilite.hilite(shebang: bool = True) -> str
¶
Pass code to the Pygments highlighter with
optional line numbers. The output should then be styled with CSS to
your liking. No styles are applied by default - only styling hooks
(i.e.: <span class="k">).
returns : A string of html.