COREBlog and pygments
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).
[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):
(You will see more imports there, put that line wherever you think is a good place for it)
.. 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!