lib/plurimath/math/formula.rb in plurimath-0.8.15 vs lib/plurimath/math/formula.rb in plurimath-0.8.16
- old
+ new
@@ -1,11 +1,11 @@
# frozen_string_literal: true
module Plurimath
module Math
class Formula < Core
- attr_accessor :value, :left_right_wrapper, :displaystyle, :input_string, :unitsml
+ attr_accessor :value, :left_right_wrapper, :displaystyle, :input_string, :unitsml, :unitsml_xml
MATH_ZONE_TYPES = %i[
omml
latex
mathml
@@ -46,12 +46,18 @@
value.map { |val| val.to_asciimath(options: options) }.join(" ")
rescue
parse_error!(:asciimath)
end
- def to_mathml(display_style: displaystyle, split_on_linebreak: false, intent: false, formatter: nil)
- options = { formatter: formatter }
+ def to_mathml(
+ intent: false,
+ formatter: nil,
+ unitsml_xml: nil,
+ split_on_linebreak: false,
+ display_style: displaystyle
+ )
+ options = { formatter: formatter, unitsml_xml: unitsml_xml }
return line_breaked_mathml(display_style, intent, options: options) if split_on_linebreak
math_attrs = {
xmlns: "http://www.w3.org/1998/Math/MathML",
display: "block",
@@ -78,10 +84,11 @@
mathml_value = mathml_content(intent, options: options)
attributes = intent_attribute(mathml_value) if intent
mrow = ox_element("mrow", attributes: attributes)
mrow[:unitsml] = true if unitsml
+ mathml_value += wrapped_unitsml_xml(mrow) if unitsml_xml && options[:unitsml_xml]
Utility.update_nodes(mrow, mathml_value)
end
def mathml_content(intent, options:)
nodes = value.map { |val| val.to_mathml_without_math_tag(intent, options: options) }
@@ -325,9 +332,15 @@
prev_node.insert_in_nodes(index, space_element(node)) if valid_previous?(pre_node)
node.remove_attr("unitsml")
end
unitsml_post_processing(node.nodes, node) if node.nodes.none?(String)
end
+ end
+
+ def wrapped_unitsml_xml(mrow)
+ node = Plurimath.xml_engine.load("<mrow>#{unitsml_xml}</mrow>")
+ mrow.attributes[:xref] = node.locate("*/@id").first if node.locate("*/@id").any?
+ node.nodes
end
def space_element(node)
element = (ox_element("mo") << "⁢")
element[:rspace] = "thickmathspace" if text_in_tag?(node.xml_nodes.nodes)