Skip to content

Instantly share code, notes, and snippets.

@tom--
Last active December 27, 2015 12:59
Show Gist options
  • Select an option

  • Save tom--/7329710 to your computer and use it in GitHub Desktop.

Select an option

Save tom--/7329710 to your computer and use it in GitHub Desktop.
Rendering fenced code blocks in Markdown files in nanoc using redcarpet with a custom renderer to run the code through pygments.

The standard redcarpet renderer generates code blocks like <pre><code class="ruby">... instead of the W3C-standard <pre><code class="language-ruby">.... This means the nanoc ColorizeSyntax Filter cannot cope with standard output from redcarpet.

One way to fix this is to customize the redcarpet renderer to generate colorized code blocks and skip the nanoc ColorizeSyntax Filter. The redcarpet readme shows how to extend its HTML renderer to do it. I put the custom renderer class in my nanoc /lib dir like this:

In /lib/default.rb

require 'redcarpet'
require 'pygments'

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

I used this custom colorizing redcarpet filter with a simple change in my nanoc /Rules, in the compile rule for markdown items:

    filter :erb
    filter :redcarpet, 
        :renderer => HTMLwithPygments,
        :options => {
            :fenced_code_blocks => true, 
            :tables => true,
            :disable_indented_code_blocks => true,
            :lax_spacing => true,
        },
        :with_toc => true
    layout 'default'

A tidy solution!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment