class Nokogiri::XML::Node

@see Nokogiri

Attributes

converted_to_haml[RW]

Whether this node has already been converted to Haml. Only used for text nodes and elements.

@return [Boolean]

Public Instance Methods

to_haml(tabs, options) click to toggle source

Returns the Haml representation of the given node.

@param tabs [Fixnum] The indentation level of the resulting Haml. @option options (see Html2haml::HTML#initialize)

# File lib/html2haml/html.rb, line 23
def to_haml(tabs, options)
  return "" if converted_to_haml || to_s.strip.empty?
  text = uninterp(self.to_s)

  #ending in a newline stops the inline nodes
  if text.end_with?("\n")
    parse_text_with_interpolation(text, tabs)
  else
    text << process_inline_nodes(next_sibling)
    parse_text_with_interpolation(text, tabs)
  end
end

Private Instance Methods

attr_hash() click to toggle source
# File lib/html2haml/html.rb, line 59
def attr_hash
  Hash[attributes.map {|k, v| [k.to_s, v.to_s]}]
end
erb_to_interpolation(text, options) click to toggle source
# File lib/html2haml/html.rb, line 38
def erb_to_interpolation(text, options)
  return text unless options[:erb]
  text = CGI.escapeHTML(uninterp(text))
  %w[<haml_loud> </haml_loud>].each {|str| text.gsub!(CGI.escapeHTML(str), str)}
  ::Nokogiri::XML.fragment(text).children.inject("") do |str, elem|
    if elem.is_a?(::Nokogiri::XML::Text)
      str + CGI.unescapeHTML(elem.to_s)
    else # <haml_loud> element
      str + '#{' + CGI.unescapeHTML(elem.inner_text.strip) + '}'
    end
  end
end
parse_text(text, tabs) click to toggle source
# File lib/html2haml/html.rb, line 63
def parse_text(text, tabs)
  parse_text_with_interpolation(uninterp(text), tabs)
end
parse_text_with_interpolation(text, tabs) click to toggle source
# File lib/html2haml/html.rb, line 67
def parse_text_with_interpolation(text, tabs)
  text.strip!
  return "" if text.empty?

  text.split("\n").map do |line|
    line.strip!
    "#{tabulate(tabs)}#{'\\' if Haml::Parser::SPECIAL_CHARACTERS.include?(line[0])}#{line}\n"
  end.join
end
process_inline_nodes(node) click to toggle source
# File lib/html2haml/html.rb, line 77
def process_inline_nodes(node)
  text = ""
  while node.is_a?(::Nokogiri::XML::Element) && node.name == "haml_loud"
    node.converted_to_haml = true
    text << '#{' <<
      CGI.unescapeHTML(node.inner_text).gsub(/\n\s*/, ' ').strip << '}'

    if node.next_sibling.is_a?(::Nokogiri::XML::Text)
      node = node.next_sibling
      text << uninterp(node.to_s)
      node.converted_to_haml = true
    end

    node = node.next_sibling
  end
  text
end
tabulate(tabs) click to toggle source
# File lib/html2haml/html.rb, line 51
def tabulate(tabs)
  '  ' * tabs
end
uninterp(text) click to toggle source
# File lib/html2haml/html.rb, line 55
def uninterp(text)
  text.gsub('#{', '\#{') #'
end