2012-05-28 22:19:40 +02:00
|
|
|
# Require gem
|
2013-12-28 01:26:31 +01:00
|
|
|
require 'haml'
|
2012-05-28 22:19:40 +02:00
|
|
|
|
2014-05-16 08:30:49 +02:00
|
|
|
# Require padrino-helpers now so that we get a chance to replace their renderer with ours in Tilt.
|
|
|
|
require 'padrino-helpers'
|
|
|
|
|
2014-02-19 03:30:29 +01:00
|
|
|
module SafeTemplate
|
|
|
|
def render(*)
|
|
|
|
super.html_safe
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Tilt::HamlTemplate
|
|
|
|
include SafeTemplate
|
|
|
|
end
|
|
|
|
|
2012-04-27 01:15:35 +02:00
|
|
|
module Middleman
|
|
|
|
module Renderers
|
2014-01-02 06:19:05 +01:00
|
|
|
# Haml precompiles filters before the scope is even available,
|
|
|
|
# thus making it impossible to pass our Middleman instance
|
|
|
|
# in. So we have to resort to heavy hackery :(
|
|
|
|
class HamlTemplate < ::Tilt::HamlTemplate
|
|
|
|
def prepare
|
|
|
|
end
|
|
|
|
|
|
|
|
def evaluate(scope, locals, &block)
|
|
|
|
::Middleman::Renderers::Haml.last_haml_scope = scope
|
|
|
|
|
2014-04-29 19:50:21 +02:00
|
|
|
options = @options.merge(filename: eval_file, line: line)
|
2014-01-02 06:19:05 +01:00
|
|
|
@engine = ::Haml::Engine.new(data, options)
|
|
|
|
output = @engine.render(scope, locals, &block)
|
|
|
|
|
|
|
|
::Middleman::Renderers::Haml.last_haml_scope = nil
|
|
|
|
|
|
|
|
output
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-04-27 01:15:35 +02:00
|
|
|
# Haml Renderer
|
2014-07-05 22:41:59 +02:00
|
|
|
class Haml < ::Middleman::Extension
|
|
|
|
cattr_accessor :last_haml_scope
|
|
|
|
|
|
|
|
def initialize(app, options={}, &block)
|
|
|
|
super
|
|
|
|
|
|
|
|
::Tilt.prefer(::Middleman::Renderers::HamlTemplate, :haml)
|
|
|
|
|
|
|
|
# Add haml helpers to context
|
|
|
|
::Middleman::TemplateContext.send :include, ::Haml::Helpers
|
2011-11-28 07:04:19 +01:00
|
|
|
end
|
2015-05-02 20:48:21 +02:00
|
|
|
|
|
|
|
def add_exposed_to_context(context)
|
|
|
|
super
|
|
|
|
|
|
|
|
context.init_haml_helpers if context.respond_to?(:init_haml_helpers)
|
|
|
|
end
|
|
|
|
|
2011-11-27 03:12:15 +01:00
|
|
|
end
|
|
|
|
end
|
2012-08-14 00:39:06 +02:00
|
|
|
end
|