Class: Plurimath::Math::Formula
Defined Under Namespace
Classes: Mrow, Mstyle
Constant Summary
collapse
- POWER_BASE_CLASSES =
%w[powerbase power base].freeze
- DERIVATIVE_CONSTS =
["𝑑", "ⅅ", "ⅆ", "d"].freeze
- MATH_ZONE_TYPES =
%i[omml latex mathml asciimath unicodemath].freeze
- OMML_NAMESPACES =
{
"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",
}.freeze
Constants inherited
from Core
Core::ALL_PARAMETERS, 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
-
#intent=(value) ⇒ Object
-
#intent_names ⇒ Object
-
#line_breaked_mathml(display_style, intent, options:) ⇒ Object
-
#line_breaking(obj) ⇒ Object
-
#mathcolor=(value) ⇒ Object
-
#mathml_content(intent, options:) ⇒ Object
-
#mathvariant=(value) ⇒ Object
-
#mini_sized? ⇒ Boolean
-
#nary_attr_value(options:) ⇒ Object
-
#new_line_support(array = []) ⇒ Object
-
#omml_content(display_style, options:) ⇒ Object
-
#reprocess_value(obj) ⇒ Object
-
#to_asciimath(formatter: nil, unitsml: {}, options: nil) ⇒ Object
-
#to_asciimath_math_zone(spacing = "", last = false, indent = true, options:) ⇒ Object
-
#to_display(type = nil, formatter: nil, unitsml: {}, unary_function_spacing: true) ⇒ Object
-
#to_html(formatter: nil, unitsml: {}, options: nil) ⇒ Object
-
#to_latex(formatter: nil, unitsml: {}, options: nil) ⇒ Object
-
#to_latex_math_zone(spacing = "", last = false, indent = true, options:) ⇒ Object
-
#to_mathml(intent: false, formatter: nil, unitsml: {}, split_on_linebreak: false, display_style: displaystyle, unary_function_spacing: true) ⇒ Object
-
#to_mathml_math_zone(spacing = "", last = false, indent = true, options:) ⇒ Object
-
#to_mathml_without_math_tag(intent, options:) ⇒ Object
-
#to_omml(display_style: displaystyle, split_on_linebreak: false, formatter: nil, unitsml: {}) ⇒ Object
-
#to_omml_math_zone(spacing = "", last = false, indent = true, display_style:, options:) ⇒ Object
-
#to_omml_without_math_tag(display_style, options:) ⇒ Object
-
#to_unicodemath(formatter: nil, unitsml: {}, options: nil) ⇒ Object
-
#to_unicodemath_math_zone(spacing = "", last = false, indent = true, options:) ⇒ 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, descendants, #dump_mathml, #dump_nodes, #dump_omml, #dump_ox_nodes, #empty_tag, #extractable?, #filtered_values, #font_style_t_tag, #get, #gsub_spacing, inherited, #insert_t_tag, #invert_unicode_symbols, #is_binary_function?, #is_mrow?, #is_mstyle?, #is_nary_function?, #is_nary_symbol?, #is_ternary_function?, #is_unary?, #latex_fields_to_print, #linebreak?, #mathml_fields_to_print, #mathml_nodes, #msty_tag_with_attrs, #nary_intent_name, #omml_fields_to_print, #omml_nodes, #omml_parameter, #omml_tag_name, #ox_element, #paren?, #pretty_print_instance_variables, #prime_unicode?, #r_element, #replacable_values, #result, #separate_table, #set, #symbol?, #tag_name, #to_ms_value, #unicodemath_fields_to_print, #unicodemath_parens, #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.
38
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/plurimath/math/formula.rb', line 38
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
#display ⇒ Object
Returns the value of attribute display.
11
12
13
|
# File 'lib/plurimath/math/formula.rb', line 11
def display
@display
end
|
#displaystyle ⇒ Object
Returns the value of attribute displaystyle.
11
12
13
|
# File 'lib/plurimath/math/formula.rb', line 11
def displaystyle
@displaystyle
end
|
Returns the value of attribute input_string.
11
12
13
|
# File 'lib/plurimath/math/formula.rb', line 11
def input_string
@input_string
end
|
#left_right_wrapper ⇒ Object
Returns the value of attribute left_right_wrapper.
11
12
13
|
# File 'lib/plurimath/math/formula.rb', line 11
def left_right_wrapper
@left_right_wrapper
end
|
#value ⇒ Object
Returns the value of attribute value.
11
12
13
|
# File 'lib/plurimath/math/formula.rb', line 11
def value
@value
end
|
Instance Method Details
#==(object) ⇒ Object
50
51
52
53
54
55
|
# File 'lib/plurimath/math/formula.rb', line 50
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
301
302
303
304
305
306
|
# File 'lib/plurimath/math/formula.rb', line 301
def cloned_objects
cloned_obj = value.map(&:cloned_objects)
formula = self.class.new(cloned_obj)
formula.left_right_wrapper = @left_right_wrapper
formula
end
|
279
280
281
282
283
|
# File 'lib/plurimath/math/formula.rb', line 279
def
return unless value.length < 2 && value.first.is_a?(Function::Text)
value.first.parameter_one
end
|
#insert(values) ⇒ Object
340
341
342
|
# File 'lib/plurimath/math/formula.rb', line 340
def insert(values)
update(Array(value) + values)
end
|
#intent=(value) ⇒ Object
383
384
385
386
387
388
389
390
391
392
393
394
395
|
# File 'lib/plurimath/math/formula.rb', line 383
def intent=(value)
return unless value
self.content = nil
update(
[
Function::Intent.new(
filter_values(@value, array_to_instance: true),
Function::Text.new(value),
),
],
)
end
|
#intent_names ⇒ Object
348
349
350
351
352
353
|
# File 'lib/plurimath/math/formula.rb', line 348
def intent_names
{
partial_derivative: ":partial-derivative",
derivative: ":derivative",
}
end
|
#line_breaked_mathml(display_style, intent, options:) ⇒ Object
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/plurimath/math/formula.rb', line 101
def line_breaked_mathml(display_style, intent, options:)
new_line_support.map do |formula|
formula.to_mathml(
display_style: display_style,
intent: intent,
formatter: options[:formatter],
unary_function_spacing: options[:unary_function_spacing],
)
end.join
end
|
#line_breaking(obj) ⇒ Object
316
317
318
319
320
321
322
323
324
325
326
327
328
329
|
# File 'lib/plurimath/math/formula.rb', line 316
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
|
#mathcolor=(value) ⇒ Object
356
357
358
359
360
361
362
363
364
365
366
367
|
# File 'lib/plurimath/math/formula.rb', line 356
def mathcolor=(value)
return if value.nil? || value.empty?
update(
[
Math::Function::Color.new(
Math::Function::Text.new(value),
filter_values(@value, array_to_instance: true),
),
],
)
end
|
#mathml_content(intent, options:) ⇒ Object
124
125
126
127
128
129
130
|
# File 'lib/plurimath/math/formula.rb', line 124
def mathml_content(intent, options:)
nodes = value.map do |val|
val.to_mathml_without_math_tag(intent, options: options)
end
intent_post_processing(nodes, intent) if intent
nodes
end
|
#mathvariant=(value) ⇒ Object
369
370
371
372
373
374
375
376
377
378
379
380
381
|
# File 'lib/plurimath/math/formula.rb', line 369
def mathvariant=(value)
return if value.nil? || value.empty?
return unless Plurimath::Utility::FONT_STYLES.key?(value.to_sym)
update(
[
Plurimath::Utility::FONT_STYLES[value.to_sym].new(
filter_values(@value, array_to_instance: true),
value,
),
],
)
end
|
#mini_sized? ⇒ Boolean
344
345
346
|
# File 'lib/plurimath/math/formula.rb', line 344
def mini_sized?
true if value&.first&.mini_sized?
end
|
#nary_attr_value(options:) ⇒ Object
285
286
287
|
# File 'lib/plurimath/math/formula.rb', line 285
def nary_attr_value(options:)
value.first.nary_attr_value(options: Hash.new(options))
end
|
#new_line_support(array = []) ⇒ Object
308
309
310
311
312
313
314
|
# File 'lib/plurimath/math/formula.rb', line 308
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_content(display_style, options:) ⇒ Object
170
171
172
|
# File 'lib/plurimath/math/formula.rb', line 170
def omml_content(display_style, options:)
value&.map { |val| val.insert_t_tag(display_style, options: options) }
end
|
#reprocess_value(obj) ⇒ Object
331
332
333
334
335
336
337
338
|
# File 'lib/plurimath/math/formula.rb', line 331
def reprocess_value(obj)
new_obj = self.class.new([])
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(formatter: nil, unitsml: {}, options: nil) ⇒ Object
57
58
59
60
61
62
63
64
65
|
# File 'lib/plurimath/math/formula.rb', line 57
def to_asciimath(formatter: nil, unitsml: {}, options: nil)
options ||= { formatter: formatter, unitsml: unitsml }.compact
options[:formula] ||= self
wrap_render_error(:asciimath) do
value.map do |val|
val.to_asciimath(options: asciimath_table_options(options, val))
end.join(" ")
end
end
|
#to_asciimath_math_zone(spacing = "", last = false, indent = true, options:) ⇒ Object
230
231
232
233
234
235
236
237
238
|
# File 'lib/plurimath/math/formula.rb', line 230
def to_asciimath_math_zone(spacing = "", last = false, indent = true,
options:)
filtered_values(value,
lang: :asciimath).map.with_index(1) do |object, index|
last = index == @values.length
object.to_asciimath_math_zone(new_space(spacing, indent), last,
indent, options: options)
end
end
|
#to_display(type = nil, formatter: nil, unitsml: {}, unary_function_spacing: true) ⇒ Object
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
# File 'lib/plurimath/math/formula.rb', line 188
def to_display(type = nil, formatter: nil, unitsml: {},
unary_function_spacing: true)
options = {
formatter: formatter,
unitsml: unitsml,
unary_function_spacing: unary_function_spacing,
}
options[:formula] ||= self
return type_error!(type) unless MATH_ZONE_TYPES.include?(type.downcase.to_sym)
math_zone = case type
when :asciimath
" |_ \"#{to_asciimath(options: options)}\"\n#{to_asciimath_math_zone(
' ', options: options
).join}"
when :latex
" |_ \"#{to_latex(options: options)}\"\n#{to_latex_math_zone(
' ', options: options
).join}"
when :mathml
mathml = to_mathml(formatter: formatter, unary_function_spacing: options[:unary_function_spacing]).gsub(
/\n\s*/, ""
)
math_display = to_mathml_math_zone(" ",
options: options).join
" |_ \"#{mathml}\"\n#{math_display}"
when :omml
omml = to_omml.gsub(/\n\s*/, "")
omml_display = to_omml_math_zone(" ",
display_style: displaystyle, options: options).join
" |_ \"#{omml}\"\n#{omml_display}"
when :unicodemath
" |_ \"#{to_unicodemath(options: options)}\"\n#{to_unicodemath_math_zone(
' ', options: options
).join}"
end
<<~MATHZONE.sub(/\n$/, "")
|_ Math zone
#{math_zone}
MATHZONE
end
|
#to_html(formatter: nil, unitsml: {}, options: nil) ⇒ Object
140
141
142
143
144
145
146
|
# File 'lib/plurimath/math/formula.rb', line 140
def to_html(formatter: nil, unitsml: {}, options: nil)
options ||= { formatter: formatter, unitsml: unitsml }.compact
options[:formula] ||= self
wrap_render_error(:html) do
value&.map { |val| val.to_html(options: options) }&.join(" ")
end
end
|
#to_latex(formatter: nil, unitsml: {}, options: nil) ⇒ Object
132
133
134
135
136
137
138
|
# File 'lib/plurimath/math/formula.rb', line 132
def to_latex(formatter: nil, unitsml: {}, options: nil)
options ||= { formatter: formatter, unitsml: unitsml }.compact
options[:formula] ||= self
wrap_render_error(:latex) do
value.map { |val| val.to_latex(options: options) }.join(" ")
end
end
|
#to_latex_math_zone(spacing = "", last = false, indent = true, options:) ⇒ Object
240
241
242
243
244
245
246
247
248
|
# File 'lib/plurimath/math/formula.rb', line 240
def to_latex_math_zone(spacing = "", last = false, indent = true,
options:)
filtered_values(value,
lang: :latex).map.with_index(1) do |object, index|
last = index == @values.length
object.to_latex_math_zone(new_space(spacing, indent), last, indent,
options: options)
end
end
|
#to_mathml(intent: false, formatter: nil, unitsml: {}, split_on_linebreak: false, display_style: displaystyle, unary_function_spacing: true) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
# File 'lib/plurimath/math/formula.rb', line 67
def to_mathml(
intent: false,
formatter: nil,
unitsml: {},
split_on_linebreak: false,
display_style: displaystyle,
unary_function_spacing: true
)
options = {
formatter: formatter,
unitsml: unitsml,
unary_function_spacing: unary_function_spacing,
}.compact
options[:formula] ||= self
if split_on_linebreak
return line_breaked_mathml(display_style, intent,
options: options)
end
wrap_render_error(:mathml) do
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(intent, options: options))
Utility.update_nodes(math, [style])
unitsml_post_processing(math, style)
dump_nodes(math, indent: 2)
end
end
|
#to_mathml_math_zone(spacing = "", last = false, indent = true, options:) ⇒ Object
250
251
252
253
254
255
256
257
258
|
# File 'lib/plurimath/math/formula.rb', line 250
def to_mathml_math_zone(spacing = "", last = false, indent = true,
options:)
filtered_values(value, lang: :mathml,
options: options).map.with_index(1) do |object, index|
last = index == @values.length
object.to_mathml_math_zone(new_space(spacing, indent), last, indent,
options: options)
end
end
|
#to_mathml_without_math_tag(intent, options:) ⇒ Object
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/plurimath/math/formula.rb', line 112
def to_mathml_without_math_tag(intent, options:)
unless left_right_wrapper
return mathml_content(intent,
options: options)
end
mathml_value = mathml_content(intent, options: options)
attributes = intent_attribute(mathml_value) if intent
mrow = ox_element("mrow", attributes: attributes)
Utility.update_nodes(mrow, mathml_value)
end
|
#to_omml(display_style: displaystyle, split_on_linebreak: false, formatter: nil, unitsml: {}) ⇒ Object
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/plurimath/math/formula.rb', line 148
def to_omml(display_style: displaystyle, split_on_linebreak: false,
formatter: nil, unitsml: {})
wrap_render_error(:omml) do
objects = split_on_linebreak ? new_line_support : [self]
options = { formatter: formatter, unitsml: unitsml }.compact
options[:formula] ||= self
para_element = Utility.ox_element("oMathPara",
attributes: OMML_NAMESPACES, 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),
options: options),
)
next if objects.length == index
para_element << omml_br_tag
end
dump_nodes(para_element, indent: 2)
end
end
|
#to_omml_math_zone(spacing = "", last = false, indent = true, display_style:, options:) ⇒ Object
260
261
262
263
264
265
266
267
|
# File 'lib/plurimath/math/formula.rb', line 260
def to_omml_math_zone(spacing = "", last = false, indent = true,
display_style:, options:)
filtered_values(value, lang: :omml).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, options: options)
end
end
|
#to_omml_without_math_tag(display_style, options:) ⇒ Object
174
175
176
|
# File 'lib/plurimath/math/formula.rb', line 174
def to_omml_without_math_tag(display_style, options:)
omml_content(display_style, options: options)
end
|
#to_unicodemath(formatter: nil, unitsml: {}, options: nil) ⇒ Object
178
179
180
181
182
183
184
185
186
|
# File 'lib/plurimath/math/formula.rb', line 178
def to_unicodemath(formatter: nil, unitsml: {}, options: nil)
options ||= { formatter: formatter, unitsml: unitsml }.compact
options[:formula] ||= self
wrap_render_error(:unicodemath) do
Utility.html_entity_to_unicode(unicodemath_value(options: options)).gsub(
/\s\/\s/, "/"
)
end
end
|
#to_unicodemath_math_zone(spacing = "", last = false, indent = true, options:) ⇒ Object
269
270
271
272
273
274
275
276
277
|
# File 'lib/plurimath/math/formula.rb', line 269
def to_unicodemath_math_zone(spacing = "", last = false, indent = true,
options:)
filtered_values(value,
lang: :unicodemath).map.with_index(1) do |object, index|
last = index == @values.length
object.to_unicodemath_math_zone(new_space(spacing, indent), last,
indent, options: options)
end
end
|
#update(object) ⇒ Object
297
298
299
|
# File 'lib/plurimath/math/formula.rb', line 297
def update(object)
@value = Array(object).flatten.compact
end
|
289
290
291
|
# File 'lib/plurimath/math/formula.rb', line 289
def validate_function_formula
value.none?(Function::Left) || value.none?(Function::Right)
end
|
#value_exist? ⇒ Boolean
293
294
295
|
# File 'lib/plurimath/math/formula.rb', line 293
def value_exist?
value && !value.empty?
end
|