summaryrefslogtreecommitdiff
path: root/docs/extensions/code_hilite.txt
blob: 57f81ca6a29a85b92bc288ac3c5104382fa5ba36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
title:      CodeHilite Extension
prev_title: Admonition Extension
prev_url:   admonition.html
next_title: HTML Tidy Extension
next_url:   html_tidy.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  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 display as un-highlighted code.

[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 shall always be provided by the end user.

Syntax
------

The CodeHilite Extension follows the same [syntax][] as regular Markdown code
blocks, with one exception. The hiliter needs to know what language to use for
the code block. There are three ways to tell the hiliter 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 codeblock 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 ...

###When No Language is Defined

CodeHilite is completely backward 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
-----

From the Python interpreter:

    >>> html = markdown.markdown(text, extensions=['codehilite'])

If you want to force every code block to have line numbers, even when using 
colons (`:::`) for language identification, set `linenums` to `True`.

    >>> html = markdown.markdown(text,
    ...     extensions=['codehilite(linenums=True)']
    ... )

If you do **not** want any code block to have line numbers, even when using 
SheBangs (`#!`) for language identification, set `linenums` to `False`.

    >>> html = markdown.markdown(text,
    ...     extensions=['codehilite(linenums=False)']
    ... )

If you want to prevent Pygments from guessing the language, only highlighting
blocks when you explicitly request it, set the `guess_lang` setting to `False`.

    >>> html = markdown.markdown(text,
    ...     extensions=['codehilite(guess_lang=False)']
    ... )