Class: Haml::Buffer
- Includes:
- Helpers
- Defined in:
- lib/gems/haml-2.0.4/lib/haml/buffer.rb
Overview
This class is used only internally. It holds the buffer of XHTML that is eventually output by Haml::Engine’s to_html method. It’s called from within the precompiled code, and helps reduce the amount of processing done within instance_eval’d code.
Constant Summary
Constants included from Helpers
Instance Attribute Summary collapse
-
#active ⇒ Object
writeonly
See #active?.
-
#buffer ⇒ Object
The string that holds the compiled XHTML.
-
#options ⇒ Object
The options hash passed in from Haml::Engine.
-
#upper ⇒ Object
The Buffer for the enclosing Haml document.
Class Method Summary collapse
Instance Method Summary collapse
-
#active? ⇒ Boolean
True if this buffer is currently being used to render a Haml template.
-
#html4? ⇒ Boolean
True if the format is HTML4.
-
#html5? ⇒ Boolean
True if the format is HTML5.
-
#html? ⇒ Boolean
True if the format is any flavor of HTML.
-
#initialize(upper = nil, options = {}) ⇒ Buffer
constructor
Creates a new buffer.
-
#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) ⇒ Object
Takes the various information about the opening tag for an element, formats it, and adds it to the buffer.
-
#push_script(result, preserve_script, in_tag = false, preserve_tag = false, escape_html = false, nuke_inner_whitespace = false) ⇒ Object
Properly formats the output of a script that was run in the instance_eval.
-
#push_text(text, dont_tab_up = false, tab_change = 0) ⇒ Object
Renders
text
with the proper tabulation. -
#tabulation ⇒ Object
Gets the current tabulation of the document.
-
#tabulation=(val) ⇒ Object
Sets the current tabulation of the document.
-
#toplevel? ⇒ Boolean
True if this buffer is a top-level template, as opposed to a nested partial.
-
#xhtml? ⇒ Boolean
True if the format is XHTML.
Methods included from Helpers
action_view?, #capture_haml, #escape_once, #find_and_preserve, #haml_concat, #haml_tag, #html_attrs, #html_escape, #init_haml_helpers, #is_haml?, #list_of, #non_haml, #precede, #preserve, #puts, #succeed, #surround, #tab_down, #tab_up
Methods included from Helpers::ActionViewExtensions
Constructor Details
#initialize(upper = nil, options = {}) ⇒ Buffer
Creates a new buffer.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/gems/haml-2.0.4/lib/haml/buffer.rb', line 69 def initialize(upper = nil, = {}) @active = true @upper = upper @options = { :attr_wrapper => "'", :ugly => false, :format => :xhtml }.merge @buffer = "" @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
#active=(value) ⇒ Object (writeonly)
See #active?
22 23 24 |
# File 'lib/gems/haml-2.0.4/lib/haml/buffer.rb', line 22 def active=(value) @active = value end |
#buffer ⇒ Object
The string that holds the compiled XHTML. This is aliased as _erbout for compatibility with ERB-specific code.
11 12 13 |
# File 'lib/gems/haml-2.0.4/lib/haml/buffer.rb', line 11 def buffer @buffer end |
#options ⇒ Object
The options hash passed in from Haml::Engine.
14 15 16 |
# File 'lib/gems/haml-2.0.4/lib/haml/buffer.rb', line 14 def @options end |
#upper ⇒ Object
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?).
19 20 21 |
# File 'lib/gems/haml-2.0.4/lib/haml/buffer.rb', line 19 def upper @upper end |
Class Method Details
.merge_attrs(to, from) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/gems/haml-2.0.4/lib/haml/buffer.rb', line 180 def self.merge_attrs(to, from) if to['id'] && from['id'] to['id'] << '_' << from.delete('id') elsif to['id'] || from['id'] from['id'] ||= to['id'] end if to['class'] && from['class'] # Make sure we don't duplicate class names from['class'] = (from['class'].split(' ') | to['class'].split(' ')).join(' ') elsif to['class'] || from['class'] from['class'] ||= to['class'] end to.merge!(from) end |
Instance Method Details
#active? ⇒ Boolean
True if this buffer is currently being used to render a Haml template. However, this returns false if a subtemplate is being rendered, even if it’s a subtemplate of this buffer’s template.
53 54 55 |
# File 'lib/gems/haml-2.0.4/lib/haml/buffer.rb', line 53 def active? @active end |
#html4? ⇒ Boolean
True if the format is HTML4
35 36 37 |
# File 'lib/gems/haml-2.0.4/lib/haml/buffer.rb', line 35 def html4? @options[:format] == :html4 end |
#html5? ⇒ Boolean
True if the format is HTML5
40 41 42 |
# File 'lib/gems/haml-2.0.4/lib/haml/buffer.rb', line 40 def html5? @options[:format] == :html5 end |
#html? ⇒ Boolean
True if the format is any flavor of HTML
30 31 32 |
# File 'lib/gems/haml-2.0.4/lib/haml/buffer.rb', line 30 def html? html4? or html5? end |
#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) ⇒ Object
Takes the various information about the opening tag for an element, formats it, and adds it to the buffer.
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/gems/haml-2.0.4/lib/haml/buffer.rb', line 149 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, old.inject({}) {|h, (key, val)| h[key.to_s] = val; h}) 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 |
#push_script(result, preserve_script, in_tag = false, preserve_tag = false, escape_html = false, nuke_inner_whitespace = false) ⇒ Object
Properly formats the output of a script that was run in the instance_eval.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/gems/haml-2.0.4/lib/haml/buffer.rb', line 103 def push_script(result, preserve_script, in_tag = false, preserve_tag = false, escape_html = false, nuke_inner_whitespace = false) tabulation = @real_tabs result = result.to_s.rstrip result = html_escape(result) if escape_html if preserve_tag result = Haml::Helpers.preserve(result) elsif preserve_script result = Haml::Helpers.find_and_preserve(result, [:preserve]) end has_newline = result.include?("\n") if in_tag && !nuke_inner_whitespace && (@options[:ugly] || !has_newline || preserve_tag) @buffer << result @real_tabs -= 1 return end @buffer << "\n" if in_tag && !nuke_inner_whitespace # Precompiled tabulation may be wrong if @tabulation > 0 && !in_tag result = tabs + result end if has_newline && !@options[:ugly] result = result.gsub "\n", "\n" + tabs(tabulation) # Add tabulation if it wasn't precompiled result = tabs(tabulation) + result if in_tag && !nuke_inner_whitespace end @buffer << "#{result}" @buffer << "\n" unless nuke_inner_whitespace if in_tag && !nuke_inner_whitespace # We never get here if @options[:ugly] is true @buffer << tabs(tabulation-1) @real_tabs -= 1 end nil end |
#push_text(text, dont_tab_up = false, tab_change = 0) ⇒ Object
Renders text
with the proper tabulation. This also deals with making a possible one-line tag one line or not.
87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/gems/haml-2.0.4/lib/haml/buffer.rb', line 87 def push_text(text, dont_tab_up = false, tab_change = 0) if @tabulation > 0 && !@options[:ugly] # 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 @dont_tab_up_next_line = false end |
#tabulation ⇒ Object
Gets the current tabulation of the document.
58 59 60 |
# File 'lib/gems/haml-2.0.4/lib/haml/buffer.rb', line 58 def tabulation @real_tabs + @tabulation end |
#tabulation=(val) ⇒ Object
Sets the current tabulation of the document.
63 64 65 66 |
# File 'lib/gems/haml-2.0.4/lib/haml/buffer.rb', line 63 def tabulation=(val) val = val - @real_tabs @tabulation = val > -1 ? val : 0 end |
#toplevel? ⇒ Boolean
True if this buffer is a top-level template, as opposed to a nested partial.
46 47 48 |
# File 'lib/gems/haml-2.0.4/lib/haml/buffer.rb', line 46 def toplevel? upper.nil? end |
#xhtml? ⇒ Boolean
True if the format is XHTML
25 26 27 |
# File 'lib/gems/haml-2.0.4/lib/haml/buffer.rb', line 25 def xhtml? not html? end |