Class: ERB::Compiler::TrimScanner

Inherits:
Scanner show all
Defined in:
lib/erb/compiler.rb

Overview

:nodoc:

Constant Summary collapse

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

Constants inherited from Scanner

Scanner::DEFAULT_ETAGS, Scanner::DEFAULT_STAGS

Instance Attribute Summary

Attributes inherited from Scanner

#etags, #stag, #stags

Instance Method Summary collapse

Methods inherited from Scanner

default_scanner=, make_scanner, register_scanner

Constructor Details

#initialize(src, trim_mode, percent) ⇒ TrimScanner

Returns a new instance of TrimScanner.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/erb/compiler.rb', line 115

def initialize(src, trim_mode, percent)
  super
  @trim_mode = trim_mode
  @percent = percent
  if @trim_mode == '>'
    @scan_reg  = /(.*?)(%>\r?\n|#{(stags + etags).join('|')}|\n|\z)/m
    @scan_line = self.method(:trim_line1)
  elsif @trim_mode == '<>'
    @scan_reg  = /(.*?)(%>\r?\n|#{(stags + etags).join('|')}|\n|\z)/m
    @scan_line = self.method(:trim_line2)
  elsif @trim_mode == '-'
    @scan_reg  = /(.*?)(^[ \t]*<%\-|<%\-|-%>\r?\n|-%>|#{(stags + etags).join('|')}|\z)/m
    @scan_line = self.method(:explicit_trim_line)
  else
    @scan_reg  = /(.*?)(#{(stags + etags).join('|')}|\n|\z)/m
    @scan_line = self.method(:scan_line)
  end
end

Instance Method Details

#explicit_trim_line(line) ⇒ Object



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/erb/compiler.rb', line 204

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

#is_erb_stag?(s) ⇒ Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/erb/compiler.rb', line 223

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

#percent_line(line, &block) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/erb/compiler.rb', line 146

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



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/erb/compiler.rb', line 134

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



159
160
161
162
163
164
165
166
# File 'lib/erb/compiler.rb', line 159

def scan_line(line)
  line.scan(@scan_reg) do |tokens|
    tokens.each do |token|
      next if token.empty?
      yield(token)
    end
  end
end

#trim_line1(line) ⇒ Object



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/erb/compiler.rb', line 168

def trim_line1(line)
  line.scan(@scan_reg) do |tokens|
    tokens.each do |token|
      next if token.empty?
      if token == "%>\n" || token == "%>\r\n"
        yield('%>')
        yield(:cr)
      else
        yield(token)
      end
    end
  end
end

#trim_line2(line) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/erb/compiler.rb', line 182

def trim_line2(line)
  head = nil
  line.scan(@scan_reg) do |tokens|
    tokens.each do |token|
      next if token.empty?
      head = token unless head
      if token == "%>\n" || token == "%>\r\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