MCPcopy Index your code
hub / github.com/Python-Markdown/markdown / CodeHilite

Class CodeHilite

markdown/extensions/codehilite.py:58–247  ·  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

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

Used in the wild real call sites across dependent graphs

searching dependent graphs…