class documentation

class CodeHilite: (source)

View In Hierarchy

Determine language of source code, and pass it on to the Pygments highlighter. Usage: code = CodeHilite(src=some_code, lang='python') html = code.hilite() Arguments: * src: Source string or any object with a .readline attribute. * lang: String name of Pygments lexer to use for highlighting. Default: `None`. * guess_lang: Auto-detect which lexer to use. Ignored if `lang` is set to a valid value. Default: `True`. * use_pygments: Pass code to pygments for code highlighting. If `False`, the code is instead wrapped for highlighting by a JavaScript library. Default: `True`. * pygments_formatter: The name of a Pygments formatter or a formatter class used for highlighting the code blocks. Default: `html`. * linenums: An alias to Pygments `linenos` formatter option. Default: `None`. * css_class: An alias to Pygments `cssclass` formatter option. Default: 'codehilite'. * lang_prefix: 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's 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()

Method __init__ Undocumented
Method hilite Pass code to the [Pygments](http://pygments.pocoo.org/) highliter 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...
Instance Variable guess_lang Undocumented
Instance Variable lang Undocumented
Instance Variable lang_prefix Undocumented
Instance Variable options Undocumented
Instance Variable pygments_formatter Undocumented
Instance Variable src Undocumented
Instance Variable use_pygments Undocumented
Method _parseHeader Determines language of a code block from shebang line and whether the said line should be removed or left in place. If the sheband line contains a path (even a single /) then it is assumed to be a real shebang line and left alone...
def __init__(self, src, **options): (source)

Undocumented

def hilite(self, shebang=True): (source)

Pass code to the [Pygments](http://pygments.pocoo.org/) highliter 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.

guess_lang = (source)

Undocumented

lang = (source)

Undocumented

lang_prefix = (source)

Undocumented

options = (source)

Undocumented

pygments_formatter = (source)

Undocumented

Undocumented

use_pygments = (source)

Undocumented

def _parseHeader(self): (source)

Determines language of a code block from shebang line and whether the said line should be removed or left in place. If the sheband line contains a path (even a single /) then it is assumed to be a real shebang line and left alone. However, if no path is given (e.i.: #!python or :::python) then it is assumed to be a mock shebang for language identification of a code fragment and removed from the code block prior to processing for code highlighting. When a mock shebang (e.i: #!python) is found, line numbering is turned on. When colons are found in place of a shebang (e.i.: :::python), line numbering is left in the current state - off by default. Also parses optional list of highlight lines, like: :::python hl_lines="1 3"