• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1CodeHilite
2==========
3
4Summary
5-------
6
7The CodeHilite Extension adds code/syntax highlighting to standard
8Python-Markdown code blocks using [Pygments][].
9
10[Python-Markdown]: http://www.freewisdom.org/projects/python-markdown/
11[Pygments]: http://pygments.org/
12
13This extension is included in the  Markdown library.
14
15Setup
16-----
17
18You will also need to [download][dl] and install the Pygments package on your
19`PYTHONPATH`. You will need to determine the appropriate CSS classes and create
20appropriate rules for them, which are either defined in or linked from the
21header of your HTML templates. See the excellent [documentation][] for more
22details. If no language is defined, Pygments will attempt to guess the
23language. When that fails, the code block will display as un-highlighted code.
24
25[dl]: http://pygments.org/download/
26[documentation]: http://pygments.org/docs
27
28**Note:** The css and/or javascript is not included as part of this extension
29but shall always be provided by the end user.
30
31Syntax
32------
33
34The CodeHilite Extension follows the same [syntax][] as regular Markdown code
35blocks, with one exception. The hiliter needs to know what language to use for
36the code block. There are three ways to tell the hiliter what language the code
37block contains and each one has a different result.
38
39[syntax]: http://daringfireball.net/projects/markdown/syntax#precode
40
41###SheBang (with path)
42
43If the first line of the codeblock contains a shebang, the language is derived
44from that and line numbers are used.
45
46        #!/usr/bin/python
47        # Code goes here ...
48
49Will result in:
50
51    #!/usr/bin/python
52    # Code goes here ...
53
54
55###SheBang (no path)
56
57If the first line contains a shebang, but the shebang line does not contain a
58path (a single `/` or even a space), then that line is removed from the code
59block before processing. Line numbers are used.
60
61        #!python
62        # Code goes here ...
63
64Will result in:
65
66    # Code goes here ...
67
68####Colons
69
70If the first line begins with three or more colons, the text following the
71colons identifies the language. The first line is removed from the code block
72before processing and line numbers are not used.
73
74        :::python
75        # Code goes here ...
76
77Will result in:
78
79    # Code goes here ...
80
81###When No Language is Defined
82
83CodeHilite is completely backward compatible so that if a code block is
84encountered that does not define a language, the block is simple wrapped in
85`<pre>` tags and output. Note: one exception would be that the Pygments
86highlighting engine will try to guess the language. Upon failure, the same
87behavior will happen as described here.
88
89        # Code goes here ...
90
91Will result in:
92
93    # Code goes here ...
94
95Lets see the source for that:
96
97    <div class="codehilite" ><pre><code># Code goes here ...
98    </code></pre></div>
99
100Usage
101-----
102
103From the Python interpreter:
104
105    >>> html = markdown.markdown(text, ['codehilite'])
106
107If you want every code block to have line numbers, even when using colons
108(`:::`) for language identification, the setting `force_linenos` is available
109to do so.
110
111    >>> html = markdown.markdown(text,
112    ...     ['codehilite(force_linenos=True)']
113    ... )
114