Class: Plurimath::Math::Core

Inherits:
Object
  • Object
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

Class Method Details

.descendantsObject



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



95
96
97
98
99
100
101
102
103
# File 'lib/plurimath/math/core.rb', line 95

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_nameObject



28
29
30
# File 'lib/plurimath/math/core.rb', line 28

def class_name
  self.class.name.split("::").last.downcase
end

#cloned_objectsObject



215
216
217
218
219
# File 'lib/plurimath/math/core.rb', line 215

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



168
169
170
171
172
173
174
175
176
# File 'lib/plurimath/math/core.rb', line 168

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



146
147
148
# File 'lib/plurimath/math/core.rb', line 146

def dump_mathml(field, intent = false, options:)
  dump_ox_nodes(field.to_mathml_without_math_tag(intent, options: options)).gsub(/\n\s*/, "")
end

#dump_nodes(nodes, indent: nil) ⇒ Object



188
189
190
191
192
# File 'lib/plurimath/math/core.rb', line 188

def dump_nodes(nodes, indent: nil)
  replacable_values(
    Plurimath.xml_engine.dump(nodes, indent: indent),
  )
end

#dump_omml(field, display_style, options:) ⇒ Object



150
151
152
153
154
# File 'lib/plurimath/math/core.rb', line 150

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



182
183
184
185
186
# File 'lib/plurimath/math/core.rb', line 182

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") << "&#8203;")
  return r_tag unless wrapper_tag

  wrapper_tag << r_tag
end

#extract_class_name_from_textObject



87
88
89
# File 'lib/plurimath/math/core.rb', line 87

def extract_class_name_from_text
  ""
end

#extractable?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/plurimath/math/core.rb', line 83

def extractable?
  false
end

#filtered_values(value, lang:, options: {}) ⇒ Object



178
179
180
# File 'lib/plurimath/math/core.rb', line 178

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



91
92
93
# File 'lib/plurimath/math/core.rb', line 91

def font_style_t_tag(display_style, options:)
  to_omml_without_math_tag(display_style, options: options)
end

#get(variable) ⇒ Object



272
273
274
# File 'lib/plurimath/math/core.rb', line 272

def get(variable)
  instance_variable_get(variable)
end

#gsub_spacing(spacing, last) ⇒ Object



199
200
201
# File 'lib/plurimath/math/core.rb', line 199

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(to_omml_without_math_tag(display_style, options: options))
end

#invert_unicode_symbolsObject



203
204
205
# File 'lib/plurimath/math/core.rb', line 203

def invert_unicode_symbols
  Mathml::Constants::UNICODE_SYMBOLS.invert[class_name] || class_name
end

#is_binary_function?Boolean

Returns:

  • (Boolean)


311
312
313
# File 'lib/plurimath/math/core.rb', line 311

def is_binary_function?
  is_a?(Function::BinaryFunction)
end

#is_mrow?Boolean

Returns:

  • (Boolean)


375
376
377
# File 'lib/plurimath/math/core.rb', line 375

def is_mrow?
  false
end

#is_mstyle?Boolean

Returns:

  • (Boolean)


371
372
373
# File 'lib/plurimath/math/core.rb', line 371

def is_mstyle?
  false
end

#is_nary_function?Boolean

Returns:

  • (Boolean)


301
# File 'lib/plurimath/math/core.rb', line 301

def is_nary_function?; end

#is_nary_symbol?Boolean

Returns:

  • (Boolean)


303
# File 'lib/plurimath/math/core.rb', line 303

def is_nary_symbol?; end

#is_ternary_function?Boolean

Returns:

  • (Boolean)


315
316
317
# File 'lib/plurimath/math/core.rb', line 315

def is_ternary_function?
  is_a?(Function::TernaryFunction)
end

#is_unary?Boolean

Returns:

  • (Boolean)


297
298
299
# File 'lib/plurimath/math/core.rb', line 297

def is_unary?
  is_a?(Math::Function::UnaryFunction)
end

#latex_fields_to_print(field, options = {}) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/plurimath/math/core.rb', line 105

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



232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/plurimath/math/core.rb', line 232

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

Returns:

  • (Boolean)


211
212
213
# File 'lib/plurimath/math/core.rb', line 211

def linebreak?
  false
end

#mathml_fields_to_print(field, options = {}) ⇒ Object



115
116
117
118
119
120
121
122
123
# File 'lib/plurimath/math/core.rb', line 115

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

#mini_sized?Boolean

Returns:

  • (Boolean)


319
320
321
# File 'lib/plurimath/math/core.rb', line 319

def mini_sized?
  false
end

#nary_attr_valueObject



44
45
46
# File 'lib/plurimath/math/core.rb', line 44

def nary_attr_value(**)
  ""
end

#nary_intent_nameObject



305
# File 'lib/plurimath/math/core.rb', line 305

def nary_intent_name; end

#omml_fields_to_print(field, options = {}) ⇒ Object



125
126
127
128
129
130
131
132
133
134
# File 'lib/plurimath/math/core.rb', line 125

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



156
157
158
# File 'lib/plurimath/math/core.rb', line 156

def omml_nodes(display_style, options:)
  to_omml_without_math_tag(display_style, options: options)
end

#omml_parameter(field, display_style, tag_name:, namespace: "m", options:) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/plurimath/math/core.rb', line 56

def omml_parameter(field, display_style, tag_name:, namespace: "m", options:)
  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_nameObject



40
41
42
# File 'lib/plurimath/math/core.rb', line 40

def omml_tag_name
  "subSup"
end

#ox_element(node, attributes: [], namespace: "") ⇒ Object



284
285
286
287
288
289
290
# File 'lib/plurimath/math/core.rb', line 284

def ox_element(node, attributes: [], namespace: "")
  Utility.ox_element(
    node,
    attributes: attributes,
    namespace: namespace,
  )
end

#paren?Boolean

Returns:

  • (Boolean)


337
338
339
# File 'lib/plurimath/math/core.rb', line 337

def paren?
  false
end

#pretty_print_instance_variablesObject



341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'lib/plurimath/math/core.rb', line 341

def pretty_print_instance_variables
  excluded_vars = [
    :@left_right_wrapper,
    :@temp_mathml_order,
    :@using_default,
    :@displaystyle,
    :@__ordered,
    :@__mixed,
    :@is_mrow,
    :@values,
  ]
  instance_variables.sort - excluded_vars
end

#prime_unicode?(field) ⇒ Boolean

Returns:

  • (Boolean)


330
331
332
333
334
335
# File 'lib/plurimath/math/core.rb', line 330

def prime_unicode?(field)
  return unless field.is_a?(Math::Symbols::Symbol)
  return true if field&.value&.include?("&#x27;")

  Utility.primes_constants.any? { |prefix, prime| unicodemath_field_value(field).include?(prime) }
end

#r_element(string, rpr_tag: true) ⇒ Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/plurimath/math/core.rb', line 72

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



194
195
196
197
# File 'lib/plurimath/math/core.rb', line 194

def replacable_values(string)
  REPLACABLES.each { |regex, str| string = string.gsub(regex, str) }
  string
end

#result(value = []) ⇒ Object



292
293
294
295
# File 'lib/plurimath/math/core.rb', line 292

def result(value = [])
  value = get("@value") || value
  value.slice_after { |d| d.is_a?(Math::Function::Linebreak) }.to_a
end

#separate_tableObject



207
208
209
# File 'lib/plurimath/math/core.rb', line 207

def separate_table
  false
end

#set(variable, value) ⇒ Object



276
277
278
# File 'lib/plurimath/math/core.rb', line 276

def set(variable, value)
  instance_variable_set(variable, value)
end

#symbol?Boolean

Returns:

  • (Boolean)


307
308
309
# File 'lib/plurimath/math/core.rb', line 307

def symbol?
  is_a?(Math::Symbols::Symbol)
end

#tag_nameObject



36
37
38
# File 'lib/plurimath/math/core.rb', line 36

def tag_name
  "subsup"
end

#to_ms_valueObject



355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/plurimath/math/core.rb', line 355

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 { |element| element.to_ms_value }.join(" ")
    end
  end
  new_arr
end

#unicodemath_fields_to_print(field, options = {}) ⇒ Object



136
137
138
139
140
141
142
143
144
# File 'lib/plurimath/math/core.rb', line 136

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



323
324
325
326
327
328
# File 'lib/plurimath/math/core.rb', line 323

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



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/plurimath/math/core.rb', line 245

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

#validate_function_formulaObject



68
69
70
# File 'lib/plurimath/math/core.rb', line 68

def validate_function_formula
  true
end

#validate_mathml_fields(field, intent, options:) ⇒ Object



160
161
162
163
164
165
166
# File 'lib/plurimath/math/core.rb', line 160

def validate_mathml_fields(field, intent, options:)
  if field.is_a?(Array)
    field&.map { |object| object.to_mathml_without_math_tag(intent, options: options) }
  else
    field&.to_mathml_without_math_tag(intent, options: options)
  end
end

#variable_value(value) ⇒ Object



221
222
223
224
225
226
227
228
229
230
# File 'lib/plurimath/math/core.rb', line 221

def variable_value(value)
  case value
  when Core
    value.cloned_objects
  when Array
    value.map { |object| variable_value(object) }
  else
    value
  end
end

#variablesObject



280
281
282
# File 'lib/plurimath/math/core.rb', line 280

def variables
  instance_variables
end