Ryan Calhounが以前の回答で述べたように、REXMLは折り返し行の長さとして80を使用します。これはバグだと確信しています(ただし、今はバグレポートを見つけることができませんでした)。Formatters :: Prettyクラスのwrite_textメソッドを上書きして、ハードコードされた80の代わりに構成可能な@width属性を使用するように修正することができました。
require "rubygems"
require "rexml/document"
include REXML
long_xml = "<root><tag>As Ryan Calhoun said in his previous answer, REXML uses 80 as its wrap line length. I'm pretty sure this is a bug (although I couldn't find a bug report just now). I was able to *fix* it by overwriting the Formatters::Pretty class's write_text method.</tag></root>"
xml = Document.new(long_xml)
#fix bug in REXML::Formatters::Pretty
class MyPrecious < REXML::Formatters::Pretty
def write_text( node, output )
s = node.to_s()
s.gsub!(/\s/,' ')
s.squeeze!(" ")
#The Pretty formatter code mistakenly used 80 instead of the @width variable
#s = wrap(s, 80-@level)
s = wrap(s, @width-@level)
s = indent_text(s, @level, " ", true)
output << (' '*@level + s)
end
end
printer = MyPrecious.new(5)
printer.width = 1000
printer.compact = true
printer.write(xml, STDOUT)