MCPcopy Create free account
hub / github.com/Python-Markdown/markdown / CodeHilite

Class CodeHilite

markdown/extensions/codehilite.py:59–248  ·  view source on GitHub ↗

Determine language of source code, and pass it on to the Pygments highlighter. Usage: ```python code = CodeHilite(src=some_code, lang='python') html = code.hilite() ``` Arguments: src: Source string or any object with a `.readline` attribute. Keyword argu

Source from the content-addressed store, hash-verified

57
58# ------------------ The Main CodeHilite Class ----------------------
59class CodeHilite:
60 """
61 Determine language of source code, and pass it on to the Pygments highlighter.
62
63 Usage:
64
65 ```python
66 code = CodeHilite(src=some_code, lang='python')
67 html = code.hilite()
68 ```
69
70 Arguments:
71 src: Source string or any object with a `.readline` attribute.
72
73 Keyword arguments:
74 lang (str): String name of Pygments lexer to use for highlighting. Default: `None`.
75 guess_lang (bool): Auto-detect which lexer to use.
76 Ignored if `lang` is set to a valid value. Default: `True`.
77 use_pygments (bool): Pass code to Pygments for code highlighting. If `False`, the code is
78 instead wrapped for highlighting by a JavaScript library. Default: `True`.
79 pygments_formatter (str): The name of a Pygments formatter or a formatter class used for
80 highlighting the code blocks. Default: `html`.
81 linenums (bool): An alias to Pygments `linenos` formatter option. Default: `None`.
82 css_class (str): An alias to Pygments `cssclass` formatter option. Default: 'codehilite'.
83 lang_prefix (str): Prefix prepended to the language. Default: "language-".
84
85 Other Options:
86
87 Any other options are accepted and passed on to the lexer and formatter. Therefore,
88 valid options include any options which are accepted by the `html` formatter or
89 whichever lexer the code's language uses. Note that most lexers do not have any
90 options. However, a few have very useful options, such as PHP's `startinline` option.
91 Any invalid options are ignored without error.
92
93 * **Formatter options**: <https://pygments.org/docs/formatters/#HtmlFormatter>
94 * **Lexer Options**: <https://pygments.org/docs/lexers/>
95
96 Additionally, when Pygments is enabled, the code&#x27;s language is passed to the
97 formatter as an extra option `lang_str`, whose value being `{lang_prefix}{lang}`.
98 This option has no effect to the Pygments&#x27; builtin formatters.
99
100 Advanced Usage:
101
102 ```python
103 code = CodeHilite(
104 src = some_code,
105 lang = 'php',
106 startinline = True, # Lexer option. Snippet does not start with `<?php`.
107 linenostart = 42, # Formatter option. Snippet starts on line 42.
108 hl_lines = [45, 49, 50], # Formatter option. Highlight lines 45, 49, and 50.
109 linenos = 'inline' # Formatter option. Avoid alignment problems.
110 )
111 html = code.hilite()
112 ```
113
114 """
115
116 def __init__(self, src: str, **options):

Callers 3

assertOutputEqualsMethod · 0.90
runMethod · 0.85
runMethod · 0.85

Calls

no outgoing calls

Tested by 1

assertOutputEqualsMethod · 0.72