Login or Sign up

Django-threadedcomments with restructured text and pygments for syntax-highlighting in pinax

Posted by: skyl on Oct. 17, 2009

We can easily get syntax highlighting in our pinax blog by following the post here,

http://www.codekoala.com/blog/2008/syntax-highlighting-rest-pygments-and-django/

Once we have followed those instructions, we can easily allow syntax highlighting with reST in our threadedcomments. It's as simple as passing the comment through the apply_markup filter in the template. This template, in a default pinax project, is supplied by ..../pinax-env/src/pinax/pinax/templates/default/threadedcomments/comments.html

{% load markup_tags %}
...
{{ response.comment|apply_markup:'restructuredtext' }}

So, we have access to the now 159 different lexers that come with pygments by default.

We can figure out how to call the different lexers by checking their aliases attribute in the shell. For instance:

>>> from pygments.lexers import PythonConsoleLexer
>>> PythonConsoleLexer.aliases
['pycon']

So, to make use of the PythonConsoleLexer in our reST we can simply:

.. code-block:: pycon

    >>> print 'this is python console'

We can print all of the lexers and their aliases with a built-in function of pygments:

>>> from pygments import lexers
>>> l = lexers.get_all_lexers() # l is a generator
>>> for x in l:
...     print x

('Diff', ('diff', 'udiff'), ('*.diff', '*.patch'), ('text/x-diff', 'text/x-patch'))
...

There are a lot more, check it out!

Comments on This Post:

Please Login (or Sign Up) to leave a comment