Change: Code Highlighting

created on Nov. 23, 2012, 10:35 a.m. by Hevok & updated on Nov. 23, 2012, 10:39 a.m. by Hevok

Pygments is a synthax highlighter. It can be used with reST or markdown.

To highlight code in reST use the sourcecode directive followed by the specified language_ (e.g. python, html, django, reST)::

.. sourecode:: python

def sum(x,y):
    return x + y

Numerous language are supported and can be tested by a demo app_:

.. language: http://pygments.org/languages/ .. _languages: http://pygments.org/languages/ .. demo app: http://pygments.org/demo/

Markdown 2.0+ has an extension system CodeHilite_ that uses Pygements.

.. _CodeHilite: http://freewisdom.org/projects/python-markdown/CodeHilite

A prerequisite is to create a style-sheet that defines color for synthax highlighting::

$ pygmentize -S default -f html -a .codehilite > code.css

To use CodeHilite_ add the following to the template:

.. sourcecode:: django

{% load markup %}
{{ entry.text|markdown:`codehilite` }}

To mark code block within the text, indent the block by 4 spaces, declare the language of the block in the first line prefixed by three subsequent colons:

.. sourcecode:: python

:::python
print("Code highlighting")

In order to be sure that untrusted embedded HTML elements are not enabled inlcude the following instead of the above::

.. sourcecode:: django

{% load markup %}
{{ entry.text|markdown:'safe,codehilite' }}
Syntax-Highlighter.jpg

Tags: rest, django
Categories: Tutorial
Parent: Markup

Comment: Corrected type.

See entry | Admin

Comment on This Data Unit