Class: Plurimath::Mathml::Parser
- Inherits:
-
Object
- Object
- Plurimath::Mathml::Parser
- Defined in:
- lib/plurimath/mathml/parser.rb
Constant Summary collapse
- SUPPORTED_ATTRS =
%w[ linebreakstyle columnlines mathvariant accentunder linebreak mathcolor notation accent close open ].freeze
Instance Attribute Summary collapse
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
- #attrs_hash(node) ⇒ Object
-
#initialize(text) ⇒ Parser
constructor
A new instance of Parser.
- #parse ⇒ Object
- #parse_nodes(nodes) ⇒ Object
- #validate_attributes(attributes) ⇒ Object
Constructor Details
#initialize(text) ⇒ Parser
Returns a new instance of Parser.
23 24 25 |
# File 'lib/plurimath/mathml/parser.rb', line 23 def initialize(text) @text = text end |
Instance Attribute Details
#text ⇒ Object
Returns the value of attribute text.
8 9 10 |
# File 'lib/plurimath/mathml/parser.rb', line 8 def text @text end |
Instance Method Details
#attrs_hash(node) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/plurimath/mathml/parser.rb', line 56 def attrs_hash(node) { node.name.to_sym => { attributes: validate_attributes(node.attributes), value: parse_nodes(node.nodes), }, } end |
#parse ⇒ Object
27 28 29 30 31 32 33 34 35 |
# File 'lib/plurimath/mathml/parser.rb', line 27 def parse ox_nodes = Plurimath.xml_engine.load(text) display_style = ox_nodes&.locate("mstyle/@displaystyle")&.first nodes = parse_nodes(ox_nodes.nodes) Math::Formula.new( Transform.new.apply(nodes).flatten.compact, display_style: (display_style || true), ) end |
#parse_nodes(nodes) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/plurimath/mathml/parser.rb', line 37 def parse_nodes(nodes) nodes.map do |node| next if Plurimath.xml_engine.is_xml_comment?(node) if node.is_a?(String) node elsif !node.attributes.empty? attrs_hash(node) else { node.name.to_sym => parse_nodes(node.nodes) } end end end |
#validate_attributes(attributes) ⇒ Object
51 52 53 54 |
# File 'lib/plurimath/mathml/parser.rb', line 51 def validate_attributes(attributes) attributes&.select! { |key, _| SUPPORTED_ATTRS.include?(key.to_s) } attributes&.transform_keys(&:to_sym) if attributes&.any? end |