Created
January 5, 2011 11:40
-
-
Save pat/766217 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~ 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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
pygments.appspot.com is shutting down soon, please find an alternative source for syntax highlighting