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


380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/erb.rb', line 380

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


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

def stag
  @stag
end

Instance Method Details

#explicit_trim_line(line) ⇒ Object


466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'lib/erb.rb', line 466

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


485
486
487
# File 'lib/erb.rb', line 485

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

#percent_line(line, &block) ⇒ Object


408
409
410
411
412
413
414
415
416
417
418
419
# File 'lib/erb.rb', line 408

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


396
397
398
399
400
401
402
403
404
405
406
# File 'lib/erb.rb', line 396

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


421
422
423
424
425
426
427
428
# File 'lib/erb.rb', line 421

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


430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/erb.rb', line 430

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


444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'lib/erb.rb', line 444

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