Class: RGen::TemplateLanguage::OutputHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/rgen/template_language/output_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(indent = 0, mode = :explicit) ⇒ OutputHandler

Returns a new instance of OutputHandler.



11
12
13
14
15
16
# File 'lib/rgen/template_language/output_handler.rb', line 11

def initialize(indent=0, mode=:explicit)
  self.mode = mode
  @indent = indent
  @state = :wait_for_nonws
  @output = ""
end

Instance Attribute Details

#indent=(value) ⇒ Object (writeonly)

Sets the attribute indent

Parameters:

  • value

    the value to set the attribute indent to.



9
10
11
# File 'lib/rgen/template_language/output_handler.rb', line 9

def indent=(value)
  @indent = value
end

Instance Method Details

#concat(s) ⇒ Object Also known as: <<

ERB will call this method for every string s which is part of the template file in between %> and <%. If s contains a newline, it will call this method for every part of s which is terminated by a n



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rgen/template_language/output_handler.rb', line 22

def concat(s)
  return @output.concat(s) if s.is_a? OutputHandler
  s = s.to_str.gsub(/^[\t ]*\r?\n/,'') if @ignoreNextNL
  s = s.to_str.gsub(/^\s+/,'') if @ignoreNextWS
  @ignoreNextNL = @ignoreNextWS = false if s =~ /\S/
  if @mode == :direct
    @output.concat(s)
  elsif @mode == :explicit
    while s.size > 0
      #puts "DEGUB: #{@state} #{s.dump}"
      # s starts with whitespace
      if s =~ /\A(\s+)(.*)/m	
        ws = $1; rest = $2
        #puts "DEGUB: ws #{ws.dump} rest #{rest.dump}"
        if @state == :wait_for_nl
          # ws contains a newline
          if ws =~ /\A[\t ]*(\r?\n)(\s*)/m
            @output.concat($1)
            @state = :wait_for_nonws
            s = $2 + rest
          else
            @output.concat(ws)
            s = rest
          end
        else
          s = rest
        end
        # s starts with non-whitespace
      elsif s =~ /\A(\S+)(.*)/m
        nonws = $1; rest = $2
        #puts "DEGUB: nonws #{nonws.dump} rest #{rest.dump}"
        if @state == :wait_for_nonws
          # within the same output handle we can recognize a newline by ourselves
          # but if the output handler is changed, someone has to tell us
          if !@noIndentNextLine && !(@output =~ /[^\n]\z/)
            @output.concat("   "*@indent)
          else
            @noIndentNextLine = false
          end
        end
        @output.concat(nonws)
        @state = :wait_for_nl
        s = rest
      end
    end
  end
end

#direct_concat(s) ⇒ Object



76
77
78
# File 'lib/rgen/template_language/output_handler.rb', line 76

def direct_concat(s)
  @output.concat(s)
end

#ignoreNextNLObject



80
81
82
# File 'lib/rgen/template_language/output_handler.rb', line 80

def ignoreNextNL
  @ignoreNextNL = true
end

#ignoreNextWSObject



84
85
86
# File 'lib/rgen/template_language/output_handler.rb', line 84

def ignoreNextWS
  @ignoreNextWS = true
end

#mode=(m) ⇒ Object

Raises:

  • (StandardError)


92
93
94
95
# File 'lib/rgen/template_language/output_handler.rb', line 92

def mode=(m)
  raise StandardError.new("Unknown mode: #{m}") unless [:direct, :explicit].include?(m)
  @mode = m
end

#noIndentNextLineObject



88
89
90
# File 'lib/rgen/template_language/output_handler.rb', line 88

def noIndentNextLine
  @noIndentNextLine = true
end

#to_strObject Also known as: to_s



71
72
73
# File 'lib/rgen/template_language/output_handler.rb', line 71

def to_str
  @output
end