Skip to content

Instantly share code, notes, and snippets.

@pat
Created January 5, 2011 11:40
Show Gist options
  • Save pat/766217 to your computer and use it in GitHub Desktop.
Save pat/766217 to your computer and use it in GitHub Desktop.
Quick code highlighting via Pygments - all thanks to the wonderfully simple
pygments.appspot.com web service, made by Trevor Turk. Perfect for writing
documentation hosted on Heroku.
The only catch (if you're using HAML) is you'll need to start a block that
includes indented lines with :plain to stop HAML complaining about non-HAML
syntax. If anyone can figure out a way around this, I'd love to hear it!
With thanks to Chris Lloyd and Jason Weathered for showing me the light on
whitespace-preserved output (use ~ instead of = when invoking code_snippet).
require 'net/http'
require 'uri'
module ApplicationHelper
def code_snippet(language, &block)
code = capture(&block).strip
pygments_response(language, code).gsub(/\n<\/pre>/, '</pre>').html_safe
end
private
def pygments_response(language, code)
Net::HTTP.post_form(
pygments_uri, 'lang' => language, 'code' => code
).body
end
def pygments_uri
@pygments_uri ||= URI.parse('http://pygments.appspot.com/')
end
end
~ code_snippet 'ruby' do
gem 'thinking-sphinx', '2.0.1'
gem 'flying-sphinx', '0.3.1'
~ code_snippet 'ruby' do
:plain
config.gem 'thinking-sphinx',
:lib => 'thinking_sphinx',
:version => '1.4.1'
config.gem 'flying-sphinx',
:version => '0.3.1'
@zzak
Copy link

zzak commented Mar 22, 2013

pygments.appspot.com is shutting down soon, please find an alternative source for syntax highlighting

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment