Class: Erector::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/erector/output.rb

Constant Summary collapse

SPACES_PER_INDENT =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Output

Returns a new instance of Output.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/erector/output.rb', line 9

def initialize(options = {})
  @prettyprint = options.fetch(:prettyprint, AbstractWidget.prettyprint_default)
  @indentation = options.fetch(:indentation, 0)
  @current_line_length = 0
  @max_length = options[:max_length]
  @widgets = []

  @get_buffer = if options[:buffer] and options[:buffer].respond_to? :call
    options[:buffer]
  elsif options[:buffer]
    lambda { options[:buffer] }
  else
    buffer = []
    lambda { buffer }
  end
end

Instance Attribute Details

#indentationObject (readonly)

Returns the value of attribute indentation.



7
8
9
# File 'lib/erector/output.rb', line 7

def indentation
  @indentation
end

#max_lengthObject (readonly)

Returns the value of attribute max_length.



7
8
9
# File 'lib/erector/output.rb', line 7

def max_length
  @max_length
end

#prettyprintObject (readonly)

Returns the value of attribute prettyprint.



7
8
9
# File 'lib/erector/output.rb', line 7

def prettyprint
  @prettyprint
end

#widgetsObject (readonly)

Returns the value of attribute widgets.



7
8
9
# File 'lib/erector/output.rb', line 7

def widgets
  @widgets
end

Instance Method Details

#<<(s) ⇒ Object



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
# File 'lib/erector/output.rb', line 30

def <<(s)
  # raise s.inspect unless s.is_a? String
  #
  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_newlineObject

always append a newline, regardless of prettyprint setting todo: test



98
99
100
101
# File 'lib/erector/output.rb', line 98

def append_newline
  buffer << "\n"
  @current_line_length = 0
end

#at_line_start?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/erector/output.rb', line 84

def at_line_start?
  @current_line_length == 0
end

#bufferObject



26
27
28
# File 'lib/erector/output.rb', line 26

def buffer
  @get_buffer.call
end

#indentObject



88
89
90
# File 'lib/erector/output.rb', line 88

def indent
  @indentation += 1 if prettyprint
end

#markObject



103
104
105
# File 'lib/erector/output.rb', line 103

def mark
  @mark = buffer.size
end

#newlineObject



78
79
80
81
82
# File 'lib/erector/output.rb', line 78

def newline
  if prettyprint
    append_newline
  end
end

#placeholderObject

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.



64
65
66
67
68
# File 'lib/erector/output.rb', line 64

def placeholder
  s = ""
  buffer << s
  s
end

#rewind(pos = @mark) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/erector/output.rb', line 107

def rewind pos = @mark
  if buffer.kind_of?(Array)
    buffer.slice!(pos..-1)
  elsif (Object.const_defined?(:ActiveSupport) and
    buffer.kind_of?(ActiveSupport::SafeBuffer))
    # monkey patch to get around SafeBuffer's well-meaning paranoia
    # see http://yehudakatz.com/2010/02/01/safebuffers-and-rails-3-0/
    # and http://weblog.rubyonrails.org/2011/6/8/potential-xss-vulnerability-in-ruby-on-rails-applications
    String.instance_method(:slice!).bind(buffer).call(pos..-1)
  elsif buffer.kind_of?(String)
    buffer.slice!(pos..-1)
  else
    raise "Don't know how to rewind a #{buffer.class}"
  end

end

#to_aObject



74
75
76
# File 'lib/erector/output.rb', line 74

def to_a
  buffer.kind_of?(Array) ? buffer : [buffer]
end

#to_sObject



70
71
72
# File 'lib/erector/output.rb', line 70

def to_s
  RawString.new(buffer.kind_of?(String) ? buffer : buffer.join)
end

#undentObject



92
93
94
# File 'lib/erector/output.rb', line 92

def undent
  @indentation -= 1 if prettyprint
end