lib/plurimath/math/formula.rb in plurimath-0.8.1 vs lib/plurimath/math/formula.rb in plurimath-0.8.2

- old
+ new

@@ -286,30 +286,23 @@ r_tag << ox_element("br") end def unitsml_post_processing(nodes) nodes.each.with_index do |node, index| - if node.is_a?(Ox::Element) && node.attributes&.dig(:unitsml) - previous = nodes[index-1] - if previous && ["mi", "mn"].include?(previous.name) - if text_in_tag?(node.nodes) - nodes.insert(index, space_element(attributes: true)) - else - nodes.insert(index, space_element) - end - end - - node.attributes.delete_if {|k, v| k == :unitsml } + if node.is_a?(Ox::Element) && node&.attributes&.dig(:unitsml) + pre_index = index - 1 + pre_node = nodes[pre_index] if pre_index.zero? || pre_index.positive? + nodes.insert(index, space_element(node)) if valid_previous?(pre_node) + node.attributes.delete_if { |k, _| k == :unitsml } end - - unitsml_post_processing(node.nodes) if !node.nodes.any?(String) + unitsml_post_processing(node.nodes) if node.nodes.none?(String) end end - def space_element(attributes: false) + def space_element(node) element = (ox_element("mo") << "&#x2062;") - element.attributes[:rspace] = "thickmathspace" if attributes + element.attributes[:rspace] = "thickmathspace" if text_in_tag?(node.nodes) element end def text_in_tag?(nodes) next_nodes = nodes.first.nodes @@ -324,9 +317,20 @@ value.last.is_a?(Math::Symbol) && value.last.value == "&#x338;" end def unicodemath_value (negated_value? || mini_sized?) ? value&.map(&:to_unicodemath)&.join : value&.map(&:to_unicodemath)&.join(" ") + end + + def valid_previous?(previous) + return unless previous + + ["mi", "mn"].include?(previous.name) || + inside_tag?(previous) + end + + def inside_tag?(previous) + previous&.nodes&.any? { |node| valid_previous?(node) if node.is_a?(Ox::Element) } end end end end