Class: Plurimath::Math::Core

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

Constant Summary collapse

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

Instance Method Summary collapse

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)

  options[:array] << field&.to_asciimath_math_zone(hashed[:function_spacing], hashed[:last], hashed[:indent])
end

#class_nameObject



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

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

#cloned_objectsObject



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

  wrapper_tag << r_tag
end

#extract_class_name_from_textObject



70
71
72
# File 'lib/plurimath/math/core.rb', line 70

def extract_class_name_from_text
  ""
end

#extractable?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/plurimath/math/core.rb', line 66

def extractable?
  false
end

#filtered_values(value) ⇒ Object



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

def filtered_values(value)
  @values = Utility.filter_math_zone_values(value)
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_symbolsObject



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

Returns:

  • (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

Returns:

  • (Boolean)


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

def is_nary_function?;end

#is_nary_symbol?Boolean

Returns:

  • (Boolean)


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

def is_nary_symbol?;end

#is_ternary_function?Boolean

Returns:

  • (Boolean)


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

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

#is_unary?Boolean

Returns:

  • (Boolean)


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

def is_unary?
  is_a?(Math::Function::UnaryFunction)
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)

  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

#linebreakObject



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)

  options[:array] << field&.to_mathml_math_zone(hashed[:function_spacing], hashed[:last], hashed[:indent])
end

#mini_sized?Boolean

Returns:

  • (Boolean)


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

def mini_sized?
  false
end

#nary_attr_valueObject



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)

  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_nameObject



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

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



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_nameObject



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
299
300
# File 'lib/plurimath/math/core.rb', line 294

def unicodemath_parens(field)
  if field.is_a?(Math::Function::Fenced)
    field.to_unicodemath
  else
    "(#{field.to_unicodemath})" if field
  end
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

#validate_function_formulaObject



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

#variablesObject



257
258
259
# File 'lib/plurimath/math/core.rb', line 257

def variables
  instance_variables
end