Use our magic linking functions from Kramdown, just like we do with Redcarpet. Fixes #999

This commit is contained in:
Ben Hollis 2013-09-16 23:46:59 -07:00
parent 8a928863f2
commit a746be1342
6 changed files with 133 additions and 5 deletions

View file

@ -0,0 +1,41 @@
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)
attr = el.attr.dup
if attr['href'] =~ /\Amailto:/
mail_addr = attr['href'].sub(/\Amailto:/, '')
attr['href'] = obfuscate('mailto') << ":" << obfuscate(mail_addr)
content = obfuscate(content) if content == mail_addr
end
link = attr.delete('href')
middleman_app.link_to(content, link, attr)
end
end
end
end

View file

@ -23,13 +23,18 @@ module Middleman
# Once configuration is parsed
app.after_configuration do
markdown_exts = %w(markdown mdown md mkd mkdn)
begin
# Look for the user's preferred engine
if config[:markdown_engine] == :redcarpet
require "middleman-core/renderers/redcarpet"
::Tilt.prefer(::Middleman::Renderers::RedcarpetTemplate)
::Tilt.prefer(::Middleman::Renderers::RedcarpetTemplate, *markdown_exts)
MiddlemanRedcarpetHTML.middleman_app = self
elsif config[:markdown_engine] == :kramdown
require "middleman-core/renderers/kramdown"
::Tilt.prefer(::Middleman::Renderers::KramdownTemplate, *markdown_exts)
MiddlemanKramdownHTML.middleman_app = self
elsif !config[:markdown_engine].nil?
# Map symbols to classes
markdown_engine_klass = if config[:markdown_engine].is_a? Symbol
@ -41,7 +46,7 @@ module Middleman
end
# Tell tilt to use that engine
::Tilt.prefer(markdown_engine_klass)
::Tilt.prefer(markdown_engine_klass, *markdown_exts)
end
rescue LoadError
logger.warn "Requested Markdown engine (#{config[:markdown_engine]}) not found. Maybe the gem needs to be installed and required?"