簡體   English   中英

如何確保REXML :: Formatters :: Pretty使用\\ t而不是空格進行縮進

[英]How to make sure REXML::Formatters::Pretty uses \t instead of white-space for indentation

在我看來,沒有辦法確保REXML :: Formatters :: Pretty可以使用\\ t而不是空格作為XML樹中的縮進策略。 我唯一能做的就是定義每個縮進級別使用多少空白。

我錯了嗎?

不知道為什么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>

我用“ h”表示縮進是如何工作的,因為\\t不會顯示在$stdout但是在您的情況下,

f = Formatters::Prettier(1,"\t")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM