Class: ERB::Compiler::TrimScanner

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


382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# File 'lib/erb.rb', line 382

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


471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/erb.rb', line 471

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)

490
491
492
# File 'lib/erb.rb', line 490

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

#percent_line(line, &block) ⇒ Object


413
414
415
416
417
418
419
420
421
422
423
424
# File 'lib/erb.rb', line 413

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


401
402
403
404
405
406
407
408
409
410
411
# File 'lib/erb.rb', line 401

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


426
427
428
429
430
431
432
433
# File 'lib/erb.rb', line 426

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


435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/erb.rb', line 435

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


449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
# File 'lib/erb.rb', line 449

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