RailsCasts Pro episodes are now free!
Learn more or hide this
NOTE: This episode is out of date with the release of Redcarpet 2. Please see Episode 207: Syntax Highlighting (revised) for a better way to do what is shown here.
Resources
sudo easy_install pygments
gem 'redcarpet' gem 'albino' gem 'nokogiri'
def markdown(text) options = [:hard_wrap, :filter_html, :autolink, :no_intraemphasis, :fenced_code, :gh_blockcode] syntax_highlighter(Redcarpet.new(text, *options).to_html).html_safe end def syntax_highlighter(html) doc = Nokogiri::HTML(html) doc.search("//pre[@lang]").each do |pre| pre.replace Albino.colorize(pre.text.rstrip, pre[:lang]) end doc.to_s end
<%= markdown(@article.content) %>