2013-12-28 01:26:31 +01:00
|
|
|
require 'kramdown'
|
2013-09-17 08:46:59 +02:00
|
|
|
|
|
|
|
module Middleman
|
|
|
|
module Renderers
|
|
|
|
# Our own Kramdown Tilt template that simply uses our custom renderer.
|
|
|
|
class KramdownTemplate < ::Tilt::KramdownTemplate
|
2015-06-17 01:47:42 +02:00
|
|
|
def initialize(*args, &block)
|
|
|
|
super
|
|
|
|
|
|
|
|
@context = @options[:context] if @options.key?(:context)
|
|
|
|
end
|
2014-01-02 06:19:05 +01:00
|
|
|
|
2015-06-17 01:47:42 +02:00
|
|
|
def evaluate(context, *)
|
|
|
|
MiddlemanKramdownHTML.scope = @context || context
|
|
|
|
|
|
|
|
@output ||= begin
|
2013-09-17 08:46:59 +02:00
|
|
|
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
|
2014-01-02 06:19:05 +01:00
|
|
|
cattr_accessor :scope
|
2013-09-17 08:46:59 +02:00
|
|
|
|
2014-04-29 19:44:24 +02:00
|
|
|
def convert_img(el, _)
|
2013-10-30 17:18:25 +01:00
|
|
|
attrs = el.attr.dup
|
2013-10-20 04:02:34 +02:00
|
|
|
|
2013-09-17 08:46:59 +02:00
|
|
|
link = attrs.delete('src')
|
2014-01-02 06:19:05 +01:00
|
|
|
scope.image_tag(link, attrs)
|
2013-09-17 08:46:59 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def convert_a(el, indent)
|
|
|
|
content = inner(el, indent)
|
2013-10-30 17:18:25 +01:00
|
|
|
|
2016-01-14 20:21:42 +01:00
|
|
|
if el.attr['href'].start_with?('mailto:')
|
2013-10-30 17:18:25 +01:00
|
|
|
mail_addr = el.attr['href'].sub(/\Amailto:/, '')
|
2013-12-28 01:26:31 +01:00
|
|
|
href = obfuscate('mailto') << ':' << obfuscate(mail_addr)
|
2013-09-17 08:46:59 +02:00
|
|
|
content = obfuscate(content) if content == mail_addr
|
2014-08-18 23:00:28 +02:00
|
|
|
return %(<a href="#{href}">#{content}</a>)
|
2013-09-17 08:46:59 +02:00
|
|
|
end
|
|
|
|
|
2013-10-30 17:18:25 +01:00
|
|
|
attr = el.attr.dup
|
2013-09-17 08:46:59 +02:00
|
|
|
link = attr.delete('href')
|
2015-06-17 01:47:42 +02:00
|
|
|
|
2014-01-02 06:19:05 +01:00
|
|
|
scope.link_to(content, link, attr)
|
2013-09-17 08:46:59 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|