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

Class CodeHiliteExtension

markdown/extensions/codehilite.py:291–351  ·  view source on GitHub ↗

Add source code highlighting to markdown code blocks.

Source from the content-addressed store, hash-verified

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