Sha256: 3187b0b9616e7a292e1c9ef28bbf30bc8332d2f8f41cc931adaca223799e10c7

Contents?: true

Size: 1.69 KB

Versions: 20

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module Haml

  # This module makes Haml work with Rails using the template handler API.
  class Plugin
    class << self
      attr_accessor :annotate_rendered_view_with_filenames
    end
    self.annotate_rendered_view_with_filenames = false

    def handles_encoding?; true; end

    def compile(template, source)
      options = Haml::Template.options.dup
      if template.respond_to?(:type)
        options[:mime_type] = template.type
      elsif template.respond_to? :mime_type
        options[:mime_type] = template.mime_type
      end
      options[:filename] = template.identifier

      preamble = '@output_buffer = output_buffer ||= ActionView::OutputBuffer.new if defined?(ActionView::OutputBuffer);'
      postamble = ''

      if self.class.annotate_rendered_view_with_filenames
        # short_identifier is only available in Rails 6+. On older versions, 'inspect' gives similar results.
        ident = template.respond_to?(:short_identifier) ? template.short_identifier : template.inspect
        preamble += "haml_concat '<!-- BEGIN #{ident} -->'.html_safe;"
        postamble += "haml_concat '<!-- END #{ident} -->'.html_safe;"
      end

      Haml::Engine.new(source, options).compiler.precompiled_with_ambles(
        [],
        after_preamble: preamble,
        before_postamble: postamble
      )
    end

    def self.call(template, source = nil)
      source ||= template.source

      new.compile(template, source)
    end

    def cache_fragment(block, name = {}, options = nil)
      @view.fragment_for(block, name, options) do
        eval("_hamlout.buffer", block.binding)
      end
    end
  end
end

ActionView::Template.register_template_handler(:haml, Haml::Plugin)

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
brakeman-7.0.0 bundle/ruby/3.1.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-6.2.2 bundle/ruby/3.1.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-6.2.2.rc1 bundle/ruby/3.3.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-6.2.1 bundle/ruby/3.1.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-6.2.0 bundle/ruby/3.1.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-6.1.2 bundle/ruby/3.3.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-6.1.1 bundle/ruby/3.0.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-6.1.0 bundle/ruby/3.1.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-6.0.1 bundle/ruby/3.1.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-6.0.0 bundle/ruby/3.0.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-5.4.1 bundle/ruby/3.1.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-5.4.0 bundle/ruby/2.7.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-5.3.1 bundle/ruby/2.7.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-5.3.0 bundle/ruby/2.7.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-5.2.3 bundle/ruby/2.7.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-5.2.2 bundle/ruby/2.7.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-5.2.1 bundle/ruby/2.7.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-5.2.0 bundle/ruby/2.7.0/gems/haml-5.2.2/lib/haml/plugin.rb
brakeman-5.1.2 bundle/ruby/2.7.0/gems/haml-5.2.2/lib/haml/plugin.rb
haml-5.2.2 lib/haml/plugin.rb