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) ⇒ Object
-
#extract_class_name_from_text ⇒ Object
-
#extractable? ⇒ Boolean
-
#filtered_values(value) ⇒ 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_unary? ⇒ Boolean
-
#latex_fields_to_print(field, options = {}) ⇒ Object
-
#line_breaking(obj) ⇒ Object
-
#linebreak ⇒ Object
-
#mathml_fields_to_print(field, options = {}) ⇒ Object
-
#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
-
#r_element(string, rpr_tag: true) ⇒ Object
-
#replacable_values(string) ⇒ Object
-
#result(value = []) ⇒ Object
-
#separate_table ⇒ Object
-
#set(variable, value) ⇒ Object
-
#tag_name ⇒ 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
74
75
76
77
78
79
80
81
82
|
# File 'lib/plurimath/math/core.rb', line 74
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)
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
180
181
182
183
184
|
# File 'lib/plurimath/math/core.rb', line 180
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
133
134
135
136
137
138
139
140
141
|
# File 'lib/plurimath/math/core.rb', line 133
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
115
116
117
|
# File 'lib/plurimath/math/core.rb', line 115
def dump_mathml(field)
dump_ox_nodes(field.to_mathml_without_math_tag).gsub(/\n\s*/, "")
end
|
#dump_nodes(nodes, indent: nil) ⇒ Object
153
154
155
156
157
|
# File 'lib/plurimath/math/core.rb', line 153
def dump_nodes(nodes, indent: nil)
replacable_values(
Plurimath.xml_engine.dump(nodes, indent: indent),
)
end
|
#dump_omml(field, display_style) ⇒ Object
119
120
121
122
123
|
# File 'lib/plurimath/math/core.rb', line 119
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
147
148
149
150
151
|
# File 'lib/plurimath/math/core.rb', line 147
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) ⇒ Object
31
32
33
34
35
|
# File 'lib/plurimath/math/core.rb', line 31
def empty_tag(wrapper_tag)
r_tag = ox_element("r", namespace: "m")
r_tag << (ox_element("t", namespace: "m") << "​")
wrapper_tag << r_tag
end
|
66
67
68
|
# File 'lib/plurimath/math/core.rb', line 66
def
""
end
|
62
63
64
|
# File 'lib/plurimath/math/core.rb', line 62
def
false
end
|
#filtered_values(value) ⇒ Object
#font_style_t_tag(display_style) ⇒ Object
70
71
72
|
# File 'lib/plurimath/math/core.rb', line 70
def font_style_t_tag(display_style)
to_omml_without_math_tag(display_style)
end
|
#get(variable) ⇒ Object
241
242
243
|
# File 'lib/plurimath/math/core.rb', line 241
def get(variable)
instance_variable_get(variable)
end
|
#gsub_spacing(spacing, last) ⇒ Object
164
165
166
|
# File 'lib/plurimath/math/core.rb', line 164
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
168
169
170
|
# File 'lib/plurimath/math/core.rb', line 168
def invert_unicode_symbols
Mathml::Constants::UNICODE_SYMBOLS.invert[class_name] || class_name
end
|
#latex_fields_to_print(field, options = {}) ⇒ Object
84
85
86
87
88
89
90
91
92
|
# File 'lib/plurimath/math/core.rb', line 84
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)
options[:array] << field&.to_latex_math_zone(hashed[:function_spacing], hashed[:last], hashed[:indent])
end
|
#line_breaking(obj) ⇒ Object
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
# File 'lib/plurimath/math/core.rb', line 197
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
176
177
178
|
# File 'lib/plurimath/math/core.rb', line 176
def linebreak
false
end
|
#mathml_fields_to_print(field, options = {}) ⇒ Object
94
95
96
97
98
99
100
101
102
|
# File 'lib/plurimath/math/core.rb', line 94
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)
options[:array] << field&.to_mathml_math_zone(hashed[:function_spacing], hashed[:last], hashed[:indent])
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
104
105
106
107
108
109
110
111
112
113
|
# File 'lib/plurimath/math/core.rb', line 104
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)
options[:array] << field&.to_omml_math_zone(hashed[:function_spacing], hashed[:last], hashed[:indent], display_style: display_style)
end
|
#omml_nodes(display_style) ⇒ Object
125
126
127
|
# File 'lib/plurimath/math/core.rb', line 125
def omml_nodes(display_style)
to_omml_without_math_tag(display_style)
end
|
#omml_parameter(field, display_style, tag_name:, namespace: "m") ⇒ Object
37
38
39
40
41
42
43
44
45
|
# File 'lib/plurimath/math/core.rb', line 37
def omml_parameter(field, display_style, tag_name:, namespace: "m")
tag = ox_element(tag_name, namespace: namespace)
return empty_tag(tag) unless field
Utility.update_nodes(
tag,
field.insert_t_tag(display_style),
)
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
253
254
255
256
257
258
259
|
# File 'lib/plurimath/math/core.rb', line 253
def ox_element(node, attributes: [], namespace: "")
Utility.ox_element(
node,
attributes: attributes,
namespace: namespace,
)
end
|
#r_element(string, rpr_tag: true) ⇒ Object
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/plurimath/math/core.rb', line 51
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
159
160
161
162
|
# File 'lib/plurimath/math/core.rb', line 159
def replacable_values(string)
REPLACABLES.each { |regex, str| string.gsub!(regex, str) }
string
end
|
#result(value = []) ⇒ Object
261
262
263
264
|
# File 'lib/plurimath/math/core.rb', line 261
def result(value = [])
value = get("@value") || value
value.slice_after { |d| d.is_a?(Math::Function::Linebreak) }.to_a
end
|
#separate_table ⇒ Object
172
173
174
|
# File 'lib/plurimath/math/core.rb', line 172
def separate_table
false
end
|
#set(variable, value) ⇒ Object
245
246
247
|
# File 'lib/plurimath/math/core.rb', line 245
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
|
#updated_object_values(param, obj:, update_value: false) ⇒ Object
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
# File 'lib/plurimath/math/core.rb', line 214
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
|
47
48
49
|
# File 'lib/plurimath/math/core.rb', line 47
def validate_function_formula
true
end
|
#validate_mathml_fields(field) ⇒ Object
129
130
131
|
# File 'lib/plurimath/math/core.rb', line 129
def validate_mathml_fields(field)
field.nil? ? ox_element("mi") : field.to_mathml_without_math_tag
end
|
#variable_value(value) ⇒ Object
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/plurimath/math/core.rb', line 186
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
249
250
251
|
# File 'lib/plurimath/math/core.rb', line 249
def variables
instance_variables
end
|