Class | Hpricot::Elem |
In: |
lib/haml/html.rb
|
Parent: | Object |
@see Haml::HTML::Node#to_haml
# File lib/haml/html.rb, line 233 233: def to_haml(tabs, options) 234: return "" if converted_to_haml 235: if name == "script" && 236: (attr_hash['type'].nil? || attr_hash['type'] == "text/javascript") && 237: (attr_hash.keys - ['type']).empty? 238: return to_haml_filter(:javascript, tabs, options) 239: elsif name == "style" && 240: (attr_hash['type'].nil? || attr_hash['type'] == "text/css") && 241: (attr_hash.keys - ['type']).empty? 242: return to_haml_filter(:css, tabs, options) 243: end 244: 245: output = tabulate(tabs) 246: if options[:erb] && name[0...5] == 'haml:' 247: case name[5..-1] 248: when "loud" 249: lines = CGI.unescapeHTML(inner_text).split("\n"). 250: map {|s| s.rstrip}.reject {|s| s.strip.empty?} 251: lines.first.gsub!(/^[ \t]*/, "= ") 252: 253: if lines.size > 1 # Multiline script block 254: # Normalize the indentation so that the last line is the base 255: indent_str = lines.last[/^[ \t]*/] 256: indent_re = /^[ \t]{0,#{indent_str.count(" ") + 8 * indent_str.count("\t")}}/ 257: lines.map! {|s| s.gsub!(indent_re, '')} 258: 259: # Add an extra " " to make it indented relative to "= " 260: lines[1..-1].each {|s| s.gsub!(/^/, " ")} 261: 262: # Add | at the end, properly aligned 263: length = lines.map {|s| s.size}.max + 1 264: lines.map! {|s| "%#{-length}s|" % s} 265: 266: if next_sibling && next_sibling.is_a?(Hpricot::Elem) && next_sibling.name == "haml:loud" && 267: next_sibling.inner_text.split("\n").reject {|s| s.strip.empty?}.size > 1 268: lines << "-#" 269: end 270: end 271: return lines.map {|s| output + s + "\n"}.join 272: when "silent" 273: return CGI.unescapeHTML(inner_text).split("\n").map do |line| 274: next "" if line.strip.empty? 275: "#{output}- #{line.strip}\n" 276: end.join 277: when "block" 278: return render_children("", tabs, options) 279: end 280: end 281: 282: if self.next && self.next.text? && self.next.content =~ /\A[^\s]/ 283: if self.previous.nil? || self.previous.text? && 284: (self.previous.content =~ /[^\s]\Z/ || 285: self.previous.content =~ /\A\s*\Z/ && self.previous.previous.nil?) 286: nuke_outer_whitespace = true 287: else 288: output << "= succeed #{self.next.content.slice!(/\A[^\s]+/).dump} do\n" 289: tabs += 1 290: output << tabulate(tabs) 291: end 292: end 293: 294: output << "%#{name}" unless name == 'div' && 295: (static_id?(options) || 296: static_classname?(options) && 297: attr_hash['class'].split(' ').any?(&method(:haml_css_attr?))) 298: 299: if attr_hash 300: if static_id?(options) 301: output << "##{attr_hash['id']}" 302: remove_attribute('id') 303: end 304: if static_classname?(options) 305: leftover = attr_hash['class'].split(' ').reject do |c| 306: next unless haml_css_attr?(c) 307: output << ".#{c}" 308: end 309: remove_attribute('class') 310: set_attribute('class', leftover.join(' ')) unless leftover.empty? 311: end 312: output << haml_attributes(options) if attr_hash.length > 0 313: end 314: 315: output << ">" if nuke_outer_whitespace 316: output << "/" if empty? && !etag 317: 318: if children && children.size == 1 319: child = children.first 320: if child.is_a?(::Hpricot::Text) 321: if !child.to_s.include?("\n") 322: text = child.to_haml(tabs + 1, options) 323: return output + " " + text.lstrip.gsub(/^\\/, '') unless text.chomp.include?("\n") 324: return output + "\n" + text 325: elsif ["pre", "textarea"].include?(name) || 326: (name == "code" && parent.is_a?(::Hpricot::Elem) && parent.name == "pre") 327: return output + "\n#{tabulate(tabs + 1)}:preserve\n" + 328: innerText.gsub(/^/, tabulate(tabs + 2)) 329: end 330: elsif child.is_a?(::Hpricot::Elem) && child.name == "haml:loud" 331: return output + child.to_haml(tabs + 1, options).lstrip 332: end 333: end 334: 335: render_children(output + "\n", tabs, options) 336: end