From f89c815868aa756ad36f7a2bf4824b563f640cef Mon Sep 17 00:00:00 2001 From: Ben Hollis Date: Thu, 4 Apr 2013 22:43:28 -0700 Subject: [PATCH] More agressively set the context for our custom Markdown renderer, and fall back if we still don't manage to get it right. Fixes #662. --- .../lib/middleman-core/renderers/markdown.rb | 1 + .../lib/middleman-core/renderers/redcarpet.rb | 37 ++++++++----------- 2 files changed, 17 insertions(+), 21 deletions(-) diff --git a/middleman-core/lib/middleman-core/renderers/markdown.rb b/middleman-core/lib/middleman-core/renderers/markdown.rb index 06d017cb..08b8c8b7 100644 --- a/middleman-core/lib/middleman-core/renderers/markdown.rb +++ b/middleman-core/lib/middleman-core/renderers/markdown.rb @@ -29,6 +29,7 @@ module Middleman if config[:markdown_engine] == :redcarpet require "middleman-core/renderers/redcarpet" ::Tilt.prefer(::Middleman::Renderers::RedcarpetTemplate) + MiddlemanRedcarpetHTML.middleman_app = self elsif !config[:markdown_engine].nil? # Map symbols to classes markdown_engine_klass = if config[:markdown_engine].is_a? Symbol diff --git a/middleman-core/lib/middleman-core/renderers/redcarpet.rb b/middleman-core/lib/middleman-core/renderers/redcarpet.rb index fe34a1c5..7c3412ac 100644 --- a/middleman-core/lib/middleman-core/renderers/redcarpet.rb +++ b/middleman-core/lib/middleman-core/renderers/redcarpet.rb @@ -4,15 +4,6 @@ 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 @@ -39,27 +30,31 @@ module Middleman 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 + cattr_accessor :middleman_app def image(link, title, alt_text) - middleman_app.image_tag(link, :title => title, :alt => alt_text) + if middleman_app && middleman_app.respond_to?(:image_tag) + middleman_app.image_tag(link, :title => title, :alt => alt_text) + else + img = "\"#{alt_text}\""" + end end def link(link, title, content) - middleman_app.link_to(content, link, :title => title) + if middleman_app && middleman_app.respond_to?(:link_to) + middleman_app.link_to(content, link, :title => title) + else + a = "#{content}" + end end end