Class: ERB::Compiler::TrimScanner

Inherits:
Scanner
  • Object
show all
Defined in:
lib/missing/erb.rb

Overview

:nodoc:

Constant Summary collapse

ERB_STAG =
%w(<%= <%# <%)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Scanner

default_scanner=, make_scanner, regist_scanner

Constructor Details

#initialize(src, trim_mode, percent) ⇒ TrimScanner

Returns a new instance of TrimScanner.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/missing/erb.rb', line 112

def initialize(src, trim_mode, percent)
  super
  @trim_mode = trim_mode
  @percent = percent
  if @trim_mode == '>'
    @scan_line = self.method(:trim_line1)
  elsif @trim_mode == '<>'
    @scan_line = self.method(:trim_line2)
  elsif @trim_mode == '-'
    @scan_line = self.method(:explicit_trim_line)
  else
    @scan_line = self.method(:scan_line)
  end
end

Instance Attribute Details

#stagObject

Returns the value of attribute stag.



126
127
128
# File 'lib/missing/erb.rb', line 126

def stag
  @stag
end

Instance Method Details

#explicit_trim_line(line) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/missing/erb.rb', line 198

def explicit_trim_line(line)
  line.scan(/(.*?)(^[ \t]*<%\-|<%\-|<%%|%%>|<%=|<%#|<%|-%>\n|-%>|%>|\z)/m) do |tokens|
    tokens.each do |token|
      next if token.empty?
      if @stag.nil? && /[ \t]*<%-/ =~ token
        yield('<%')
      elsif @stag && token == "-%>\n"
        yield('%>')
        yield(:cr)
      elsif @stag && token == '-%>'
        yield('%>')
      else
        yield(token)
      end
    end
  end
end

#is_erb_stag?(s) ⇒ Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/missing/erb.rb', line 217

def is_erb_stag?(s)
  ERB_STAG.member?(s)
end

#percent_line(line, &block) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/missing/erb.rb', line 140

def percent_line(line, &block)
  if @stag || line[0] != ?%
    return @scan_line.call(line, &block)
  end

  line[0] = ''
  if line[0] == ?%
    @scan_line.call(line, &block)
  else
    yield(PercentLine.new(line.chomp))
  end
end

#scan(&block) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/missing/erb.rb', line 128

def scan(&block)
  @stag = nil
  if @percent
    @src.each_line do |line|
      percent_line(line, &block)
    end
  else
    @scan_line.call(@src, &block)
  end
  nil
end

#scan_line(line) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/missing/erb.rb', line 153

def scan_line(line)
  line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>|\n|\z)/m) do |tokens|
    tokens.each do |token|
      next if token.empty?
      yield(token)
    end
  end
end

#trim_line1(line) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/missing/erb.rb', line 162

def trim_line1(line)
  line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>\n|%>|\n|\z)/m) do |tokens|
    tokens.each do |token|
      next if token.empty?
      if token == "%>\n"
        yield('%>')
        yield(:cr)
      else
        yield(token)
      end
    end
  end
end

#trim_line2(line) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/missing/erb.rb', line 176

def trim_line2(line)
  head = nil
  line.scan(/(.*?)(<%%|%%>|<%=|<%#|<%|%>\n|%>|\n|\z)/m) do |tokens|
    tokens.each do |token|
      next if token.empty?
      head = token unless head
      if token == "%>\n"
        yield('%>')
        if is_erb_stag?(head)
          yield(:cr)
        else
          yield("\n")
        end
        head = nil
      else
        yield(token)
        head = nil if token == "\n"
      end
    end
  end
end