Created
September 7, 2012 09:58
-
-
Save jaimeiniesta/3664768 to your computer and use it in GitHub Desktop.
How to colorize code in your comments using CodeRay
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
<%= colorize comment.body %> |
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
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 |
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
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 |
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
gem 'coderay' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment