Class: Plurimath::Math::Formula

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Core

#empty_tag, #insert_t_tag, #omml_parameter, #tag_name

Constructor Details

#initialize(value = [], left_right_wrapper = true, displaystyle: true) ⇒ Formula

Returns a new instance of Formula.



8
9
10
11
12
13
14
15
16
17
# File 'lib/plurimath/math/formula.rb', line 8

def initialize(
  value = [],
  left_right_wrapper = true,
  displaystyle: true
)
  @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 = displaystyle
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

#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



19
20
21
22
# File 'lib/plurimath/math/formula.rb', line 19

def ==(object)
  object.value == value &&
    object.left_right_wrapper == left_right_wrapper
end

#class_nameObject



118
119
120
# File 'lib/plurimath/math/formula.rb', line 118

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

#extract_class_from_textObject



122
123
124
125
126
# File 'lib/plurimath/math/formula.rb', line 122

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

  value.first.parameter_one
end

#mathml_contentObject



50
51
52
# File 'lib/plurimath/math/formula.rb', line 50

def mathml_content
  value.map(&:to_mathml_without_math_tag)
end

#nary_attr_valueObject



128
129
130
# File 'lib/plurimath/math/formula.rb', line 128

def nary_attr_value
  value.first.nary_attr_value
end

#nary_tagObject



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

def nary_tag
  nary_tag = Utility.ox_element("nary", namespace: "m")
  e_tag    = Utility.ox_element("e", namespace: "m")
  Utility.update_nodes(e_tag, value.last.insert_t_tag)
  Utility.update_nodes(
    nary_tag,
    (value.first.omml_nary_tag << e_tag),
  )
  [nary_tag]
end

#nary_tag_able?Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
# File 'lib/plurimath/math/formula.rb', line 132

def nary_tag_able?
  value.length == 2 &&
    ["underover", "powerbase"].include?(value&.first&.class_name) &&
    (
      value&.first&.parameter_one&.to_omml_without_math_tag&.length == 1 ||
      value&.first&.parameter_one.to_omml_without_math_tag.match?(/^&#x\w*\d*;$/)
    )
end

#omml_contentObject



97
98
99
# File 'lib/plurimath/math/formula.rb', line 97

def omml_content
  value&.map(&:insert_t_tag)
end

#omml_math_attrsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/plurimath/math/formula.rb', line 62

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



24
25
26
# File 'lib/plurimath/math/formula.rb', line 24

def to_asciimath
  value.map(&:to_asciimath).join(" ")
end

#to_htmlObject



58
59
60
# File 'lib/plurimath/math/formula.rb', line 58

def to_html
  value&.map(&:to_html)&.join(" ")
end

#to_latexObject



54
55
56
# File 'lib/plurimath/math/formula.rb', line 54

def to_latex
  value&.map(&:to_latex)&.join(" ")
end

#to_mathml(display_style: displaystyle) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/plurimath/math/formula.rb', line 28

def to_mathml(display_style: displaystyle)
  math_attrs = {
    xmlns: "http://www.w3.org/1998/Math/MathML",
    display: "block",
  }
  style_attrs = { displaystyle: 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])
  Ox.dump(math, indent: 2).gsub("&amp;", "&")
end

#to_mathml_without_math_tagObject



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

def to_mathml_without_math_tag
  return mathml_content unless left_right_wrapper

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

#to_ommlObject



85
86
87
88
89
90
91
92
93
94
95
# File 'lib/plurimath/math/formula.rb', line 85

def to_omml
  para_element = Utility.ox_element(
    "oMathPara",
    attributes: omml_math_attrs,
    namespace: "m",
  )
  math_element = Utility.ox_element("oMath", namespace: "m")
  Utility.update_nodes(math_element, omml_content)
  Utility.update_nodes(para_element, Array(math_element))
  Ox.dump(para_element, indent: 2).gsub("&amp;", "&").lstrip
end

#to_omml_without_math_tagObject



101
102
103
104
105
# File 'lib/plurimath/math/formula.rb', line 101

def to_omml_without_math_tag
  return nary_tag if nary_tag_able?

  omml_content
end

#validate_function_formulaObject



141
142
143
# File 'lib/plurimath/math/formula.rb', line 141

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