Sha256: fc79ed87da3c3dcf336c8b33174fba5f20af39144d9caf2fca39cb1a9974784b

Contents?: true

Size: 996 Bytes

Versions: 4

Compression:

Stored size: 996 Bytes

Contents

module Temple
  module ERB
    # Example ERB parser
    #
    # @api public
    class Parser
      include Mixins::Options

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

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

      def call(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 << [:escape, indicator.size <= 1, [:dynamic, code]]
            else
              result << [:code, code]
            end
          end
        end
        result << [:static, input[pos..-1]]
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
temple-0.3.4 lib/temple/erb/parser.rb
temple-0.3.2 lib/temple/erb/parser.rb
temple-0.3.1 lib/temple/erb/parser.rb
temple-0.3.0 lib/temple/erb/parser.rb