Following on in the series of how I have improved my blog, its time to address the issue of syntax highlighting. I admit, this is something which I think has really let me down and something which is long overdue.
There are a number of difference approaches available to solve this problem, however the best approach I came across is from http://alexgorbatchev.com/wiki/SyntaxHighlighter. This is a javascript+css library which can be installed on your blog, which thankfully is a very simply task. The CSS you need to reference are the following:
The javascript I have referenced in shown below.
In my posts, I can then simply wrap the snippet in a pre block and assign the appropriate brush class to highlight the syntax.
public string test() { return "abc"; }
The following are a list of brushes for the different languages I have referenced.
C# – c-sharp
CSS – css
JavaScript – js
Java – java
Ruby – ruby
Python – python
Scala – scala
SQL – sql
Xml – xml
For those who are using Windows Live Writer, there is a plugin to make syntax highlighting even easier! This is available to download from http://www.codeplex.com/precode
Hopefully this should make code much easier to read in the future. Another approach I was tempted by is using GitHub Gists. Gists are snippets of code hosted on github which you can then embed into your posts, as shown below.
The reason I didn’t go down this route is that it makes it difficult to insert code when writing a blog ‘offline’ and when GitHub is down. As a result, I decided to stick with the classic approach for now. But what do you think?