Class: RGen::TemplateLanguage::OutputHandler

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

Constant Summary collapse

NL =
"\n"
LFNL =
"\r\n"
NL_CHAR =
"\n"
LF_CHAR =
"\r"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of OutputHandler.



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

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

Instance Attribute Details

#noIndentNextLineObject

Returns the value of attribute noIndentNextLine.



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

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



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/rgen/template_language/output_handler.rb', line 37

def concat(s)
  if @ignoreNextNL
    idx = s.index(NL)
    if idx && s[0..idx].strip.empty?
      s = s[idx+1..-1]
    end
    @ignoreNextNL = false unless s.strip.empty?
  end
  if @ignoreNextWS
    s = s.lstrip
    @ignoreNextWS = false unless s.empty?
  end
  if @mode == :direct
    @output.concat(s)
  elsif @mode == :explicit
    while s.size > 0
      if @state == :wait_for_nl
        idx = s.index(NL)
        if idx
          if s[idx-1] == LF_CHAR
            @output.concat(s[0..idx].rstrip)
            @output.concat(LFNL)
          else
            @output.concat(s[0..idx].rstrip)
            @output.concat(NL)
          end
          s = s[idx+1..-1]
          @state = :wait_for_nonws
        else
          @output.concat(s)
          break
        end
      elsif @state == :wait_for_nonws
        s = s.lstrip
        if !s.empty?
          unless @noIndentNextLine || (@output[-1] && @output[-1] != NL_CHAR)
            @output.concat(@indent_string)
          else
            @noIndentNextLine = false
          end
          @state = :wait_for_nl
        end
      end
    end
  end
end

#direct_concat(s) ⇒ Object



90
91
92
# File 'lib/rgen/template_language/output_handler.rb', line 90

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

#direct_concat_allow_indent(s) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/rgen/template_language/output_handler.rb', line 94

def direct_concat_allow_indent(s)
  unless @noIndentNextLine || (@output[-1] && @output[-1] != NL_CHAR)
    @output.concat(@indent_string)
  else
    @noIndentNextLine = false
  end
  @state = :wait_for_nl
  @output.concat(s)
end

#ignoreNextNLObject



104
105
106
# File 'lib/rgen/template_language/output_handler.rb', line 104

def ignoreNextNL
  @ignoreNextNL = true
end

#ignoreNextWSObject



108
109
110
# File 'lib/rgen/template_language/output_handler.rb', line 108

def ignoreNextWS
  @ignoreNextWS = true
end

#indent=(i) ⇒ Object



19
20
21
# File 'lib/rgen/template_language/output_handler.rb', line 19

def indent=(i)
  @indent_string = @indentString*i
end

#mode=(m) ⇒ Object

Raises:

  • (StandardError)


112
113
114
115
# File 'lib/rgen/template_language/output_handler.rb', line 112

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

#to_strObject Also known as: to_s



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

def to_str
  @output
end