Class: Plurimath::Math::Core
- Inherits:
-
Object
- Object
- Plurimath::Math::Core
show all
- Defined in:
- lib/plurimath/math/core.rb
Constant Summary
collapse
- REPLACABLES =
{
/&/ => "&",
/^\n/ => "",
}.freeze
- ALL_PARAMETERS =
%i[
parameter_one
parameter_two
parameter_three
parameter_four
].freeze
Class Method Summary
collapse
Instance Method Summary
collapse
-
#ascii_fields_to_print(field, options = {}) ⇒ Object
-
#class_name ⇒ Object
-
#cloned_objects ⇒ Object
-
#common_math_zone_conversion(field, options = {}) ⇒ Object
-
#dump_mathml(field, intent = false, options:) ⇒ Object
-
#dump_nodes(nodes, indent: nil) ⇒ Object
-
#dump_omml(field, display_style, options:) ⇒ Object
-
#dump_ox_nodes(nodes) ⇒ Object
-
#empty_tag(wrapper_tag = nil) ⇒ Object
-
#extract_class_name_from_text ⇒ Object
-
#extractable? ⇒ Boolean
-
#filtered_values(value, lang:, options: {}) ⇒ Object
-
#font_style_t_tag(display_style, options:) ⇒ Object
-
#get(variable) ⇒ Object
-
#gsub_spacing(spacing, last) ⇒ Object
-
#insert_t_tag(display_style, options:) ⇒ Object
-
#invert_unicode_symbols ⇒ Object
-
#is_binary_function? ⇒ Boolean
-
#is_mrow? ⇒ Boolean
-
#is_mstyle? ⇒ Boolean
-
#is_nary_function? ⇒ Boolean
-
#is_nary_symbol? ⇒ Boolean
-
#is_ternary_function? ⇒ Boolean
-
#is_unary? ⇒ Boolean
-
#latex_fields_to_print(field, options = {}) ⇒ Object
-
#line_breaking(obj) ⇒ Object
-
#linebreak? ⇒ Boolean
-
#mathml_fields_to_print(field, options = {}) ⇒ Object
-
#mathml_nodes(intent, options:) ⇒ Object
-
#mini_sized? ⇒ Boolean
-
#msty_tag_with_attrs ⇒ Object
-
#nary_attr_value ⇒ Object
-
#nary_intent_name ⇒ Object
-
#omml_fields_to_print(field, options = {}) ⇒ Object
-
#omml_nodes(display_style, options:) ⇒ Object
-
#omml_parameter(field, display_style, tag_name:, options:, namespace: "m") ⇒ Object
-
#omml_tag_name ⇒ Object
-
#ox_element(node, attributes: [], namespace: "") ⇒ Object
-
#paren? ⇒ Boolean
-
#pretty_print_instance_variables ⇒ Object
-
#prime_unicode?(field) ⇒ Boolean
-
#r_element(string, rpr_tag: true) ⇒ Object
-
#replacable_values(string) ⇒ Object
-
#result(value = []) ⇒ Object
-
#separate_table ⇒ Object
-
#set(variable, value) ⇒ Object
-
#symbol? ⇒ Boolean
-
#tag_name ⇒ Object
-
#to_ms_value ⇒ Object
-
#unicodemath_fields_to_print(field, options = {}) ⇒ Object
-
#unicodemath_parens(field, options:) ⇒ Object
-
#updated_object_values(param, obj:, update_value: false) ⇒ Object
-
#validate_function_formula ⇒ Object
-
#validate_mathml_fields(field, intent, options:) ⇒ Object
-
#variable_value(value) ⇒ Object
-
#variables ⇒ Object
Class Method Details
.descendants ⇒ Object
24
25
26
|
# File 'lib/plurimath/math/core.rb', line 24
def self.descendants
@descendants
end
|
.inherited(subclass) ⇒ Object
18
19
20
21
22
|
# File 'lib/plurimath/math/core.rb', line 18
def self.inherited(subclass)
@descendants ||= []
@descendants << subclass
super
end
|
Instance Method Details
#ascii_fields_to_print(field, options = {}) ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
|
# File 'lib/plurimath/math/core.rb', line 101
def ascii_fields_to_print(field, options = {})
return if field.nil?
hashed = common_math_zone_conversion(field, options)
options[:array] << "#{hashed[:spacing]}|_ \"#{field&.to_asciimath(options: options[:options])}\"#{hashed[:field_name]}\n"
return unless Utility.validate_math_zone(field, lang: :asciimath)
options[:array] << field&.to_asciimath_math_zone(
hashed[:function_spacing], hashed[:last], hashed[:indent], options: options[:options]
)
end
|
#class_name ⇒ Object
28
29
30
|
# File 'lib/plurimath/math/core.rb', line 28
def class_name
self.class.name.split("::").last.downcase
end
|
#cloned_objects ⇒ Object
241
242
243
244
245
|
# File 'lib/plurimath/math/core.rb', line 241
def cloned_objects
object = self.class.new(nil)
variables.each { |var| object.set(var, variable_value(get(var))) }
object
end
|
#common_math_zone_conversion(field, options = {}) ⇒ Object
193
194
195
196
197
198
199
200
201
|
# File 'lib/plurimath/math/core.rb', line 193
def common_math_zone_conversion(field, options = {})
{
spacing: options[:spacing],
last: options[:last] || true,
indent: !field.is_a?(Formula),
function_spacing: "#{options[:spacing]}#{options[:additional_space]}",
field_name: (options[:field_name] ? " #{options[:field_name]}" : ""),
}
end
|
#dump_mathml(field, intent = false, options:) ⇒ Object
163
164
165
166
167
|
# File 'lib/plurimath/math/core.rb', line 163
def dump_mathml(field, intent = false, options:)
dump_ox_nodes(field.mathml_nodes(intent, options: options)).gsub(
/\n\s*/, ""
)
end
|
#dump_nodes(nodes, indent: nil) ⇒ Object
214
215
216
217
218
|
# File 'lib/plurimath/math/core.rb', line 214
def dump_nodes(nodes, indent: nil)
replacable_values(
Plurimath.xml_engine.dump(nodes, indent: indent),
)
end
|
#dump_omml(field, display_style, options:) ⇒ Object
169
170
171
172
173
174
175
|
# File 'lib/plurimath/math/core.rb', line 169
def dump_omml(field, display_style, options:)
return if field.nil?
dump_ox_nodes(field.omml_nodes(display_style, options: options)).gsub(
/\n\s*/, ""
)
end
|
#dump_ox_nodes(nodes) ⇒ Object
208
209
210
211
212
|
# File 'lib/plurimath/math/core.rb', line 208
def dump_ox_nodes(nodes)
return dump_nodes(nodes) unless nodes.is_a?(Array)
nodes.flatten.map { |node| dump_nodes(node) }.join
end
|
#empty_tag(wrapper_tag = nil) ⇒ Object
48
49
50
51
52
53
54
|
# File 'lib/plurimath/math/core.rb', line 48
def empty_tag(wrapper_tag = nil)
r_tag = ox_element("r", namespace: "m")
r_tag << (ox_element("t", namespace: "m") << "​")
return r_tag unless wrapper_tag
wrapper_tag << r_tag
end
|
93
94
95
|
# File 'lib/plurimath/math/core.rb', line 93
def
""
end
|
89
90
91
|
# File 'lib/plurimath/math/core.rb', line 89
def
false
end
|
#filtered_values(value, lang:, options: {}) ⇒ Object
203
204
205
206
|
# File 'lib/plurimath/math/core.rb', line 203
def filtered_values(value, lang:, options: {})
@values = Utility.filter_math_zone_values(value, lang: lang,
options: options)
end
|
#font_style_t_tag(display_style, options:) ⇒ Object
97
98
99
|
# File 'lib/plurimath/math/core.rb', line 97
def font_style_t_tag(display_style, options:)
omml_nodes(display_style, options: options)
end
|
#get(variable) ⇒ Object
301
302
303
|
# File 'lib/plurimath/math/core.rb', line 301
def get(variable)
instance_variable_get(variable)
end
|
#gsub_spacing(spacing, last) ⇒ Object
225
226
227
|
# File 'lib/plurimath/math/core.rb', line 225
def gsub_spacing(spacing, last)
spacing.gsub("|_", last ? " " : "| ")
end
|
#insert_t_tag(display_style, options:) ⇒ Object
32
33
34
|
# File 'lib/plurimath/math/core.rb', line 32
def insert_t_tag(display_style, options:)
Array(omml_nodes(display_style, options: options))
end
|
#invert_unicode_symbols ⇒ Object
229
230
231
|
# File 'lib/plurimath/math/core.rb', line 229
def invert_unicode_symbols
Mathml::Constants::UNICODE_SYMBOLS.invert[class_name] || class_name
end
|
#is_binary_function? ⇒ Boolean
344
345
346
|
# File 'lib/plurimath/math/core.rb', line 344
def is_binary_function?
is_a?(Function::BinaryFunction)
end
|
#is_mrow? ⇒ Boolean
412
413
414
|
# File 'lib/plurimath/math/core.rb', line 412
def is_mrow?
false
end
|
#is_mstyle? ⇒ Boolean
408
409
410
|
# File 'lib/plurimath/math/core.rb', line 408
def is_mstyle?
false
end
|
#is_nary_function? ⇒ Boolean
334
|
# File 'lib/plurimath/math/core.rb', line 334
def is_nary_function?; end
|
#is_nary_symbol? ⇒ Boolean
336
|
# File 'lib/plurimath/math/core.rb', line 336
def is_nary_symbol?; end
|
#is_ternary_function? ⇒ Boolean
348
349
350
|
# File 'lib/plurimath/math/core.rb', line 348
def is_ternary_function?
is_a?(Function::TernaryFunction)
end
|
#latex_fields_to_print(field, options = {}) ⇒ Object
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/plurimath/math/core.rb', line 113
def latex_fields_to_print(field, options = {})
return if field.nil?
hashed = common_math_zone_conversion(field, options)
options[:array] << "#{hashed[:spacing]}|_ \"#{field&.to_latex(options: options[:options])}\"#{hashed[:field_name]}\n"
return unless Utility.validate_math_zone(field, lang: :latex)
options[:array] << field&.to_latex_math_zone(hashed[:function_spacing],
hashed[:last], hashed[:indent], options: options[:options])
end
|
#line_breaking(obj) ⇒ Object
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
# File 'lib/plurimath/math/core.rb', line 258
def line_breaking(obj)
variables.each do |variable|
field = get(variable)
case field
when Core
field.line_breaking(obj)
if obj.value_exist?
updated_object_values(variable, obj: obj,
update_value: true)
end
when Array
array_line_break_field(field, variable, obj)
end
end
end
|
#linebreak? ⇒ Boolean
237
238
239
|
# File 'lib/plurimath/math/core.rb', line 237
def linebreak?
false
end
|
#mathml_fields_to_print(field, options = {}) ⇒ Object
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/plurimath/math/core.rb', line 124
def mathml_fields_to_print(field, options = {})
return if field.nil?
hashed = common_math_zone_conversion(field, options)
options[:array] << "#{hashed[:spacing]}|_ \"#{dump_mathml(field,
options: options[:options])}\"#{hashed[:field_name]}\n"
return unless Utility.validate_math_zone(field, lang: :mathml,
intent: options[:intent], options: options[:options])
options[:array] << field&.to_mathml_math_zone(
hashed[:function_spacing], hashed[:last], hashed[:indent], options: options[:options]
)
end
|
#mathml_nodes(intent, options:) ⇒ Object
177
178
179
|
# File 'lib/plurimath/math/core.rb', line 177
def mathml_nodes(intent, options:)
to_mathml_without_math_tag(intent, options: options)
end
|
#mini_sized? ⇒ Boolean
352
353
354
|
# File 'lib/plurimath/math/core.rb', line 352
def mini_sized?
false
end
|
#msty_tag_with_attrs ⇒ Object
84
85
86
87
|
# File 'lib/plurimath/math/core.rb', line 84
def msty_tag_with_attrs
attrs = { "m:val": "p" }
ox_element("sty", namespace: "m", attributes: attrs)
end
|
#nary_attr_value ⇒ Object
44
45
46
|
# File 'lib/plurimath/math/core.rb', line 44
def nary_attr_value(**)
""
end
|
#nary_intent_name ⇒ Object
338
|
# File 'lib/plurimath/math/core.rb', line 338
def nary_intent_name; end
|
#omml_fields_to_print(field, options = {}) ⇒ Object
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/plurimath/math/core.rb', line 138
def omml_fields_to_print(field, options = {})
return if field.nil?
hashed = common_math_zone_conversion(field, options)
display_style = options[:display_style]
options[:array] << "#{hashed[:spacing]}|_ \"#{dump_omml(field,
display_style, options: options[:options])}\"#{hashed[:field_name]}\n"
return unless Utility.validate_math_zone(field, lang: :omml)
options[:array] << field&.to_omml_math_zone(hashed[:function_spacing],
hashed[:last], hashed[:indent], display_style: display_style, options: options[:options])
end
|
#omml_nodes(display_style, options:) ⇒ Object
181
182
183
|
# File 'lib/plurimath/math/core.rb', line 181
def omml_nodes(display_style, options:)
to_omml_without_math_tag(display_style, options: options)
end
|
#omml_parameter(field, display_style, tag_name:, options:, namespace: "m") ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/plurimath/math/core.rb', line 56
def omml_parameter(field, display_style, tag_name:, options:,
namespace: "m")
tag = ox_element(tag_name, namespace: namespace)
return empty_tag(tag) unless field
field_value = if field.is_a?(Array)
field.map do |object|
object.insert_t_tag(display_style, options: options)
end
else
field.insert_t_tag(display_style, options: options)
end
Utility.update_nodes(tag, field_value)
end
|
#omml_tag_name ⇒ Object
40
41
42
|
# File 'lib/plurimath/math/core.rb', line 40
def omml_tag_name
"subSup"
end
|
#ox_element(node, attributes: [], namespace: "") ⇒ Object
317
318
319
320
321
322
323
|
# File 'lib/plurimath/math/core.rb', line 317
def ox_element(node, attributes: [], namespace: "")
Utility.ox_element(
node,
attributes: attributes,
namespace: namespace,
)
end
|
#paren? ⇒ Boolean
372
373
374
|
# File 'lib/plurimath/math/core.rb', line 372
def paren?
false
end
|
#pretty_print_instance_variables ⇒ Object
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
|
# File 'lib/plurimath/math/core.rb', line 376
def pretty_print_instance_variables
excluded_vars = %i[
@left_right_wrapper
@using_default
@displaystyle
@__encoding
@__register
@__ordered
@unitsml
@__mixed
@is_mrow
@values
]
instance_variables.sort - excluded_vars
end
|
#prime_unicode?(field) ⇒ Boolean
363
364
365
366
367
368
369
370
|
# File 'lib/plurimath/math/core.rb', line 363
def prime_unicode?(field)
return false unless field.is_a?(Math::Symbols::Symbol)
return true if field&.value&.include?("'")
Utility.primes_constants.any? do |_prefix, prime|
unicodemath_field_value(field).include?(prime)
end
end
|
#r_element(string, rpr_tag: true) ⇒ Object
75
76
77
78
79
80
81
82
|
# File 'lib/plurimath/math/core.rb', line 75
def r_element(string, rpr_tag: true)
r_tag = ox_element("r", namespace: "m")
if rpr_tag
r_tag << (ox_element("rPr", namespace: "m") << msty_tag_with_attrs)
end
r_tag << (ox_element("t", namespace: "m") << string)
Array(r_tag)
end
|
#replacable_values(string) ⇒ Object
220
221
222
223
|
# File 'lib/plurimath/math/core.rb', line 220
def replacable_values(string)
REPLACABLES.each { |regex, str| string = string.gsub(regex, str) }
string
end
|
#result(value = []) ⇒ Object
325
326
327
328
|
# File 'lib/plurimath/math/core.rb', line 325
def result(value = [])
value = get("@value") || value
value.slice_after { |d| d.is_a?(Math::Function::Linebreak) }.to_a
end
|
#separate_table ⇒ Object
233
234
235
|
# File 'lib/plurimath/math/core.rb', line 233
def separate_table
false
end
|
#set(variable, value) ⇒ Object
305
306
307
|
# File 'lib/plurimath/math/core.rb', line 305
def set(variable, value)
instance_variable_set(variable, value)
end
|
#symbol? ⇒ Boolean
340
341
342
|
# File 'lib/plurimath/math/core.rb', line 340
def symbol?
is_a?(Math::Symbols::Symbol)
end
|
#tag_name ⇒ Object
36
37
38
|
# File 'lib/plurimath/math/core.rb', line 36
def tag_name
"subsup"
end
|
#to_ms_value ⇒ Object
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
# File 'lib/plurimath/math/core.rb', line 392
def to_ms_value
new_arr = []
case self
when Math::Symbols::Symbol
new_arr << (value ? value.to_s : to_unicodemath(options: {}))
when Math::Number, Math::Function::Text
new_arr << value
else
parameters_to_ms_value(new_arr)
if respond_to?(:value) && value.is_a?(Array)
new_arr << value&.map(&:to_ms_value)&.join(" ")
end
end
new_arr
end
|
#unicodemath_fields_to_print(field, options = {}) ⇒ Object
151
152
153
154
155
156
157
158
159
160
161
|
# File 'lib/plurimath/math/core.rb', line 151
def unicodemath_fields_to_print(field, options = {})
return if field.nil?
hashed = common_math_zone_conversion(field, options)
options[:array] << "#{hashed[:spacing]}|_ \"#{field&.to_unicodemath(options: options[:options])}\"#{hashed[:field_name]}\n"
return unless Utility.validate_math_zone(field, lang: :unicodemath)
options[:array] << field&.to_unicodemath_math_zone(
hashed[:function_spacing], hashed[:last], hashed[:indent], options: options[:options]
)
end
|
#unicodemath_parens(field, options:) ⇒ Object
356
357
358
359
360
361
|
# File 'lib/plurimath/math/core.rb', line 356
def unicodemath_parens(field, options:)
paren = field.to_unicodemath(options: options)
return paren if field.is_a?(Math::Function::Fenced)
"(#{paren})" if field
end
|
#updated_object_values(param, obj:, update_value: false) ⇒ Object
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
|
# File 'lib/plurimath/math/core.rb', line 274
def updated_object_values(param, obj:, update_value: false)
object = self.class.new(nil)
found = false
variables.each do |variable|
value = if param == variable
found = true
if update_value
return_value = obj.value
obj.value = []
return_value
else
formula = Formula.new(get(variable))
formula.line_breaking(obj)
set(variable, obj)
get(variable)
end
else
return_value = get(variable)
set(variable, nil) if found
return_value
end
object.set(variable, Utility.filter_values(value))
end
object.hide_function_name = true if object.methods.include?(:hide_function_name)
obj.update(object)
end
|
71
72
73
|
# File 'lib/plurimath/math/core.rb', line 71
def validate_function_formula
true
end
|
#validate_mathml_fields(field, intent, options:) ⇒ Object
185
186
187
188
189
190
191
|
# File 'lib/plurimath/math/core.rb', line 185
def validate_mathml_fields(field, intent, options:)
if field.is_a?(Array)
field&.map { |object| object.mathml_nodes(intent, options: options) }
else
field&.mathml_nodes(intent, options: options)
end
end
|
#variable_value(value) ⇒ Object
247
248
249
250
251
252
253
254
255
256
|
# File 'lib/plurimath/math/core.rb', line 247
def variable_value(value)
case value
when Core
value.cloned_objects
when Array
value.map { |object| variable_value(object) }
else
value
end
end
|
#variables ⇒ Object
309
310
311
312
313
314
315
|
# File 'lib/plurimath/math/core.rb', line 309
def variables
instance_variables.reject do |var|
name = var.to_s
name.start_with?("@__", "@lutaml") ||
name == "@using_default"
end
end
|