CodeHilite — Python-Markdown 3.8 documentation (original) (raw)

Summary

The CodeHilite extension adds code/syntax highlighting to standard Python-Markdown code blocks using Pygments.

This extension is included in the standard Markdown library.

Setup

Step 1: Download and Install Pygments

You will also need to download and install the Pygments package on yourPYTHONPATH. The CodeHilite extension will produce HTML output without Pygments, but it won’t highlight anything (same behavior as settinguse_pygments to False).

Step 2: Add CSS Classes

You will need to define the appropriate CSS classes with appropriate rules. The CSS rules either need to be defined in or linked from the header of your HTML templates. Pygments can generate CSS rules for you. Just run the following command from the command line:

pygmentize -S default -f html -a .codehilite > styles.css

If you are using a different css_class (default: .codehilite), then set the value of the -a option to that class name. The CSS rules will be written to the styles.css file which you can copy to your site and link from your HTML templates.

If you would like to use a different theme, swap out default for the desired theme. For a list of themes installed on your system (additional themes can be installed via Pygments plugins), run the following command:

pygmentize -L style

See Pygments’ excellent documentation for more details. If no language is defined, Pygments will attempt to guess the language. When that fails, the code block will not be highlighted.

See Also

GitHub user richeland has provided a number of different CSS style sheets which work with Pygments along with a preview of each theme. The css_class used is .highlight. Therefore, one would need to override thecss_class option when using richeland’s CSS styles. However, the Python-Markdown project makes no guarantee that richeland’s CSS styles will work with the version of Pygments you are using. To ensure complete compatibility, you should generate the CSS rules from your own installation of Pygments.

Syntax

The CodeHilite extension follows the same syntax as regular Markdown code blocks, with one exception. The highlighter needs to know what language to use for the code block. There are three ways to tell the highlighter what language the code block contains and each one has a different result.

Note

The format of the language identifier only effects the display of line numbers if linenums is set to None (the default). If set to True or False(see Usage below) the format of the identifier has no effect on the display of line numbers – it only serves as a means to define the language of the code block.

Shebang (with path)

If the first line of the code block contains a shebang, the language is derived from that and line numbers are used.

    #!/usr/bin/python
    # Code goes here ...

Will result in:

#!/usr/bin/python
# Code goes here ...

Shebang (no path)

If the first line contains a shebang, but the shebang line does not contain a path (a single / or even a space), then that line is removed from the code block before processing. Line numbers are used.

    #!python
    # Code goes here ...

Will result in:

# Code goes here ...

Colons

If the first line begins with three or more colons, the text following the colons identifies the language. The first line is removed from the code block before processing and line numbers are not used.

    :::python
    # Code goes here ...

Will result in:

# Code goes here ...

Certain lines can be selected for emphasis with the colon syntax. When using Pygments’ default CSS styles, emphasized lines have a yellow background. This is useful to direct the reader’s attention to specific lines.

    :::python hl_lines="1 3"
    # This line is emphasized
    # This line isn't
    # This line is emphasized

Will result in:

# This line is emphasized
# This line isn't
# This line is emphasized

Note

hl_lines is named for Pygments’ option meaning “highlighted lines”.

When No Language is Defined

CodeHilite is completely backwards compatible so that if a code block is encountered that does not define a language, the block is simply wrapped in<pre> tags and output.

    # Code goes here ...

Will result in:

# Code goes here ...

Lets see the source for that:

<div class="codehilite"><pre><code># Code goes here ...
</code></pre></div>

Note

When no language is defined, the Pygments highlighting engine will try to guess the language (unless guess_lang is set to False). Upon failure, the same behavior will happen as described above.

Usage

See Extensions for general extension usage. Use codehilite as the name of the extension.

See the Library Reference for information about configuring extensions.

The following options are provided to configure the output:

A trivial example:

markdown.markdown(some_text, extensions=['codehilite'])

To keep the code block’s language in the Pygments generated HTML output, one can provide a custom Pygments formatter that takes the lang_str option. For example,

from pygments.formatters import HtmlFormatter
from markdown.extensions.codehilite import CodeHiliteExtension


class CustomHtmlFormatter(HtmlFormatter):
    def __init__(self, lang_str='', **options):
        super().__init__(**options)
        # lang_str has the value {lang_prefix}{lang}
        # specified by the CodeHilite's options
        self.lang_str = lang_str

    def _wrap_code(self, source):
        yield 0, f'<code class="{self.lang_str}">'
        yield from source
        yield 0, '</code>'


some_text = '''\
    :::python
    print('hellow world')
'''

markdown.markdown(
    some_text,
    extensions=[CodeHiliteExtension(pygments_formatter=CustomHtmlFormatter)],
)

The formatter above will output the following HTML structure for a code block:

<div class="codehilite">
    <pre>
        <code class="language-python">
        ...
        </code>
    </pre>
</div>