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
100
101
102
103
104
105
106
107
108
109
110
|
# File 'lib/plurimath/math/core.rb', line 100
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
240
241
242
243
244
|
# File 'lib/plurimath/math/core.rb', line 240
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
192
193
194
195
196
197
198
199
200
|
# File 'lib/plurimath/math/core.rb', line 192
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
162
163
164
165
166
|
# File 'lib/plurimath/math/core.rb', line 162
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
213
214
215
216
217
|
# File 'lib/plurimath/math/core.rb', line 213
def dump_nodes(nodes, indent: nil)
replacable_values(
Plurimath.xml_engine.dump(nodes, indent: indent),
)
end
|
#dump_omml(field, display_style, options:) ⇒ Object
168
169
170
171
172
173
174
|
# File 'lib/plurimath/math/core.rb', line 168
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
207
208
209
210
211
|
# File 'lib/plurimath/math/core.rb', line 207
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
|
92
93
94
|
# File 'lib/plurimath/math/core.rb', line 92
def
""
end
|
88
89
90
|
# File 'lib/plurimath/math/core.rb', line 88
def
false
end
|
#filtered_values(value, lang:, options: {}) ⇒ Object
202
203
204
205
|
# File 'lib/plurimath/math/core.rb', line 202
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
96
97
98
|
# File 'lib/plurimath/math/core.rb', line 96
def font_style_t_tag(display_style, options:)
omml_nodes(display_style, options: options)
end
|
#get(variable) ⇒ Object
298
299
300
|
# File 'lib/plurimath/math/core.rb', line 298
def get(variable)
instance_variable_get(variable)
end
|
#gsub_spacing(spacing, last) ⇒ Object
224
225
226
|
# File 'lib/plurimath/math/core.rb', line 224
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
228
229
230
|
# File 'lib/plurimath/math/core.rb', line 228
def invert_unicode_symbols
Mathml::Constants::UNICODE_SYMBOLS.invert[class_name] || class_name
end
|
#is_binary_function? ⇒ Boolean
341
342
343
|
# File 'lib/plurimath/math/core.rb', line 341
def is_binary_function?
is_a?(Function::BinaryFunction)
end
|
#is_mrow? ⇒ Boolean
409
410
411
|
# File 'lib/plurimath/math/core.rb', line 409
def is_mrow?
false
end
|
#is_mstyle? ⇒ Boolean
405
406
407
|
# File 'lib/plurimath/math/core.rb', line 405
def is_mstyle?
false
end
|
#is_nary_function? ⇒ Boolean
331
|
# File 'lib/plurimath/math/core.rb', line 331
def is_nary_function?; end
|
#is_nary_symbol? ⇒ Boolean
333
|
# File 'lib/plurimath/math/core.rb', line 333
def is_nary_symbol?; end
|
#is_ternary_function? ⇒ Boolean
345
346
347
|
# File 'lib/plurimath/math/core.rb', line 345
def is_ternary_function?
is_a?(Function::TernaryFunction)
end
|
#latex_fields_to_print(field, options = {}) ⇒ Object
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/plurimath/math/core.rb', line 112
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
257
258
259
260
261
262
263
264
265
266
267
268
269
|
# File 'lib/plurimath/math/core.rb', line 257
def line_breaking(obj)
variables.each do |variable|
field = get(variable)
case field
when Core
field.line_breaking(obj)
updated_object_values(variable, obj: obj,
update_value: true) if obj.value_exist?
when Array
array_line_break_field(field, variable, obj)
end
end
end
|
#linebreak? ⇒ Boolean
236
237
238
|
# File 'lib/plurimath/math/core.rb', line 236
def linebreak?
false
end
|
#mathml_fields_to_print(field, options = {}) ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# File 'lib/plurimath/math/core.rb', line 123
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
176
177
178
|
# File 'lib/plurimath/math/core.rb', line 176
def mathml_nodes(intent, options:)
to_mathml_without_math_tag(intent, options: options)
end
|
#mini_sized? ⇒ Boolean
349
350
351
|
# File 'lib/plurimath/math/core.rb', line 349
def mini_sized?
false
end
|
#msty_tag_with_attrs ⇒ Object
83
84
85
86
|
# File 'lib/plurimath/math/core.rb', line 83
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
335
|
# File 'lib/plurimath/math/core.rb', line 335
def nary_intent_name; end
|
#omml_fields_to_print(field, options = {}) ⇒ Object
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/plurimath/math/core.rb', line 137
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
180
181
182
|
# File 'lib/plurimath/math/core.rb', line 180
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
|
# 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 { |object|
object.insert_t_tag(display_style, options: options)
}
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
314
315
316
317
318
319
320
|
# File 'lib/plurimath/math/core.rb', line 314
def ox_element(node, attributes: [], namespace: "")
Utility.ox_element(
node,
attributes: attributes,
namespace: namespace,
)
end
|
#paren? ⇒ Boolean
369
370
371
|
# File 'lib/plurimath/math/core.rb', line 369
def paren?
false
end
|
#pretty_print_instance_variables ⇒ Object
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
# File 'lib/plurimath/math/core.rb', line 373
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
360
361
362
363
364
365
366
367
|
# File 'lib/plurimath/math/core.rb', line 360
def prime_unicode?(field)
return false unless field.is_a?(Math::Symbols::Symbol)
return true if field&.value&.include?("'")
Utility.primes_constants.any? { |_prefix, prime|
unicodemath_field_value(field).include?(prime)
}
end
|
#r_element(string, rpr_tag: true) ⇒ Object
74
75
76
77
78
79
80
81
|
# File 'lib/plurimath/math/core.rb', line 74
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
219
220
221
222
|
# File 'lib/plurimath/math/core.rb', line 219
def replacable_values(string)
REPLACABLES.each { |regex, str| string = string.gsub(regex, str) }
string
end
|
#result(value = []) ⇒ Object
322
323
324
325
|
# File 'lib/plurimath/math/core.rb', line 322
def result(value = [])
value = get("@value") || value
value.slice_after { |d| d.is_a?(Math::Function::Linebreak) }.to_a
end
|
#separate_table ⇒ Object
232
233
234
|
# File 'lib/plurimath/math/core.rb', line 232
def separate_table
false
end
|
#set(variable, value) ⇒ Object
302
303
304
|
# File 'lib/plurimath/math/core.rb', line 302
def set(variable, value)
instance_variable_set(variable, value)
end
|
#symbol? ⇒ Boolean
337
338
339
|
# File 'lib/plurimath/math/core.rb', line 337
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
|
# File 'lib/plurimath/math/core.rb', line 389
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
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/plurimath/math/core.rb', line 150
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
353
354
355
356
357
358
|
# File 'lib/plurimath/math/core.rb', line 353
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
# File 'lib/plurimath/math/core.rb', line 271
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
|
70
71
72
|
# File 'lib/plurimath/math/core.rb', line 70
def validate_function_formula
true
end
|
#validate_mathml_fields(field, intent, options:) ⇒ Object
184
185
186
187
188
189
190
|
# File 'lib/plurimath/math/core.rb', line 184
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
246
247
248
249
250
251
252
253
254
255
|
# File 'lib/plurimath/math/core.rb', line 246
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
306
307
308
309
310
311
312
|
# File 'lib/plurimath/math/core.rb', line 306
def variables
instance_variables.reject do |var|
name = var.to_s
name.start_with?("@__", "@lutaml") ||
name == "@using_default"
end
end
|