Module: AxeML

Defined in:
lib/axeml.rb

Class Method Summary collapse

Class Method Details

.transform(axeml) ⇒ Object



6
7
8
9
10
# File 'lib/axeml.rb', line 6

def self.transform(axeml)
  doc = Nokogiri::XML::Document.new
  transform_element(axeml, doc)
  doc
end

.transform_element(axeml, doc, parent = doc) ⇒ Object



12
13
14
15
16
17
18
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
# File 'lib/axeml.rb', line 12

def self.transform_element(axeml, doc, parent = doc)
  case axeml[0]
  when Symbol
    el = parent.add_child(Nokogiri::XML::Element.new(axeml[0].to_s, doc))

    axeml[1..-1].each do |e|
      case e 
      when Array
        transform_element(e, doc, el)
      when String
        if e.html_safe?
          el.add_child(e)
        else
          el.content += e
        end
      when Fixnum, Float
        el.content += e.to_s
      when Hash
        e.each do |k,v|
          el[k.to_s] = v
        end
      end
    end

    el
  when Array
    axeml.each do |a|
      transform_element(a, doc, parent)
    end
  end
  
end