OPTION 1:
Simpe as this: http://railscasts.com/episodes/207-syntax-highlighting?autoplay=true
When using rails3 change helper to this:
def coderay(text)
text.gsub!(/\<code(?: lang="(.+?)")?\>(.+?)\<\/code\>/m) do
code = CodeRay.scan($2, $1).div(:css => :class)
"<notextile>#{code}</notextile>"
end
return text.html_safe
end
If you use HAML you will want to use ~ sign:
~ raw textilize(coderay(...))
OPTION 2:
CodeRay has built-in support for RedCloth.
- Add required gem files into your Gemfile.
gem "RedCloth", :require => 'redcloth'
gem 'coderay', :require => ['coderay', 'coderay/for_redcloth']
- Render the code string like you would do it with RedCloth (~ instead of = because I using HAML).
~ raw textilize("Some code here @that should@ be formated.")
~ raw textilize("Some code here @[ruby]that should@ be formated.")
- You can also render a file.
# File at /my/file/path/filename.doc
h1. My title
bc[ruby].. # Nekaj za napisat
def self.myfun
puts "asdas"
end
# Inside your view file
~ raw textilize(File.read("/my/file/path/filename.doc"))
I prefere the second option.
You can find more about styling with Textile markup language at http://en.wikipedia.org/wiki/Textile_(markup_language)