Syntax Highlighting and Redcloth
I’ve been blogging a lot lately. I dunno what’s gotten into me. I just have a lot to say, I guess.
When I’m blogging, I tend to like using code snippets. Unfortunately, at the moment I lack any sort of syntax highlighting for these snippets. That kinda sucks.
Now, I use Textile, and thus RedCloth, for my post formatting. This works out reasonably well, except that it doesn’t have any support for syntax highlighting.
So that leads me to wonder. Does anyone know of a pre-existing way to integrate syntax highlighting with RedCloth? ‘Cause I could really use that about now.
Unfortunately, preliminary Googlings have suggested that no, there is no such serendipity. As such, I’ve been thinking a little about what such an integration would look like. At the moment, declaring a monospace code segment looks like this:
Blah blah blah This is normal text No one really cares Oh wow this is code How cool is that Woo hoo Aw man back to text Poo
All the information it really needs for syntax highlighting is which syntax it’s in. The easiest thing to do would be assume Ruby and hope it doesn’t screw up everything else too much, but that’s kind of pathetic. We can do better.
What if we just added a simple declaration? We could just somehow say: “this next block of code is Ruby.” Let’s use ”$”, ‘cause it’s like an “S” for “Syntax” and it’s not already being used.
Text text text $ruby code code code Text text text
Actually, really, there’s no need to force that declaration to be right before the code. Most of the time I’m going to use the same syntax for the whole file, so I might as well just declare it at the top. Or before the first code. Whatever.
Techinically, it shouldn’t be that difficult. Just lock up RedCloth’s Smithsonian, and use Syntax or CodeRay to do the parsing.
So, if anyone knows of a pre-existing solution for this issue, let me know. Otherwise, I’ll take it into my own hands. And I’ll be sure to post the result here.
About Me
Feed
Goodbye, Microsoft



I’ve been using Dann Webb’s CodeHighlighter for syntax highlighting and I really like it. It applies classes to the given code unobtrusively, and it’s easy to write new grammars to add support for other languages.
To apply, you just apply a class attribute to the
codeelement named for the language you want to highlight (i.e.class="ruby").When I was investigating what syntax highlighters are out there, the most used ones are :
Basically SyntaxHighlighter is written in javascript so it should not be too hard to just put another include at the top of the page, and use <pre> (or <code>) tags with RedCloth somehow.
GeSHi is PHP, so that may prove a bit more problematic to integrate with a Ruby blog … it does support a whole bunch of languages though.
Ideally, I’d like to use a server-side, and thus Ruby-based, solution. That way the Atom feed can be highlighted (highlit?) and such. Plus it would take almost as much work just to automatically insert classes to the RedCloth output as it would to just run the whole thing through CodeRay. I think I’ll give that a shot…
I don’t know if you found a solution yet but I recently had the same issue and concocted a solution. I wrote it up here on my blog