class Haml::Buffer

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

def self.merge_attrs(to, from)

Returns:
  • ({String => String}) - `to`, after being merged

Parameters:
  • from ({String => #to_s}) -- The attribute hash to merge from
  • to ({String => String}) -- The attribute hash to merge into
def self.merge_attrs(to, from)
  from['id'] = Precompiler.filter_and_join(from['id'], '_') if from['id']
  if to['id'] && from['id']
    to['id'] << '_' << from.delete('id').to_s
  elsif to['id'] || from['id']
    from['id'] ||= to['id']
  end
  from['class'] = Precompiler.filter_and_join(from['class'], ' ') if from['class']
  if to['class'] && from['class']
    # Make sure we don't duplicate class names
    from['class'] = (from['class'].to_s.split(' ') | to['class'].split(' ')).sort.join(' ')
  elsif to['class'] || from['class']
    from['class'] ||= to['class']
  end
  from_data = from['data'].is_a?(Hash)
  to_data = to['data'].is_a?(Hash)
  if from_data && to_data
    to['data'] = to['data'].merge(from['data'])
  elsif to_data
    to = Haml::Util.map_keys(to.delete('data')) {|name| "data-#{name}"}.merge(to)
  elsif from_data
    from = Haml::Util.map_keys(from.delete('data')) {|name| "data-#{name}"}.merge(from)
  end
  to.merge!(from)
end

def active?

Returns:
  • (Boolean) -
def active?
  @active
end

def adjust_tabs(tab_change)

Parameters:
  • tab_change (Fixnum) -- The number of tabs by which to increase
def adjust_tabs(tab_change)
  @real_tabs += tab_change
end

def append_if_string=(value)

def append_if_string=(value)
  if value.is_a?(String) && !value.is_a?(ActionView::NonConcattingString)
    ActiveSupport::Deprecation.warn("- style block helpers are deprecated. Please use =", caller)
    buffer << value
  end
end

def html4?

Returns:
  • (Boolean) - Whether or not the format is HTML4
def html4?
  @options[:format] == :html4
end

def html5?

Returns:
  • (Boolean) - Whether or not the format is HTML5.
def html5?
  @options[:format] == :html5
end

def html?

Returns:
  • (Boolean) - Whether or not the format is any flavor of HTML
def html?
  html4? or html5?
end

def initialize(upper = nil, options = {})

Parameters:
  • options ({Symbol => Object}) -- An options hash.
  • upper (Buffer) -- The parent buffer
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

def open_tag(name, self_closing, try_one_line, preserve_tag, escape_html, class_id,

formats it, and appends it to the buffer.
Takes the various information about the opening tag for an element,
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

def parse_object_ref(ref)

just like you can do with `dom_id()` and `dom_class()` in Rails
The second object, if present, is used as a prefix,
one to create an attributes hash.
Takes an array of objects and uses the class and id of the first
def parse_object_ref(ref)
  prefix = ref[1]
  ref = ref[0]
  # Let's make sure the value isn't nil. If it is, return the default Hash.
  return {} if ref.nil?
  class_name =
    if ref.respond_to?(:haml_object_ref)
      ref.haml_object_ref
    else
      underscore(ref.class)
    end
  id = "#{class_name}_#{ref.id || 'new'}"
  if prefix
    class_name = "#{ prefix }_#{ class_name}"
    id = "#{ prefix }_#{ id }"
  end
  {'id' => id, 'class' => class_name}
end

def push_text(text, tab_change, dont_tab_up)

Parameters:
  • dont_tab_up (Boolean) -- If true, don't indent the first line of `text`
  • tab_change (Fixnum) -- The number of tabs by which to increase
  • text (String) -- The text to append
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

def rstrip!

Doesn't do anything if we're at the beginning of a capture_haml block.
Remove the whitespace from the right side of the buffer string.
def rstrip!
  if capture_position.nil?
    buffer.rstrip!
    return
  end
  buffer << buffer.slice!(capture_position..-1).rstrip
end

def tabs(count = 0)

Gets `count` tabs. Mostly for internal use.
def tabs(count = 0)
  tabs = [count + @tabulation, 0].max
  @@tab_cache[tabs] ||= '  ' * tabs
end

def tabulation

Returns:
  • (Fixnum) - The current indentation level of the document
def tabulation
  @real_tabs + @tabulation
end

def tabulation=(val)

Parameters:
  • val (Fixnum) -- The new tabulation
def tabulation=(val)
  val = val - @real_tabs
  @tabulation = val > -1 ? val : 0
end

def toplevel?

Returns:
  • (Boolean) - Whether or not this buffer is a top-level template,
def toplevel?
  upper.nil?
end

def underscore(camel_cased_word)

but copied here so it'll run properly without Rails.
Based on the method of the same name in Rails' Inflector,
Changes a word from camel case to underscores.
def underscore(camel_cased_word)
  camel_cased_word.to_s.gsub(/::/, '_').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr("-", "_").
    downcase
end

def xhtml?

Returns:
  • (Boolean) - Whether or not the format is XHTML
def xhtml?
  not html?
end