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, indentString = " ", mode = :explicit) ⇒ OutputHandler

Returns a new instance of OutputHandler.



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

def initialize(indent=0, indentString="   ", mode=:explicit)
  self.mode = mode
  @indent = indent
  @indentString = indentString
  @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

#noIndentNextLineObject

Returns the value of attribute noIndentNextLine.



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

def noIndentNextLine
  @noIndentNextLine
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



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
# File 'lib/rgen/template_language/output_handler.rb', line 24

def concat(s)
  return @output.concat(s) if s.is_a? OutputHandler
  #puts [object_id, noIndentNextLine, @state, @output.to_s, s].inspect
  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
      if @state == :wait_for_nl
        if s =~ /\A([^\r\n]*\r?\n)(.*)/m
          rest = $2
          @output.concat($1.gsub(/[\t ]+(?=\r|\n)/,''))
          s = rest || ""
          @state = :wait_for_nonws
        else
          @output.concat(s)
          s = ""
        end
      elsif @state == :wait_for_nonws
        if s =~ /\A\s*(\S+.*)/m
          s = $1 || ""
          if !@noIndentNextLine && !(@output.to_s.size > 0 && @output.to_s[-1] != "\n"[0])
            @output.concat(@indentString * @indent)
          else
            @noIndentNextLine = false
          end
          @state = :wait_for_nl
        else
          s = ""
        end
      end
    end
  end
end

#direct_concat(s) ⇒ Object



67
68
69
# File 'lib/rgen/template_language/output_handler.rb', line 67

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

#ignoreNextNLObject



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

def ignoreNextNL
  @ignoreNextNL = true
end

#ignoreNextWSObject



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

def ignoreNextWS
  @ignoreNextWS = true
end

#mode=(m) ⇒ Object

Raises:

  • (StandardError)


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

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

#to_strObject Also known as: to_s



62
63
64
# File 'lib/rgen/template_language/output_handler.rb', line 62

def to_str
  @output
end