#PDXtoLHR this is happening #opalworldwide (at Portland International Airport (PDX))

#PDXtoLHR this is happening #opalworldwide (at Portland International Airport (PDX))

Syntax Highlighting for Toto

Toto is great, but with a development oriented blog I needed a clean way to display code to my readers. So first thing on my todo list was to add Code block support with Syntax highlighting to Toto.

to turn this:

def do_something(test) test.each do |t| puts t end end

into this:


def do_something(test)
  test.each do |t|
    puts t
  end
end

After some research I settled on a gem called coderay. You can install it using


gem install coderay

Next I did some Monkeypatching.


require 'coderay'

#open the Toto module
module Toto
  class Article
    #overrided body and summary to use coderay
    def body
      markdown coderay(self[:body].sub(@config[:summary][:delim], '')) rescue markdown self[:body]
    end

    def summary length = nil
      config = @config[:summary]
      sum = if self[:body] =~ config[:delim]
        self[:body].split(config[:delim]).first
      else
        self[:body].match(/(.{1,#{length || config[:length] || config[:max]}}.*?)(\n|\Z)/m).to_s
      end
      markdown(coderay(sum.length == self[:body].length ? sum : sum.strip.sub(/\.\Z/, '…')))     
    end

    #adding a method for coderay
    def coderay(text)
      text.gsub(/\(.+?)\/m) do
        CodeRay.scan($3, $2).div(:css => :class, :line_numbers => :table)
      end
    end
  end
end

After that I added the following to the config.ru to load our file


require './toto-extend.rb'

And then I styled it using my favorite theme Solarized. You can grab the css here.

Still have a few kinks to work out, but this should get you going. If you need any help feel free to give me a shout on Twitter


Edit

Not surprising there is a better way to add functionality to a gem. Instead of Monkeypatching it in, you can Fork the repo on Github. Make your changes, and reference it in your Gemfile like so


gem "toto", :git => "git@github.com:jeffboek/toto.git"