There are more than couple of ways to do syntax highlighting for Ruby code. Your geek blog doesn't look nice if the code cannot be read properly or cannot be copied easily. Gists work well. Most blogs now would probably just use gist.github.com especially if it's lengthly file or there are way to many stuff on it that you don't want to scare your visitors. By naming the file "syntax.rb" on gist.github.com for instance, it would know what code you are using and syntax highlighting will be based on the filename. See syntax.rb .
The following code used to work well for me:
def to_html(markdown)
out = []
noncode = []
code_block = nil
markdown.split("\n").each do |line|
if !code_block and line.strip.downcase == ''
out << Maruku.new(noncode.join("\n")).to_html
noncode = []
code_block = []
elsif code_block and line.strip.downcase == ''
convertor = Syntax::Convertors::HTML.for_syntax "ruby"
highlighted = convertor.convert(code_block.join("\n"))
out << "#{highlighted}"
code_block = nil
elsif code_block
code_block << line
else
noncode << line
end
end
out << Maruku.new(noncode.join("\n")).to_html
out.join("\n")
end
There are at least two problems with this code:
I had to convert everything to HAML recently. Starting to feel an aversion towards using and overusing ERB.
This code works for me:
def to_html(markdown)
markdown = markdown.gsub("", '').gsub("", "
")
markdown = RDiscount.new(markdown)
markdown = markdown.to_html
end
Why the code above is better:
The following video shows the difference:
And the final result can be seen on my blog.