Class: Haml::Buffer
- Inherits:
-
Object
- Object
- Haml::Buffer
- Includes:
- Helpers, Util
- Defined in:
- /build/buildd/ruby-haml-3.1.4/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_eval
ed code.
Constant Summary
Constants included from Util
Instance Attribute Summary (collapse)
- - (Boolean) active writeonly
-
- (String) buffer
The string that holds the compiled HTML.
-
- (Fixnum?) capture_position
nil if there’s no capture_haml block running, and the position at which it’s beginning the capture if there is one.
-
- ({String => Object}) options
The options hash passed in from Engine.
-
- (Buffer) upper
The Buffer for the enclosing Haml document.
Class Method Summary (collapse)
-
+ ({String => String}) merge_attrs(to, from)
Merges two attribute hashes.
Instance Method Summary (collapse)
-
- (Boolean) active?
Whether or not this buffer is currently being used to render a Haml template.
-
- adjust_tabs(tab_change)
Modifies the indentation of the document.
- - attributes(class_id, obj_ref, *attributes_hashes)
-
- (Boolean) html4?
Whether or not the format is HTML4.
-
- (Boolean) html5?
Whether or not the format is HTML5.
-
- (Boolean) html?
Whether or not the format is any flavor of HTML.
-
- (Buffer) initialize(upper = nil, options = {})
constructor
A new instance of Buffer.
-
- push_text(text, tab_change, dont_tab_up)
Appends text to the buffer, properly tabulated.
-
- rstrip!
Remove the whitespace from the right side of the buffer string.
-
- (Fixnum) tabulation
The current indentation level of the document.
-
- tabulation=(val)
Sets the current tabulation of the document.
-
- (Boolean) toplevel?
Whether or not this buffer is a top-level template, as opposed to a nested partial.
-
- (Boolean) xhtml?
Whether or not the format is XHTML.
Methods included from Util
#abstract, #ap_geq?, #ap_geq_3?, #assert_html_safe!, #av_template_class, #caller_info, #check_encoding, #check_haml_encoding, #def_static_method, #dump, #enum_cons, #enum_slice, #enum_with_index, #flatten, #haml_warn, #has?, #html_safe, #inspect_obj, #intersperse, #ironruby?, #lcs, #load, #map_hash, #map_keys, #map_vals, #merge_adjacent_strings, #ord, #paths, #powerset, #rails_env, #rails_root, #rails_safe_buffer_class, #rails_xss_safe?, #restrict, #ruby1_8?, #ruby1_8_6?, #scope, #set_eql?, #set_hash, #silence_haml_warnings, #silence_warnings, #static_method_name, #strip_string_array, #substitute, #to_hash, #try_sass, #version_geq, #version_gt, #windows?
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, #succeed, #surround, #tab_down, #tab_up, #with_tabs
Methods included from Helpers::ActionViewExtensions
#page_class, #with_raw_haml_concat
Constructor Details
- (Buffer) initialize(upper = nil, options = {})
A new instance of Buffer
90 91 92 93 94 95 96 97 98 99 100 |
# File '/build/buildd/ruby-haml-3.1.4/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
- (Boolean) active=(value) (writeonly)
37 38 39 |
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/buffer.rb', line 37
def active=(value)
@active = value
end
|
- (String) buffer
The string that holds the compiled HTML. This is aliased as _erbout
for compatibility with ERB-specific code.
14 15 16 |
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/buffer.rb', line 14
def buffer
@buffer
end
|
- (Fixnum?) capture_position
nil if there’s no capture_haml block running, and the position at which it’s beginning the capture if there is one.
33 34 35 |
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/buffer.rb', line 33
def capture_position
@capture_position
end
|
- ({String => Object}) options
The options hash passed in from Engine.
20 21 22 |
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/buffer.rb', line 20
def options
@options
end
|
- (Buffer) upper
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?).
27 28 29 |
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/buffer.rb', line 27
def upper
@upper
end
|
Class Method Details
+ ({String => String}) merge_attrs(to, from)
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
.
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/buffer.rb', line 225
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['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
|
Instance Method Details
- (Boolean) active?
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.
70 71 72 |
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/buffer.rb', line 70
def active?
@active
end
|
- adjust_tabs(tab_change)
Modifies the indentation of the document.
126 127 128 |
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/buffer.rb', line 126
def adjust_tabs(tab_change)
@real_tabs += tab_change
end
|
- attributes(class_id, obj_ref, *attributes_hashes)
191 192 193 194 195 196 197 198 199 |
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/buffer.rb', line 191
def attributes(class_id, obj_ref, *attributes_hashes)
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
Compiler.build_attributes(
html?, @options[:attr_wrapper], @options[:escape_attrs], attributes)
end
|
- (Boolean) html4?
Whether or not the format is HTML4
50 51 52 |
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/buffer.rb', line 50
def html4?
@options[:format] == :html4
end
|
- (Boolean) html5?
Whether or not the format is HTML5.
55 56 57 |
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/buffer.rb', line 55
def html5?
@options[:format] == :html5
end
|
- (Boolean) html?
Whether or not the format is any flavor of HTML
45 46 47 |
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/buffer.rb', line 45
def html?
html4? or html5?
end
|
- push_text(text, tab_change, dont_tab_up)
Appends text to the buffer, properly tabulated. Also modifies the document’s indentation.
109 110 111 112 113 114 115 116 117 118 119 120 |
# File '/build/buildd/ruby-haml-3.1.4/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!
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.
203 204 205 206 207 208 209 210 |
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/buffer.rb', line 203
def rstrip!
if capture_position.nil?
buffer.rstrip!
return
end
buffer << buffer.slice!(capture_position..-1).rstrip
end
|
- (Fixnum) tabulation
The current indentation level of the document
75 76 77 |
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/buffer.rb', line 75
def tabulation
@real_tabs + @tabulation
end
|
- tabulation=(val)
Sets the current tabulation of the document.
82 83 84 85 |
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/buffer.rb', line 82
def tabulation=(val)
val = val - @real_tabs
@tabulation = val > -1 ? val : 0
end
|
- (Boolean) toplevel?
Whether or not this buffer is a top-level template, as opposed to a nested partial
61 62 63 |
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/buffer.rb', line 61
def toplevel?
upper.nil?
end
|
- (Boolean) xhtml?
Whether or not the format is XHTML
40 41 42 |
# File '/build/buildd/ruby-haml-3.1.4/lib/haml/buffer.rb', line 40
def xhtml?
not html?
end
|