Module | Haml::HTML::Node |
In: |
lib/haml/html.rb
|
A module containing utility methods that every Hpricot node should have.
converted_to_haml | [RW] |
Whether this node has already been converted to Haml. Only used for text nodes and elements.
@return [Boolean] |
Returns the Haml representation of the given node.
@param tabs [Fixnum] The indentation level of the resulting Haml. @option options (see Haml::HTML#initialize)
# File lib/haml/html.rb, line 22 22: def to_haml(tabs, options) 23: return "" if converted_to_haml || to_s.strip.empty? 24: text = uninterp(self.to_s) 25: node = next_node 26: while node.is_a?(::Hpricot::Elem) && node.name == "haml:loud" 27: node.converted_to_haml = true 28: text << '#{' << 29: CGI.unescapeHTML(node.inner_text).gsub(/\n\s*/, ' ').strip << '}' 30: 31: if node.next_node.is_a?(::Hpricot::Text) 32: node = node.next_node 33: text << uninterp(node.to_s) 34: node.converted_to_haml = true 35: end 36: 37: node = node.next_node 38: end 39: return parse_text_with_interpolation(text, tabs) 40: end
# File lib/haml/html.rb, line 44 44: def erb_to_interpolation(text, options) 45: return text unless options[:erb] 46: text = CGI.escapeHTML(uninterp(text)) 47: %w[<haml:loud> </haml:loud>].each {|str| text.gsub!(CGI.escapeHTML(str), str)} 48: ::Hpricot::XML(text).children.inject("") do |str, elem| 49: if elem.is_a?(::Hpricot::Text) 50: str + CGI.unescapeHTML(elem.to_s) 51: else # <haml:loud> element 52: str + '#{' + CGI.unescapeHTML(elem.innerText.strip) + '}' 53: end 54: end 55: end
# File lib/haml/html.rb, line 69 69: def parse_text(text, tabs) 70: parse_text_with_interpolation(uninterp(text), tabs) 71: end
# File lib/haml/html.rb, line 73 73: def parse_text_with_interpolation(text, tabs) 74: text.strip! 75: return "" if text.empty? 76: 77: text.split("\n").map do |line| 78: line.strip! 79: "#{tabulate(tabs)}#{'\\' if Haml::Engine::SPECIAL_CHARACTERS.include?(line[0])}#{line}\n" 80: end.join 81: end