Class: Plurimath::Math::Core

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

Instance Method Summary collapse

Instance Method Details

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



68
69
70
71
72
73
74
75
76
# File 'lib/plurimath/math/core.rb', line 68

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



6
7
8
# File 'lib/plurimath/math/core.rb', line 6

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

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



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

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



109
110
111
112
# File 'lib/plurimath/math/core.rb', line 109

def dump_mathml(field)
  mathml = dump_ox_nodes(field.to_mathml_without_math_tag)
  mathml.gsub(/\n\s*/, "").gsub("&amp;", "&")
end

#dump_omml(field, display_style) ⇒ Object



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

def dump_omml(field, display_style)
  return if field.nil?

  omml = field.omml_nodes(display_style)
  omml_string = omml.is_a?(Array) ? omml.flatten.map { |obj| dump_ox_nodes(obj) }.join : dump_ox_nodes(omml)
  omml_string.gsub(/\n\s*/, "").gsub("&amp;", "&")
end

#dump_ox_nodes(nodes) ⇒ Object



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

def dump_ox_nodes(nodes)
  Plurimath.xml_engine.dump(nodes)
end

#empty_tag(wrapper_tag) ⇒ Object



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

def empty_tag(wrapper_tag)
  r_tag = Utility.ox_element("r", namespace: "m")
  r_tag << (Utility.ox_element("t", namespace: "m") << "&#8203;")
  wrapper_tag << r_tag
end

#extract_class_from_textObject



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

def extract_class_from_text
  ""
end

#extractable?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/plurimath/math/core.rb', line 56

def extractable?
  false
end

#filtered_values(value) ⇒ Object



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

def filtered_values(value)
  @values = Utility.filter_math_zone_values(value)
end

#font_style_t_tag(display_style) ⇒ Object



64
65
66
# File 'lib/plurimath/math/core.rb', line 64

def font_style_t_tag(display_style)
  to_omml_without_math_tag(display_style)
end

#gsub_spacing(spacing, last) ⇒ Object



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

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

#insert_t_tag(display_style) ⇒ Object



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

def insert_t_tag(display_style)
  Array(to_omml_without_math_tag(display_style))
end

#invert_unicode_symbolsObject



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

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

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



78
79
80
81
82
83
84
85
86
# File 'lib/plurimath/math/core.rb', line 78

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

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



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

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_valueObject



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

def nary_attr_value
  ""
end

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



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

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



122
123
124
# File 'lib/plurimath/math/core.rb', line 122

def omml_nodes(display_style)
  to_omml_without_math_tag(display_style)
end

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



32
33
34
35
36
37
38
39
40
# File 'lib/plurimath/math/core.rb', line 32

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



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

def omml_tag_name
  "subSup"
end

#r_element(string, rpr_tag: true) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/plurimath/math/core.rb', line 46

def r_element(string, rpr_tag: true)
  r_tag = Utility.ox_element("r", namespace: "m")
  if rpr_tag
    sty_tag = Utility.ox_element("sty", namespace: "m", attributes: { "m:val": "p" })
    r_tag << (Utility.ox_element("rPr", namespace: "m") << sty_tag)
  end
  r_tag << (Utility.ox_element("t", namespace: "m") << string)
  Array(r_tag)
end

#tag_nameObject



14
15
16
# File 'lib/plurimath/math/core.rb', line 14

def tag_name
  "subsup"
end

#validate_function_formulaObject



42
43
44
# File 'lib/plurimath/math/core.rb', line 42

def validate_function_formula
  true
end

#validate_mathml_fields(field) ⇒ Object



126
127
128
# File 'lib/plurimath/math/core.rb', line 126

def validate_mathml_fields(field)
  field.nil? ? Utility.ox_element("mi") : field.to_mathml_without_math_tag
end