Class: Plurimath::Math::Core
- Inherits:
-
Object
- Object
- Plurimath::Math::Core
show all
- Defined in:
- lib/plurimath/math/core.rb
Constant Summary
collapse
- REPLACABLES =
{
/&/ => "&",
/^\n/ => "",
}
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) ⇒ Object
-
#dump_nodes(nodes, indent: nil) ⇒ Object
-
#dump_omml(field, display_style) ⇒ Object
-
#dump_ox_nodes(nodes) ⇒ Object
-
#empty_tag(wrapper_tag = nil) ⇒ Object
-
#extract_class_name_from_text ⇒ Object
-
#extractable? ⇒ Boolean
-
#filtered_values(value, lang:) ⇒ Object
-
#font_style_t_tag(display_style) ⇒ Object
-
#get(variable) ⇒ Object
-
#gsub_spacing(spacing, last) ⇒ Object
-
#insert_t_tag(display_style) ⇒ Object
-
#invert_unicode_symbols ⇒ Object
-
#is_binary_function? ⇒ 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 ⇒ Object
-
#mathml_fields_to_print(field, options = {}) ⇒ Object
-
#mini_sized? ⇒ Boolean
-
#nary_attr_value ⇒ Object
-
#omml_fields_to_print(field, options = {}) ⇒ Object
-
#omml_nodes(display_style) ⇒ Object
-
#omml_parameter(field, display_style, tag_name:, namespace: "m") ⇒ Object
-
#omml_tag_name ⇒ Object
-
#ox_element(node, attributes: [], namespace: "") ⇒ 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
-
#tag_name ⇒ Object
-
#unicodemath_parens(field) ⇒ Object
-
#updated_object_values(param, obj:, update_value: false) ⇒ Object
-
#validate_function_formula ⇒ Object
-
#validate_mathml_fields(field) ⇒ Object
-
#variable_value(value) ⇒ Object
-
#variables ⇒ Object
Instance Method Details
#ascii_fields_to_print(field, options = {}) ⇒ Object
78
79
80
81
82
83
84
85
86
|
# File 'lib/plurimath/math/core.rb', line 78
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}\"#{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])
end
|
#class_name ⇒ Object
11
12
13
|
# File 'lib/plurimath/math/core.rb', line 11
def class_name
self.class.name.split("::").last.downcase
end
|
#cloned_objects ⇒ Object
188
189
190
191
192
|
# File 'lib/plurimath/math/core.rb', line 188
def cloned_objects
object = self.class.new rescue self.class.new(nil)
variables.each { |var| object.set(var, variable_value(get(var))) }
object
end
|
#common_math_zone_conversion(field, options = {}) ⇒ Object
141
142
143
144
145
146
147
148
149
|
# File 'lib/plurimath/math/core.rb', line 141
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) ⇒ Object
119
120
121
|
# File 'lib/plurimath/math/core.rb', line 119
def dump_mathml(field)
dump_ox_nodes(field.to_mathml_without_math_tag).gsub(/\n\s*/, "")
end
|
#dump_nodes(nodes, indent: nil) ⇒ Object
161
162
163
164
165
|
# File 'lib/plurimath/math/core.rb', line 161
def dump_nodes(nodes, indent: nil)
replacable_values(
Plurimath.xml_engine.dump(nodes, indent: indent),
)
end
|
#dump_omml(field, display_style) ⇒ Object
123
124
125
126
127
|
# File 'lib/plurimath/math/core.rb', line 123
def dump_omml(field, display_style)
return if field.nil?
dump_ox_nodes(field.omml_nodes(display_style)).gsub(/\n\s*/, "")
end
|
#dump_ox_nodes(nodes) ⇒ Object
155
156
157
158
159
|
# File 'lib/plurimath/math/core.rb', line 155
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
31
32
33
34
35
36
37
|
# File 'lib/plurimath/math/core.rb', line 31
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
|
70
71
72
|
# File 'lib/plurimath/math/core.rb', line 70
def
""
end
|
66
67
68
|
# File 'lib/plurimath/math/core.rb', line 66
def
false
end
|
#filtered_values(value, lang:) ⇒ Object
151
152
153
|
# File 'lib/plurimath/math/core.rb', line 151
def filtered_values(value, lang:)
@values = Utility.filter_math_zone_values(value, lang: lang)
end
|
#font_style_t_tag(display_style) ⇒ Object
74
75
76
|
# File 'lib/plurimath/math/core.rb', line 74
def font_style_t_tag(display_style)
to_omml_without_math_tag(display_style)
end
|
#get(variable) ⇒ Object
249
250
251
|
# File 'lib/plurimath/math/core.rb', line 249
def get(variable)
instance_variable_get(variable)
end
|
#gsub_spacing(spacing, last) ⇒ Object
172
173
174
|
# File 'lib/plurimath/math/core.rb', line 172
def gsub_spacing(spacing, last)
spacing.gsub(/\|_/, last ? " " : "| ")
end
|
#insert_t_tag(display_style) ⇒ Object
15
16
17
|
# File 'lib/plurimath/math/core.rb', line 15
def insert_t_tag(display_style)
Array(to_omml_without_math_tag(display_style))
end
|
#invert_unicode_symbols ⇒ Object
176
177
178
|
# File 'lib/plurimath/math/core.rb', line 176
def invert_unicode_symbols
Mathml::Constants::UNICODE_SYMBOLS.invert[class_name] || class_name
end
|
#is_binary_function? ⇒ Boolean
282
283
284
|
# File 'lib/plurimath/math/core.rb', line 282
def is_binary_function?
is_a?(Function::BinaryFunction)
end
|
#is_nary_function? ⇒ Boolean
278
|
# File 'lib/plurimath/math/core.rb', line 278
def is_nary_function?;end
|
#is_nary_symbol? ⇒ Boolean
280
|
# File 'lib/plurimath/math/core.rb', line 280
def is_nary_symbol?;end
|
#is_ternary_function? ⇒ Boolean
286
287
288
|
# File 'lib/plurimath/math/core.rb', line 286
def is_ternary_function?
is_a?(Function::TernaryFunction)
end
|
#latex_fields_to_print(field, options = {}) ⇒ Object
88
89
90
91
92
93
94
95
96
|
# File 'lib/plurimath/math/core.rb', line 88
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}\"#{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])
end
|
#line_breaking(obj) ⇒ Object
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
|
# File 'lib/plurimath/math/core.rb', line 205
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
if result(field).length > 1
updated_object_values(variable, obj: obj)
else
field.each { |object| object.line_breaking(obj) }
end
end
end
end
|
#linebreak ⇒ Object
184
185
186
|
# File 'lib/plurimath/math/core.rb', line 184
def linebreak
false
end
|
#mathml_fields_to_print(field, options = {}) ⇒ Object
98
99
100
101
102
103
104
105
106
|
# File 'lib/plurimath/math/core.rb', line 98
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)}\"#{hashed[:field_name]}\n"
return unless Utility.validate_math_zone(field, lang: :mathml)
options[:array] << field&.to_mathml_math_zone(hashed[:function_spacing], hashed[:last], hashed[:indent])
end
|
#mini_sized? ⇒ Boolean
290
291
292
|
# File 'lib/plurimath/math/core.rb', line 290
def mini_sized?
false
end
|
#nary_attr_value ⇒ Object
27
28
29
|
# File 'lib/plurimath/math/core.rb', line 27
def nary_attr_value
""
end
|
#omml_fields_to_print(field, options = {}) ⇒ Object
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/plurimath/math/core.rb', line 108
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)}\"#{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)
end
|
#omml_nodes(display_style) ⇒ Object
129
130
131
|
# File 'lib/plurimath/math/core.rb', line 129
def omml_nodes(display_style)
to_omml_without_math_tag(display_style)
end
|
#omml_parameter(field, display_style, tag_name:, namespace: "m") ⇒ Object
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/plurimath/math/core.rb', line 39
def omml_parameter(field, display_style, tag_name:, 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) }
else
field.insert_t_tag(display_style)
end
Utility.update_nodes(tag, field_value)
end
|
#omml_tag_name ⇒ Object
23
24
25
|
# File 'lib/plurimath/math/core.rb', line 23
def omml_tag_name
"subSup"
end
|
#ox_element(node, attributes: [], namespace: "") ⇒ Object
261
262
263
264
265
266
267
|
# File 'lib/plurimath/math/core.rb', line 261
def ox_element(node, attributes: [], namespace: "")
Utility.ox_element(
node,
attributes: attributes,
namespace: namespace,
)
end
|
#prime_unicode?(field) ⇒ Boolean
300
301
302
303
304
305
|
# File 'lib/plurimath/math/core.rb', line 300
def prime_unicode?(field)
return 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
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/plurimath/math/core.rb', line 55
def r_element(string, rpr_tag: true)
r_tag = ox_element("r", namespace: "m")
if rpr_tag
attrs = { "m:val": "p" }
sty_tag = ox_element("sty", namespace: "m", attributes: attrs)
r_tag << (ox_element("rPr", namespace: "m") << sty_tag)
end
r_tag << (ox_element("t", namespace: "m") << string)
Array(r_tag)
end
|
#replacable_values(string) ⇒ Object
167
168
169
170
|
# File 'lib/plurimath/math/core.rb', line 167
def replacable_values(string)
REPLACABLES.each { |regex, str| string.gsub!(regex, str) }
string
end
|
#result(value = []) ⇒ Object
269
270
271
272
|
# File 'lib/plurimath/math/core.rb', line 269
def result(value = [])
value = get("@value") || value
value.slice_after { |d| d.is_a?(Math::Function::Linebreak) }.to_a
end
|
#separate_table ⇒ Object
180
181
182
|
# File 'lib/plurimath/math/core.rb', line 180
def separate_table
false
end
|
#set(variable, value) ⇒ Object
253
254
255
|
# File 'lib/plurimath/math/core.rb', line 253
def set(variable, value)
instance_variable_set(variable, value)
end
|
#tag_name ⇒ Object
19
20
21
|
# File 'lib/plurimath/math/core.rb', line 19
def tag_name
"subsup"
end
|
#unicodemath_parens(field) ⇒ Object
294
295
296
297
298
|
# File 'lib/plurimath/math/core.rb', line 294
def unicodemath_parens(field)
return field.to_unicodemath if field.is_a?(Math::Function::Fenced)
"(#{field.to_unicodemath})" if field
end
|
#updated_object_values(param, obj:, update_value: false) ⇒ Object
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
# File 'lib/plurimath/math/core.rb', line 222
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
|
51
52
53
|
# File 'lib/plurimath/math/core.rb', line 51
def validate_function_formula
true
end
|
#validate_mathml_fields(field) ⇒ Object
133
134
135
136
137
138
139
|
# File 'lib/plurimath/math/core.rb', line 133
def validate_mathml_fields(field)
if field.is_a?(Array)
field&.map(&:to_mathml_without_math_tag)
else
field&.to_mathml_without_math_tag
end
end
|
#variable_value(value) ⇒ Object
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/plurimath/math/core.rb', line 194
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
257
258
259
|
# File 'lib/plurimath/math/core.rb', line 257
def variables
instance_variables
end
|