File: filter.rb

package info (click to toggle)
ruby-slim 5.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 828 kB
  • sloc: ruby: 5,583; makefile: 12
file content (93 lines) | stat: -rw-r--r-- 3,085 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# frozen_string_literal: true
module Slim
  module Splat
    # @api private
    class Filter < ::Slim::Filter
      define_options :merge_attrs, :attr_quote, :sort_attrs, :default_tag, :format, :disable_capture,
                     hyphen_attrs: %w(data aria), use_html_safe: ''.respond_to?(:html_safe?),
                     hyphen_underscore_attrs: false

      def call(exp)
        @splat_options = nil
        exp = compile(exp)
        if @splat_options
          opts = options.to_hash.reject { |k, v| !Filter.options.valid_key?(k) }.inspect
          [:multi, [:code, "#{@splat_options} = #{opts}"], exp]
        else
          exp
        end
      end

      # Handle tag expression `[:html, :tag, name, attrs, content]`
      #
      # @param [String] name Tag name
      # @param [Array] attrs Temple expression
      # @param [Array] content Temple expression
      # @return [Array] Compiled temple expression
      def on_html_tag(name, attrs, content = nil)
        return super if name != '*'
        builder, block = make_builder(attrs[2..-1])
        if content
          [:multi,
           block,
           [:slim, :output, false,
            "#{builder}.build_tag #{empty_exp?(content) ? '{}' : 'do'}",
            compile(content)]]
        else
          [:multi,
           block,
           [:dynamic, "#{builder}.build_tag"]]
        end
      end

      # Handle attributes expression `[:html, :attrs, *attrs]`
      #
      # @param [Array] attrs Array of temple expressions
      # @return [Array] Compiled temple expression
      def on_html_attrs(*attrs)
        if attrs.any? { |attr| splat?(attr) }
          builder, block = make_builder(attrs)
          [:multi,
           block,
           [:dynamic, "#{builder}.build_attrs"]]
        else
          super
        end
      end

      protected

      def splat?(attr)
        # Splat attribute given
        attr[0] == :slim && attr[1] == :splat ||
          # Hyphenated attribute also needs splat handling
          (attr[0] == :html && attr[1] == :attr && options[:hyphen_attrs].include?(attr[2]) &&
           attr[3][0] == :slim && attr[3][1] == :attrvalue)
      end

      def make_builder(attrs)
        @splat_options ||= unique_name
        builder = unique_name
        result = [:multi, [:code, "#{builder} = ::Slim::Splat::Builder.new(#{@splat_options})"]]
        attrs.each do |attr|
          result <<
            if attr[0] == :html && attr[1] == :attr
              if attr[3][0] == :slim && attr[3][1] == :attrvalue
                [:code, "#{builder}.code_attr(#{attr[2].inspect}, #{attr[3][2]}, (#{attr[3][3]}))"]
              else
                tmp = unique_name
                [:multi,
                 [:capture, tmp, compile(attr[3])],
                 [:code, "#{builder}.attr(#{attr[2].inspect}, #{tmp})"]]
              end
            elsif attr[0] == :slim && attr[1] == :splat
              [:code, "#{builder}.splat_attrs((#{attr[2]}))"]
            else
              attr
            end
        end
        [builder, result]
      end
    end
  end
end