Class: ORB::Utils::ERB

Inherits:
Object
  • Object
show all
Defined in:
lib/orb/utils/erb.rb

Class Method Summary collapse

Class Method Details

.tokenize(source) ⇒ Object

:nodoc:



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
# File 'lib/orb/utils/erb.rb', line 6

def self.tokenize(source) # :nodoc:
  require "strscan"
  source = StringScanner.new(source.chomp)
  tokens = []

  start_re = /<%(?:={1,2}|-|\#|%)?/m
  finish_re = /(?:[-=])?%>/m

  until source.eos?
    pos = source.pos
    source.scan_until(/(?:#{start_re}|#{finish_re})/)
    len = source.pos - source.matched.bytesize - pos

    case source.matched
    when start_re
      tokens << [:TEXT, source.string[pos, len]] if len.positive?
      tokens << [:OPEN, source.matched]
      raise NotImplemented unless source.scan(/(.*?)(?=#{finish_re}|\z)/m)

      tokens << [:CODE, source.matched] unless source.matched.empty?
      tokens << [:CLOSE, source.scan(finish_re)] unless source.eos?

    when finish_re
      tokens << [:CODE, source.string[pos, len]] if len.positive?
      tokens << [:CLOSE, source.matched]
    else
      raise NotImplemented, source.matched
    end
  end

  tokens
end