Sha256: 799a12e863e160c3ae0e34e1e442f926ae29a1288404484eaa1b97a9ed3f188a

Contents?: true

Size: 1.16 KB

Versions: 4

Compression:

Stored size: 1.16 KB

Contents

# frozen_string_literal: true

require_relative "transform"
module Plurimath
  class Omml
    class Parser
      attr_accessor :text

      def initialize(text)
        @text = text
      end

      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

      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

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
plurimath-0.3.9 lib/plurimath/omml/parser.rb
plurimath-0.3.8 lib/plurimath/omml/parser.rb
plurimath-0.3.7 lib/plurimath/omml/parser.rb
plurimath-0.3.6 lib/plurimath/omml/parser.rb