Sha256: d06e7766f368e7743a741cb543d76d9b8283757d450ed886482923952900680b

Contents?: true

Size: 1.83 KB

Versions: 23

Compression:

Stored size: 1.83 KB

Contents

require "redcarpet"

module Middleman
  module Renderers

    class RedcarpetTemplate < ::Tilt::RedcarpetTemplate::Redcarpet2

      def initialize(*args, &block)
        super
        
        if @options.has_key?(:context)
          @context = @options[:context]
        end
      end

      # Overwrite built-in Tilt version.
      # Don't overload :renderer option with smartypants
      # Support renderer-level options
      def generate_renderer
        return options.delete(:renderer) if options.has_key?(:renderer)

        # Pick a renderer
        renderer = MiddlemanRedcarpetHTML

        # Support SmartyPants
        if options.delete(:smartypants)
          renderer = Class.new(renderer) do
            include ::Redcarpet::Render::SmartyPants
          end
        end

        # Renderer Options
        possible_render_opts = [:filter_html, :no_images, :no_links, :no_styles, :safe_links_only, :with_toc_data, :hard_wrap, :xhtml]

        render_options = possible_render_opts.inject({}) do |sum, opt|
          sum[opt] = options.delete(opt) if options.has_key?(opt)
          sum
        end

        renderer.new(render_options)
      end

      def evaluate(context, locals, &block)
        @context ||= context

        if @engine.renderer.respond_to? :middleman_app=
          @engine.renderer.middleman_app = @context
        end
        super
      end
    end

    # Custom Redcarpet renderer that uses our helpers for images and links
    class MiddlemanRedcarpetHTML < ::Redcarpet::Render::HTML
      attr_accessor :middleman_app

      def image(link, title, alt_text)
        middleman_app.image_tag(link, :title => title, :alt => alt_text)
      end

      def link(link, title, content)
        middleman_app.link_to(content, link, :title => title)
      end
    end

    ::Tilt.register RedcarpetTemplate, 'markdown', 'mkd', 'md'
  end
end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
middleman-core-x86-mingw32-3.0.14 lib/middleman-core/renderers/redcarpet.rb
middleman-core-3.0.14 lib/middleman-core/renderers/redcarpet.rb
middleman-core-x86-mingw32-3.0.13 lib/middleman-core/renderers/redcarpet.rb
middleman-core-3.0.13 lib/middleman-core/renderers/redcarpet.rb
middleman-core-x86-mingw32-3.0.12 lib/middleman-core/renderers/redcarpet.rb
middleman-core-3.0.12 lib/middleman-core/renderers/redcarpet.rb
middleman-core-x86-mingw32-3.0.12.pre.1 lib/middleman-core/renderers/redcarpet.rb
middleman-core-3.0.12.pre.1 lib/middleman-core/renderers/redcarpet.rb
middleman-core-x86-mingw32-3.0.11 lib/middleman-core/renderers/redcarpet.rb
middleman-core-3.0.11 lib/middleman-core/renderers/redcarpet.rb
middleman-core-x86-mingw32-3.0.10 lib/middleman-core/renderers/redcarpet.rb
middleman-core-x86-mingw32-3.0.10.pre.1 lib/middleman-core/renderers/redcarpet.rb
middleman-core-3.0.10.pre.1 lib/middleman-core/renderers/redcarpet.rb
middleman-core-x86-mingw32-3.0.9 lib/middleman-core/renderers/redcarpet.rb
middleman-core-3.0.9 lib/middleman-core/renderers/redcarpet.rb
middleman-core-x86-mingw32-3.0.8 lib/middleman-core/renderers/redcarpet.rb
middleman-core-3.0.8 lib/middleman-core/renderers/redcarpet.rb
middleman-core-x86-mingw32-3.0.8.pre.2 lib/middleman-core/renderers/redcarpet.rb
middleman-core-3.0.8.pre.2 lib/middleman-core/renderers/redcarpet.rb
middleman-core-x86-mingw32-3.0.8.pre.1 lib/middleman-core/renderers/redcarpet.rb