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.



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

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



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

def stag
  @stag
end

Instance Method Details

#explicit_trim_line(line) ⇒ Object



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

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)


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

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

#percent_line(line, &block) ⇒ Object



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

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



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

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



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

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



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

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



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

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