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.
The string that holds the compiled HTML. This is aliased as `_erbout` for compatibility with ERB-specific code.
@return [String]
The options hash passed in from {Haml::Engine}.
@return [{String => Object}] @see Haml::Engine#options_for_buffer
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]
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 => #}] The attribute hash to merge from @return [{String => String}] `to`, after being merged
# File lib/haml/buffer.rb, line 228 228: def self.merge_attrs(to, from) 229: from['id'] = Compiler.filter_and_join(from['id'], '_') if from['id'] 230: if to['id'] && from['id'] 231: to['id'] << '_' << from.delete('id').to_s 232: elsif to['id'] || from['id'] 233: from['id'] ||= to['id'] 234: end 235: 236: from['class'] = Compiler.filter_and_join(from['class'], ' ') if from['class'] 237: if to['class'] && from['class'] 238: # Make sure we don't duplicate class names 239: from['class'] = (from['class'].to_s.split(' ') | to['class'].split(' ')).sort.join(' ') 240: elsif to['class'] || from['class'] 241: from['class'] ||= to['class'] 242: end 243: 244: from_data = from['data'].is_a?(Hash) 245: to_data = to['data'].is_a?(Hash) 246: if from_data && to_data 247: to['data'] = to['data'].merge(from['data']) 248: elsif to_data 249: to = Haml::Util.map_keys(to.delete('data')) {|name| "data-#{name}"}.merge(to) 250: elsif from_data 251: from = Haml::Util.map_keys(from.delete('data')) {|name| "data-#{name}"}.merge(from) 252: end 253: 254: to.merge!(from) 255: end
@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 90: def initialize(upper = nil, options = {}) 91: @active = true 92: @upper = upper 93: @options = options 94: @buffer = ruby1_8? ? "" : "".encode(Encoding.find(options[:encoding])) 95: @tabulation = 0 96: 97: # The number of tabs that Engine thinks we should have 98: # @real_tabs + @tabulation is the number of tabs actually output 99: @real_tabs = 0 100: 101: @preserve_pattern = /<[\s]*#{@options[:preserve].join("|")}/ 102: end
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 70: def active? 71: @active 72: end
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 128 128: def adjust_tabs(tab_change) 129: @real_tabs += tab_change 130: end
# File lib/haml/template/plugin.rb, line 73 73: def append_if_string=(value) 74: if value.is_a?(String) && !value.is_a?(ActionView::NonConcattingString) 75: ActiveSupport::Deprecation.warn("- style block helpers are deprecated. Please use =", caller) 76: buffer << value 77: end 78: end
# File lib/haml/buffer.rb, line 194 194: def attributes(class_id, obj_ref, *attributes_hashes) 195: attributes = class_id 196: attributes_hashes.each do |old| 197: self.class.merge_attrs(attributes, to_hash(old.map {|k, v| [k.to_s, v]})) 198: end 199: self.class.merge_attrs(attributes, parse_object_ref(obj_ref)) if obj_ref 200: Compiler.build_attributes( 201: html?, @options[:attr_wrapper], @options[:escape_attrs], attributes) 202: end
@return [Boolean] Whether or not the format is HTML4
# File lib/haml/buffer.rb, line 50 50: def html4? 51: @options[:format] == :html4 52: end
@return [Boolean] Whether or not the format is HTML5.
# File lib/haml/buffer.rb, line 55 55: def html5? 56: @options[:format] == :html5 57: end
@return [Boolean] Whether or not the format is any flavor of HTML
# File lib/haml/buffer.rb, line 45 45: def html? 46: html4? or html5? 47: end
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 111 111: def push_text(text, tab_change, dont_tab_up) 112: if @tabulation > 0 113: # Have to push every line in by the extra user set tabulation. 114: # Don't push lines with just whitespace, though, 115: # because that screws up precompiled indentation. 116: text.gsub!(/^(?!\s+$)/, tabs) 117: text.sub!(tabs, '') if dont_tab_up 118: end 119: 120: @buffer << text 121: @real_tabs += tab_change 122: end
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 206 206: def rstrip! 207: if capture_position.nil? 208: buffer.rstrip! 209: return 210: end 211: 212: buffer << buffer.slice!(capture_position..1).rstrip 213: end
@return [Fixnum] The current indentation level of the document
# File lib/haml/buffer.rb, line 75 75: def tabulation 76: @real_tabs + @tabulation 77: end
Sets the current tabulation of the document.
@param val [Fixnum] The new tabulation
# File lib/haml/buffer.rb, line 82 82: def tabulation=(val) 83: val = val - @real_tabs 84: @tabulation = val > 1 ? val : 0 85: end
Takes an array of objects and uses the class and id of the first one to create an attributes hash. The second object, if present, is used as a prefix, just like you can do with `dom_id()` and `dom_class()` in Rails
# File lib/haml/buffer.rb, line 270 270: def parse_object_ref(ref) 271: prefix = ref[1] 272: ref = ref[0] 273: # Let's make sure the value isn't nil. If it is, return the default Hash. 274: return {} if ref.nil? 275: class_name = 276: if ref.respond_to?(:haml_object_ref) 277: ref.haml_object_ref 278: else 279: underscore(ref.class) 280: end 281: id = "#{class_name}_#{ref.id || 'new'}" 282: if prefix 283: class_name = "#{ prefix }_#{ class_name}" 284: id = "#{ prefix }_#{ id }" 285: end 286: 287: {'id' => id, 'class' => class_name} 288: end
Gets `count` tabs. Mostly for internal use.
# File lib/haml/buffer.rb, line 261 261: def tabs(count = 0) 262: tabs = [count + @tabulation, 0].max 263: @@tab_cache[tabs] ||= ' ' * tabs 264: end
Changes a word from camel case to underscores. Based on the method of the same name in Rails’ Inflector, but copied here so it’ll run properly without Rails.
# File lib/haml/buffer.rb, line 293 293: def underscore(camel_cased_word) 294: camel_cased_word.to_s.gsub(/::/, '_'). 295: gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). 296: gsub(/([a-z\d])([A-Z])/,'\1_\2'). 297: tr("-", "_"). 298: downcase 299: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.