class Haml::Buffer

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_eval`ed code.

Attributes

active[W]

@return [Boolean] @see active?

buffer[RW]

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

@return [String]

capture_position[RW]

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

@return [Fixnum, nil]

options[RW]

The options hash passed in from {Haml::Engine}.

@return [{String => Object}] @see Haml::Options#for_buffer

upper[RW]

The {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?}).

@return [Buffer]

Public Class Methods

new(upper = nil, options = {}) click to toggle source

@param upper [Buffer] The parent buffer @param options [{Symbol => Object}] An options hash.

See {Haml::Engine#options\_for\_buffer}
# File lib/haml/buffer.rb, line 92
def initialize(upper = nil, options = {})
  @active     = true
  @upper      = upper
  @options    = Options.buffer_defaults
  @options    = @options.merge(options) unless options.empty?
  @buffer     = new_encoded_string
  @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

Public Instance Methods

active?() click to toggle source

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.

@return [Boolean]

# File lib/haml/buffer.rb, line 72
def active?
  @active
end
adjust_tabs(tab_change) click to toggle source

Modifies the indentation of the document.

@param tab_change [Fixnum] The number of tabs by which to increase

or decrease the document's indentation
# File lib/haml/buffer.rb, line 129
def adjust_tabs(tab_change)
  @real_tabs += tab_change
end
fix_textareas!(input) click to toggle source

Works like #{find_and_preserve}, but allows the first newline after a preserved opening tag to remain unencoded, and then outdents the content. This change was motivated primarily by the change in Rails 3.2.3 to emit a newline after textarea helpers.

@param input [String] The text to process @since Haml 4.0.1 @private

# File lib/haml/buffer.rb, line 152
def fix_textareas!(input)
  return input unless input.include?('<textarea'.freeze)

  pattern = /<(textarea)([^>]*)>(\n|&#x000A;)(.*?)<\/textarea>/im
  input.gsub!(pattern) do |s|
    match = pattern.match(s)
    content = match[4]
    if match[3] == '&#x000A;'
      content.sub!(/\A /, '&#x0020;')
    else
      content.sub!(/\A[ ]*/, '')
    end
    "<#{match[1]}#{match[2]}>\n#{content}</#{match[1]}>"
  end
  input
end
html4?() click to toggle source

@return [Boolean] Whether or not the format is HTML4

# File lib/haml/buffer.rb, line 52
def html4?
  @options[:format] == :html4
end
html5?() click to toggle source

@return [Boolean] Whether or not the format is HTML5.

# File lib/haml/buffer.rb, line 57
def html5?
  @options[:format] == :html5
end
html?() click to toggle source

@return [Boolean] Whether or not the format is any flavor of HTML

# File lib/haml/buffer.rb, line 47
def html?
  html4? or html5?
end
push_text(text, tab_change, dont_tab_up) click to toggle source

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

@param text [String] The text to append @param tab_change [Fixnum] The number of tabs by which to increase

or decrease the document's indentation

@param dont_tab_up [Boolean] If true, don’t indent the first line of ‘text`

# File lib/haml/buffer.rb, line 112
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

  @real_tabs += tab_change
  @buffer << text
end
rstrip!() click to toggle source

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.

# File lib/haml/buffer.rb, line 135
def rstrip!
  if capture_position.nil?
    buffer.rstrip!
    return
  end

  buffer << buffer.slice!(capture_position..-1).rstrip
end
tabulation() click to toggle source

@return [Fixnum] The current indentation level of the document

# File lib/haml/buffer.rb, line 77
def tabulation
  @real_tabs + @tabulation
end
tabulation=(val) click to toggle source

Sets the current tabulation of the document.

@param val [Fixnum] The new tabulation

# File lib/haml/buffer.rb, line 84
def tabulation=(val)
  val = val - @real_tabs
  @tabulation = val > -1 ? val : 0
end
toplevel?() click to toggle source

@return [Boolean] Whether or not this buffer is a top-level template,

as opposed to a nested partial
# File lib/haml/buffer.rb, line 63
def toplevel?
  upper.nil?
end
xhtml?() click to toggle source

@return [Boolean] Whether or not the format is XHTML

# File lib/haml/buffer.rb, line 42
def xhtml?
  not html?
end

Private Instance Methods

new_encoded_string() click to toggle source
# File lib/haml/buffer.rb, line 171
def new_encoded_string
  "".encode(options[:encoding])
end
tabs(count = 0) click to toggle source

Gets ‘count` tabs. Mostly for internal use.

# File lib/haml/buffer.rb, line 177
def tabs(count = 0)
  tabs = [count + @tabulation, 0].max
  @@tab_cache[tabs] ||= '  ' * tabs
end