lib/plurimath/math/formula.rb in plurimath-0.3.8 vs lib/plurimath/math/formula.rb in plurimath-0.3.9
- old
+ new
@@ -1,16 +1,17 @@
# frozen_string_literal: true
module Plurimath
module Math
class Formula < Core
- attr_accessor :value, :left_right_wrapper, :displaystyle
+ attr_accessor :value, :left_right_wrapper, :displaystyle, :input_string
def initialize(
value = [],
left_right_wrapper = true,
- displaystyle: true
+ displaystyle: true,
+ input_string: nil
)
@value = value.is_a?(Array) ? value : [value]
left_right_wrapper = false if @value.first.is_a?(Function::Left)
@left_right_wrapper = left_right_wrapper
@displaystyle = displaystyle
@@ -21,10 +22,12 @@
object.left_right_wrapper == left_right_wrapper
end
def to_asciimath
value.map(&:to_asciimath).join(" ")
+ rescue
+ parse_error!(:asciimath)
end
def to_mathml(display_style: displaystyle)
math_attrs = {
xmlns: "http://www.w3.org/1998/Math/MathML",
@@ -34,10 +37,12 @@
math = Utility.ox_element("math", attributes: math_attrs)
style = Utility.ox_element("mstyle", attributes: style_attrs)
Utility.update_nodes(style, mathml_content)
Utility.update_nodes(math, [style])
Ox.dump(math, indent: 2).gsub("&", "&")
+ rescue
+ parse_error!(:mathml)
end
def to_mathml_without_math_tag
return mathml_content unless left_right_wrapper
@@ -51,14 +56,18 @@
value.map(&:to_mathml_without_math_tag)
end
def to_latex
value&.map(&:to_latex)&.join(" ")
+ rescue
+ parse_error!(:latex)
end
def to_html
value&.map(&:to_html)&.join(" ")
+ rescue
+ parse_error!(:html)
end
def omml_math_attrs
{
"xmlns:m": "http://schemas.openxmlformats.org/officeDocument/2006/math",
@@ -90,10 +99,12 @@
)
math_element = Utility.ox_element("oMath", namespace: "m")
Utility.update_nodes(math_element, omml_content)
Utility.update_nodes(para_element, Array(math_element))
Ox.dump(para_element, indent: 2).gsub("&", "&").lstrip
+ rescue
+ parse_error!(:omml)
end
def omml_content
value&.map(&:insert_t_tag)
end
@@ -113,14 +124,10 @@
(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
@@ -138,9 +145,15 @@
)
end
def validate_function_formula
(value.none?(Function::Left) || value.none?(Function::Right))
+ end
+
+ protected
+
+ def parse_error!(type)
+ Math.parse_error!(input_string, type)
end
end
end
end