class XmlToSliml

Attributes

to_s[R]

Public Class Methods

new(obj, spacer: ' ') click to toggle source
# File lib/xml_to_sliml.rb, line 12
def initialize(obj, spacer: '  ')

  @spacer = spacer
  @to_s = ''
  doc = (obj.is_a?(Rexle) or obj.is_a?(Rexle::Element)) ? obj : Rexle.new(obj)
  @to_s << scanbuild(doc.to_a)    

end

Private Instance Methods

fa(h) click to toggle source

format attributes

# File lib/xml_to_sliml.rb, line 25
def fa(h)

  "{%s}" % h.map {|k, v| "%s: %s" % [k, v.inspect]}.join(' ')

end
scanbuild(x, indent=0) click to toggle source
# File lib/xml_to_sliml.rb, line 31
def scanbuild(x, indent=0)

  name, attributes, *remaining = x

  children = remaining.shift
  text = ''

    
  if children.is_a? Array then
    nested = scanbuild(children, indent+1) 
  elsif children
    text = children
  end

  pad = @spacer * indent

  buffer = pad + name
  buffer << ' ' + fa(attributes) if attributes.any?
  buffer << ' ' + text unless text.strip.empty?
  

  while remaining.any? do
    children = remaining.shift
    if children and children.is_a? Array then
      buffer << "\n" + scanbuild(children, indent+1) 
    end
  end

  buffer
end