Skip to content

Instantly share code, notes, and snippets.

@jaimeiniesta
Created September 7, 2012 09:58
Show Gist options
  • Save jaimeiniesta/3664768 to your computer and use it in GitHub Desktop.
Save jaimeiniesta/3664768 to your computer and use it in GitHub Desktop.
How to colorize code in your comments using CodeRay
<%= colorize comment.body %>
module CommentsHelper
def coderay(text)
text.gsub(/\<code( lang="(.+?)")?\>(.+?)\<\/code\>/m) do
CodeRay.scan($3, $2.blank? ? :html : $2.blank? ? :html : $2).div
end
end
end
module CommentsHelper
def coderay(text)
text.gsub(/\<code( lang="(.+?)")?\>(.+?)\<\/code\>/m) do
CodeRay.scan($3, $2.blank? ? :html : $2.blank? ? :html : $2).div
end
end
def colorize(text)
text = coderay(text)
text = simple_format(text, {}, :sanitize => false)
text = sanitize(text, {:tags => %w(table thead tbody tr td pre span pr p), :attributes => %w(id class style)})
text = auto_link(text, :sanitize => false)
text.html_safe
end
end
gem 'coderay'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment