Class: ERB::Compiler::TrimScanner
Overview
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.
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
|
# File 'lib/erb.rb', line 384
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
|
# File 'lib/erb.rb', line 473
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
492
493
494
|
# File 'lib/erb.rb', line 492
def is_erb_stag?(s)
ERB_STAG.member?(s)
end
|
#percent_line(line, &block) ⇒ Object
415
416
417
418
419
420
421
422
423
424
425
426
|
# File 'lib/erb.rb', line 415
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
403
404
405
406
407
408
409
410
411
412
413
|
# File 'lib/erb.rb', line 403
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
428
429
430
431
432
433
434
435
|
# File 'lib/erb.rb', line 428
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
437
438
439
440
441
442
443
444
445
446
447
448
449
|
# File 'lib/erb.rb', line 437
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
# File 'lib/erb.rb', line 451
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
|