Module: Metanorma::Mirror::MathUtil
- Defined in:
- lib/metanorma/mirror/math_util.rb
Overview
MathUtil is the single home for math-content extraction helpers.
These helpers convert mathML XML objects into strings (stripped of XML declarations) and extract asciimath from stem elements. They are used by handlers and renderers that need to carry math content through the mirror pipeline.
Class Method Summary collapse
-
.asciimath_from_stem(stem) ⇒ Object
Extract asciimath text from a stem element.
- .mathml_from_math(math) ⇒ Object
- .strip_xml_decl(math_xml) ⇒ Object
-
.text_from_stem(stem) ⇒ Object
Best-effort plain-text extraction from a stem element.
Class Method Details
.asciimath_from_stem(stem) ⇒ Object
Extract asciimath text from a stem element. Returns nil if no asciimath is present.
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/metanorma/mirror/math_util.rb', line 26 def self.asciimath_from_stem(stem) asciimath = SafeAttr.read(stem, :asciimath) return nil unless asciimath.is_a?(Array) parts = asciimath.filter_map do |a| next a if a.is_a?(String) next SafeAttr.read(a, :text).to_s if a.is_a?(Lutaml::Model::Serializable) nil end joined = parts.join.strip joined.empty? ? nil : joined end |
.mathml_from_math(math) ⇒ Object
19 20 21 22 |
# File 'lib/metanorma/mirror/math_util.rb', line 19 def self.mathml_from_math(math) xml = math.is_a?(Array) ? math.map(&:to_xml).join : math.to_xml strip_xml_decl(xml) end |
.strip_xml_decl(math_xml) ⇒ Object
15 16 17 |
# File 'lib/metanorma/mirror/math_util.rb', line 15 def self.strip_xml_decl(math_xml) math_xml.sub(STRIP_XML_DECL_PATTERN, "") end |
.text_from_stem(stem) ⇒ Object
Best-effort plain-text extraction from a stem element. Tries asciimath first; falls back to SafeAttr :text.
42 43 44 45 46 47 48 49 50 |
# File 'lib/metanorma/mirror/math_util.rb', line 42 def self.text_from_stem(stem) asciimath = asciimath_from_stem(stem) return asciimath if asciimath text = SafeAttr.read(stem, :text) return text.to_s if text.is_a?(String) && !text.strip.empty? "" end |