Class: Mathtype::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/mathtype.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(equation) ⇒ Converter

Returns a new instance of Converter.



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/mathtype.rb', line 11

def initialize(equation)
  ole = Ole::Storage.open(equation, "rb+")
  eq = ole.file.read("Equation Native")[28..-1]

  data = Mathtype::Equation.read(eq).snapshot
  @builder = Nokogiri::XML::Builder.new do |xml|
    @xml = xml
    xml.root do
      process(object: data)
    end
  end
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



10
11
12
# File 'lib/mathtype.rb', line 10

def builder
  @builder
end

#xmlObject (readonly)

Returns the value of attribute xml.



9
10
11
# File 'lib/mathtype.rb', line 9

def xml
  @xml
end

Instance Method Details

#process(element: "mtef", object:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/mathtype.rb', line 28

def process(element: "mtef", object:)
  if object.is_a? Hash
    name = Mathtype::RECORD_NAMES[object[:record_type]]
    if name
      xml.send(name) do
        (object[:payload] || {}).each do |k, v|
          process(element: k, object: v)
        end
      end
    else
      xml.send(element) do
        object.each do |k, v|
          process(element: k, object: v)
        end
      end
    end
  elsif object.is_a? Array
    object.each do |a|
      process(element: element, object: a)
    end
  else
    process_final_element(element, object)
  end
end

#process_final_element(element, object) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/mathtype.rb', line 53

def process_final_element(element, object)
  if object.is_a? Hash
    xml.send(element, object)
  else
    xml.send(element) { xml.text object }
  end
end

#to_xmlObject



24
25
26
# File 'lib/mathtype.rb', line 24

def to_xml
  @builder.to_xml
end