Syntax coloring have been added on this blog. I was jealous that Steve had syntax coloring on his website but not me (but hey, I still have a theme, when do your css naked day end?).
I was already using a markup language (PHP Markdown) to write post, as it is easier to write technical documentation using markup than HTML or an HTML WYSIWYG editor. So, I’ve downloaded GeSHi, which is a Generic Syntax Highlighter. I have then patched my markdown.php file to add the parsing of code block by GeSHi. Now, when I create a code block, I just add a first line which is the language of the code between square brackets (i.e. [php])
Here’s the patch to markdown.php v1.0.1m, note that the require_once() line needs to be adapted to point to the location of your geshi folder.
--- markdown.php 2008-06-21 08:58:10.000000000 -0400 +++ markdown-geshi.php 2009-04-26 11:45:39.000000000 -0400 @@ -1083,12 +1083,21 @@ $codeblock = $matches[1]; $codeblock = $this->outdent($codeblock); - $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES); # trim leading newlines and trailing newlines $codeblock = preg_replace('/\A\n+|\n+\z/', '', $codeblock); + // if there's a language tag, use GeSHi to convert the code + if (preg_match('/^\[(\w+)\]\s*/', $codeblock, $match)) { + require_once(realpath(dirname(__FILE__).'/../geshi/geshi.php')); + $codeblock = str_replace("[${match[1]}]\n", '', $codeblock); + $geshi = new GeSHi($codeblock, $match[1]); + $codeblock = $geshi->parse_code(); + } else { + $codeblock = htmlspecialchars($codeblock, ENT_NOQUOTES); $codeblock = "<pre><code>$codeblock\n</code></pre>"; + } + return "\n\n".$this->hashBlock($codeblock)."\n\n"; }
Advocating Markdown
Markdown is a really nice format to work with, as the resulting file can be read even if you don’t have a converter. Quoting the Markdown website:
The overriding design goal for Markdown’s formatting syntax is to make it as readable as possible. The idea is that a Markdown-formatted document should be publishable as-is, as plain text, without looking like it’s been marked up with tags or formatting instructions.
For those lucky enough to work on Mac, support for Markdown in TextMate is realy great, the bundle can convert to LaTeX, HTML, RTF, and PDF.
If you’re more the command line type, the original implementation of Markdown is in Perl and is really easy to use too.
And if you want to convert Markdown to almost anything from the command line, Pandoc is wonderful and is available from MacPorts, with capabilities to convert to reStructuredText, HTML, LaTeX, ConTeXt, PDF, RTF, DocBook XML, OpenDocument XML, ODT, GNU Texinfo, MediaWiki markup, groff man pages, and S5 HTML slide shows.
