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

Class CodeHiliteExtension

markdown/extensions/codehilite.py:292–352  ·  view source on GitHub ↗

Add source code highlighting to markdown code blocks.

Source from the content-addressed store, hash-verified

290
291
292class CodeHiliteExtension(Extension):
293 """ Add source code highlighting to markdown code blocks. """
294
295 def __init__(self, **kwargs):
296 # define default configs
297 self.config = {
298 'linenums': [
299 None, "Use lines numbers. True|table|inline=yes, False=no, None=auto. Default: `None`."
300 ],
301 'guess_lang': [
302 True, "Automatic language detection - Default: `True`."
303 ],
304 'css_class': [
305 "codehilite", "Set class name for wrapper <div> - Default: `codehilite`."
306 ],
307 'pygments_style': [
308 'default', 'Pygments HTML Formatter Style (Colorscheme). Default: `default`.'
309 ],
310 'noclasses': [
311 False, 'Use inline styles instead of CSS classes - Default `False`.'
312 ],
313 'use_pygments': [
314 True, 'Highlight code blocks with pygments. Disable if using a JavaScript library. Default: `True`.'
315 ],
316 'lang_prefix': [
317 'language-', 'Prefix prepended to the language when `use_pygments` is false. Default: `language-`.'
318 ],
319 'pygments_formatter': [
320 'html', 'Use a specific formatter for Pygments highlighting. Default: `html`.'
321 ],
322 }
323 """ Default configuration options. """
324
325 for key, value in kwargs.items():
326 if key in self.config:
327 self.setConfig(key, value)
328 else:
329 # manually set unknown keywords.
330 if isinstance(value, str):
331 try:
332 # Attempt to parse `str` as a boolean value
333 value = parseBoolValue(value, preserve_none=True)
334 except ValueError:
335 pass # Assume it's not a boolean value. Use as-is.
336 self.config[key] = [value, '']
337
338 def extendMarkdown(self, md):
339 """
340 Register the processor.
341
342 | Class Instance | Registry | Name | Priority |
343 | --------------------------------------------------------------------------- | ---------------------------------------------------------------- | ------ | :------: |
344 | [`HiliteTreeprocessor`][markdown.extensions.codehilite.HiliteTreeprocessor] | [`treeprocessors`][markdown.treeprocessors.build_treeprocessors] | `hilite` | `30` |
345
346 """
347 # flake8: noqa: E501 341-343
348 hiliter = HiliteTreeprocessor(md)
349 hiliter.config = self.getConfigs()

Callers 15

testLinenumsTrueMethod · 0.90
testLinenumsFalseMethod · 0.90
testLinenumsNoneMethod · 0.90
testUsePygmentsFalseMethod · 0.90
testLangPrefixEmptyMethod · 0.90
testLangPrefixMethod · 0.90
testUnknownOptionMethod · 0.90
testFormatterLangStrMethod · 0.90

Calls

no outgoing calls

Tested by 14

testLinenumsTrueMethod · 0.72
testLinenumsFalseMethod · 0.72
testLinenumsNoneMethod · 0.72
testUsePygmentsFalseMethod · 0.72
testLangPrefixEmptyMethod · 0.72
testLangPrefixMethod · 0.72
testUnknownOptionMethod · 0.72
testFormatterLangStrMethod · 0.72