module CGI::Util

def pretty(string, shift = " ")


#
#
#
#
print CGI.pretty("", "\t")

#
#
#
#
print CGI.pretty("")

unit to use; it defaults to two spaces.
+string+ is the HTML string to indent. +shift+ is the indentation

Prettify (indent) an HTML string.
def pretty(string, shift = "  ")
  lines = string.gsub(/(?!\A)<.*?>/m, "\n\\0").gsub(/<.*?>(?!\n)/m, "\\0\n")
  end_pos = 0
  while end_pos = lines.index(/^<\/(\w+)/, end_pos)
    element = $1.dup
    start_pos = lines.rindex(/^\s*<#{element}/i, end_pos)
    lines[start_pos ... end_pos] = "__" + lines[start_pos ... end_pos].gsub(/\n(?!\z)/, "\n" + shift) + "__"
  end
  lines.gsub(/^((?:#{Regexp::quote(shift)})*)__(?=<\/?\w)/, '\1')
end