cybo42

Turning caffeine into code...

Making Octopress Groovy

One of the reasons I picked Octopress for my blog was how easy it was to share code snippets and link to code on Github. When I migrated one of my older posts I found that the syntax highlighting was not working for Groovy code samples. After some investigation, I determined it was not going to be to hard to get things working, reaffirming my choice in using it. I created a fork with the changes detailed below.

Latest and Greatest

Octopress uses a gem called pygments.rb to generate the code snippets. This was the root of my issue. Octopress ships with version 0.1.3 of the pygments.rb gem which does not have support for Groovy. I updated the pygments.rb gem to the latest (0.2.4).

That seemed to do the trick.

1
2
3
['Octopress', 'is', 'Groovy', 'Yeah! baby!'].each{
  println it
}

Since I was feeling adventurous I also updated all the other Octopress gems to their latest versions. To keep things organized updated the .rvimrc and created an octopress gemset.

Don’t stop there

I am also a big fan of Gradle, a build tool which uses Groovy as a DSL to define your build files. The file extension on the build files are .gradle which means they do not get highlighted correctly. A mapping was added into plugins/pygments_code.rb to allow Gradle syntax to be treated as Groovy.

1
2
3
4
5
6
7
8
9
10
11
index a5d1b79..1676a3e 100644
--- a/plugins/pygments_code.rb
+++ b/plugins/pygments_code.rb
@@ -7,7 +7,6 @@ FileUtils.mkdir_p(PYGMENTS_CACHE_DIR)

 module HighlightCode
   def highlight(str, lang)
+    lang = 'groovy' if lang == 'gradle'
     lang = 'ruby' if lang == 'ru'
     lang = 'objc' if lang == 'm'
     lang = 'perl' if lang == 'pl'