1 require 'rexml/formatters/default'
5 # Pretty-prints an XML document. This destroys whitespace in text nodes
6 # and will insert carriage returns and indentations.
8 # TODO: Add an option to print attributes on new lines
11 # If compact is set to true, then the formatter will attempt to use as
12 # little space as possible
13 attr_accessor :compact
14 # The width of a page. Used for formatting text
17 # Create a new pretty printer.
20 # An object implementing '<<(String)', to which the output will be written.
22 # An integer greater than 0. The indentation of each level will be
23 # this number of spaces. If this is < 1, the behavior of this object
24 # is undefined. Defaults to 2.
26 # If true, the printer will insert whitespace before closing empty
27 # tags, thereby allowing Internet Explorer's feeble XML parser to
28 # function. Defaults to false.
29 def initialize( indentation=2, ie_hack=false )
30 @indentation = indentation
37 def write_element(node, output)
39 output << "<#{node.expanded_name}"
41 node.attributes.each_attribute do |attr|
44 end unless node.attributes.empty?
46 if node.children.empty?
53 # If compact and all children are text, and if the formatted output
54 # is less than the specified width, then try to print everything on
58 if node.children.inject(true) {|s,c| s & c.kind_of?(Text)}
62 node.children.each { |child| write( child, string ) }
64 if string.length < @width
72 @level += @indentation
73 node.children.each { |child|
74 next if child.kind_of?(Text) and child.to_s.strip.length == 0
75 write( child, output )
78 @level -= @indentation
81 output << "</#{node.expanded_name}"
86 def write_text( node, output )
90 s = wrap(s, 80-@level)
91 s = indent_text(s, @level, " ", true)
92 output << (' '*@level + s)
95 def write_comment( node, output)
96 output << ' ' * @level
100 def write_cdata( node, output)
101 output << ' ' * @level
105 def write_document( node, output )
106 # Ok, this is a bit odd. All XML documents have an XML declaration,
107 # but it may not write itself if the user didn't specifically add it,
108 # either through the API or in the input document. If it doesn't write
109 # itself, then we don't need a carriage return... which makes this
110 # logic more complex.
111 node.children.each { |child|
112 next if child == node.children[-1] and child.instance_of?(Text)
113 unless child == node.children[0] or child.instance_of?(Text) or
114 (child == node.children[1] and !node.children[0].writethis)
117 write( child, output )
122 def indent_text(string, level=1, style="\t", indentfirstline=true)
123 return string if level < 0
124 string.gsub(/\n/, "\n#{style*level}")
127 def wrap(string, width)
128 # Recursivly wrap string at width.
129 return string if string.length <= width
130 place = string.rindex(' ', width) # Position in string with last ' ' before cutoff
131 return string[0,place] + "\n" + wrap(string[place+1..-1], width)