ERB::Compiler::TrimScanner (Class)

In: libraries/erb.rb
Parent: Scanner

Constants

TrimSplitRegexp = /(<%%)|(%%>)|(<%=)|(<%#)|(<%)|(%>\n)|(%>)|(\n)/
ExplicitTrimRegexp = /(^[ \t]*<%-)|(-%>\n?\z)|(<%-)|(-%>)|(<%%)|(%%>)|(<%=)|(<%#)|(<%)|(%>)|(\n)/
ERB_STAG = %w(<%= <%# <%)

Attributes

stag  [RW] 

Public Class methods

[Source]

    # File libraries/erb.rb, line 53
53:       def initialize(src, trim_mode, percent)
54:         super
55:         @trim_mode = trim_mode
56:         @percent = percent
57:         if @trim_mode == '>'
58:           @scan_line = self.method(:trim_line1)
59:         elsif @trim_mode == '<>'
60:           @scan_line = self.method(:trim_line2)
61:         elsif @trim_mode == '-'
62:           @scan_line = self.method(:explicit_trim_line)
63:         else
64:           @scan_line = self.method(:scan_line)
65:         end
66:       end

Public Instance methods

[Source]

     # File libraries/erb.rb, line 134
134:       def explicit_trim_line(line)
135:         line.split(ExplicitTrimRegexp).each do |token|
136:           next if token.empty?
137:           if @stag.nil? && /[ \t]*<%-/ =~ token
138:             yield('<%')
139:           elsif @stag && /-%>\n/ =~ token
140:             yield('%>')
141:             yield(:cr)
142:           elsif @stag && token == '-%>'
143:             yield('%>')
144:           else
145:             yield(token)
146:           end
147:         end
148:       end

[Source]

     # File libraries/erb.rb, line 151
151:       def is_erb_stag?(s)
152:         ERB_STAG.member?(s)
153:       end

[Source]

    # File libraries/erb.rb, line 83
83:       def percent_line(line, &block)
84:         if @stag || line[0] != ?%
85:           return @scan_line.call(line, &block)
86:         end
87: 
88:         line[0] = ''
89:         if line[0] == ?%
90:           @scan_line.call(line, &block)
91:         else
92:           yield(PercentLine.new(line.chomp))
93:         end
94:       end

[Source]

    # File libraries/erb.rb, line 69
69:       def scan(&block)
70:         @stag = nil
71:         if @percent
72:           @src.each do |line|
73:             percent_line(line, &block)
74:           end
75:         else
76:           @src.each do |line|
77:             @scan_line.call(line, &block)
78:           end
79:         end
80:         nil
81:       end

[Source]

     # File libraries/erb.rb, line 96
 96:       def scan_line(line)
 97:         line.split(SplitRegexp).each do |token|
 98:           next if token.empty?
 99:           yield(token)
100:         end
101:       end

[Source]

     # File libraries/erb.rb, line 103
103:       def trim_line1(line)
104:         line.split(TrimSplitRegexp).each do |token|
105:           next if token.empty?
106:           if token == "%>\n"
107:             yield('%>')
108:             yield(:cr)
109:             break
110:           end
111:           yield(token)
112:         end
113:       end

[Source]

     # File libraries/erb.rb, line 115
115:       def trim_line2(line)
116:         head = nil
117:         line.split(TrimSplitRegexp).each do |token|
118:           next if token.empty?
119:           head = token unless head
120:           if token == "%>\n"
121:             yield('%>')
122:             if  is_erb_stag?(head)
123:               yield(:cr)
124:             else
125:               yield("\n")
126:             end
127:             break
128:           end
129:           yield(token)
130:         end
131:       end

[Validate]