Code syntax highlighting in my Rails 4.0.4 app using Redcarpet & Pygments.rb?

796 Views Asked by At

The objective

I am trying to render markdown documentation with snippets of code in it, and I'd like to highlight the syntax like on Github or BitBucket.

My Environment

  • Rails 4.0.4
  • Ruby 2.1.1

In my gemfile

gem 'haml-rails'
gem 'redcarpet' # markdown
gem 'rdiscount'
gem 'pygments.rb', '~> 0.5.4'

Initializer

I have tried this, but my snippets of code don't get highlighted:

module Haml::Filters

  remove_filter("Markdown") #remove the existing Markdown filter

  module Markdown

    include Haml::Filters::Base

    class HTMLwithPygments < Redcarpet::Render::HTML
      def block_code(code, language)
        Pygments.highlight(code, lexer: language)
      end
    end


    def render(text)
      #Redcarpet::Markdown.new(Redcarpet::Render::HTML.new(prettify: true), fenced_code_blocks: true).render(text)
      Redcarpet::Markdown.new(HTMLwithPygments, fenced_code_blocks: true).render(text)
    end

  end
end

tested with this simple YAML

 hello:
    world: "hey"

the code generated looks right (?)

<div class="highlight">
   <pre>  
    <span class="l-Scalar-Plain">hello</span><span class="p-Indicator">:</span>
    <span class="l-Scalar-Plain">world</span><span class="p-Indicator">:</span> 
    <span class="s">"hey"</span>
    </pre>
</div>

but it's plain grey, not highlighted

Do I need to install any CSS files for these classes to render properly??

Any suggestion?

1

There are 1 best solutions below

0
On BEST ANSWER

How I fixed it:

bash
$ pygmentize -S default -f html > style.css 

and added this file to my css folder.