Sha256: 6acbce4fef17616223436f022c2455170e48c5da846b9a1757c40014a202f694

Contents?: true

Size: 1.54 KB

Versions: 22

Compression:

Stored size: 1.54 KB

Contents

# frozen_string_literal: true
require 'tilt'

module Hamlit
  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
        text.gsub!(/^/, ' ' * indent_width)
      end

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

      private

      def compile_with_tilt(node, name, indent_width: 0)
        if ::Hamlit::HamlUtil.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 = ::Hamlit::HamlUtil.slow_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, "::Hamlit::Filters::TiltBase.render('#{name}', #{text}, indent_width: #{indent_width})"]
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
hamlit-2.12.0-java lib/hamlit/filters/tilt_base.rb
hamlit-2.12.0 lib/hamlit/filters/tilt_base.rb
hamlit-2.11.1 lib/hamlit/filters/tilt_base.rb
hamlit-2.11.0-java lib/hamlit/filters/tilt_base.rb
hamlit-2.11.0 lib/hamlit/filters/tilt_base.rb
hamlit-2.10.1-java lib/hamlit/filters/tilt_base.rb
hamlit-2.10.1 lib/hamlit/filters/tilt_base.rb
hamlit-2.10.0-java lib/hamlit/filters/tilt_base.rb
hamlit-2.10.0 lib/hamlit/filters/tilt_base.rb
hamlit-2.9.5-java lib/hamlit/filters/tilt_base.rb
hamlit-2.9.4-java lib/hamlit/filters/tilt_base.rb
hamlit-2.9.5 lib/hamlit/filters/tilt_base.rb
hamlit-2.9.4 lib/hamlit/filters/tilt_base.rb
hamlit-2.9.3-java lib/hamlit/filters/tilt_base.rb
hamlit-2.9.3 lib/hamlit/filters/tilt_base.rb
hamlit-2.9.2-java lib/hamlit/filters/tilt_base.rb
hamlit-2.9.2 lib/hamlit/filters/tilt_base.rb
hamlit-2.9.1-java lib/hamlit/filters/tilt_base.rb
hamlit-2.9.1 lib/hamlit/filters/tilt_base.rb
hamlit-2.9.0 lib/hamlit/filters/tilt_base.rb