1

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?

Mark
  • 7,507
  • 12
  • 52
  • 88

1 Answers1

3

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)

xpepermint
  • 35,055
  • 30
  • 109
  • 163
  • Hey @xpepermint - thanks for the info. I followed option 2 and am getting my code on a single line. Do I have to make any special changes when adding my code into the DB? Cheers! – Stu Mar 06 '12 at 11:14
  • what is slim's equivalent for haml's `~` ? http://www.rubydoc.info/gems/slim/frames – ahnbizcad Oct 26 '14 at 04:18
  • What's the erb equivalent? – ahnbizcad Oct 26 '14 at 04:24
  • what is $2 and $1? Where can we find a table or documentation for it? It's certainly not in the official coderay reference – ahnbizcad Oct 27 '14 at 00:37