pretty(string, shift = " ")
public
Prettify (indent) an HTML
string.
string is the HTML
string to indent. shift is the indentation unit to use; it
defaults to two spaces.
print CGI::pretty("<HTML><BODY></BODY></HTML>")
print CGI::pretty("<HTML><BODY></BODY></HTML>", "\t")
# File lib/cgi/util.rb, line 211
def pretty(string, shift = " ")
lines = string.gsub(/(?!\A)<.*?>/, "\n\\0").gsub(/<.*?>(?!\n)/, "\\0\n")
end_pos = 0
while end_pos = lines.index(/^<\/(\w+)/, end_pos)
element = $1.dup
start_pos = lines.rindex(/^\s*<#{element}/, 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