Class: Hamlit::HamlBuffer
- Includes:
- HamlHelpers, HamlUtil
- Defined in:
- lib/hamlit/parser/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 HamlHelpers
Hamlit::HamlHelpers::HTML_ESCAPE, Hamlit::HamlHelpers::HTML_ESCAPE_ONCE_REGEX, Hamlit::HamlHelpers::HTML_ESCAPE_REGEX
Instance Attribute Summary collapse
- #active ⇒ Boolean writeonly
-
#buffer ⇒ String
The string that holds the compiled HTML.
-
#capture_position ⇒ Fixnum?
nil if there’s no capture_haml block running, and the position at which it’s beginning the capture if there is one.
-
#options ⇒ {String => Object}
The options hash passed in from HamlEngine.
-
#upper ⇒ Buffer
The Buffer for the enclosing Haml document.
Instance Method Summary collapse
-
#active? ⇒ Boolean
Whether or not this buffer is currently being used to render a Haml template.
-
#adjust_tabs(tab_change) ⇒ Object
Modifies the indentation of the document.
- #attributes(class_id, obj_ref, *attributes_hashes) ⇒ Object
-
#fix_textareas!(input) ⇒ Object
Works like #Hamlit::HamlHelpers#find_and_preserve, but allows the first newline after a preserved opening tag to remain unencoded, and then outdents the content.
-
#html4? ⇒ Boolean
Whether or not the format is HTML4.
-
#html5? ⇒ Boolean
Whether or not the format is HTML5.
-
#html? ⇒ Boolean
Whether or not the format is any flavor of HTML.
-
#initialize(upper = nil, options = {}) ⇒ HamlBuffer
constructor
A new instance of HamlBuffer.
-
#push_text(text, tab_change, dont_tab_up) ⇒ Object
Appends text to the buffer, properly tabulated.
-
#rstrip! ⇒ Object
Remove the whitespace from the right side of the buffer string.
-
#tabulation ⇒ Fixnum
The current indentation level of the document.
-
#tabulation=(val) ⇒ Object
Sets the current tabulation of the document.
-
#toplevel? ⇒ Boolean
Whether or not this buffer is a top-level template, as opposed to a nested partial.
-
#xhtml? ⇒ Boolean
Whether or not the format is XHTML.
Methods included from HamlUtil
#balance, #check_encoding, #check_haml_encoding, #contains_interpolation?, #handle_interpolation, #html_safe, #human_indentation, #inspect_obj, #rails_xss_safe?, #silence_warnings, #unescape_interpolation
Methods included from HamlHelpers
action_view?, #block_is_haml?, #capture_haml, #escape_once, #find_and_preserve, #haml_concat, #haml_indent, #haml_tag, #haml_tag_if, #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 Hamlit::HamlHelpers::XssMods
#capture_haml_with_haml_xss, #escape_once_with_haml_xss, #find_and_preserve_with_haml_xss, #haml_concat_with_haml_xss, #haml_indent_with_haml_xss, #html_escape_with_haml_xss, included, #list_of_with_haml_xss, #precede_with_haml_xss, #preserve_with_haml_xss, #succeed_with_haml_xss, #surround_with_haml_xss
Constructor Details
#initialize(upper = nil, options = {}) ⇒ HamlBuffer
Returns a new instance of HamlBuffer.
92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 92 def initialize(upper = nil, = {}) @active = true @upper = upper @options = HamlOptions.buffer_defaults @options = @options.merge() unless .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 |
Instance Attribute Details
#active=(value) ⇒ Boolean (writeonly)
39 40 41 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 39 def active=(value) @active = value end |
#buffer ⇒ String
The string that holds the compiled HTML. This is aliased as ‘_erbout` for compatibility with ERB-specific code.
16 17 18 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 16 def buffer @buffer end |
#capture_position ⇒ Fixnum?
nil if there’s no capture_haml block running, and the position at which it’s beginning the capture if there is one.
35 36 37 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 35 def capture_position @capture_position end |
#options ⇒ {String => Object}
The options hash passed in from Hamlit::HamlEngine.
22 23 24 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 22 def @options end |
#upper ⇒ 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?).
29 30 31 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 29 def upper @upper end |
Instance Method Details
#active? ⇒ Boolean
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.
72 73 74 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 72 def active? @active end |
#adjust_tabs(tab_change) ⇒ Object
Modifies the indentation of the document.
129 130 131 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 129 def adjust_tabs(tab_change) @real_tabs += tab_change end |
#attributes(class_id, obj_ref, *attributes_hashes) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 133 def attributes(class_id, obj_ref, *attributes_hashes) attributes = class_id attributes_hashes.each do |old| result = {} old.each { |k, v| result[k.to_s] = v } HamlAttributeBuilder.merge_attributes!(attributes, result) end HamlAttributeBuilder.merge_attributes!(attributes, parse_object_ref(obj_ref)) if obj_ref HamlAttributeBuilder.build_attributes( html?, @options[:attr_wrapper], @options[:escape_attrs], @options[:hyphenate_data_attrs], attributes) end |
#fix_textareas!(input) ⇒ Object
Works like #Hamlit::HamlHelpers#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.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 164 def fix_textareas!(input) return input unless input.include?('<textarea'.freeze) pattern = /<(textarea)([^>]*)>(\n|
)(.*?)<\/textarea>/im input.gsub!(pattern) do |s| match = pattern.match(s) content = match[4] if match[3] == '
' content.sub!(/\A /, ' ') else content.sub!(/\A[ ]*/, '') end "<#{match[1]}#{match[2]}>\n#{content}</#{match[1]}>" end input end |
#html4? ⇒ Boolean
Returns Whether or not the format is HTML4.
52 53 54 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 52 def html4? @options[:format] == :html4 end |
#html5? ⇒ Boolean
Returns Whether or not the format is HTML5.
57 58 59 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 57 def html5? @options[:format] == :html5 end |
#html? ⇒ Boolean
Returns Whether or not the format is any flavor of HTML.
47 48 49 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 47 def html? html4? or html5? end |
#push_text(text, tab_change, dont_tab_up) ⇒ Object
Appends text to the buffer, properly tabulated. Also modifies the document’s indentation.
112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/hamlit/parser/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! ⇒ Object
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.
147 148 149 150 151 152 153 154 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 147 def rstrip! if capture_position.nil? buffer.rstrip! return end buffer << buffer.slice!(capture_position..-1).rstrip end |
#tabulation ⇒ Fixnum
Returns The current indentation level of the document.
77 78 79 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 77 def tabulation @real_tabs + @tabulation end |
#tabulation=(val) ⇒ Object
Sets the current tabulation of the document.
84 85 86 87 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 84 def tabulation=(val) val = val - @real_tabs @tabulation = val > -1 ? val : 0 end |
#toplevel? ⇒ Boolean
Returns Whether or not this buffer is a top-level template, as opposed to a nested partial.
63 64 65 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 63 def toplevel? upper.nil? end |
#xhtml? ⇒ Boolean
Returns Whether or not the format is XHTML.
42 43 44 |
# File 'lib/hamlit/parser/haml_buffer.rb', line 42 def xhtml? not html? end |