Sha256: 297417bcde2c7dd34896cecaa7aa4c6c3734e310e3b2c0feb3aa15321db98ebd

Contents?: true

Size: 998 Bytes

Versions: 5

Compression:

Stored size: 998 Bytes

Contents

module Temple
  module ERB
    class Parser
      include Mixins::Options

      ERB_PATTERN = /(<%%|%%>)|<%(==?|\#)?(.*?)?-?%>/m

      ESCAPED = {
        '<%%' => '<%',
        '%%>' => '%>',
      }.freeze

      def compile(input)
        result = [:multi]
        pos = 0
        input.scan(ERB_PATTERN) do |escaped, indicator, code|
          m = Regexp.last_match
          text = input[pos...m.begin(0)]
          pos  = m.end(0)
          result << [:static, text] if !text.empty?
          if escaped
            result << [:static, ESCAPED[escaped]]
          else
            case indicator
            when '#'
              code.count("\n").times { result << [:newline] }
            when /=/
              result << (indicator.length > 1 || !options[:auto_escape] ? [:dynamic, code] : [:escape, :dynamic, code])
            else
              result << [:block, code]
            end
          end
        end
        result << [:static, input[pos..-1]]
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
temple-0.1.8 lib/temple/erb/parser.rb
temple-0.1.7 lib/temple/erb/parser.rb
temple-0.1.6 lib/temple/erb/parser.rb
temple-0.1.5 lib/temple/erb/parser.rb
temple-0.1.4 lib/temple/erb/parser.rb