Class: Plurimath::Math::Formula
- Inherits:
-
Core
- Object
- Core
- Plurimath::Math::Formula
show all
- Defined in:
- lib/plurimath/math/formula.rb
Constant Summary
collapse
- MATH_ZONE_TYPES =
%i[
omml
latex
mathml
asciimath
].freeze
Constants inherited
from Core
Core::REPLACABLES
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#==(object) ⇒ Object
-
#cloned_objects ⇒ Object
-
#extract_class_name_from_text ⇒ Object
-
#initialize(value = [], left_right_wrapper = true, display_style: true, input_string: nil) ⇒ Formula
constructor
A new instance of Formula.
-
#insert(values) ⇒ Object
-
#line_breaked_mathml(display_style) ⇒ Object
-
#line_breaking(obj) ⇒ Object
-
#mathml_content ⇒ Object
-
#nary_attr_value ⇒ Object
-
#new_line_support(array = []) ⇒ Object
-
#omml_attrs ⇒ Object
-
#omml_content(display_style) ⇒ Object
-
#reprocess_value(obj) ⇒ Object
-
#to_asciimath ⇒ Object
-
#to_asciimath_math_zone(spacing = "", last = false, indent = true) ⇒ Object
-
#to_display(type = nil) ⇒ Object
-
#to_html ⇒ Object
-
#to_latex ⇒ Object
-
#to_latex_math_zone(spacing = "", last = false, indent = true) ⇒ Object
-
#to_mathml(display_style: displaystyle, split_on_linebreak: false) ⇒ Object
-
#to_mathml_math_zone(spacing = "", last = false, indent = true) ⇒ Object
-
#to_mathml_without_math_tag ⇒ Object
-
#to_omml(display_style: displaystyle, split_on_linebreak: false) ⇒ Object
-
#to_omml_math_zone(spacing = "", last = false, indent = true, display_style:) ⇒ Object
-
#to_omml_without_math_tag(display_style) ⇒ Object
-
#update(object) ⇒ Object
-
#validate_function_formula ⇒ Object
-
#value_exist? ⇒ Boolean
Methods inherited from Core
#ascii_fields_to_print, #class_name, #common_math_zone_conversion, #dump_mathml, #dump_nodes, #dump_omml, #dump_ox_nodes, #empty_tag, #extractable?, #filtered_values, #font_style_t_tag, #get, #gsub_spacing, #insert_t_tag, #invert_unicode_symbols, #is_unary?, #latex_fields_to_print, #linebreak, #mathml_fields_to_print, #omml_fields_to_print, #omml_nodes, #omml_parameter, #omml_tag_name, #ox_element, #r_element, #replacable_values, #result, #separate_table, #set, #tag_name, #updated_object_values, #validate_mathml_fields, #variable_value, #variables
Constructor Details
#initialize(value = [], left_right_wrapper = true, display_style: true, input_string: nil) ⇒ Formula
Returns a new instance of Formula.
15
16
17
18
19
20
21
22
23
24
25
|
# File 'lib/plurimath/math/formula.rb', line 15
def initialize(
value = [],
left_right_wrapper = true,
display_style: 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 = boolean_display_style(display_style)
end
|
Instance Attribute Details
#displaystyle ⇒ Object
Returns the value of attribute displaystyle.
6
7
8
|
# File 'lib/plurimath/math/formula.rb', line 6
def displaystyle
@displaystyle
end
|
Returns the value of attribute input_string.
6
7
8
|
# File 'lib/plurimath/math/formula.rb', line 6
def input_string
@input_string
end
|
#left_right_wrapper ⇒ Object
Returns the value of attribute left_right_wrapper.
6
7
8
|
# File 'lib/plurimath/math/formula.rb', line 6
def left_right_wrapper
@left_right_wrapper
end
|
#value ⇒ Object
Returns the value of attribute value.
6
7
8
|
# File 'lib/plurimath/math/formula.rb', line 6
def value
@value
end
|
Instance Method Details
#==(object) ⇒ Object
27
28
29
30
31
32
|
# File 'lib/plurimath/math/formula.rb', line 27
def ==(object)
object.respond_to?(:value) &&
object.respond_to?(:left_right_wrapper) &&
object.value == value &&
object.left_right_wrapper == left_right_wrapper
end
|
#cloned_objects ⇒ Object
206
207
208
209
210
211
|
# File 'lib/plurimath/math/formula.rb', line 206
def cloned_objects
cloned_obj = value.map(&:cloned_objects)
formula = self.class.new(cloned_obj)
formula.left_right_wrapper = @left_right_wrapper
formula
end
|
184
185
186
187
188
|
# File 'lib/plurimath/math/formula.rb', line 184
def
return unless value.length < 2 && value.first.is_a?(Function::Text)
value.first.parameter_one
end
|
#insert(values) ⇒ Object
245
246
247
|
# File 'lib/plurimath/math/formula.rb', line 245
def insert(values)
update(Array(value) + values)
end
|
#line_breaked_mathml(display_style) ⇒ Object
57
58
59
60
61
|
# File 'lib/plurimath/math/formula.rb', line 57
def line_breaked_mathml(display_style)
new_line_support.map do |formula|
formula.to_mathml(display_style: display_style)
end.join
end
|
#line_breaking(obj) ⇒ Object
221
222
223
224
225
226
227
228
229
230
231
232
233
234
|
# File 'lib/plurimath/math/formula.rb', line 221
def line_breaking(obj)
if result.size > 1
breaked_result = result.first.last.omml_line_break(result)
update(Array(breaked_result.shift))
obj.update(breaked_result.flatten)
reprocess_value(obj)
return
end
value.each.with_index(1) do |object, index|
object.line_breaking(obj)
break obj.insert(value.slice!(index..value.size)) if obj.value_exist?
end
end
|
#mathml_content ⇒ Object
72
73
74
|
# File 'lib/plurimath/math/formula.rb', line 72
def mathml_content
value.map(&:to_mathml_without_math_tag)
end
|
#nary_attr_value ⇒ Object
190
191
192
|
# File 'lib/plurimath/math/formula.rb', line 190
def nary_attr_value
value.first.nary_attr_value
end
|
#new_line_support(array = []) ⇒ Object
213
214
215
216
217
218
219
|
# File 'lib/plurimath/math/formula.rb', line 213
def new_line_support(array = [])
cloned = cloned_objects
obj = self.class.new
cloned.line_breaking(obj)
array << cloned
obj.value_exist? ? obj.new_line_support(array) : array
end
|
#omml_attrs ⇒ Object
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/plurimath/math/formula.rb', line 88
def omml_attrs
{
"xmlns:m": "http://schemas.openxmlformats.org/officeDocument/2006/math",
"xmlns:mc": "http://schemas.openxmlformats.org/markup-compatibility/2006",
"xmlns:mo": "http://schemas.microsoft.com/office/mac/office/2008/main",
"xmlns:mv": "urn:schemas-microsoft-com:mac:vml",
"xmlns:o": "urn:schemas-microsoft-com:office:office",
"xmlns:r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
"xmlns:v": "urn:schemas-microsoft-com:vml",
"xmlns:w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
"xmlns:w10": "urn:schemas-microsoft-com:office:word",
"xmlns:w14": "http://schemas.microsoft.com/office/word/2010/wordml",
"xmlns:w15": "http://schemas.microsoft.com/office/word/2012/wordml",
"xmlns:wne": "http://schemas.microsoft.com/office/word/2006/wordml",
"xmlns:wp": "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
"xmlns:wp14": "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
"xmlns:wpc": "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
"xmlns:wpg": "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",
"xmlns:wpi": "http://schemas.microsoft.com/office/word/2010/wordprocessingInk",
"xmlns:wps": "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
}
end
|
#omml_content(display_style) ⇒ Object
129
130
131
|
# File 'lib/plurimath/math/formula.rb', line 129
def omml_content(display_style)
value&.map { |val| val.insert_t_tag(display_style) }
end
|
#reprocess_value(obj) ⇒ Object
236
237
238
239
240
241
242
243
|
# File 'lib/plurimath/math/formula.rb', line 236
def reprocess_value(obj)
new_obj = self.class.new([])
self.line_breaking(new_obj)
if new_obj.value_exist?
obj.value.insert(0, Function::Linebreak.new)
obj.value.insert(0, self.class.new(new_obj.value))
end
end
|
#to_asciimath ⇒ Object
34
35
36
37
38
|
# File 'lib/plurimath/math/formula.rb', line 34
def to_asciimath
value.map(&:to_asciimath).join(" ")
rescue
parse_error!(:asciimath)
end
|
#to_asciimath_math_zone(spacing = "", last = false, indent = true) ⇒ Object
156
157
158
159
160
161
|
# File 'lib/plurimath/math/formula.rb', line 156
def to_asciimath_math_zone(spacing = "", last = false, indent = true)
filtered_values(value).map.with_index(1) do |object, index|
last = index == @values.length
object.to_asciimath_math_zone(new_space(spacing, indent), last, indent)
end
end
|
#to_display(type = nil) ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
# File 'lib/plurimath/math/formula.rb', line 137
def to_display(type = nil)
return type_error! unless MATH_ZONE_TYPES.include?(type.downcase.to_sym)
math_zone = case type
when :asciimath
" |_ \"#{to_asciimath}\"\n#{to_asciimath_math_zone(" ").join}"
when :latex
" |_ \"#{to_latex}\"\n#{to_latex_math_zone(" ").join}"
when :mathml
" |_ \"#{to_mathml.gsub(/\n\s*/, "")}\"\n#{to_mathml_math_zone(" ").join}"
when :omml
" |_ \"#{to_omml.gsub(/\n\s*/, "")}\"\n#{to_omml_math_zone(" ", display_style: displaystyle).join}"
end
<<~MATHZONE.sub(/\n$/, "")
|_ Math zone
#{math_zone}
MATHZONE
end
|
#to_html ⇒ Object
82
83
84
85
86
|
# File 'lib/plurimath/math/formula.rb', line 82
def to_html
value&.map(&:to_html)&.join(" ")
rescue
parse_error!(:html)
end
|
#to_latex ⇒ Object
76
77
78
79
80
|
# File 'lib/plurimath/math/formula.rb', line 76
def to_latex
value&.map(&:to_latex)&.join(" ")
rescue
parse_error!(:latex)
end
|
#to_latex_math_zone(spacing = "", last = false, indent = true) ⇒ Object
163
164
165
166
167
168
|
# File 'lib/plurimath/math/formula.rb', line 163
def to_latex_math_zone(spacing = "", last = false, indent = true)
filtered_values(value).map.with_index(1) do |object, index|
last = index == @values.length
object.to_latex_math_zone(new_space(spacing, indent), last, indent)
end
end
|
#to_mathml(display_style: displaystyle, split_on_linebreak: false) ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
# File 'lib/plurimath/math/formula.rb', line 40
def to_mathml(display_style: displaystyle, split_on_linebreak: false)
return line_breaked_mathml(display_style) if split_on_linebreak
math_attrs = {
xmlns: "http://www.w3.org/1998/Math/MathML",
display: "block",
}
style_attrs = { displaystyle: boolean_display_style(display_style) }
math = ox_element("math", attributes: math_attrs)
style = ox_element("mstyle", attributes: style_attrs)
Utility.update_nodes(style, mathml_content)
Utility.update_nodes(math, [style])
dump_nodes(math, indent: 2)
rescue
parse_error!(:mathml)
end
|
#to_mathml_math_zone(spacing = "", last = false, indent = true) ⇒ Object
170
171
172
173
174
175
|
# File 'lib/plurimath/math/formula.rb', line 170
def to_mathml_math_zone(spacing = "", last = false, indent = true)
filtered_values(value).map.with_index(1) do |object, index|
last = index == @values.length
object.to_mathml_math_zone(new_space(spacing, indent), last, indent)
end
end
|
#to_mathml_without_math_tag ⇒ Object
63
64
65
66
67
68
69
70
|
# File 'lib/plurimath/math/formula.rb', line 63
def to_mathml_without_math_tag
return mathml_content unless left_right_wrapper
Utility.update_nodes(
Utility.ox_element("mrow"),
mathml_content,
)
end
|
#to_omml(display_style: displaystyle, split_on_linebreak: false) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/plurimath/math/formula.rb', line 111
def to_omml(display_style: displaystyle, split_on_linebreak: false)
objects = split_on_linebreak ? new_line_support : [self]
para_element = Utility.ox_element("oMathPara", attributes: omml_attrs, namespace: "m")
objects.each.with_index(1) do |object, index|
para_element << Utility.update_nodes(
Utility.ox_element("oMath", namespace: "m"),
object.omml_content(boolean_display_style(display_style)),
)
next if objects.length == index
para_element << omml_br_tag
end
dump_nodes(para_element, indent: 2)
rescue
parse_error!(:omml)
end
|
#to_omml_math_zone(spacing = "", last = false, indent = true, display_style:) ⇒ Object
177
178
179
180
181
182
|
# File 'lib/plurimath/math/formula.rb', line 177
def to_omml_math_zone(spacing = "", last = false, indent = true, display_style:)
filtered_values(value).map.with_index(1) do |object, index|
last = index == @values.length
object.to_omml_math_zone(new_space(spacing, indent), last, indent, display_style: display_style)
end
end
|
#to_omml_without_math_tag(display_style) ⇒ Object
133
134
135
|
# File 'lib/plurimath/math/formula.rb', line 133
def to_omml_without_math_tag(display_style)
omml_content(display_style)
end
|
#update(object) ⇒ Object
202
203
204
|
# File 'lib/plurimath/math/formula.rb', line 202
def update(object)
self.value = Array(object)
end
|
194
195
196
|
# File 'lib/plurimath/math/formula.rb', line 194
def validate_function_formula
(value.none?(Function::Left) || value.none?(Function::Right))
end
|
#value_exist? ⇒ Boolean
198
199
200
|
# File 'lib/plurimath/math/formula.rb', line 198
def value_exist?
value && !value.empty?
end
|