class Hpricot::Elem

@private
@see Hpricot

def dynamic_attribute?(name, options)

def dynamic_attribute?(name, options)
  options[:erb] and dynamic_attributes.key?(name)
end

def dynamic_attributes

def dynamic_attributes
  @dynamic_attributes ||= begin
    Hash[attr_hash.map do |name, value|
      next if value.empty?
      full_match = nil
      ruby_value = value.gsub(%r{<haml:loud>\s*(.+?)\s*</haml:loud>}) do
        full_match = $`.empty? && $'.empty?
        CGI.unescapeHTML(full_match ? $1: "\#{#{$1}}")
      end
      next if ruby_value == value
      [name, full_match ? ruby_value : %("#{ruby_value}")]
    end]
  end
end

def haml_attribute_pair(name, value, options)

Returns the string representation of a single attribute key value pair
def haml_attribute_pair(name, value, options)
  value = dynamic_attribute?(name, options) ? dynamic_attributes[name] : value.inspect
  if options[:html_style_attributes]
    "#{name}=#{value}"
  else
    name = name.index(/\W/) ? name.inspect : ":#{name}"
    "#{name} => #{value}"
  end
end

def haml_attributes(options)

that's prettier than that produced by Hash#inspect
Returns a string representation of an attributes hash
def haml_attributes(options)
  attrs = attr_hash.sort.map do |name, value|
    haml_attribute_pair(name, value, options)
  end
  if options[:html_style_attributes]
    "(#{attrs.join(' ')})"
  else
    "{#{attrs.join(', ')}}"
  end
end

def haml_css_attr?(attr)

def haml_css_attr?(attr)
  attr =~ /^[-:\w]+$/
end

def render_children(so_far, tabs, options)

def render_children(so_far, tabs, options)
  (self.children || []).inject(so_far) do |output, child|
    output + child.to_haml(tabs + 1, options)
  end
end

def static_attribute?(name, options)

def static_attribute?(name, options)
  attr_hash[name] && !dynamic_attribute?(name, options)
end

def static_classname?(options)

def static_classname?(options)
  static_attribute?('class', options)
end

def static_id?(options)

def static_id?(options)
  static_attribute?('id', options) && haml_css_attr?(attr_hash['id'])
end

def to_haml(tabs, options)

Other tags:
    See: Haml::HTML::Node#to_haml -
def to_haml(tabs, options)
  return "" if converted_to_haml
  if name == "script" &&
      (attr_hash['type'].nil? || attr_hash['type'] == "text/javascript") &&
      (attr_hash.keys - ['type']).empty?
    return to_haml_filter(:javascript, tabs, options)
  elsif name == "style" &&
      (attr_hash['type'].nil? || attr_hash['type'] == "text/css") &&
      (attr_hash.keys - ['type']).empty?
    return to_haml_filter(:css, tabs, options)
  end
  output = tabulate(tabs)
  if options[:erb] && name[0...5] == 'haml:'
    case name[5..-1]
    when "loud"
      lines = CGI.unescapeHTML(inner_text).split("\n").
        map {|s| s.rstrip}.reject {|s| s.strip.empty?}
      lines.first.gsub!(/^[ \t]*/, "= ")
      if lines.size > 1 # Multiline script block
        # Normalize the indentation so that the last line is the base
        indent_str = lines.last[/^[ \t]*/]
        indent_re = /^[ \t]{0,#{indent_str.count(" ") + 8 * indent_str.count("\t")}}/
        lines.map! {|s| s.gsub!(indent_re, '')}
        # Add an extra "  " to make it indented relative to "= "
        lines[1..-1].each {|s| s.gsub!(/^/, "  ")}
        # Add | at the end, properly aligned
        length = lines.map {|s| s.size}.max + 1
        lines.map! {|s| "%#{-length}s|" % s}
        if next_sibling && next_sibling.is_a?(Hpricot::Elem) && next_sibling.name == "haml:loud" &&
            next_sibling.inner_text.split("\n").reject {|s| s.strip.empty?}.size > 1
          lines << "-#"
        end
      end
      return lines.map {|s| output + s + "\n"}.join
    when "silent"
      return CGI.unescapeHTML(inner_text).split("\n").map do |line|
        next "" if line.strip.empty?
        "#{output}- #{line.strip}\n"
      end.join
    when "block"
      return render_children("", tabs, options)
    end
  end
  if self.next && self.next.text? && self.next.content =~ /\A[^\s]/
    if self.previous.nil? || self.previous.text? &&
        (self.previous.content =~ /[^\s]\Z/ ||
         self.previous.content =~ /\A\s*\Z/ && self.previous.previous.nil?)
      nuke_outer_whitespace = true
    else
      output << "= succeed #{self.next.content.slice!(/\A[^\s]+/).dump} do\n"
      tabs += 1
      output << tabulate(tabs)
    end
  end
  output << "%#{name}" unless name == 'div' &&
    (static_id?(options) ||
     static_classname?(options) &&
     attr_hash['class'].split(' ').any?(&method(:haml_css_attr?)))
  if attr_hash
    if static_id?(options)
      output << "##{attr_hash['id']}"
      remove_attribute('id')
    end
    if static_classname?(options)
      leftover = attr_hash['class'].split(' ').reject do |c|
        next unless haml_css_attr?(c)
        output << ".#{c}"
      end
      remove_attribute('class')
      set_attribute('class', leftover.join(' ')) unless leftover.empty?
    end
    output << haml_attributes(options) if attr_hash.length > 0
  end
  output << ">" if nuke_outer_whitespace
  output << "/" if empty? && !etag
  if children && children.size == 1
    child = children.first
    if child.is_a?(::Hpricot::Text)
      if !child.to_s.include?("\n")
        text = child.to_haml(tabs + 1, options)
        return output + " " + text.lstrip.gsub(/^\\/, '') unless text.chomp.include?("\n")
        return output + "\n" + text
      elsif ["pre", "textarea"].include?(name) ||
          (name == "code" && parent.is_a?(::Hpricot::Elem) && parent.name == "pre")
        return output + "\n#{tabulate(tabs + 1)}:preserve\n" +
          innerText.gsub(/^/, tabulate(tabs + 2))
      end
    elsif child.is_a?(::Hpricot::Elem) && child.name == "haml:loud"
      return output + child.to_haml(tabs + 1, options).lstrip
    end
  end
  render_children(output + "\n", tabs, options)
end

def to_haml_filter(filter, tabs, options)

def to_haml_filter(filter, tabs, options)
  content =
    if children.first.is_a?(::Hpricot::CData)
      children.first.content
    else
      CGI.unescapeHTML(self.innerText)
    end
  content = erb_to_interpolation(content, options)
  content.gsub!(/\A\s*\n(\s*)/, '\1')
  original_indent = content[/\A(\s*)/, 1]
  if content.split("\n").all? {|l| l.strip.empty? || l =~ /^#{original_indent}/}
    content.gsub!(/^#{original_indent}/, tabulate(tabs + 1))
  end
  "#{tabulate(tabs)}:#{filter}\n#{content}"
end