Class: Erector::Output
Constant Summary collapse
- SPACES_PER_INDENT =
2
Instance Attribute Summary collapse
-
#indentation ⇒ Object
readonly
Returns the value of attribute indentation.
-
#max_length ⇒ Object
readonly
Returns the value of attribute max_length.
-
#prettyprint ⇒ Object
readonly
Returns the value of attribute prettyprint.
-
#widgets ⇒ Object
readonly
Returns the value of attribute widgets.
Instance Method Summary collapse
- #<<(s) ⇒ Object
-
#append_newline ⇒ Object
always append a newline, regardless of prettyprint setting todo: test.
- #at_line_start? ⇒ Boolean
- #buffer ⇒ Object
- #indent ⇒ Object
-
#initialize(options = {}, &get_buffer) ⇒ Output
constructor
A new instance of Output.
- #newline ⇒ Object
-
#placeholder ⇒ Object
Inserts a blank string into the output stream and returns a pointer to it.
- #to_a ⇒ Object
- #to_s ⇒ Object
- #undent ⇒ Object
Constructor Details
#initialize(options = {}, &get_buffer) ⇒ Output
Returns a new instance of Output.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/erector/output.rb', line 7 def initialize( = {}, & get_buffer) @prettyprint = .fetch(:prettyprint, Widget.prettyprint_default) @indentation = .fetch(:indentation, 0) @current_line_length = 0 @max_length = [:max_length] @widgets = [] if get_buffer @get_buffer = get_buffer elsif buffer = [:output] @get_buffer = lambda { buffer } else buffer = [] @get_buffer = lambda { buffer } end end |
Instance Attribute Details
#indentation ⇒ Object (readonly)
Returns the value of attribute indentation.
5 6 7 |
# File 'lib/erector/output.rb', line 5 def indentation @indentation end |
#max_length ⇒ Object (readonly)
Returns the value of attribute max_length.
5 6 7 |
# File 'lib/erector/output.rb', line 5 def max_length @max_length end |
#prettyprint ⇒ Object (readonly)
Returns the value of attribute prettyprint.
5 6 7 |
# File 'lib/erector/output.rb', line 5 def prettyprint @prettyprint end |
#widgets ⇒ Object (readonly)
Returns the value of attribute widgets.
5 6 7 |
# File 'lib/erector/output.rb', line 5 def @widgets end |
Instance Method Details
#<<(s) ⇒ Object
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 |
# File 'lib/erector/output.rb', line 27 def <<(s) s = s.to_s unless s.is_a? String append_indentation if @max_length && s.length + @current_line_length > @max_length leading_spaces = s =~ /^( +)/ ? $1.size : 0 trailing_spaces = s =~ /( +)$/ ? $1.size : 0 append(" " * leading_spaces) need_space = false words = s.split(/ /) words.each do |word| if (need_space ? 1 : 0) + word.length > space_left append_newline append_indentation need_space = false end append(" ") if need_space append(word) need_space = true end append(" " * trailing_spaces) else append(s) end self end |
#append_newline ⇒ Object
always append a newline, regardless of prettyprint setting todo: test
92 93 94 95 |
# File 'lib/erector/output.rb', line 92 def append_newline buffer << "\n" @current_line_length = 0 end |
#at_line_start? ⇒ Boolean
78 79 80 |
# File 'lib/erector/output.rb', line 78 def at_line_start? @current_line_length == 0 end |
#buffer ⇒ Object
23 24 25 |
# File 'lib/erector/output.rb', line 23 def buffer @get_buffer.call end |
#indent ⇒ Object
82 83 84 |
# File 'lib/erector/output.rb', line 82 def indent @indentation += 1 if prettyprint end |
#newline ⇒ Object
72 73 74 75 76 |
# File 'lib/erector/output.rb', line 72 def newline if prettyprint append_newline end end |
#placeholder ⇒ Object
Inserts a blank string into the output stream and returns a pointer to it. If the caller holds on to this pointer, she can later go back and insert text earlier in the stream. This is used for, e.g., inserting stuff inside the HEAD element that is not known until after the entire page renders.
58 59 60 61 62 |
# File 'lib/erector/output.rb', line 58 def placeholder s = "" buffer << s s end |
#to_a ⇒ Object
68 69 70 |
# File 'lib/erector/output.rb', line 68 def to_a buffer.kind_of?(Array) ? buffer : [buffer] end |
#to_s ⇒ Object
64 65 66 |
# File 'lib/erector/output.rb', line 64 def to_s RawString.new(buffer.kind_of?(String) ? buffer : buffer.join) end |
#undent ⇒ Object
86 87 88 |
# File 'lib/erector/output.rb', line 86 def undent @indentation -= 1 if prettyprint end |