Posted on Feb. 21, 2008 at 7:12 P.M.

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!

at 5:09 a.m.
on Feb. 22, 2008

Hi Eric,

Thanks for sharing your tips. I've been building my own Django-powered blog but I'm new to publishing concepts. Your posts have been timely in helping me connect the dots and saving me a few minutes in searching.

-Brian

Pete
at 12:02 p.m.
on Feb. 23, 2008

How does restructuredtext compare to markdown? I've had a quick look at them both, and it seems that markdown has a more pleasant link syntax but rst looks more extendable (directives are a little more focussed and less messy in syntax than markdown extensions are proving to be).

Trouble is, I can't find any really good documentation on rst (ironic). The stuff on the docutils page is fairly awful -- can't even seem to find out how to make a linked image, or a link with a title attribute!

at 2:45 p.m.
on Feb. 23, 2008

I agree that the stuff on the docutils site could use some work. I pretty much agree with your assessment that rst is more extendable but markdown's syntax is (by default) a bit better.

The image directive is here: http://docutils.sourceforge.net/docs/ref/rst/directives.html#image

Here's a good reference to the syntax: http://docutils.sourceforge.net/docs/user/rst/quickref.html

Pete
at 7:28 p.m.
on Feb. 23, 2008

Yeah, I see how to include an image and also how to make a link... but how are they combined? Like an HTML "img" tag *inside* an "a href" link tag.

In markdown it's down by nesting (might not be supported by the python parser yet, though I think markdown2.py works):

[![alt text](/path/img.jpg "Image Title")](http://url.com/ "Link Title")

Or as (preferred) reference style:

... [![alt text][image]][link] ...

afterwards:

[image]: /path/img.jpg "Image Title"
[link]: http://url.com/ "Link Title"

I can't see a way of doing that in rst -- or of getting the title attributes, either.

at 7:35 p.m.
on Feb. 23, 2008

I have gotten an image link by doing the following:

.. image:: http://media.eflorenzano.com/img/powncesample.png
:target: http://media.eflorenzano.com/static/powncecreate.html

As for title attributes, I'm still not sure about that. Maybe a custom directive is in order?

at 7:36 p.m.
on Feb. 23, 2008

...and sorry for the formatting on that last comment. I think you know what I was going for, though.

Pete
at 8:17 p.m.
on Feb. 23, 2008

Ahhh... thanks! I wasted ages looking for that!

A directive for link titles... something like:

used inline in |some link text|?

.. |some link text| link:: http://foo.com/
:title: title
:rel: whatever

hmmm... could work, actually looks cleaner than the default links to me. I'll look into it.

Pete
at 3:09 p.m.
on Feb. 24, 2008

Meh I take it back -- rst is no good for me. It seems the HTML writer is hardcoded to only output certain attributes that cannot be altered or added to by directives at all. That makes it impossible to add title or rel attributes to links without pretty much rewriting the output classes.

It's also hardcoded to output XHTML instead of HTML, which is annoying. Back to markdown I guess; shame as the syntax was really promising but the implementation is fairly awful :(

Search

 

Recent Links

  • Callcast - Discussion with Jeff Croft
  • Great discussion with Jeff Croft and Kevin Fricovsky, talking about Django, design, web standards, and various other things. Kevin has really been on fire lately in his blog, and Jeff has some good stuff to say. Both their sites are bookmarks, for sure.

  • git-issues: A distributed issue tracker built-in to Git.
  • I predicted this back in March--can't believe a solution has surfaced so soon. It makes so much sense to build in an issue tracker to a revision control system. Since you're working with the code, might as well track the issues in the same system and take advantage of the extra metadata. This is really cool (and as a bonus, it's written in Python) so I hope to see it really grow and flourish!

  • How I Work Daily
  • Daily blog by Kevin Fricovsky. In addition to having some really great content, he has started to post audio interviews with people from the Django community. This is a site to keep an eye on in the coming days and months.

  • django-arcade
  • Demo site for django-arcade, an open source reusable Django app to add new flash games to any django-powered site. Looks very cool for easily creating game portals. It also comes from my future employer.

  • Facebook Chat and Scalability (with Erlang)
  • Eugene Letuchy talks about how they they took Facebook Chat from no users to 70 million users, with the help of Erlang.

  • Simon Willison: The Implications of OpenID
  • I somehow missed this presentation when it came out, but it's an absolutely fantastic overview and defense of OpenID by Simon Willison. If you are in any way interested in what OpenID is and what it can offer, you owe it to yourself to check out this presentation.

  • See the rest of my links...

Pownce

Badges

  • django badge
  • apache badge
  • GeoURL
  • XFN Friendly
  • Valid HTML 4.01 Transitional