Class: Arrolio::MathML::InlineRunExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/arrolio/math_ml/inline_run_extractor.rb

Overview

Walks a parsed Mml tree (from Mml.parse(xml_string, version: 3)) and emits an Array of Content::InlineRun values, applying baseline_shift and font_size_scale to match the math layout semantics of each MathML element.

The extractor relies on the canonical Mml gem (plurimath/mml) for parsing, keeping Arrolio free of its own MathML grammar. MathML presentation elements map as follows:

<mi>                  → italic math run (:math_identifier)
<mn>, <mo>, <mtext>   → regular math run (parent style)
<ms>                  → quoted string run (parent style)
<msub>                → base + subscript run
<msup>                → base + superscript run
<msubsup>             → base + sub + sup
<munder>/<mover>      → rendered as sub/sup for visual fidelity
<mfrac>               → numerator + '/' + denominator (inline)
<mrow>, <mstyle>      → transparent group, recurse children

Anything else recurses transparently.

Constant Summary collapse

ELEMENT_HANDLERS =

Each entry is [value_attribute, child_collection_attribute, kind]

  • value_attribute: node method returning the text content (or nil)
  • child_collection_attribute: node method returning child elements of that type (Array)
  • kind: :text | :identifier | :transparent | :subscript_container | :superscript_container | :fraction | :ignored
{
  'Mml::V3::Mi' => [:value, :mi_value, :identifier],
  'Mml::V3::Mn' => [:value, :mn_value, :text],
  'Mml::V3::Mo' => [:value, :mo_value, :text],
  'Mml::V3::Mtext' => [:value, :mtext_value, :text],
  'Mml::V3::Ms' => [:value, :ms_value, :text],
  'Mml::V3::Mrow' => [nil, nil, :transparent_all],
  'Mml::V3::Mstyle' => [nil, nil, :transparent_all],
  'Mml::V3::Msub' => [nil, :msub_value, :subscript_container],
  'Mml::V3::Msup' => [nil, :msup_value, :superscript_container],
  'Mml::V3::Msubsup' => [nil, :msubsup_value, :subsup_container],
  'Mml::V3::Munder' => [nil, :munder_value, :subscript_container],
  'Mml::V3::Mover' => [nil, :mover_value, :superscript_container],
  'Mml::V3::Munderover' => [nil, :munderover_value, :subsup_container],
  'Mml::V3::Mfrac' => [nil, :mfrac_value, :fraction],
  'Mml::V3::Menclose' => [nil, :menclose_value, :transparent],
  'Mml::V3::Mphantom' => [nil, :mphantom_value, :transparent],
  'Mml::V3::Mpadded' => [nil, :mpadded_value, :transparent],
  'Mml::V3::Merror' => [nil, :merror_value, :transparent],
  'Mml::V3::Semantics' => [nil, :semantics_value, :transparent],
  'Mml::V3::Math' => [nil, nil, :transparent_all]
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_style: :math) ⇒ InlineRunExtractor

Returns a new instance of InlineRunExtractor.



74
75
76
# File 'lib/arrolio/math_ml/inline_run_extractor.rb', line 74

def initialize(base_style: :math)
  @base_style = base_style.to_sym
end

Instance Attribute Details

#base_styleObject (readonly)

Returns the value of attribute base_style.



58
59
60
# File 'lib/arrolio/math_ml/inline_run_extractor.rb', line 58

def base_style
  @base_style
end

Class Method Details

.extract_from_xml(xml_string, base_style: :math) ⇒ Object

Parse xml_string as MathML and extract the runs. Returns an empty Array if parsing fails.



87
88
89
90
91
92
93
94
95
# File 'lib/arrolio/math_ml/inline_run_extractor.rb', line 87

def self.extract_from_xml(xml_string, base_style: :math)
  return [] if xml_string.nil? || xml_string.to_s.empty?

  parsed = ::Mml.parse(xml_string.to_s, version: 3)
  new(base_style: base_style).extract(parsed)
rescue StandardError => e
  Arrolio::Logger.warn "Mml parse failed: #{e.class}: #{e.message[0, 80]}"
  []
end

Instance Method Details

#default_baselineObject



62
63
64
# File 'lib/arrolio/math_ml/inline_run_extractor.rb', line 62

def default_baseline
  ::Arrolio::Content::InlineRun::BASELINE_NORMAL
end

#extract(math_node) ⇒ Object



78
79
80
81
82
83
# File 'lib/arrolio/math_ml/inline_run_extractor.rb', line 78

def extract(math_node)
  runs = []
  walk(math_node, runs, baseline: default_baseline, scale: 1.0,
                        style_id: base_style)
  runs
end

#sub_baselineObject



66
67
68
# File 'lib/arrolio/math_ml/inline_run_extractor.rb', line 66

def sub_baseline
  ::Arrolio::Content::InlineRun::BASELINE_SUB
end

#sub_sup_scaleObject



60
# File 'lib/arrolio/math_ml/inline_run_extractor.rb', line 60

def sub_sup_scale = 0.7

#sup_baselineObject



70
71
72
# File 'lib/arrolio/math_ml/inline_run_extractor.rb', line 70

def sup_baseline
  ::Arrolio::Content::InlineRun::BASELINE_SUP
end