Update 03/09 16:23: *I just discovered CodeHilite which uses Pygments to markup the code for highlighting. Removing my solution’s JavaScript dependency is a nice win. *

I’ve been working on a documentation tool as a side-project and one of the aims is to use Markdown as the source format. It’s nice when code snippets are syntax highlighted in documentation so I am working on a Python Markdown extension to enable that, without introducing too much of a burden on the document author.

The author “tags” the pre block in markdown with @prettyprint and the extension then adds the necessary class attribute to the <pre> so google-code-prettify can then syntax highlight the content.

For example, take the following markdown:

@prettyprint py
    class PrettyPreExtension (markdown.Extension):
    
        def extendMarkdown(self, md, md_globals):
            md.registerExtension(self)
            self.processor = PreTreeprocessor()
            self.processor.md = md
            self.processor.config = self.getConfigs()</pre>

Markdown with the prettypre extension enabled produces the following HTML which can be syntax highlighted by google-code-prettify ( \ line breaks manually inserted ):

<pre class="prettyprint"><code class="language-py">
class PrettyPreExtension (markdown.Extension):
 
    def extendMarkdown(self, md, md_globals):
        md.registerExtension(self)
        self.processor = PreTreeprocessor()
        self.processor.md = md
        self.processor.config = self.getConfigs()
</code></pre>

The snippets above are included in this [Gist][5].

[5]: https://gist.github.com/jkeyes/5123992