File: tilt_base.rb

package info (click to toggle)
ruby-hamlit 2.15.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,996 kB
  • sloc: ruby: 10,548; ansic: 536; sh: 23; makefile: 8
file content (49 lines) | stat: -rw-r--r-- 1,576 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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.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