| Module | Haml::HTML::Node |
| In: |
lib/haml/html.rb
|
A module containing utility methods that every Hpricot node should have.
We have to do everything in `included` rather than including the methods in the module itself because if we do that, they don‘t propagate to the already-defined subclasses of the modules including this.
# File lib/haml/html.rb, line 17
17: def self.included(base)
18: base.class_eval do
19: # Whether this node has already been converted to Haml.
20: # Only used for text nodes and elements.
21: #
22: # @return [Boolean]
23: attr_accessor :converted_to_haml
24:
25: # Returns the Haml representation of the given node.
26: #
27: # @param tabs [Fixnum] The indentation level of the resulting Haml.
28: # @option options (see Haml::HTML#initialize)
29: def to_haml(tabs, options)
30: return "" if converted_to_haml || to_s.strip.empty?
31: text = uninterp(self.to_s)
32: node = next_node
33: while node.is_a?(::Hpricot::Elem) && node.name == "haml:loud"
34: node.converted_to_haml = true
35: text << '#{' <<
36: CGI.unescapeHTML(node.inner_text).gsub(/\n\s*/, ' ').strip << '}'
37:
38: if node.next_node.is_a?(::Hpricot::Text)
39: node = node.next_node
40: text << uninterp(node.to_s)
41: node.converted_to_haml = true
42: end
43:
44: node = node.next_node
45: end
46: return parse_text_with_interpolation(text, tabs)
47: end
48:
49: private
50:
51: def erb_to_interpolation(text, options)
52: return text unless options[:erb]
53: text = CGI.escapeHTML(uninterp(text))
54: %w[<haml:loud> </haml:loud>].each {|str| text.gsub!(CGI.escapeHTML(str), str)}
55: ::Hpricot::XML(text).children.inject("") do |str, elem|
56: if elem.is_a?(::Hpricot::Text)
57: str + CGI.unescapeHTML(elem.to_s)
58: else # <haml:loud> element
59: str + '#{' + CGI.unescapeHTML(elem.innerText.strip) + '}'
60: end
61: end
62: end
63:
64: def tabulate(tabs)
65: ' ' * tabs
66: end
67:
68: def uninterp(text)
69: text.gsub('#{', '\#{') #'
70: end
71:
72: def attr_hash
73: attributes.to_hash
74: end
75:
76: def parse_text(text, tabs)
77: parse_text_with_interpolation(uninterp(text), tabs)
78: end
79:
80: def parse_text_with_interpolation(text, tabs)
81: text.strip!
82: return "" if text.empty?
83:
84: text.split("\n").map do |line|
85: line.strip!
86: "#{tabulate(tabs)}#{'\\' if Haml::Engine::SPECIAL_CHARACTERS.include?(line[0])}#{line}\n"
87: end.join
88: end
89: end
90: end
# File lib/haml/html.rb, line 51
51: def erb_to_interpolation(text, options)
52: return text unless options[:erb]
53: text = CGI.escapeHTML(uninterp(text))
54: %w[<haml:loud> </haml:loud>].each {|str| text.gsub!(CGI.escapeHTML(str), str)}
55: ::Hpricot::XML(text).children.inject("") do |str, elem|
56: if elem.is_a?(::Hpricot::Text)
57: str + CGI.unescapeHTML(elem.to_s)
58: else # <haml:loud> element
59: str + '#{' + CGI.unescapeHTML(elem.innerText.strip) + '}'
60: end
61: end
62: end
# File lib/haml/html.rb, line 76
76: def parse_text(text, tabs)
77: parse_text_with_interpolation(uninterp(text), tabs)
78: end
# File lib/haml/html.rb, line 80
80: def parse_text_with_interpolation(text, tabs)
81: text.strip!
82: return "" if text.empty?
83:
84: text.split("\n").map do |line|
85: line.strip!
86: "#{tabulate(tabs)}#{'\\' if Haml::Engine::SPECIAL_CHARACTERS.include?(line[0])}#{line}\n"
87: end.join
88: end
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 29
29: def to_haml(tabs, options)
30: return "" if converted_to_haml || to_s.strip.empty?
31: text = uninterp(self.to_s)
32: node = next_node
33: while node.is_a?(::Hpricot::Elem) && node.name == "haml:loud"
34: node.converted_to_haml = true
35: text << '#{' <<
36: CGI.unescapeHTML(node.inner_text).gsub(/\n\s*/, ' ').strip << '}'
37:
38: if node.next_node.is_a?(::Hpricot::Text)
39: node = node.next_node
40: text << uninterp(node.to_s)
41: node.converted_to_haml = true
42: end
43:
44: node = node.next_node
45: end
46: return parse_text_with_interpolation(text, tabs)
47: end