Class: Plurimath::Math::Formula

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

Constant Summary collapse

MATH_ZONE_TYPES =
%i[
  omml
  latex
  mathml
  asciimath
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Core

#ascii_fields_to_print, #class_name, #common_math_zone_conversion, #dump_mathml, #dump_omml, #dump_ox_nodes, #empty_tag, #extractable?, #filtered_values, #font_style_t_tag, #gsub_spacing, #insert_t_tag, #invert_unicode_symbols, #latex_fields_to_print, #mathml_fields_to_print, #omml_fields_to_print, #omml_nodes, #omml_parameter, #omml_tag_name, #r_element, #tag_name, #validate_mathml_fields

Constructor Details

#initialize(value = [], left_right_wrapper = true, display_style: true, input_string: nil) ⇒ Formula

Returns a new instance of Formula.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/plurimath/math/formula.rb', line 14

def initialize(
  value = [],
  left_right_wrapper = true,
  display_style: true,
  input_string: nil
)
  @value = value.is_a?(Array) ? value : [value]
  left_right_wrapper = false if @value.first.is_a?(Function::Left)
  @left_right_wrapper = left_right_wrapper
  @displaystyle = boolean_display_style(display_style)
end

Instance Attribute Details

#displaystyleObject

Returns the value of attribute displaystyle.



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

def displaystyle
  @displaystyle
end

#input_stringObject

Returns the value of attribute input_string.



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

def input_string
  @input_string
end

#left_right_wrapperObject

Returns the value of attribute left_right_wrapper.



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

def left_right_wrapper
  @left_right_wrapper
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#==(object) ⇒ Object



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

def ==(object)
  object.respond_to?(:value) &&
    object.respond_to?(:left_right_wrapper) &&
    object.value == value &&
    object.left_right_wrapper == left_right_wrapper
end

#extract_class_from_textObject



171
172
173
174
175
# File 'lib/plurimath/math/formula.rb', line 171

def extract_class_from_text
  return false unless (value.length < 2 && value&.first&.is_a?(Function::Text))

  value.first.parameter_one
end

#mathml_contentObject



63
64
65
# File 'lib/plurimath/math/formula.rb', line 63

def mathml_content
  value.map(&:to_mathml_without_math_tag)
end

#nary_attr_valueObject



177
178
179
# File 'lib/plurimath/math/formula.rb', line 177

def nary_attr_value
  value.first.nary_attr_value
end

#omml_content(display_style) ⇒ Object



116
117
118
# File 'lib/plurimath/math/formula.rb', line 116

def omml_content(display_style)
  value&.map { |val| val.insert_t_tag(display_style) }
end

#omml_math_attrsObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/plurimath/math/formula.rb', line 79

def omml_math_attrs
  {
    "xmlns:m": "http://schemas.openxmlformats.org/officeDocument/2006/math",
    "xmlns:mc": "http://schemas.openxmlformats.org/markup-compatibility/2006",
    "xmlns:mo": "http://schemas.microsoft.com/office/mac/office/2008/main",
    "xmlns:mv": "urn:schemas-microsoft-com:mac:vml",
    "xmlns:o": "urn:schemas-microsoft-com:office:office",
    "xmlns:r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
    "xmlns:v": "urn:schemas-microsoft-com:vml",
    "xmlns:w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
    "xmlns:w10": "urn:schemas-microsoft-com:office:word",
    "xmlns:w14": "http://schemas.microsoft.com/office/word/2010/wordml",
    "xmlns:w15": "http://schemas.microsoft.com/office/word/2012/wordml",
    "xmlns:wne": "http://schemas.microsoft.com/office/word/2006/wordml",
    "xmlns:wp": "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
    "xmlns:wp14": "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
    "xmlns:wpc": "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
    "xmlns:wpg": "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",
    "xmlns:wpi": "http://schemas.microsoft.com/office/word/2010/wordprocessingInk",
    "xmlns:wps": "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
  }
end

#to_asciimathObject



33
34
35
36
37
# File 'lib/plurimath/math/formula.rb', line 33

def to_asciimath
  value.map(&:to_asciimath).join(" ")
rescue
  parse_error!(:asciimath)
end

#to_asciimath_math_zone(spacing = "", last = false, indent = true) ⇒ Object



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

def to_asciimath_math_zone(spacing = "", last = false, indent = true)
  filtered_values(value).map.with_index(1) do |object, index|
    last = index == @values.length
    object.to_asciimath_math_zone(new_space(spacing, indent), last, indent)
  end
end

#to_display(type = nil) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/plurimath/math/formula.rb', line 124

def to_display(type = nil)
  return type_error! unless MATH_ZONE_TYPES.include?(type.downcase.to_sym)

  math_zone = case type
              when :asciimath
                "  |_ \"#{to_asciimath}\"\n#{to_asciimath_math_zone("     ").join}"
              when :latex
                "  |_ \"#{to_latex}\"\n#{to_latex_math_zone("     ").join}"
              when :mathml
                "  |_ \"#{to_mathml.gsub(/\n\s*/, "")}\"\n#{to_mathml_math_zone("     ").join}"
              when :omml
                "  |_ \"#{to_omml.gsub(/\n\s*/, "")}\"\n#{to_omml_math_zone("     ", display_style: displaystyle).join}"
              end
  <<~MATHZONE.sub(/\n$/, "")
  |_ Math zone
  #{math_zone}
  MATHZONE
end

#to_htmlObject



73
74
75
76
77
# File 'lib/plurimath/math/formula.rb', line 73

def to_html
  value&.map(&:to_html)&.join(" ")
rescue
  parse_error!(:html)
end

#to_latexObject



67
68
69
70
71
# File 'lib/plurimath/math/formula.rb', line 67

def to_latex
  value&.map(&:to_latex)&.join(" ")
rescue
  parse_error!(:latex)
end

#to_latex_math_zone(spacing = "", last = false, indent = true) ⇒ Object



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

def to_latex_math_zone(spacing = "", last = false, indent = true)
  filtered_values(value).map.with_index(1) do |object, index|
    last = index == @values.length
    object.to_latex_math_zone(new_space(spacing, indent), last, indent)
  end
end

#to_mathml(display_style: displaystyle) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/plurimath/math/formula.rb', line 39

def to_mathml(display_style: displaystyle)
  math_attrs = {
    xmlns: "http://www.w3.org/1998/Math/MathML",
    display: "block",
  }
  style_attrs = { displaystyle: boolean_display_style(display_style) }
  math  = Utility.ox_element("math", attributes: math_attrs)
  style = Utility.ox_element("mstyle", attributes: style_attrs)
  Utility.update_nodes(style, mathml_content)
  Utility.update_nodes(math, [style])
  Plurimath.xml_engine.dump(math, indent: 2).gsub("&amp;", "&")
rescue
  parse_error!(:mathml)
end

#to_mathml_math_zone(spacing = "", last = false, indent = true) ⇒ Object



157
158
159
160
161
162
# File 'lib/plurimath/math/formula.rb', line 157

def to_mathml_math_zone(spacing = "", last = false, indent = true)
  filtered_values(value).map.with_index(1) do |object, index|
    last = index == @values.length
    object.to_mathml_math_zone(new_space(spacing, indent), last, indent)
  end
end

#to_mathml_without_math_tagObject



54
55
56
57
58
59
60
61
# File 'lib/plurimath/math/formula.rb', line 54

def to_mathml_without_math_tag
  return mathml_content unless left_right_wrapper

  Utility.update_nodes(
    Utility.ox_element("mrow"),
    mathml_content,
  )
end

#to_omml(display_style: displaystyle) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/plurimath/math/formula.rb', line 102

def to_omml(display_style: displaystyle)
  para_element = Utility.ox_element(
    "oMathPara",
    attributes: omml_math_attrs,
    namespace: "m",
  )
  math_element = Utility.ox_element("oMath", namespace: "m")
  content = omml_content(boolean_display_style(display_style))
  para_element << Utility.update_nodes(math_element, content)
  Plurimath.xml_engine.dump(para_element, indent: 2).gsub("&amp;", "&").lstrip
rescue
  parse_error!(:omml)
end

#to_omml_math_zone(spacing = "", last = false, indent = true, display_style:) ⇒ Object



164
165
166
167
168
169
# File 'lib/plurimath/math/formula.rb', line 164

def to_omml_math_zone(spacing = "", last = false, indent = true, display_style:)
  filtered_values(value).map.with_index(1) do |object, index|
    last = index == @values.length
    object.to_omml_math_zone(new_space(spacing, indent), last, indent, display_style: display_style)
  end
end

#to_omml_without_math_tag(display_style) ⇒ Object



120
121
122
# File 'lib/plurimath/math/formula.rb', line 120

def to_omml_without_math_tag(display_style)
  omml_content(display_style)
end

#validate_function_formulaObject



181
182
183
# File 'lib/plurimath/math/formula.rb', line 181

def validate_function_formula
  (value.none?(Function::Left) || value.none?(Function::Right))
end