Class: XMLtoHAML

Inherits:
Object
  • Object
show all
Includes:
REXML
Defined in:
lib/xml-to-haml.rb

Instance Method Summary collapse

Constructor Details

#initialize(s) ⇒ XMLtoHAML

Returns a new instance of XMLtoHAML.



10
11
12
13
# File 'lib/xml-to-haml.rb', line 10

def initialize(s)
  doc = Document.new(s)
  @haml = xml_to_haml(doc)
end

Instance Method Details

#to_sObject



15
16
17
# File 'lib/xml-to-haml.rb', line 15

def to_s
  @haml
end

#xml_to_haml(nodex, indent = '') ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/xml-to-haml.rb', line 19

def xml_to_haml(nodex, indent ='')

  buffer = nodex.elements.map do |node|

    attributex = ' '
    buffer = "%s" % [indent + '%' + node.name]

    if node.attributes.size > 0 then
      alist = Array.new(node.attributes.size)
      i = 0
      node.attributes.each { |x, y|
        alist[i] = " :#{x} => '#{y}'"  
        i += 1
      }
      attributex = '{' + alist.join(",")  + '} ' 
    end
 
    buffer +=  "%s" % [attributex + node.text] if not node.text.nil?  
    buffer += "\n"
    buffer += xml_to_haml(node, indent + '  ')

    buffer
  end

  buffer.join
end