Class: Nanoc::Filters::Redcarpet

Inherits:
Nanoc::Filter show all
Defined in:
lib/nanoc/filters/redcarpet.rb

Overview

Since:

Constant Summary

Constant Summary

Constants inherited from Nanoc::Filter

Nanoc::Filter::TMP_BINARY_ITEMS_DIR

Instance Attribute Summary

Attributes inherited from Nanoc::Filter

#assigns

Instance Method Summary (collapse)

Methods inherited from Nanoc::Filter

#depend_on, #filename, from_binary?, #initialize, #output_filename, requires, setup, #setup_and_run, to_binary?, type

Methods included from PluginRegistry::PluginMethods

#all, #identifier, #identifiers, #named, #register

Methods inherited from Context

#get_binding, #initialize

Constructor Details

This class inherits a constructor from Nanoc::Filter

Instance Method Details

- (Object) run(content, params = {})

@return [String] The filtered content

Since:

  • 3.2.0



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nanoc/filters/redcarpet.rb', line 43

def run(content, params = {})
  if ::Redcarpet::VERSION > '2'
    options          = params.fetch(:options,          {})
    renderer_class   = params.fetch(:renderer,         ::Redcarpet::Render::HTML)
    renderer_options = params.fetch(:renderer_options, {})
    with_toc         = params.fetch(:with_toc,         false)

    if options.is_a?(Array)
      warn 'WARNING: You are passing an array of options to the :redcarpet filter, but Redcarpet 2.x expects a hash instead. This will likely fail.'
    end

    # Setup TOC
    if with_toc
      unless renderer_class <= ::Redcarpet::Render::HTML
        raise "Unexpected renderer: #{renderer_class}"
      end

      # `with_toc` implies `with_toc_data` for the HTML renderer
      renderer_options[:with_toc_data] = true
    end

    # Create renderer
    if renderer_class == ::Redcarpet::Render::HTML_TOC
      renderer = renderer_class.new
    else
      renderer = renderer_class.new(renderer_options)
    end

    # Render
    if with_toc
      renderer_toc = ::Redcarpet::Render::HTML_TOC.new
      toc  = ::Redcarpet::Markdown.new(renderer_toc, options).render(content)
      body = ::Redcarpet::Markdown.new(renderer,     options).render(content)
      toc + body
    else
      ::Redcarpet::Markdown.new(renderer, options).render(content)
    end
  end
end