Sha256: 917a134b354f5a93bf6a2b9bfb5345e868e771a2bd86f101a99d513510bc0ff4

Contents?: true

Size: 1.9 KB

Versions: 94

Compression:

Stored size: 1.9 KB

Contents

module Slim
  # In Slim you don't need to close any blocks:
  #
  #   - if Slim.awesome?
  #     | But of course it is!
  #
  # However, the parser is not smart enough (and that's a good thing) to
  # automatically insert end's where they are needed. Luckily, this filter
  # does *exactly* that (and it does it well!)
  #
  # @api private
  class EndInserter < Filter
    IF_RE = /\A(if|begin|unless|else|elsif|when|rescue|ensure)\b|\bdo\s*(\|[^\|]*\|)?\s*$/
    ELSE_RE = /\A(else|elsif|when|rescue|ensure)\b/
    END_RE = /\Aend\b/

    # Handle multi expression `[:multi, *exps]`
    #
    # @return [Array] Corrected Temple expression with ends inserted
    def on_multi(*exps)
      result = [:multi]
      # This variable is true if the previous line was
      # (1) a control code and (2) contained indented content.
      prev_indent = false

      exps.each do |exp|
        if control?(exp)
          raise(Temple::FilterError, 'Explicit end statements are forbidden') if exp[2] =~ END_RE

          # Two control code in a row. If this one is *not*
          # an else block, we should close the previous one.
          append_end(result) if prev_indent && exp[2] !~ ELSE_RE

          # Indent if the control code starts a block.
          prev_indent = exp[2] =~ IF_RE
        elsif exp[0] != :newline && prev_indent
          # This is *not* a control code, so we should close the previous one.
          # Ignores newlines because they will be inserted after each line.
          append_end(result)
          prev_indent = false
        end

        result << compile(exp)
      end

      # The last line can be a control code too.
      prev_indent ? append_end(result) : result
    end

    private

    # Appends an end
    def append_end(result)
      result << [:code, 'end']
    end

    # Checks if an expression is a Slim control code
    def control?(exp)
      exp[0] == :slim && exp[1] == :control
    end
  end
end

Version data entries

94 entries across 80 versions & 5 rubygems

Version Path
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
brakeman-6.1.2 bundle/ruby/3.3.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
brakeman-6.1.1 bundle/ruby/3.0.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
brakeman-6.1.0 bundle/ruby/3.1.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
brakeman-6.0.1 bundle/ruby/3.1.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
brakeman-6.0.0 bundle/ruby/3.0.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
brakeman-5.4.1 bundle/ruby/3.1.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
brakeman-5.4.0 bundle/ruby/2.7.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
brakeman-5.3.1 bundle/ruby/2.7.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
brakeman-5.3.0 bundle/ruby/2.7.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
brakeman-5.2.3 bundle/ruby/2.7.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
brakeman-5.2.2 bundle/ruby/2.7.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
brakeman-5.2.1 bundle/ruby/2.7.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
brakeman-5.2.0 bundle/ruby/2.7.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
brakeman-5.1.2 bundle/ruby/2.7.0/gems/slim-4.1.0/lib/slim/end_inserter.rb
brakeman-5.1.1 bundle/ruby/2.7.0/gems/slim-4.1.0/lib/slim/end_inserter.rb