Sha256: 292429e3d48a16e52c1e0756b2969e99b79ae06f739bda1629131874553fc7cb

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

require "kramdown"

module Middleman
  module Renderers

    # Our own Kramdown Tilt template that simply uses our custom renderer.
    class KramdownTemplate < ::Tilt::KramdownTemplate
      def evaluate(scope, locals, &block)
        @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 :middleman_app

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

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

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

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

        attr = el.attr.dup
        link = attr.delete('href')
        middleman_app.link_to(content, link, attr)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
middleman-core-3.2.1 lib/middleman-core/renderers/kramdown.rb
middleman-core-3.2.0 lib/middleman-core/renderers/kramdown.rb