Saturday, January 2, 2010

Syntax Highlighting and Blogger

I spent an hour or so poking around trying to get code highlighting working in blogger.

My first attempt was to use tohtml, which I've used at work a number of times to get colored code to paste into Outlook when doing code reviews.  It does code highlighting for a TON of languages and generally creates stuff that looks pretty good.  The coloring options aren't too impressive, but they work.



The output HTML is messy, since it's embedding all the styles within the HTML, but it works perfectly for pasting into blogger.  It doesn't require any javascript or anything else to run at page load, which I like.  It does make the page download a big larger, but for now I'm preferring it over the other solutions I found.  Here's a quick example output of some ruby code:

def score(dice)
  count_dice_types(dice).each_pair.inject(0) { |score, pair|
    value, count = pair
    score + score_dice_value(value, count)
  }
end

I did find another solution that worked well, but decided I didn't like the implementation.  It uses a SWF to format the code, which seems incredibly heavyweight to me.  You also have to add code to the blogger layout HTML, which gets lost any time you change your template (granted that isn't often).  But it might be useful to others, so here's the post I found that explains it:

http://blog.cartercole.com/2009/10/awesome-syntax-highlighting-made-easy.html

Here's a simple example (screenshot, not live, as I removed the code from my layout):



After initially writing this post I also tried embedding a gist.  It seemed like it should work, but the layout was really really funky.  I may try it again later, but for now I'm going to skip it and stick with tohtml.



Syntax highlighting with blogger is annoying. tohtml.com is the best solution I've found so far.

No comments:

Post a Comment