Class: Haml::Buffer

### Home Classes Methods

### In Files

- lib/haml/buffer.rb

### Parent

Object

### Methods

- ::merge_attrs
- ::new
- #active?
- #adjust_tabs
- #attributes
- #html4?
- #html5?
- #html?
- #push_text
- #rstrip!
- #tabulation
- #tabulation=
- #toplevel?
- #xhtml?

### Included Modules

- Haml::Helpers
- Haml::Util

### Class/Module Index

Quicksearch

- Haml
- Haml::Buffer
- Haml::Compiler
- Haml::Engine
- Haml::Error
- Haml::ErubisTemplateHandler
- Haml::Exec
- Haml::Exec::Generic
- Haml::Exec::Haml
- Haml::Filters
- Haml::Filters::Base
- Haml::Filters::Cdata
- Haml::Filters::Css
- Haml::Filters::Erb
- Haml::Filters::Escaped
- Haml::Filters::Javascript
- Haml::Filters::Plain
- Haml::Filters::PrecompiledTiltFilter
- Haml::Filters::Preserve
- Haml::Filters::Ruby
- Haml::Filters::SassRailsTemplate
- Haml::Filters::ScssRailsTemplate
- Haml::Filters::TiltFilter
- Haml::Helpers
- Haml::Helpers::ActionViewExtensions
- Haml::Helpers::ErrorReturn
- Haml::Helpers::XssMods
- Haml::Options
- Haml::Parser
- Haml::Parser::Line
- Haml::Parser::ParseNode
- Haml::Plugin
- Haml::Railtie
- Haml::SafeErubisTemplate
- Haml::SyntaxError
- Haml::Template
- Haml::Util
- Haml::Util::StaticConditionalContext
- ActionView
- ActionView::Base
- ActionView::Helpers
- ActionView::Helpers::CaptureHelper
- ActionView::Helpers::FormHelper
- ActionView::Helpers::FormTagHelper
- ActionView::Helpers::HamlSupport
- ActionView::Helpers::InstanceTag
- ActionView::Helpers::TagHelper
- ActionView::Helpers::Tags
- ActionView::Helpers::Tags::TextArea
- ActionView::Helpers::TextHelper
- Object

No matching classes.

# 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

merge_attrs(to, from) click to toggle source

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

ids are concatenated with `“_”`, and classes are concatenated with `“ ”`. data hashes are simply merged.

Destructively modifies both `to` and `from`.

@param to [{String => String}] The attribute hash to merge into @param from [{String => to_s}] The attribute hash to merge from @return [{String => String}] `to`, after being merged

    # File lib/haml/buffer.rb, line 233
    def self.merge_attrs(to, from)
      from['id'] = Compiler.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'] = Compiler.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.delete('data') || {}
      to_data = to.delete('data') || {}

      # forces to_data & from_data into a hash
      from_data = { nil => from_data } unless from_data.is_a?(Hash)
      to_data = { nil => to_data } unless to_data.is_a?(Hash)

      merged_data = to_data.merge(from_data)

      to['data'] = merged_data unless merged_data.empty?
      to.merge!(from)
    end

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 90
    def initialize(upper = nil, options = {})
      @active     = true
      @upper      = upper
      @options    = options
      @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 70
    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 126
    def adjust_tabs(tab_change)
      @real_tabs += tab_change
    end

attributes(class_id, obj_ref, *attributes_hashes) click to toggle source

    # File lib/haml/buffer.rb, line 199
    def attributes(class_id, obj_ref, *attributes_hashes)
      attributes = class_id
      attributes_hashes.each do |old|
        self.class.merge_attrs(attributes, Hash[old.map {|k, v| [k.to_s, v]}])
      end
      self.class.merge_attrs(attributes, parse_object_ref(obj_ref)) if obj_ref
      Compiler.build_attributes(
        html?, @options[:attr_wrapper], @options[:escape_attrs], @options[:hyphenate_data_attrs], attributes)
    end

html4?() click to toggle source

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

    # File lib/haml/buffer.rb, line 50
    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 55
    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 45
    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 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+$)/, tabs)
        text.sub!(tabs, '') if dont_tab_up
      end

      @buffer << text
      @real_tabs += tab_change
    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 211
    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 75
    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 82
    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 61
    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 40
    def xhtml?
      not html?
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.