Class: Plurimath::Math::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/plurimath/math/core.rb

Constant Summary collapse

REPLACABLES =
{
  /&/ => "&",
  /^\n/ => "",
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.descendantsObject



17
18
19
# File 'lib/plurimath/math/core.rb', line 17

def self.descendants
  @descendants
end

.inherited(subclass) ⇒ Object



11
12
13
14
15
# File 'lib/plurimath/math/core.rb', line 11

def self.inherited(subclass)
  @descendants ||= []
  @descendants << subclass
  super
end

Instance Method Details

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



88
89
90
91
92
93
94
95
96
# File 'lib/plurimath/math/core.rb', line 88

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



21
22
23
# File 'lib/plurimath/math/core.rb', line 21

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

#cloned_objectsObject



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

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



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

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



139
140
141
# File 'lib/plurimath/math/core.rb', line 139

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



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

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

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



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

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



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

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



41
42
43
44
45
46
47
# File 'lib/plurimath/math/core.rb', line 41

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



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

def extract_class_name_from_text
  ""
end

#extractable?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/plurimath/math/core.rb', line 76

def extractable?
  false
end

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



171
172
173
# File 'lib/plurimath/math/core.rb', line 171

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



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

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

#get(variable) ⇒ Object



265
266
267
# File 'lib/plurimath/math/core.rb', line 265

def get(variable)
  instance_variable_get(variable)
end

#gsub_spacing(spacing, last) ⇒ Object



192
193
194
# File 'lib/plurimath/math/core.rb', line 192

def gsub_spacing(spacing, last)
  spacing.gsub(/\|_/, last ? "  " : "| ")
end

#insert_t_tag(display_style, options:) ⇒ Object



25
26
27
# File 'lib/plurimath/math/core.rb', line 25

def insert_t_tag(display_style, options:)
  Array(to_omml_without_math_tag(display_style, options: options))
end

#invert_unicode_symbolsObject



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

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

#is_binary_function?Boolean

Returns:

  • (Boolean)


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

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

#is_nary_function?Boolean

Returns:

  • (Boolean)


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

def is_nary_function?;end

#is_nary_symbol?Boolean

Returns:

  • (Boolean)


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

def is_nary_symbol?;end

#is_ternary_function?Boolean

Returns:

  • (Boolean)


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

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

#is_unary?Boolean

Returns:

  • (Boolean)


290
291
292
# File 'lib/plurimath/math/core.rb', line 290

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

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



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

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



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

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

#linebreakObject



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

def linebreak
  false
end

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



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

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)


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

def mini_sized?
  false
end

#nary_attr_valueObject



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

def nary_attr_value(**)
  ""
end

#nary_intent_nameObject



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

def nary_intent_name;end

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



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

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



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

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



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/plurimath/math/core.rb', line 49

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



33
34
35
# File 'lib/plurimath/math/core.rb', line 33

def omml_tag_name
  "subSup"
end

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



277
278
279
280
281
282
283
# File 'lib/plurimath/math/core.rb', line 277

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

#prime_unicode?(field) ⇒ Boolean

Returns:

  • (Boolean)


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

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



65
66
67
68
69
70
71
72
73
74
# File 'lib/plurimath/math/core.rb', line 65

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



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

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

#result(value = []) ⇒ Object



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

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

#separate_tableObject



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

def separate_table
  false
end

#set(variable, value) ⇒ Object



269
270
271
# File 'lib/plurimath/math/core.rb', line 269

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

#tag_nameObject



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

def tag_name
  "subsup"
end

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



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

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



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

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



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/plurimath/math/core.rb', line 238

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



61
62
63
# File 'lib/plurimath/math/core.rb', line 61

def validate_function_formula
  true
end

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



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

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



214
215
216
217
218
219
220
221
222
223
# File 'lib/plurimath/math/core.rb', line 214

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

#variablesObject



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

def variables
  instance_variables
end