Class: ERB::Compiler

Inherits:
Object show all
Defined in:
lib/erb.rb

Overview

:nodoc:

Defined Under Namespace

Classes: Buffer, PercentLine, Scanner, SimpleScanner, TrimScanner

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(trim_mode) ⇒ Compiler

Returns a new instance of Compiler.



618
619
620
621
622
623
624
# File 'lib/erb.rb', line 618

def initialize(trim_mode)
  @percent, @trim_mode = prepare_trim_mode(trim_mode)
  @put_cmd = 'print'
  @insert_cmd = @put_cmd
  @pre_cmd = []
  @post_cmd = []
end

Instance Attribute Details

#insert_cmdObject

Returns the value of attribute insert_cmd.



626
627
628
# File 'lib/erb.rb', line 626

def insert_cmd
  @insert_cmd
end

#percentObject (readonly)

Returns the value of attribute percent.



625
626
627
# File 'lib/erb.rb', line 625

def percent
  @percent
end

#post_cmdObject

Returns the value of attribute post_cmd.



626
627
628
# File 'lib/erb.rb', line 626

def post_cmd
  @post_cmd
end

#pre_cmdObject

Returns the value of attribute pre_cmd.



626
627
628
# File 'lib/erb.rb', line 626

def pre_cmd
  @pre_cmd
end

#put_cmdObject

Returns the value of attribute put_cmd.



626
627
628
# File 'lib/erb.rb', line 626

def put_cmd
  @put_cmd
end

#trim_modeObject (readonly)

Returns the value of attribute trim_mode.



625
626
627
# File 'lib/erb.rb', line 625

def trim_mode
  @trim_mode
end

Instance Method Details

#compile(s) ⇒ Object



529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
# File 'lib/erb.rb', line 529

def compile(s)
  out = Buffer.new(self)

  content = ''
  scanner = make_scanner(s)
  scanner.scan do |token|
    next if token.nil? 
    next if token == ''
	if scanner.stag.nil?
	  case token
      when PercentLine
 out.push("#{@put_cmd} #{content_dump(content)}") if content.size > 0
 content = ''
        out.push(token.to_s)
        out.cr
	  when :cr
 out.cr
	  when '<%', '<%=', '<%#'
 scanner.stag = token
 out.push("#{@put_cmd} #{content_dump(content)}") if content.size > 0
 content = ''
	  when "\n"
 content << "\n"
 out.push("#{@put_cmd} #{content_dump(content)}")
 content = ''
	  when '<%%'
 content << '<%'
	  else
 content << token
	  end
	else
	  case token
	  when '%>'
 case scanner.stag
 when '<%'
   if content[-1] == ?\n
		content.chop!
		out.push(content)
		out.cr
   else
		out.push(content)
   end
 when '<%='
   out.push("#{@insert_cmd}((#{content}).to_s)")
 when '<%#'
   # out.push("# #{content_dump(content)}")
 end
 scanner.stag = nil
 content = ''
	  when '%%>'
 content << '%>'
	  else
 content << token
	  end
	end
  end
  out.push("#{@put_cmd} #{content_dump(content)}") if content.size > 0
  out.close
  out.script
end

#content_dump(s) ⇒ Object



520
521
522
523
524
525
526
527
# File 'lib/erb.rb', line 520

def content_dump(s)
  n = s.count("\n")
  if n > 0
    s.dump + "\n" * n
  else
    s.dump
  end
end

#make_scanner(src) ⇒ Object



614
615
616
# File 'lib/erb.rb', line 614

def make_scanner(src)
  Scanner.make_scanner(src, @trim_mode, @percent)
end

#prepare_trim_mode(mode) ⇒ Object



590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# File 'lib/erb.rb', line 590

def prepare_trim_mode(mode)
  case mode
  when 1
	return [false, '>']
  when 2
	return [false, '<>']
  when 0
	return [false, nil]
  when String
	perc = mode.include?('%')
	if mode.include?('-')
	  return [perc, '-']
	elsif mode.include?('<>')
	  return [perc, '<>']
	elsif mode.include?('>')
	  return [perc, '>']
	else
	  [perc, nil]
	end
  else
	return [false, nil]
  end
end