Class: Uniword::Mhtml::MathConverter
- Inherits:
-
Object
- Object
- Uniword::Mhtml::MathConverter
- 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
Class Method Summary collapse
-
.asciimath_to_omml(asciimath_string, _delimiters = ["$$", "$$"]) ⇒ String
Convert AsciiMath to OOXML Math (OMML).
-
.extract_math(element) ⇒ Hash
Extract math content from element.
-
.math_element?(element) ⇒ Boolean
Detect math content in HTML element.
-
.mathml_to_omml(mathml_string) ⇒ String
Convert MathML to OOXML Math (OMML).
-
.plurimath_available? ⇒ Boolean
Check if Plurimath gem is available.
-
.wrap_in_omml(mathml_string) ⇒ String
private
Wrap MathML in basic OMML structure.
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.
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.}" 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.
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.
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) = %w[math mml:math m:oMath m:oMathPara] return true if .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.
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.}" } wrap_in_omml(mathml_string) end else wrap_in_omml(mathml_string) end end |
.plurimath_available? ⇒ Boolean
Check if Plurimath gem 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.
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 |