Sha256: 1e5395246cfadb612645fe0cef35e910e63c484324756d463db6c40731319e0b

Contents?: true

Size: 1.3 KB

Versions: 30

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Nanoc::Filters
  # @api private
  class Redcarpet < Nanoc::Filter
    identifier :redcarpet

    requires 'redcarpet'

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

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
nanoc-4.11.7 lib/nanoc/filters/redcarpet.rb
nanoc-4.11.6 lib/nanoc/filters/redcarpet.rb
nanoc-4.11.5 lib/nanoc/filters/redcarpet.rb
nanoc-4.11.4 lib/nanoc/filters/redcarpet.rb
nanoc-4.11.3 lib/nanoc/filters/redcarpet.rb
nanoc-4.11.2 lib/nanoc/filters/redcarpet.rb
nanoc-4.11.1 lib/nanoc/filters/redcarpet.rb
nanoc-4.11.0 lib/nanoc/filters/redcarpet.rb
nanoc-4.10.4 lib/nanoc/filters/redcarpet.rb
nanoc-4.10.3 lib/nanoc/filters/redcarpet.rb
nanoc-4.10.2 lib/nanoc/filters/redcarpet.rb
nanoc-4.10.1 lib/nanoc/filters/redcarpet.rb
nanoc-4.10.0 lib/nanoc/filters/redcarpet.rb
nanoc-4.9.9 lib/nanoc/filters/redcarpet.rb
nanoc-4.9.8 lib/nanoc/filters/redcarpet.rb
nanoc-4.9.7 lib/nanoc/filters/redcarpet.rb
nanoc-4.9.6 lib/nanoc/filters/redcarpet.rb
nanoc-4.9.5 lib/nanoc/filters/redcarpet.rb
nanoc-4.9.4 lib/nanoc/filters/redcarpet.rb
nanoc-4.9.3 lib/nanoc/filters/redcarpet.rb