lib/plurimath/math/formula.rb in plurimath-0.3.5 vs lib/plurimath/math/formula.rb in plurimath-0.3.6
- old
+ new
@@ -1,10 +1,10 @@
# frozen_string_literal: true
module Plurimath
module Math
- class Formula
+ class Formula < Core
attr_accessor :value, :left_right_wrapper
def initialize(value = [], left_right_wrapper = true)
@value = value.is_a?(Array) ? value : [value]
left_right_wrapper = false if @value.first.is_a?(Function::Left)
@@ -83,51 +83,59 @@
attributes: omml_math_attrs,
namespace: "m",
)
math_element = Utility.ox_element("oMath", namespace: "m")
Utility.update_nodes(math_element, omml_content)
- para_element << math_element
- Ox.dump(para_element, indent: 2).gsub("&", "&")
+ Utility.update_nodes(para_element, Array(math_element))
+ Ox.dump(para_element, indent: 2).gsub("&", "&").lstrip
end
def omml_content
- value.map do |object|
- if object.is_a?(Symbol)
- mt = Utility.ox_element("t", namespace: "m")
- mt << object.value
- else
- object.to_omml_without_math_tag
- end
- end
+ value&.map(&:insert_t_tag)
end
def to_omml_without_math_tag
- if value.length == 2 && ["underover", "powerbase"].include?(
- value&.first&.class_name,
- )
- nary_tag
- else
- r_element = Utility.ox_element("r", namespace: "m")
- r_element << Utility.rpr_element if ["symbol", "number", "text"].include?(value&.first&.class_name)
- Utility.update_nodes(r_element, omml_content)
- end
+ return nary_tag if nary_tag_able?
+
+ omml_content
end
def nary_tag
nary_tag = Utility.ox_element("nary", namespace: "m")
e_tag = Utility.ox_element("e", namespace: "m")
- e_tag << value&.last&.to_omml_without_math_tag
+ Utility.update_nodes(e_tag, value.last.insert_t_tag)
Utility.update_nodes(
nary_tag,
- [
- value.first.omml_nary_tag,
- e_tag,
- ].flatten.compact,
+ (value.first.omml_nary_tag << e_tag),
)
+ [nary_tag]
end
def class_name
self.class.name.split("::").last.downcase
+ end
+
+ def extract_class_from_text
+ return false unless (value.length < 2 && value&.first&.is_a?(Function::Text))
+
+ value.first.parameter_one
+ end
+
+ def nary_attr_value
+ value.first.nary_attr_value
+ end
+
+ def nary_tag_able?
+ value.length == 2 &&
+ ["underover", "powerbase"].include?(value&.first&.class_name) &&
+ (
+ value&.first&.parameter_one&.to_omml_without_math_tag&.length == 1 ||
+ value&.first&.parameter_one.to_omml_without_math_tag.match?(/^&#x\w*\d*;$/)
+ )
+ end
+
+ def validate_function_formula
+ (value.none?(Function::Left) || value.none?(Function::Right))
end
end
end
end