title:      CodeHilite Extension
prev_title: Admonition Extension
prev_url:   admonition.html
next_title: HeaderId Extension
next_url:   header_id.html

CodeHilite
==========

Summary
-------

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

[Pygments]: http://pygments.org/

This extension is included in the standard Markdown library.

Setup
-----

You will also need to [download][dl] and install the Pygments package on your
`PYTHONPATH`. You will need to determine the appropriate CSS classes and create
appropriate rules for them, which are either defined in or linked from the
header of your HTML templates. See the 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.

[dl]: http://pygments.org/download/
[documentation]: http://pygments.org/docs

!!! Note
    The CSS and/or JavaScript is not included as part of this extension
    but must be provided by the end user. The Pygments project provides
    default CSS styles which you may find to be a useful starting point.

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](#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.

[syntax]: http://daringfireball.net/projects/markdown/syntax#precode

### 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

!!! 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](index.html) for general extension usage, specify `markdown.extensions.codehilite`
as the name of the extension.

See the [Library Reference](../reference.html#extensions) for information about
configuring extensions.

The following options are provided to configure the output:

* **`linenums`**:
    Use line numbers. Possible values are `True` for yes, `False` for no and
    `None` for auto. Defaults to `None`.

    Using `True` will force every code block to have line numbers, even when
    using colons (`:::`) for language identification.

    Using `False` will turn off all line numbers, even when using shebangs
    (`#!`) for language identification.

* **`guess_lang`**:
    Automatic language detection. Defaults to `True`.

    Using `False` will prevent Pygments from guessing the language, and thus
    highlighting blocks only when you explicitly set the language.

* **`css_class`**:
    Set CSS class name for the wrapper `<div>` tag. Defaults to
    `codehilite`.

* **`pygments_style`**:
    Pygments HTML Formatter Style (`ColorScheme`). Defaults to `default`.

    !!! Note
        This is useful only when `noclasses` is set to `True`, otherwise the
        CSS styles must be provided by the end user.

* **`noclasses`**:
    Use inline styles instead of CSS classes. Defaults to `False`.

* **`use_pygments`**:
    Defaults to `True`. Set to `False` to disable the use of Pygments.
    If a language is defined for a code block, it will be assigned to the
    `<code>` tag as a class in the manner suggested by the [HTML5 spec][spec]
    (alternate output will not be entertained) and might be used by a JavaScript
    library in the browser to highlight the code block.

[spec]: http://www.w3.org/TR/html5/text-level-semantics.html#the-code-element
