Class: ERB::Compiler::TrimScanner

Inherits:
Scanner
  • Object
show all
Defined in:
lib/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.



288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/erb.rb', line 288

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



302
303
304
# File 'lib/erb.rb', line 302

def stag
  @stag
end

Instance Method Details

#explicit_trim_line(line) ⇒ Object



374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/erb.rb', line 374

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)


393
394
395
# File 'lib/erb.rb', line 393

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

#percent_line(line, &block) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/erb.rb', line 316

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



304
305
306
307
308
309
310
311
312
313
314
# File 'lib/erb.rb', line 304

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

#scan_line(line) ⇒ Object



329
330
331
332
333
334
335
336
# File 'lib/erb.rb', line 329

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



338
339
340
341
342
343
344
345
346
347
348
349
350
# File 'lib/erb.rb', line 338

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



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/erb.rb', line 352

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