Software Engineer

puppet syntax highlighting with geshi

· by jsnby · Read in about 2 min · (300 Words)
Puppet

It’s no secret that I use WordPress for this blog. One of the reasons I like WordPress is the wide variety of plugins that are available. Since I blog a lot about perl/php/puppet code, I like to have a plugin that does syntax highlighting. For this task, I use the WP-Syntax plugin which is built on top of GeSHi.

Unfortunately, it doesn’t have a language file for puppet. A quick search turned up this language file. I installed it and noticed that it wasn’t picking up a lot of the keywords as resource types or attributes, nor was it recognizing C style multi-line comments (you know the ones that start with /* and end with */).

I was able to fix the comment issue right away. Then, I focused my attention towards automatically pulling the keywords either directly from the puppet source or from the puppet docs in some way programatically. I decided to grab them from the docs since I’m still a couple of versions behind (I’m running 2.6.9 and the latest stable version as of this writing is 2.7.5). I wrote a little perl script to spit out some pre-formatted text that I could just drop in as a replacement for the existing keywords arrays. Since I’ve been messing around with the puppetlabs-firewall module lately, I went ahead and added that resource type and attribute names manually. The script I used to pull and parse the data can be found on GitHub.

To use my language file, grab a copy of puppet.php from GitHub, then put it into your wp-content/plugins/wp-syntax/geshi/geshi folder. Then, use it like you would any other language WP-Syntax supports:

<pre lang="puppet">
exec { "some_exec":
    command => '/bin/echo "hello world" > /tmp/hello_world.txt',
}
</pre>

It’s not perfect, but I hope to continue to improve it over time. Enjoy!