Sha256: 9c2852fab6c1c4ab67de4880594d6c7302d4a0a0650e354b6e2accc8d8a8d435

Contents?: true

Size: 1.82 KB

Versions: 23

Compression:

Stored size: 1.82 KB

Contents

# frozen_string_literal: true
require 'tilt'

module Haml
  class Filters
    class TiltBase < Base
      def self.render(name, source, indent_width: 0)
        text = ::Tilt["t.#{name}"].new { source }.render
        return text if indent_width == 0
        if text.frozen?
          text.gsub(/^/, ' ' * indent_width)
        else
          text.gsub!(/^/, ' ' * indent_width)
        end
      end

      def explicit_require?(needed_registration)
        Gem::Version.new(Tilt::VERSION) >= Gem::Version.new('2.0.0') &&
          !Tilt.registered?(needed_registration)
      end

      private

      # TODO: support interpolation
      def precompiled_with_tilt(node, name)
        src = ::Tilt["t.#{name}"].new { node.value[:text] }.send(:precompiled, {}).first
        [:dynamic, src]
      end

      def compile_with_tilt(node, name, indent_width: 0)
        if ::Haml::Util.contains_interpolation?(node.value[:text])
          dynamic_compile(node, name, indent_width: indent_width)
        else
          static_compile(node, name, indent_width: indent_width)
        end
      end

      def static_compile(node, name, indent_width: 0)
        temple = [:multi, [:static, TiltBase.render(name, node.value[:text], indent_width: indent_width)]]
        node.value[:text].split("\n").size.times do
          temple << [:newline]
        end
        temple
      end

      def dynamic_compile(node, name, indent_width: 0)
        # original: Haml::Filters#compile
        text = ::Haml::Util.unescape_interpolation(node.value[:text]).gsub(/(\\+)n/) do |s|
          escapes = $1.size
          next s if escapes % 2 == 0
          "#{'\\' * (escapes - 1)}\n"
        end
        text.prepend("\n").sub!(/\n"\z/, '"')

        [:dynamic, "::Haml::Filters::TiltBase.render('#{name}', #{text}, indent_width: #{indent_width})"]
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
haml-6.3.0 lib/haml/filters/tilt_base.rb
haml-6.2.5 lib/haml/filters/tilt_base.rb
haml-6.2.4 lib/haml/filters/tilt_base.rb
haml-6.2.3 lib/haml/filters/tilt_base.rb
haml-6.2.2 lib/haml/filters/tilt_base.rb
haml-6.2.1 lib/haml/filters/tilt_base.rb
haml-6.2.0 lib/haml/filters/tilt_base.rb
haml-6.1.4-java lib/haml/filters/tilt_base.rb
haml-6.1.4 lib/haml/filters/tilt_base.rb
haml-6.1.3-java lib/haml/filters/tilt_base.rb
haml-6.1.3 lib/haml/filters/tilt_base.rb
haml-6.1.2-java lib/haml/filters/tilt_base.rb
haml-6.1.2 lib/haml/filters/tilt_base.rb
haml-6.1.1-java lib/haml/filters/tilt_base.rb
haml-6.1.1 lib/haml/filters/tilt_base.rb
haml-6.1.0-java lib/haml/filters/tilt_base.rb
haml-6.1.0 lib/haml/filters/tilt_base.rb
haml-6.0.12-java lib/haml/filters/tilt_base.rb
haml-6.0.12 lib/haml/filters/tilt_base.rb
haml-6.0.11-java lib/haml/filters/tilt_base.rb