plurimath / mathml2asciimath

Convert MathML to AsciiMath
https://www.metanorma.com
BSD 2-Clause "Simplified" License
4 stars 0 forks source link

Unable to parse MathML without proper namespace #13

Closed ronaldtse closed 4 years ago

ronaldtse commented 4 years ago

Whatever the reason in IEV, MathML used uses <math> without any namespace.

I had to use this code to replace namespaces and perform conversion:

      def mathml_to_asciimath(input)
        return input if input.nil? || input.empty?

        if input.match?(/<math>/)
          text = clean_mathml(input)
          text = text.gsub(
              "<math>",
              '<math xmlns="http://www.w3.org/1998/Math/MathML">'
            )

          # puts text

          to_asciimath = Nokogiri::XML("<root>#{text}</root>", nil, "UTF-8")

          maths = to_asciimath.xpath('//mathml:math', 'mathml' => "http://www.w3.org/1998/Math/MathML")

          maths.each do |math_element|
            asciimath = MathML2AsciiMath.m2a(math_element.to_xml)
            asciimath.gsub!("\n", " ")
            # puts "ASCIIMATH!!  #{asciimath}"
            math_element.replace "$$#{asciimath}$$"
          end

          input = to_asciimath.root.children.to_s
        end

        html_to_asciimath(input)
      end