COREBlog and pygments
January 2018
Sun Mon Tue Wed Thu Fri Sat
  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      
About
This site is an effort to share some of the base knowledge I have gathered through all this years working with Linux, FreeBSD, OpenBSD, Python or Zope, among others. So, take a look around and I hope you will find the contents useful.
Recent Entries
Recent Comments
Recent Trackbacks
Categories
OpenBSD (9 items)
BSD (0 items)
FreeBSD (19 items)
Linux (3 items)
Security (3 items)
Python (22 items)
Zope (13 items)
Daily (144 items)
e-shell (9 items)
Hacks (14 items)
PostgreSQL (3 items)
OSX (8 items)
Nintendo DS (0 items)
enlightenment (0 items)
Apache (3 items)
Nintendo Wii (1 items)
Django (24 items)
Music (12 items)
Plone (7 items)
Varnish (0 items)
Lugo (2 items)
Sendmail (0 items)
europython (7 items)
Cherokee (1 items)
self (1 items)
Nature (1 items)
Hiking (0 items)
uwsgi (0 items)
nginx (0 items)
cycling (9 items)
Networking (1 items)
DNS (0 items)
Archives

Syndicate this site (XML)

RSS/RDF 0.91

12 junio
2008

COREBlog and pygments

or how to get your source code blogged and colorized!

This is a little recipe for all of you, COREBlog users out there.

In this very site, I've posted some interesting things about (for example) Django, or some shell scripting examples. As I'm using reStructuredText as my markup language, I always marked source code snippets with the usual :: (which in rst will be translated into <pre></pre> in HTML format). This ends in the sourcecode appearing as any other pre/code/similar text, which isn't as cool as seeing the code as in my favourite development editor.

In Emacs, I use a special mode for each language I usually work with, so the code gets (among other things) colorized. Wouldn't it be cool to have my code colorized online too?

After searching a little bit, I found Pygments:

a generic syntax highlighter for general use in all kinds of software such as forum systems, wikis or other applications that need to prettify source code.

Of course, it is Python code, so I thought that it should be pretty easy to integrate it with some other Python-based apps like COREBlog (in fact I've created a little Zope Product to create paste sites on top of Pygments, but that's another story).

To integrate Pygments and COREBlog all you need is to get the file rst-directive.py from Pygments and save it inside your COREBlog Product folder:

[Frey] /usr/local/www/Zope210/e-shell/Products/COREBlog> ls -l rst_directive.py
-rw-r--r--  1 wu  wheel  2490 Nov 21  2007 rst_directive.py
[Frey] /usr/local/www/Zope210/e-shell/Products/COREBlog>

(I've renamed the file to rst_directive.py to avoid problems importing the file later)

Then, all you have to do is import this module into COREBlog. To do that, add the following line on top of the COREBlog.py file inside your Product folder (where you put the rst-directive file):

import rst_directive

(You will see more imports there, put that line wherever you think is a good place for it)

Now restart your Zope instance and you will be able to use the sourcecode directive in rst, someway similar to:

.. sourcecode:: python

  import rst_directive

(which I've used some lines above)

That will generate all the necessary HTML code to highlight properly the source code snippet.

Finally, you will have to add some CSS styles to your site stylesheet. You can get all the necessary stuff to put into your css files using the get_style_defs() method from Pygments:

>>> from pygments.formatters import HtmlFormatter
>>> print HtmlFormatter().get_style_defs()

Just copy the CSS style definitions you will see on-screen to your COREBlog skin CSS (probably located, inside the ZMI in yoursite -> contents tab -> skins -> yourskin -> style_css dtml method) and you are done!

Note: of course you can colorize much more source code than only Python, just take a look at Pygments Lexers to know how to call the proper lexer for your programming language.

Posted by wu at 19:49 | Comments (0) | Trackbacks (0)
<< nvidia > ati | Main | Problem with ssl in Python 2.5 >>
Comments
Re: COREBlog and pygments

Very nice recipe! I think I'll put that into my blog too!

Posted by: betabug at junio 13,2008 10:22
Re: COREBlog and pygments

Thnx!

In my oppinion, colorized code is nicer to look at (and sometimes the colours help understanding it).

Of course it could be improved, adding some code to generate on-the-fly the highlighting CSS tags, or separating them into an additional CSS "file" (for example).

Posted by: Wu at junio 13,2008 11:18
Trackbacks
Please send trackback to:http://blog.e-shell.org/71/tbping
There are no trackbacks.
Post a comment