class Middleman::Renderers::Haml

Haml Renderer

def self.render_with_options(text, compiler_options)

Haml 5 and older
def self.render_with_options(text, compiler_options)
  modified_options = options.dup
  modified_options[:context] = compiler_options[:context]
  text = template_class.new(nil, 1, modified_options) { text }.render
  super(text, compiler_options)
end

def add_exposed_to_context(context)

def add_exposed_to_context(context)
  super
  context.init_haml_helpers if context.respond_to?(:init_haml_helpers)
end

def compile_with_tilt(node, name, indent_width: 0)

def compile_with_tilt(node, name, indent_width: 0)
  options = { context: @context }
  source = node.value[:text]
  result = ::Tilt["t.#{name}"].new(nil, 1, options) { source }.render
  temple = [:multi, [:static, result.gsub(/^/, ' ' * indent_width)]]
  source.lines.size.times do
    temple << [:newline]
  end
  temple
end

def initialize(app, options={}, &block)

def initialize(app, options={}, &block)
  super
  if Object.const_defined?('::Haml::Options') # Haml 5 and older
    ::Haml::Options.defaults[:context] = nil
    ::Haml::Options.send :attr_accessor, :context
  else # Haml 6+
    ::Haml::Engine.define_options context: nil
  end
  if defined?(::Haml::TempleEngine)
    ::Haml::TempleEngine.define_options context: nil
  end
  [::Haml::Filters::Sass, ::Haml::Filters::Scss, ::Haml::Filters::Markdown].each do |f|
    f.class_exec do
      if respond_to?(:template_class) # Haml 5 and older
        def self.render_with_options(text, compiler_options)
          modified_options = options.dup
          modified_options[:context] = compiler_options[:context]
          text = template_class.new(nil, 1, modified_options) { text }.render
          super(text, compiler_options)
        end
      else # Haml 6+
        def initialize(options = {})
          super
          @context = options[:context]
        end
        def compile_with_tilt(node, name, indent_width: 0)
          options = { context: @context }
          source = node.value[:text]
          result = ::Tilt["t.#{name}"].new(nil, 1, options) { source }.render
          temple = [:multi, [:static, result.gsub(/^/, ' ' * indent_width)]]
          source.lines.size.times do
            temple << [:newline]
          end
          temple
        end
      end
    end
  end
  ::Tilt.prefer(::Middleman::Renderers::HamlTemplate, :haml)
  # Add haml helpers to context
  ::Middleman::TemplateContext.send :include, ::Haml::Helpers
end

def initialize(options = {})

Haml 6+
def initialize(options = {})
  super
  @context = options[:context]
end