Class: RGen::TemplateLanguage::OutputHandler
- Inherits:
-
Object
- Object
- RGen::TemplateLanguage::OutputHandler
- Defined in:
- lib/rgen/template_language/output_handler.rb
Instance Attribute Summary collapse
-
#indent ⇒ Object
writeonly
Sets the attribute indent.
Instance Method Summary collapse
-
#concat(s) ⇒ Object
(also: #<<)
ERB will call this method for every string s which is part of the template file in between %> and <%.
- #direct_concat(s) ⇒ Object
- #ignoreNextNL ⇒ Object
- #ignoreNextWS ⇒ Object
-
#initialize(indent = 0, mode = :explicit) ⇒ OutputHandler
constructor
A new instance of OutputHandler.
- #mode=(m) ⇒ Object
- #noIndentNextLine ⇒ Object
- #to_str ⇒ Object (also: #to_s)
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
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 |
# 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 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(" "*@indent) else @noIndentNextLine = false end @state = :wait_for_nl else s = "" end end end end end |
#direct_concat(s) ⇒ Object
64 65 66 |
# File 'lib/rgen/template_language/output_handler.rb', line 64 def direct_concat(s) @output.concat(s) end |
#ignoreNextNL ⇒ Object
68 69 70 |
# File 'lib/rgen/template_language/output_handler.rb', line 68 def ignoreNextNL @ignoreNextNL = true end |
#ignoreNextWS ⇒ Object
72 73 74 |
# File 'lib/rgen/template_language/output_handler.rb', line 72 def ignoreNextWS @ignoreNextWS = true end |
#mode=(m) ⇒ Object
80 81 82 83 |
# File 'lib/rgen/template_language/output_handler.rb', line 80 def mode=(m) raise StandardError.new("Unknown mode: #{m}") unless [:direct, :explicit].include?(m) @mode = m end |
#noIndentNextLine ⇒ Object
76 77 78 |
# File 'lib/rgen/template_language/output_handler.rb', line 76 def noIndentNextLine @noIndentNextLine = true end |
#to_str ⇒ Object Also known as: to_s
59 60 61 |
# File 'lib/rgen/template_language/output_handler.rb', line 59 def to_str @output end |