Using Pygments on Heroku

Austin Vance

After seeing RailsCast #272 Markdown with Redcarpet I wanted to be able to use markdown with pygments and albino in my blog, but I ran into a small issue, Heroku. The do not have pygments installed. This I thought would be a plan shattering problem but all was saved.

I found a version of Pygments on the google app engine deployed by trevorturk at Github. After finding this I went ahead and modified what the rails cast had told me to do so code formatting would work with a cloud version of Pygments. After installing and running for a few days I realized his version of Pygments was old and didn't have language support for coffeescript or anyother of the fancy new fun languages. So I deployed my own.

located at

http://pygments-1-4.appspot.com/

Here is how you can use Pygments 1.4 with your rails app on heroku with out a hitch.

First do everything the RailsCast tells you. then...

In application_helper.rb add

# Add at the top of the file
require 'net/http'
require 'uri'

# Replace the syntax_highlighter method with...
def syntax_highlighter(html)
  doc = Nokogiri::HTML(html)
  doc.search("//pre[@lang]").each do |pre|
      pre.replace Net::HTTP.post_form(URI.parse('http://pygments-1-4.appspot.com/'),
                                      {'lang'=>pre[:lang], 'code'=>pre.text.strip}).body
  end
  doc.css('body > *').to_s
end

and we have coffeescript syntax highlighted

hooray = -> alert "we have coffeescript"

| Back
blog comments powered by Disqus