class Qwik::RB_XMLFormatter
Constants
- CDATA
- COMMENT
- DOCTYPE
- EQ
- GT
- LF
- LT
- NL
- QT
- SL
- SP
To eliminate costs for object creation, the following strings are defined as constants. It makes format_xml 1.2 times faster.
- XMLDECL
Meta element
Public Instance Methods
format(ar, indent=0, sindent=0)
click to toggle source
# File vendor/qwik/lib/qwik/wabisabi-format-xml.rb, line 40 def format(ar, indent=0, sindent=0) n = (0 <= indent) ? LF : NL sn = (0 <= sindent) ? LF : NL if !ar[0].is_a?(Symbol) out = '' ar.each {|x| if x.is_a?(Array) out << format(x, indent, sindent) # recursive elsif x.is_a?(String) out << x.escapeHTML elsif x.is_a?(NilClass) # do nothing else p "what?", x out << x.to_s.escapeHTML end } return out end element = ar[0].to_s.escapeHTML if element == XMLDECL # XML Declaration attributes = '' attributes += " version=\""+ar[1]+"\"" if ar[1] attributes += " encoding=\""+ar[2]+"\"" if ar[2] attributes += " standalone=\""+ar[3]+"\"" if ar[3] return "<?xml"+attributes+"?>" elsif element == DOCTYPE # doctype return "<!DOCTYPE "+ar[1]+' '+ar[2]+ " \""+ar[3]+"\" \""+ar[4]+"\">" elsif element == COMMENT # comment return "<!--"+ar[1]+"-->" elsif element == CDATA # CDATA return "<![CDATA["+ar[1]+"]]>" end offset = 1 attributes = '' while ar[offset].is_a?(Hash) attr = ar[offset] offset += 1 attr.keys.sort{|a, b| a.to_s <=> b.to_s }.each {|k| v = attr[k] attributes << SP << k.to_s.escapeHTML << EQ << QT << v.to_s.escapeHTML << QT } end if ar[offset].nil? && ar.length == offset o = '' o << LT << element << attributes << n << SL << GT return o end out = '' out << LT << element << attributes << sn << GT (offset...ar.length).each {|i| x = ar[i] if x.is_a?(Array) out << format(x, indent, sindent) # recursive elsif x.is_a?(String) out << x.escapeHTML else out << x.to_s.escapeHTML end } out << LT << SL << element << n << GT out end