new blog, RedCloth and CodeRay

I’ve decided to go back to writing my own blog software. Simply Mephisto is too much for my needs and playing around with building my own Mephisto plugin just to create a plain old boring static page is just too time consuming. In fact I was able to knock up a new blog in less time.

My main issue with writing my own blog the last time was getting RedCloth to play nicely with CodeRay. Fortunately I stumbled upon the solution on GitHub.

http://github.com/leethal/redcloth-with-coderay/tree/master

It wasn’t quite what I wanted but gave me a good start and a few modifications later we have.

“./config/initializers/redcloth_with_coderay.rb”

module RedclothWithCoderay
  
  SOURCE_TAG_REGEXP = /(<pre><code>(.+?)<\/source>)/m

  # The RedCloth extension that performs the syntax highlighting.
  def refs_syntax_highlighter(text)
    text.gsub!(SOURCE_TAG_REGEXP) do |m|
  
      all_of_it = $~[0]
      lang = ($~[2] || :ruby).to_sym
      code = $~[3].strip

      highlighted = CodeRay.scan(code, lang).html(:wrap => :div, :css => :class, :line_numbers => :inline)

      "<notextile>#{highlighted}</notextile>"
    end
  end

end

RedCloth.class_eval { include RedclothWithCoderay }

“./app/helpers/application_helper.rb”

module ApplicationHelper
  def textilize(text)
    RedCloth.new(text).to_html(:textile, :refs_syntax_highlighter)
  end
end

Typically after getting this to work adding text after any code block was susequently hampered by a bug in RedCloth-4.1.9 which had me stumped for a whole day. I have reverted to RedCloth-4.1.1 in the meantime and its all lovely again.

http://jgarber.lighthouseapp.com/projects/13054/tickets/119-notextile-blocks-included-in-following-paragrap

So woo, it works and there be joy abound. Now to import all my old blog entries and fiddle with the code highlighting to make them work super great.