Question

I want to use syntax highlighting along with redcloth.

The description of coderay says:

Fast and easy syntax highlighting for selected languages, written in Ruby. Comes with RedCloth integration and LOC counter. 1

But I did not find a documentation about how to do this?

Where should I put the code to do the hightlighting? I thought about putting it into module ApplicationHelper is this a good idea?

Was it helpful?

Solution

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.

  1. Add required gem files into your Gemfile.
gem "RedCloth", :require => 'redcloth'
gem 'coderay', :require => ['coderay', 'coderay/for_redcloth']
  1. 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.")
  1. 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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top