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