Class: Haml::Buffer

Inherits:
Object show all
Includes:
Helpers, Util
Defined in:
lib/haml/buffer.rb

Overview

This class is used only internally. It holds the buffer of HTML that is eventually output as the resulting document. It's called from within the precompiled code, and helps reduce the amount of processing done within instance_evaled code.

Constant Summary

Constants included from Util

Util::RUBY_VERSION

Constants included from Helpers

Helpers::HTML_ESCAPE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#def_static_method, #enum_with_index, #has?, #map_hash, #map_keys, #map_vals, #merge_adjacent_strings, #powerset, #ruby1_8?, #scope, #static_method_name, #to_hash

Methods included from Helpers

action_view?, #block_is_haml?, #capture_haml, #escape_once, #find_and_preserve, #haml_concat, #haml_indent, #haml_tag, #html_attrs, #html_escape, #init_haml_helpers, #is_haml?, #list_of, #non_haml, #precede, #preserve, #puts, #succeed, #surround, #tab_down, #tab_up

Methods included from Helpers::ActionViewExtensions

#page_class

Constructor Details

#initialize(upper = nil, options = {}) ⇒ Buffer

Returns a new instance of Buffer.

Parameters:



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/haml/buffer.rb', line 90

def initialize(upper = nil, options = {})
  @active = true
  @upper = upper
  @options = options
  @buffer = ruby1_8? ? "" : "".encode(Encoding.find(options[:encoding]))
  @tabulation = 0

  # The number of tabs that Engine thinks we should have
  # @real_tabs + @tabulation is the number of tabs actually output
  @real_tabs = 0
end

Instance Attribute Details

#active=(value) ⇒ Boolean (writeonly)

Returns:

  • (Boolean)

See Also:



37
38
39
# File 'lib/haml/buffer.rb', line 37

def active=(value)
  @active = value
end

#bufferString

The string that holds the compiled HTML. This is aliased as _erbout for compatibility with ERB-specific code.

Returns:

  • (String)


14
15
16
# File 'lib/haml/buffer.rb', line 14

def buffer
  @buffer
end

#capture_positionFixnum?

nil if there's no capture_haml block running, and the position at which it's beginning the capture if there is one.

Returns:

  • (Fixnum, nil)


33
34
35
# File 'lib/haml/buffer.rb', line 33

def capture_position
  @capture_position
end

#optionsHash<String, Object>

The options hash passed in from Engine.

Returns:

See Also:



20
21
22
# File 'lib/haml/buffer.rb', line 20

def options
  @options
end

#upperBuffer

The Haml::Buffer for the enclosing Haml document. This is set for partials and similar sorts of nested templates. It's nil at the top level (see #toplevel?).

Returns:



27
28
29
# File 'lib/haml/buffer.rb', line 27

def upper
  @upper
end

Class Method Details

.merge_attrs(to, from) ⇒ Hash<String, String>

Merges two attribute hashes. This is the same as to.merge!(from), except that it merges id and class attributes.

ids are concatenated with "_", and classes are concatenated with " ".

Destructively modifies both to and from.

Parameters:

  • to (Hash<String, String>)

    The attribute hash to merge into

  • from (Hash<String, String>)

    The attribute hash to merge from

Returns:

  • (Hash<String, String>)

    to, after being merged



246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/haml/buffer.rb', line 246

def self.merge_attrs(to, from)
  if to['id'] && from['id']
    to['id'] << '_' << from.delete('id')
  elsif to['id'] || from['id']
    from['id'] ||= to['id']
  end

  if to['class'] && from['class']
    # Make sure we don't duplicate class names
    from['class'] = (from['class'].split(' ') | to['class'].split(' ')).sort.join(' ')
  elsif to['class'] || from['class']
    from['class'] ||= to['class']
  end

  to.merge!(from)
end

Instance Method Details

#active?Boolean

Whether or not this buffer is currently being used to render a Haml template. Returns false if a subtemplate is being rendered, even if it's a subtemplate of this buffer's template.

Returns:

  • (Boolean)


70
71
72
# File 'lib/haml/buffer.rb', line 70

def active?
  @active
end

#adjust_tabs(tab_change) ⇒ Object

Modifies the indentation of the document.

Parameters:

  • tab_change (Fixnum)

    The number of tabs by which to increase or decrease the document's indentation



126
127
128
# File 'lib/haml/buffer.rb', line 126

def adjust_tabs(tab_change)
  @real_tabs += tab_change
end

#html4?Boolean

Returns Whether or not the format is HTML4.

Returns:

  • (Boolean)

    Whether or not the format is HTML4



50
51
52
# File 'lib/haml/buffer.rb', line 50

def html4?
  @options[:format] == :html4
end

#html5?Boolean

Returns Whether or not the format is HTML5.

Returns:

  • (Boolean)

    Whether or not the format is HTML5.



55
56
57
# File 'lib/haml/buffer.rb', line 55

def html5?
  @options[:format] == :html5
end

#html?Boolean

Returns Whether or not the format is any flavor of HTML.

Returns:

  • (Boolean)

    Whether or not the format is any flavor of HTML



45
46
47
# File 'lib/haml/buffer.rb', line 45

def html?
  html4? or html5?
end

#open_tag(name, self_closing, try_one_line, preserve_tag, escape_html, class_id, nuke_outer_whitespace, nuke_inner_whitespace, obj_ref, content, *attributes_hashes) ⇒ Object

Takes the various information about the opening tag for an element, formats it, and appends it to the buffer.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/haml/buffer.rb', line 192

def open_tag(name, self_closing, try_one_line, preserve_tag, escape_html, class_id,
             nuke_outer_whitespace, nuke_inner_whitespace, obj_ref, content, *attributes_hashes)
  tabulation = @real_tabs

  attributes = class_id
  attributes_hashes.each do |old|
    self.class.merge_attrs(attributes, to_hash(old.map {|k, v| [k.to_s, v]}))
  end
  self.class.merge_attrs(attributes, parse_object_ref(obj_ref)) if obj_ref

  if self_closing && xhtml?
    str = " />" + (nuke_outer_whitespace ? "" : "\n")
  else
    str = ">" + ((if self_closing && html?
                    nuke_outer_whitespace
                  else
                    try_one_line || preserve_tag || nuke_inner_whitespace
                  end) ? "" : "\n")
  end

  attributes = Precompiler.build_attributes(html?, @options[:attr_wrapper], attributes)
  @buffer << "#{nuke_outer_whitespace || @options[:ugly] ? '' : tabs(tabulation)}<#{name}#{attributes}#{str}"

  if content
    @buffer << "#{content}</#{name}>" << (nuke_outer_whitespace ? "" : "\n")
    return
  end

  @real_tabs += 1 unless self_closing || nuke_inner_whitespace
end

#push_text(text, tab_change, dont_tab_up) ⇒ Object

Appends text to the buffer, properly tabulated. Also modifies the document's indentation.

Parameters:

  • text (String)

    The text to append

  • tab_change (Fixnum)

    The number of tabs by which to increase or decrease the document's indentation

  • dont_tab_up (Boolean)

    If true, don't indent the first line of text



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/haml/buffer.rb', line 109

def push_text(text, tab_change, dont_tab_up)
  if @tabulation > 0
    # Have to push every line in by the extra user set tabulation.
    # Don't push lines with just whitespace, though,
    # because that screws up precompiled indentation.
    text.gsub!(/^(?!\s+$)/m, tabs)
    text.sub!(tabs, '') if dont_tab_up
  end

  @buffer << text
  @real_tabs += tab_change
end

#rstrip!Object

Remove the whitespace from the right side of the buffer string. Doesn't do anything if we're at the beginning of a capture_haml block.



225
226
227
228
229
230
231
232
# File 'lib/haml/buffer.rb', line 225

def rstrip!
  if capture_position.nil?
    buffer.rstrip!
    return
  end

  buffer << buffer.slice!(capture_position..-1).rstrip
end

#tabulationFixnum

Returns The current indentation level of the document.

Returns:

  • (Fixnum)

    The current indentation level of the document



75
76
77
# File 'lib/haml/buffer.rb', line 75

def tabulation
  @real_tabs + @tabulation
end

#tabulation=(val) ⇒ Object

Sets the current tabulation of the document.

Parameters:

  • val (Fixnum)

    The new tabulation



82
83
84
85
# File 'lib/haml/buffer.rb', line 82

def tabulation=(val)
  val = val - @real_tabs
  @tabulation = val > -1 ? val : 0
end

#toplevel?Boolean

Returns Whether or not this buffer is a top-level template, as opposed to a nested partial.

Returns:

  • (Boolean)

    Whether or not this buffer is a top-level template, as opposed to a nested partial



61
62
63
# File 'lib/haml/buffer.rb', line 61

def toplevel?
  upper.nil?
end

#xhtml?Boolean

Returns Whether or not the format is XHTML.

Returns:

  • (Boolean)

    Whether or not the format is XHTML



40
41
42
# File 'lib/haml/buffer.rb', line 40

def xhtml?
  not html?
end