Class: Uniword::Mhtml::MathConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/mhtml/math_converter.rb

Overview

Converts mathematical content between formats for MHTML.

Responsibility: Convert MathML and AsciiMath to Word’s OOXML math format (OMML) for proper rendering in Microsoft Word. This class handles math content serialization with graceful fallbacks.

Supports:

  • MathML to OMML conversion

  • AsciiMath to OMML conversion

  • Fallback wrapping when Plurimath is unavailable

Examples:

Convert MathML to OMML

omml = MathConverter.mathml_to_omml("<math><mi>x</mi></math>")

Convert AsciiMath to OMML

omml = MathConverter.asciimath_to_omml("x^2 + y^2 = z^2")

Class Method Summary collapse

Class Method Details

.asciimath_to_omml(asciimath_string, _delimiters = ["$$", "$$"]) ⇒ String

Convert AsciiMath to OOXML Math (OMML).

Parses AsciiMath notation and converts to OMML using Plurimath. Falls back to plain text if Plurimath is unavailable.

Examples:

Convert AsciiMath formula

omml = MathConverter.asciimath_to_omml("x^2 + y^2 = z^2")

Parameters:

  • asciimath_string (String)

    The AsciiMath content

  • delimiters (Array<String>)

    The delimiters used (default: [‘$$’, ‘$$’])

Returns:

  • (String)

    The OMML representation or plain text



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/uniword/mhtml/math_converter.rb', line 70

def self.asciimath_to_omml(asciimath_string, _delimiters = ["$$", "$$"])
  return "" if asciimath_string.nil? || asciimath_string.empty?

  if plurimath_available?
    begin
      parsed = Plurimath::Math.parse(asciimath_string, :asciimath)
      parsed.to_omml
    rescue Plurimath::Math::ParseError => e
      Uniword.logger&.debug do
        "AsciiMath conversion failed: #{e.message}"
      end
      asciimath_string
    end
  else
    # No conversion possible without Plurimath
    asciimath_string
  end
end

.extract_math(element) ⇒ Hash

Extract math content from element.

Extracts the mathematical content from various element types.

Examples:

Extract math from element

math = MathConverter.extract_math(element)
# => { type: :mathml, content: "<math>...</math>" }

Parameters:

  • element (Nokogiri::XML::Element)

    The HTML element

Returns:

  • (Hash)

    Hash with :type and :content



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/uniword/mhtml/math_converter.rb', line 140

def self.extract_math(element)
  if element["data-mathml"]
    { type: :mathml, content: element["data-mathml"] }
  elsif /^(math|mml:math)$/.match?(element.name)
    { type: :mathml, content: element.to_s }
  elsif /^m:(oMath|oMathPara)$/.match?(element.name)
    { type: :omml, content: element.to_s }
  elsif element["class"]&.include?("asciimath")
    { type: :asciimath, content: element.text }
  else
    { type: :unknown, content: element.to_s }
  end
end

.math_element?(element) ⇒ Boolean

Detect math content in HTML element.

Identifies math elements by tag name or special markers.

Examples:

Check if element is math

is_math = MathConverter.math_element?(element)

Parameters:

  • element (Nokogiri::XML::Element)

    The HTML element

Returns:

  • (Boolean)

    true if element contains math content



119
120
121
122
123
124
125
126
127
128
# File 'lib/uniword/mhtml/math_converter.rb', line 119

def self.math_element?(element)
  return false unless element.respond_to?(:name)

  math_tags = %w[math mml:math m:oMath m:oMathPara]
  return true if math_tags.include?(element.name)
  return true if element["class"]&.include?("math")
  return true if element["data-mathml"]

  false
end

.mathml_to_omml(mathml_string) ⇒ String

Convert MathML to OOXML Math (OMML).

Uses Plurimath for conversion if available, otherwise wraps the MathML in basic OMML structure.

Examples:

Convert simple MathML

omml = MathConverter.mathml_to_omml("<math><mi>x</mi></math>")

Parameters:

  • mathml_string (String)

    The MathML content

Returns:

  • (String)

    The OMML representation



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/uniword/mhtml/math_converter.rb', line 44

def self.mathml_to_omml(mathml_string)
  return "" if mathml_string.nil? || mathml_string.empty?

  if plurimath_available?
    begin
      Plurimath::Math.parse(mathml_string, :mathml).to_omml
    rescue Plurimath::Math::ParseError => e
      Uniword.logger&.debug { "MathML conversion failed: #{e.message}" }
      wrap_in_omml(mathml_string)
    end
  else
    wrap_in_omml(mathml_string)
  end
end

.plurimath_available?Boolean

Check if Plurimath gem is available.

Returns:

  • (Boolean)

    true if Plurimath is available



25
26
27
28
29
30
31
32
# File 'lib/uniword/mhtml/math_converter.rb', line 25

def self.plurimath_available?
  @plurimath_available ||= begin
    require "plurimath"
    true
  rescue LoadError
    false
  end
end

.wrap_in_omml(mathml_string) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Wrap MathML in basic OMML structure.

Provides minimal OMML wrapping when Plurimath is not available. This allows Word to potentially recognize the math content.

Parameters:

  • mathml_string (String)

    The MathML content

Returns:

  • (String)

    The wrapped OMML



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/uniword/mhtml/math_converter.rb', line 98

def self.wrap_in_omml(mathml_string)
  <<~OMML
    <m:oMathPara xmlns:m="http://schemas.microsoft.com/office/2004/12/omml">
      <m:oMath>
        <m:r>
          <m:t>#{mathml_string}</m:t>
        </m:r>
      </m:oMath>
    </m:oMathPara>
  OMML
end