Sha256: 3a590b71b21b49a03a92362ec1efa3145ac2ef35ab65153c4b15a47daf44695c

Contents?: true

Size: 1.11 KB

Versions: 7

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true
module Temple
  module ERB
    # Example ERB parser
    #
    # @api public
    class Parser < Temple::Parser
      ERB_PATTERN = /(\n|<%%|%%>)|<%(==?|\#)?(.*?)?-?%>/m

      def call(input)
        result = [:multi]
        pos = 0
        input.scan(ERB_PATTERN) do |token, indicator, code|
          text = input[pos...$~.begin(0)]
          pos  = $~.end(0)
          if token
            case token
            when "\n"
              result << [:static, "#{text}\n"] << [:newline]
            when '<%%', '%%>'
              result << [:static, text] unless text.empty?
              token.slice!(1)
              result << [:static, token]
            end
          else
            result << [:static, text] unless text.empty?
            case indicator
            when '#'
              result << [:code, "\n" * code.count("\n")]
            when /=/
              result << [:escape, indicator.size == 2, [:dynamic, code]]
            else
              result << [:code, code]
            end
          end
        end
        result << [:static, input[pos..-1]]
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 2 rubygems

Version Path
brakeman-6.2.2 bundle/ruby/3.1.0/gems/temple-0.10.3/lib/temple/erb/parser.rb
brakeman-6.2.2.rc1 bundle/ruby/3.3.0/gems/temple-0.10.3/lib/temple/erb/parser.rb
brakeman-6.2.1 bundle/ruby/3.1.0/gems/temple-0.10.3/lib/temple/erb/parser.rb
brakeman-6.2.0 bundle/ruby/3.1.0/gems/temple-0.10.3/lib/temple/erb/parser.rb
temple-0.10.3 lib/temple/erb/parser.rb
temple-0.10.2 lib/temple/erb/parser.rb
temple-0.10.1 lib/temple/erb/parser.rb