Over the past week, I've had several people write me asking how I prefer to do syntax highlighting. It's funny that this question cropped up now, just as I changed the way that it's handled on this blog. The way that I used to do it was what I posted to djangosnippets almost a year ago: use a regular expression to parse out <code></code> blocks, highlight the stuff in-between, and spit it back out.
The problem with that method was that that would require some more sophisticated logic now that I'm using RestructuredText to write all of my posts. Unwilling to think any harder than necessary, I did a quick google search, and the second result was exactly what I was looking for: a RestructuredText directive, ready-made by the Pygments people.
The trick is to put this file somewhere on your python path. Then, in the __init__.py of one of the Django apps that will use syntax highlighting, just import the file. It's that simple! (I love RestructuredText.) But it's not only RestructuredText that benefits from this style of plugin. Markdown, too, has a similar plugin--again provided by the Pygments people.
.. sourcecode:: python
print "This is an example of how to use RestructuredText's new directive."
I hope that this answers some of the questions that people had. On a similar note, I'm extremely happy to see that people have been finding the Contact Me link on the right side of the page. Please continue to send me any questions and comments that you have for me!
In creating an any website with textual content, you have the choice of either writing plaintext or writing in a markup language of some kind. The immediately obvious choice for markup language is HTML (or XHTML), but HTML is not as human-readable as something like Textile, Markdown, or Restructured Text. The advantage of choosing one of those human-readable alternatives is that content encoded using one of them can be translated very easily into HTML.
When one of my friends started designing his blog using Django, it got me thinking about how best to deal with that translated HTML. It seems like a waste to keep re-translating it every time a visitor views the page, but it also seems like it's redundant to keep the translated HTML stored in the database.
Here's my solution to the problem: cache it. For a month. Here's an example, using Restructured Text:
from django.db import models
from django.contrib.markup.templatetags.markup import restructuredtext
from django.core.cache import cache
from django.utils.safestring import mark_safe
class MyContent(models.Model):
content = models.TextField()
def _get_content_html(self):
key = 'mycontent_html_%s' % str(self.pk)
html = cache.get(key)
if not html:
html = restructuredtext(self.content)
cache.set(key, html, 60*60*24*30)
return mark_safe(html)
content_html = property(_get_content_html)
def save(self):
if self.id:
cache.delete('mycontent_html_%s' % str(self.pk))
super(MyContent, self).save()
What I'm doing here is writing a method which either gets the translated HTML from the cache, or translates it and stores it in the cache for a month. Then, it returns it as safe HTML to display in a template. The last thing that we do is override the save method on the model, so that whenever the model is re-saved, the cache is deleted.
There we go! We now have the HTML-rendered data that we want, and no duplicated data in the database. Keep in mind that this way of doing things becomes more and more useful the more RAM that your webserver has.
All Content

