REXML :: Formatters :: Prettyがホワイト・スペースの代わりに\ tを使用できるかどうかを確認する方法がないようです。 XMLツリーのインデント戦略のためのスペース。私ができる唯一のことは、インデントレベルごとに使用される空白の数を定義することです。REXML :: Formatters :: Prettyがインデント用に空白の代わりに tを使用することを確認する
私は間違っていますか?
REXML :: Formatters :: Prettyがホワイト・スペースの代わりに\ tを使用できるかどうかを確認する方法がないようです。 XMLツリーのインデント戦略のためのスペース。私ができる唯一のことは、インデントレベルごとに使用される空白の数を定義することです。REXML :: Formatters :: Prettyがインデント用に空白の代わりに tを使用することを確認する
私は間違っていますか?
ない、それは間違いなく内部的にそれをサポートする可能性があるためREXML
ライブラリは、このオプションをご提供していませんが、あなたは自分自身のフォーマッタを転がることができる理由を確認してください。
module REXML
module Formatters
class Prettier < Pretty
attr_accessor :style
def initialize(indentation = 2, indent_style =" ", ie_hack=false)
@style = indent_style
super(indentation,ie_hack)
end
protected
def write_element(node, output)
output << style*@level
output << "<#{node.expanded_name}"
node.attributes.each_attribute do |attr|
output << " "
attr.write(output)
end unless node.attributes.empty?
if node.children.empty?
if @ie_hack
output << " "
end
output << "/"
else
output << ">"
# If compact and all children are text, and if the formatted output
# is less than the specified width, then try to print everything on
# one line
skip = false
if compact
if node.children.inject(true) {|s,c| s & c.kind_of?(Text)}
string = ""
old_level = @level
@level = 0
node.children.each { |child| write(child, string) }
@level = old_level
if string.length < @width
output << string
skip = true
end
end
end
unless skip
output << "\n"
@level += @indentation
node.children.each { |child|
next if child.kind_of?(Text) and child.to_s.strip.length == 0
write(child, output)
output << "\n"
}
@level -= @indentation
output << style*@level
end
output << "</#{node.expanded_name}"
end
output << ">"
end
def write_text(node, output)
s = node.to_s()
s.gsub!(/\s/,' ')
s.squeeze!(" ")
s = wrap(s, @width - @level)
s = indent_text(s, @level, style, true)
output << (style*@level + s)
end
def write_comment(node, output)
output << style * @level
Default.instance_method(:write_comment).bind(self).call(node,output)
end
def write_cdata(node, output)
output << style * @level
Default.instance_method(:write_cdata).bind(self).call(node,output)
end
end
end
end
今、あなたはあなた自身のインデントレベルとインデントを指定することができますスタイル
require "rexml/document"
include REXML
string = <<EOF
<mydoc>
<someelement attribute="nanoo">Text, text, text</someelement>
</mydoc>
EOF
doc = Document.new string
f = Formatters::Prettier(2,"h")
f.write(doc,$stdout)
#<mydoc>
#hh<someelement attribute='nanoo'>
#hhhhText, text, text
#hh</someelement>
#</mydoc>
私は\t
が$stdout
に表示されませんよう、インデントがどのように動作するかを示すために「h」を使用しています場合には、これは
f = Formatters::Prettier(1,"\t")
作品は絶対に素晴らしい!どうもありがとうございます! – tbop
実際に私はあなたのコードの理由を理解できませんでしたが、コメントを書くときにタブの後ろに2つの空白が追加されていると思われます。 – tbop
@tbopそれは 'Pretty'がそれを行い、' super'コールがこれを引き起こしているようだからです。私は上記のコードを修正しましたが、まだテストしていませんが、代わりにメソッドを 'Default'にバインドして動作するはずです。 – engineersmnky
なぜでしょうか?あなたが '\ t'を解析しない限り、' \ t'は一般にx個の空白文字のショートカットです。これを2または4に設定することはできませんでしたか?(優先度に応じて?) – engineersmnky
Ah? Windowsではすべてのテキストエディタが\ tと空白の間に明確な違いを示しています。 1つのxmlを入力として与えられたxmlを構築しています。後で差をつけたいのですから、字下げルールが違うという事実はそれを完全に混乱させます。 – tbop
あなたが達成しようとしているものの例を提供するかもしれません。テキストエディタタブは、一般的に設定可能な空白の数でインデントされますが、 '\ t'文字を含む真のテキストファイルは異なります。ほとんどのエディタで開くと、' \ t'文字の設定値がその行に適用されます。 – engineersmnky