1
title: CodeHilite Extension
2
prev_title: Admonition Extension
3
prev_url: admonition.html
4
next_title: HeaderId Extension
5
next_url: header_id.html
13
The CodeHilite extension adds code/syntax highlighting to standard
14
Python-Markdown code blocks using [Pygments][].
16
[Pygments]: http://pygments.org/
18
This extension is included in the standard Markdown library.
23
You will also need to [download][dl] and install the Pygments package on your
24
`PYTHONPATH`. You will need to determine the appropriate CSS classes and create
25
appropriate rules for them, which are either defined in or linked from the
26
header of your HTML templates. See the excellent [documentation][] for more
27
details. If no language is defined, Pygments will attempt to guess the
28
language. When that fails, the code block will not be highlighted.
30
[dl]: http://pygments.org/download/
31
[documentation]: http://pygments.org/docs
34
The CSS and/or JavaScript is not included as part of this extension
35
but must be provided by the end user. The Pygments project provides
36
default CSS styles which you may find to be a useful starting point.
41
The CodeHilite extension follows the same [syntax][] as regular Markdown code
42
blocks, with one exception. The highlighter needs to know what language to use for
43
the code block. There are three ways to tell the highlighter what language the
44
code block contains and each one has a different result.
47
The format of the language identifier only effects the display of line numbers
48
if `linenums` is set to `None` (the default). If set to `True` or `False`
49
(see [Usage](#usage) below) the format of the identifier has no effect on the
50
display of line numbers -- it only serves as a means to define the language
53
[syntax]: http://daringfireball.net/projects/markdown/syntax#precode
55
### Shebang (with path) ###
57
If the first line of the code block contains a shebang, the language is derived
58
from that and line numbers are used.
68
### Shebang (no path) ###
70
If the first line contains a shebang, but the shebang line does not contain a
71
path (a single `/` or even a space), then that line is removed from the code
72
block before processing. Line numbers are used.
83
If the first line begins with three or more colons, the text following the
84
colons identifies the language. The first line is removed from the code block
85
before processing and line numbers are not used.
94
Certain lines can be selected for emphasis with the colon syntax. When
95
using Pygments' default CSS styles, emphasized lines have a yellow background.
96
This is useful to direct the reader's attention to specific lines.
98
:::python hl_lines="1 3"
99
# This line is emphasized
101
# This line is emphasized
104
`hl_lines` is named for Pygments' option meaning "highlighted lines".
106
### When No Language is Defined ###
108
CodeHilite is completely backwards compatible so that if a code block is
109
encountered that does not define a language, the block is simply wrapped in
110
`<pre>` tags and output.
118
Lets see the source for that:
120
<div class="codehilite"><pre><code># Code goes here ...
124
When no language is defined, the Pygments highlighting engine will try to guess
125
the language (unless `guess_lang` is set to `False`). Upon failure, the same
126
behavior will happen as described above.
131
See [Extensions](index.html) for general extension usage, specify `markdown.extensions.codehilite`
132
as the name of the extension.
134
See the [Library Reference](../reference.html#extensions) for information about
135
configuring extensions.
137
The following options are provided to configure the output:
140
Use line numbers. Possible values are `True` for yes, `False` for no and
141
`None` for auto. Defaults to `None`.
143
Using `True` will force every code block to have line numbers, even when
144
using colons (`:::`) for language identification.
146
Using `False` will turn off all line numbers, even when using shebangs
147
(`#!`) for language identification.
150
Automatic language detection. Defaults to `True`.
152
Using `False` will prevent Pygments from guessing the language, and thus
153
highlighting blocks only when you explicitly set the language.
156
Set CSS class name for the wrapper `<div>` tag. Defaults to
159
* **`pygments_style`**:
160
Pygments HTML Formatter Style (`ColorScheme`). Defaults to `default`.
163
This is useful only when `noclasses` is set to `True`, otherwise the
164
CSS styles must be provided by the end user.
167
Use inline styles instead of CSS classes. Defaults to `False`.
169
* **`use_pygments`**:
170
Defaults to `True`. Set to `False` to disable the use of Pygments.
171
If a language is defined for a code block, it will be assigned to the
172
`<code>` tag as a class in the manner suggested by the [HTML5 spec][spec]
173
(alternate output will not be entertained) and might be used by a JavaScript
174
library in the browser to highlight the code block.
176
[spec]: http://www.w3.org/TR/html5/text-level-semantics.html#the-code-element