Class | Hpricot::Elem |
In: |
lib/haml/html.rb
|
Parent: | Object |
@see Hpricot @private
@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
# File lib/haml/html.rb, line 383 383: def dynamic_attribute?(name, options) 384: options[:erb] and dynamic_attributes.key?(name) 385: end
# File lib/haml/html.rb, line 346 346: def dynamic_attributes 347: @dynamic_attributes ||= begin 348: Haml::Util.map_hash(attr_hash) do |name, value| 349: next if value.empty? 350: full_match = nil 351: ruby_value = value.gsub(%r{<haml:loud>\s*(.+?)\s*</haml:loud>}) do 352: full_match = $`.empty? && $'.empty? 353: CGI.unescapeHTML(full_match ? $1: "\#{#{$1}}") 354: end 355: next if ruby_value == value 356: [name, full_match ? ruby_value : %("#{ruby_value}")] 357: end 358: end 359: end
Returns a string representation of an attributes hash that‘s prettier than that produced by Hash#inspect
# File lib/haml/html.rb, line 401 401: def haml_attributes(options) 402: attrs = attr_hash.sort.map do |name, value| 403: value = dynamic_attribute?(name, options) ? dynamic_attributes[name] : value.inspect 404: name = name.index(/\W/) ? name.inspect : ":#{name}" 405: "#{name} => #{value}" 406: end 407: "{#{attrs.join(', ')}}" 408: end
# File lib/haml/html.rb, line 340 340: def render_children(so_far, tabs, options) 341: (self.children || []).inject(so_far) do |output, child| 342: output + child.to_haml(tabs + 1, options) 343: end 344: end
# File lib/haml/html.rb, line 379 379: def static_attribute?(name, options) 380: attr_hash[name] && !dynamic_attribute?(name, options) 381: end
# File lib/haml/html.rb, line 391 391: def static_classname?(options) 392: static_attribute?('class', options) 393: end
# File lib/haml/html.rb, line 387 387: def static_id?(options) 388: static_attribute?('id', options) && haml_css_attr?(attr_hash['id']) 389: end
# File lib/haml/html.rb, line 361 361: def to_haml_filter(filter, tabs, options) 362: content = 363: if children.first.is_a?(::Hpricot::CData) 364: children.first.content 365: else 366: CGI.unescapeHTML(self.innerText) 367: end 368: 369: content = erb_to_interpolation(content, options) 370: content.gsub!(/\A\s*\n(\s*)/, '\1') 371: original_indent = content[/\A(\s*)/, 1] 372: if content.split("\n").all? {|l| l.strip.empty? || l =~ /^#{original_indent}/} 373: content.gsub!(/^#{original_indent}/, tabulate(tabs + 1)) 374: end 375: 376: "#{tabulate(tabs)}:#{filter}\n#{content}" 377: end