Class: Nanoc::Filters::Redcarpet Private

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

Overview

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.

Since:

  • 3.2.0

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 Int::PluginRegistry::PluginMethods

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

Methods inherited from Int::Context

#get_binding, #initialize

Constructor Details

This class inherits a constructor from Nanoc::Filter

Instance Method Details

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

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.

Runs the content through [Redcarpet](github.com/vmg/redcarpet). This method optionally takes processing options to pass on to Redcarpet.

Overloads:

  • #run(content, params = {}) ⇒ String

    For Redcarpet 1.x

    Parameters:

    • content (String)

      The content to filter

    Options Hash (params):

    • :options (Array) — default: []

      A list of options to pass on to Redcarpet

    Returns:

    • (String)

      The filtered content

  • #run(content, params = {}) ⇒ String

    For Redcarpet 2.x

    Parameters:

    • content (String)

      The content to filter

    Options Hash (params):

    • :options (Hash) — default: {}

      A list of options to pass on to Redcarpet itself (not the renderer)

    • :renderer (::Redcarpet::Render::Base) — default: ::Redcarpet::Render::HTML

      The class of the renderer to use

    • :renderer_options (Hash) — default: {}

      A list of options to pass on to the Redcarpet renderer

    • :with_toc (Boolean) — default: false

      A boolean to request a table of contents

    Returns:

    • (String)

      The filtered content

    Since:

    • 3.2.4

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
82
# 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
    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
end