| Class | Hpricot::Elem |
| In: |
lib/haml/html.rb
|
| Parent: | Object |
@see Hpricot
@see Haml::HTML::Node#to_haml
# File lib/haml/html.rb, line 173
173: def to_haml(tabs, options)
174: output = "#{tabulate(tabs)}"
175: if options[:rhtml] && name[0...5] == 'haml:'
176: return output + send("haml_tag_#{name[5..-1]}", CGI.unescapeHTML(self.inner_text))
177: end
178:
179: output += "%#{name}" unless name == 'div' &&
180: (static_id?(options) || static_classname?(options))
181:
182: if attr_hash
183: if static_id?(options)
184: output += "##{attr_hash['id']}"
185: remove_attribute('id')
186: end
187: if static_classname?(options)
188: attr_hash['class'].split(' ').each { |c| output += ".#{c}" }
189: remove_attribute('class')
190: end
191: output += haml_attributes(options) if attr_hash.length > 0
192: end
193:
194: (self.children || []).inject(output + "\n") do |output, child|
195: output + child.to_haml(tabs + 1, options)
196: end
197: end
# File lib/haml/html.rb, line 228
228: def dynamic_attribute?(name, options)
229: options[:rhtml] and dynamic_attributes.key?(name)
230: end
# File lib/haml/html.rb, line 201
201: def dynamic_attributes
202: @dynamic_attributes ||= begin
203: Haml::Util.map_hash(attr_hash) do |name, value|
204: next if value.empty?
205: full_match = nil
206: ruby_value = value.gsub(%r{<haml:loud>\s*(.+?)\s*</haml:loud>}) do
207: full_match = $`.empty? && $'.empty?
208: CGI.unescapeHTML(full_match ? $1: "\#{#{$1}}")
209: end
210: next if ruby_value == value
211: [name, full_match ? ruby_value : %("#{ruby_value}")]
212: end
213: end
214: end
Returns a string representation of an attributes hash that‘s prettier than that produced by Hash#inspect
# File lib/haml/html.rb, line 242
242: def haml_attributes(options)
243: attrs = attr_hash.map do |name, value|
244: value = dynamic_attribute?(name, options) ? dynamic_attributes[name] : value.inspect
245: name = name.index(/\W/) ? name.inspect : ":#{name}"
246: "#{name} => #{value}"
247: end
248: "{ #{attrs.join(', ')} }"
249: end
# File lib/haml/html.rb, line 216
216: def haml_tag_loud(text)
217: "= #{text.gsub(/\n\s*/, ' ').strip}\n"
218: end
# File lib/haml/html.rb, line 220
220: def haml_tag_silent(text)
221: text.split("\n").map { |line| "- #{line.strip}\n" }.join
222: end
# File lib/haml/html.rb, line 224
224: def static_attribute?(name, options)
225: attr_hash[name] and !dynamic_attribute?(name, options)
226: end
# File lib/haml/html.rb, line 236
236: def static_classname?(options)
237: static_attribute?('class', options)
238: end