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



98
99
100
101
102
103
104
105
106
# File 'lib/plurimath/math/core.rb', line 98

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



222
223
224
225
226
# File 'lib/plurimath/math/core.rb', line 222

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



175
176
177
178
179
180
181
182
183
# File 'lib/plurimath/math/core.rb', line 175

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



149
150
151
# File 'lib/plurimath/math/core.rb', line 149

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



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

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

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



153
154
155
156
157
# File 'lib/plurimath/math/core.rb', line 153

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



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

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



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

def extract_class_name_from_text
  ""
end

#extractable?Boolean

Returns:

  • (Boolean)


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

def extractable?
  false
end

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



185
186
187
# File 'lib/plurimath/math/core.rb', line 185

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



94
95
96
# File 'lib/plurimath/math/core.rb', line 94

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

#get(variable) ⇒ Object



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

def get(variable)
  instance_variable_get(variable)
end

#gsub_spacing(spacing, last) ⇒ Object



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

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_symbolsObject



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

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

#is_binary_function?Boolean

Returns:

  • (Boolean)


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

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

#is_mrow?Boolean

Returns:

  • (Boolean)


384
385
386
# File 'lib/plurimath/math/core.rb', line 384

def is_mrow?
  false
end

#is_mstyle?Boolean

Returns:

  • (Boolean)


380
381
382
# File 'lib/plurimath/math/core.rb', line 380

def is_mstyle?
  false
end

#is_nary_function?Boolean

Returns:

  • (Boolean)


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

def is_nary_function?; end

#is_nary_symbol?Boolean

Returns:

  • (Boolean)


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

def is_nary_symbol?; end

#is_ternary_function?Boolean

Returns:

  • (Boolean)


322
323
324
# File 'lib/plurimath/math/core.rb', line 322

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

#is_unary?Boolean

Returns:

  • (Boolean)


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

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

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



108
109
110
111
112
113
114
115
116
# File 'lib/plurimath/math/core.rb', line 108

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



239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/plurimath/math/core.rb', line 239

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)


218
219
220
# File 'lib/plurimath/math/core.rb', line 218

def linebreak?
  false
end

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



118
119
120
121
122
123
124
125
126
# File 'lib/plurimath/math/core.rb', line 118

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



159
160
161
# File 'lib/plurimath/math/core.rb', line 159

def mathml_nodes(intent, options:)
  to_mathml_without_math_tag(intent, options: options)
end

#mini_sized?Boolean

Returns:

  • (Boolean)


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

def mini_sized?
  false
end

#msty_tag_with_attrsObject



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

def msty_tag_with_attrs
  attrs = { "m:val": "p" }
  ox_element("sty", namespace: "m", attributes: attrs)
end

#nary_attr_valueObject



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

def nary_attr_value(**)
  ""
end

#nary_intent_nameObject



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

def nary_intent_name; end

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



128
129
130
131
132
133
134
135
136
137
# File 'lib/plurimath/math/core.rb', line 128

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



163
164
165
# File 'lib/plurimath/math/core.rb', line 163

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



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

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

#paren?Boolean

Returns:

  • (Boolean)


344
345
346
# File 'lib/plurimath/math/core.rb', line 344

def paren?
  false
end

#pretty_print_instance_variablesObject



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
# File 'lib/plurimath/math/core.rb', line 348

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

#prime_unicode?(field) ⇒ Boolean

Returns:

  • (Boolean)


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

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
# 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
    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



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

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

#result(value = []) ⇒ Object



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

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

#separate_tableObject



214
215
216
# File 'lib/plurimath/math/core.rb', line 214

def separate_table
  false
end

#set(variable, value) ⇒ Object



283
284
285
# File 'lib/plurimath/math/core.rb', line 283

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

#symbol?Boolean

Returns:

  • (Boolean)


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

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



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'lib/plurimath/math/core.rb', line 364

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



139
140
141
142
143
144
145
146
147
# File 'lib/plurimath/math/core.rb', line 139

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



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

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



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/plurimath/math/core.rb', line 252

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



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

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



228
229
230
231
232
233
234
235
236
237
# File 'lib/plurimath/math/core.rb', line 228

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

#variablesObject



287
288
289
# File 'lib/plurimath/math/core.rb', line 287

def variables
  instance_variables
end