Sha256: 9e6f79d3f08a5e74995f5f2720bbdb716792292e36980604a0bc4a76bcbbd5fd

Contents?: true

Size: 1.54 KB

Versions: 11

Compression:

Stored size: 1.54 KB

Contents

require 'kramdown'

module Middleman
  module Renderers
    # Our own Kramdown Tilt template that simply uses our custom renderer.
    class KramdownTemplate < ::Tilt::KramdownTemplate
      def initialize(*args, &block)
        super

        @context = @options[:context] if @options.key?(:context)
      end

      def evaluate(context, *)
        MiddlemanKramdownHTML.scope = @context || context

        @output ||= begin
          output, warnings = MiddlemanKramdownHTML.convert(@engine.root, @engine.options)
          @engine.warnings.concat(warnings)
          output
        end
      end
    end

    # Custom Kramdown renderer that uses our helpers for images and links
    class MiddlemanKramdownHTML < ::Kramdown::Converter::Html
      cattr_accessor :scope

      def convert_img(el, _)
        attrs = el.attr.dup

        link = attrs.delete('src')
        scope.image_tag(link, attrs)
      end

      def convert_a(el, indent)
        content = inner(el, indent)

        if el.attr['href'].start_with?('mailto:')
          mail_addr = el.attr['href'].sub(/\Amailto:/, '')
          href = obfuscate('mailto') << ':' << obfuscate(mail_addr)
          content = obfuscate(content) if content == mail_addr
          return %(<a href="#{href}">#{content}</a>)
        end

        attr = el.attr.dup
        link = attr.delete('href')

        # options to link_to are expected to be symbols, but in Markdown
        # everything is a string.
        attr.transform_keys!(&:to_sym)

        scope.link_to(content, link, attr)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
middleman-core-4.5.1 lib/middleman-core/renderers/kramdown.rb
middleman-core-4.5.0 lib/middleman-core/renderers/kramdown.rb
middleman-core-4.4.3 lib/middleman-core/renderers/kramdown.rb
middleman-core-4.4.2 lib/middleman-core/renderers/kramdown.rb
middleman-core-4.4.0 lib/middleman-core/renderers/kramdown.rb
middleman-core-4.3.11 lib/middleman-core/renderers/kramdown.rb
middleman-core-4.3.10 lib/middleman-core/renderers/kramdown.rb
middleman-core-4.3.8 lib/middleman-core/renderers/kramdown.rb
middleman-core-4.3.7 lib/middleman-core/renderers/kramdown.rb
middleman-core-4.3.6 lib/middleman-core/renderers/kramdown.rb
middleman-core-4.3.5 lib/middleman-core/renderers/kramdown.rb