class Nokogiri::XML::Node

Public Instance Methods

to_pretty_html(indent=0) click to toggle source
# File lib/amber/render/table_of_contents.rb, line 274
def to_pretty_html(indent=0)
  indent_str = "  " * indent
  children_html = []
  text_html = nil
  if children.size == 1 && children.first.name == "text"
    text_html = children.first.content
  else
    children.each do |child|
      if child.name == "text"
        children_html << "#{"  " * (indent+1)}#{child.content}" if !child.content.empty?
      else
        children_html << child.to_pretty_html(indent+1)
      end
    end
  end
  attrs = []
  attributes.each do |attribute|
    attrs << %(#{attribute[0]}="#{attribute[1]}")
  end
  if attrs.any?
    attr_html = " " + attrs.join(' ')
  else
    attr_html = ""
  end
  html = []
  if text_html
    html << "#{indent_str}<#{name}#{attr_html}>#{text_html}</#{name}>"
  elsif children_html.any?
    html << "#{indent_str}<#{name}#{attr_html}>"
    html += children_html
    html << "#{indent_str}</#{name}>"
  else
    html << "#{indent_str}<#{name}#{attr_html}></#{name}>"
  end
  html.join("\n")
end