Class: Nanoc::Filters::Redcarpet Private

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

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Instance Method Summary collapse

Instance Method Details

#run(content, params = {}) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nanoc/filters/redcarpet.rb', line 10

def run(content, params = {})
  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)

  # 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
  renderer =
    if renderer_class == ::Redcarpet::Render::HTML_TOC
      renderer_class.new
    else
      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