Class: Plurimath::Omml::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/plurimath/omml/parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ Parser

Returns a new instance of Parser.



9
10
11
# File 'lib/plurimath/omml/parser.rb', line 9

def initialize(text)
  @text = text
end

Instance Attribute Details

#textObject

Returns the value of attribute text.



7
8
9
# File 'lib/plurimath/omml/parser.rb', line 7

def text
  @text
end

Instance Method Details

#organize_table_td(node) ⇒ Object



42
43
44
45
46
# File 'lib/plurimath/omml/parser.rb', line 42

def organize_table_td(node)
  node.locate("e/?").each do |child_node|
    child_node.name = "mtd" if child_node.name == "r"
  end
end

#parseObject



13
14
15
16
17
18
19
20
21
22
# File 'lib/plurimath/omml/parser.rb', line 13

def parse
  nodes = Ox.load(text, strip_namespace: true)
  @hash = { sequence: parse_nodes(nodes.nodes) }
  nodes = JSON.parse(@hash.to_json, symbolize_names: true)
  Math::Formula.new(
    Transform.new.apply(
      nodes,
    ),
  )
end

#parse_nodes(nodes) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/plurimath/omml/parser.rb', line 24

def parse_nodes(nodes)
  nodes.map do |node|
    if node.is_a?(String)
      node == "" ? nil : node
    elsif !node.attributes.empty?
      {
        node.name => {
          attributes: node.attributes,
          value: parse_nodes(node.nodes),
        },
      }
    else
      organize_table_td(node) if %w[mr eqArr].include?(node.name)
      { node.name => parse_nodes(node.nodes) }
    end
  end
end