plurimath 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/rake.yml +13 -0
  3. data/.github/workflows/release.yml +22 -0
  4. data/.gitignore +1 -0
  5. data/.hound.yml +5 -0
  6. data/.rubocop.yml +8 -0
  7. data/AsciiMath-Supported-Data.adoc +2000 -0
  8. data/Gemfile +3 -0
  9. data/Latex-Supported-Data.adoc +1879 -0
  10. data/MathML-Supported-Data.adoc +287 -0
  11. data/README.adoc +96 -0
  12. data/lib/plurimath/asciimath/constants.rb +267 -229
  13. data/lib/plurimath/asciimath/parse.rb +125 -26
  14. data/lib/plurimath/asciimath/parser.rb +6 -3
  15. data/lib/plurimath/asciimath/transform.rb +1135 -208
  16. data/lib/plurimath/asciimath.rb +1 -1
  17. data/lib/plurimath/html/constants.rb +50 -0
  18. data/lib/plurimath/html/parse.rb +149 -0
  19. data/lib/plurimath/html/parser.rb +26 -0
  20. data/lib/plurimath/html/transform.rb +363 -0
  21. data/lib/plurimath/html.rb +1 -1
  22. data/lib/plurimath/latex/constants.rb +3729 -1906
  23. data/lib/plurimath/latex/parse.rb +167 -51
  24. data/lib/plurimath/latex/parser.rb +12 -4
  25. data/lib/plurimath/latex/transform.rb +598 -183
  26. data/lib/plurimath/math/base.rb +15 -0
  27. data/lib/plurimath/math/formula.rb +96 -11
  28. data/lib/plurimath/math/function/bar.rb +34 -0
  29. data/lib/plurimath/math/function/base.rb +32 -5
  30. data/lib/plurimath/math/function/binary_function.rb +104 -17
  31. data/lib/plurimath/math/function/cancel.rb +8 -0
  32. data/lib/plurimath/math/function/ceil.rb +3 -0
  33. data/lib/plurimath/math/function/color.rb +16 -6
  34. data/lib/plurimath/math/function/f.rb +8 -0
  35. data/lib/plurimath/math/function/fenced.rb +95 -3
  36. data/lib/plurimath/math/function/floor.rb +15 -0
  37. data/lib/plurimath/math/function/font_style/bold.rb +37 -0
  38. data/lib/plurimath/math/function/font_style/double_struck.rb +37 -0
  39. data/lib/plurimath/math/function/font_style/fraktur.rb +37 -0
  40. data/lib/plurimath/math/function/font_style/italic.rb +37 -0
  41. data/lib/plurimath/math/function/font_style/monospace.rb +37 -0
  42. data/lib/plurimath/math/function/font_style/normal.rb +37 -0
  43. data/lib/plurimath/math/function/font_style/sans-serif.rb +37 -0
  44. data/lib/plurimath/math/function/font_style/script.rb +37 -0
  45. data/lib/plurimath/math/function/font_style.rb +18 -25
  46. data/lib/plurimath/math/function/frac.rb +35 -5
  47. data/lib/plurimath/math/function/g.rb +7 -0
  48. data/lib/plurimath/math/function/hat.rb +12 -0
  49. data/lib/plurimath/math/function/inf.rb +21 -1
  50. data/lib/plurimath/math/function/int.rb +23 -2
  51. data/lib/plurimath/math/function/left.rb +25 -4
  52. data/lib/plurimath/math/function/lim.rb +40 -2
  53. data/lib/plurimath/math/function/limits.rb +8 -0
  54. data/lib/plurimath/math/function/log.rb +61 -4
  55. data/lib/plurimath/math/function/longdiv.rb +12 -0
  56. data/lib/plurimath/math/function/mbox.rb +31 -0
  57. data/lib/plurimath/math/function/menclose.rb +46 -0
  58. data/lib/plurimath/math/function/merror.rb +12 -0
  59. data/lib/plurimath/math/function/mod.rb +25 -4
  60. data/lib/plurimath/math/function/msgroup.rb +37 -0
  61. data/lib/plurimath/math/function/msline.rb +12 -0
  62. data/lib/plurimath/math/function/multiscript.rb +30 -0
  63. data/lib/plurimath/math/function/norm.rb +18 -1
  64. data/lib/plurimath/math/function/obrace.rb +17 -0
  65. data/lib/plurimath/math/function/oint.rb +2 -2
  66. data/lib/plurimath/math/function/over.rb +36 -0
  67. data/lib/plurimath/math/function/overset.rb +36 -7
  68. data/lib/plurimath/math/function/phantom.rb +28 -0
  69. data/lib/plurimath/math/function/power.rb +33 -9
  70. data/lib/plurimath/math/function/power_base.rb +110 -5
  71. data/lib/plurimath/math/function/prod.rb +29 -2
  72. data/lib/plurimath/math/function/right.rb +44 -0
  73. data/lib/plurimath/math/function/root.rb +27 -4
  74. data/lib/plurimath/math/function/rule.rb +33 -0
  75. data/lib/plurimath/math/function/scarries.rb +12 -0
  76. data/lib/plurimath/math/function/scarry.rb +12 -0
  77. data/lib/plurimath/math/function/sqrt.rb +24 -2
  78. data/lib/plurimath/math/function/stackrel.rb +27 -0
  79. data/lib/plurimath/math/function/substack.rb +7 -1
  80. data/lib/plurimath/math/function/sum.rb +56 -2
  81. data/lib/plurimath/math/function/sup.rb +3 -0
  82. data/lib/plurimath/math/function/table/align.rb +24 -0
  83. data/lib/plurimath/math/function/table/array.rb +44 -0
  84. data/lib/plurimath/math/function/table/bmatrix.rb +37 -0
  85. data/lib/plurimath/math/function/table/matrix.rb +32 -0
  86. data/lib/plurimath/math/function/table/multline.rb +24 -0
  87. data/lib/plurimath/math/function/table/pmatrix.rb +24 -0
  88. data/lib/plurimath/math/function/table/split.rb +24 -0
  89. data/lib/plurimath/math/function/table/vmatrix.rb +24 -0
  90. data/lib/plurimath/math/function/table.rb +190 -20
  91. data/lib/plurimath/math/function/td.rb +27 -9
  92. data/lib/plurimath/math/function/ternary_function.rb +83 -8
  93. data/lib/plurimath/math/function/text.rb +45 -8
  94. data/lib/plurimath/math/function/tr.rb +28 -4
  95. data/lib/plurimath/math/function/ubrace.rb +17 -0
  96. data/lib/plurimath/math/function/ul.rb +29 -0
  97. data/lib/plurimath/math/function/unary_function.rb +85 -7
  98. data/lib/plurimath/math/function/underline.rb +12 -0
  99. data/lib/plurimath/math/function/underover.rb +107 -0
  100. data/lib/plurimath/math/function/underset.rb +39 -0
  101. data/lib/plurimath/math/function/vec.rb +10 -0
  102. data/lib/plurimath/math/function.rb +13 -2
  103. data/lib/plurimath/math/number.rb +11 -3
  104. data/lib/plurimath/math/symbol.rb +57 -9
  105. data/lib/plurimath/math/unicode.rb +11 -0
  106. data/lib/plurimath/math.rb +15 -6
  107. data/lib/plurimath/mathml/constants.rb +224 -179
  108. data/lib/plurimath/mathml/parser.rb +24 -7
  109. data/lib/plurimath/mathml/transform.rb +249 -148
  110. data/lib/plurimath/mathml.rb +1 -1
  111. data/lib/plurimath/omml/parser.rb +42 -0
  112. data/lib/plurimath/omml/transform.rb +278 -0
  113. data/lib/plurimath/omml.rb +1 -1
  114. data/lib/plurimath/unitsml.rb +4 -0
  115. data/lib/plurimath/utility.rb +395 -0
  116. data/lib/plurimath/version.rb +1 -1
  117. data/plurimath.gemspec +1 -0
  118. metadata +66 -9
  119. data/.github/workflows/test.yml +0 -36
  120. data/README.md +0 -40
  121. data/lib/plurimath/mathml/parse.rb +0 -63
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Plurimath
4
+ module Math
5
+ class Base
6
+ def initialize(field)
7
+ field.is_a?(Parslet::Slice) ? field.to_s : field
8
+ end
9
+
10
+ def class_name
11
+ self.class.name.split("::").last.downcase
12
+ end
13
+ end
14
+ end
15
+ end
@@ -14,29 +14,114 @@ module Plurimath
14
14
  end
15
15
 
16
16
  def to_asciimath
17
- value.map(&:to_asciimath).join
17
+ value.map(&:to_asciimath).join(" ")
18
18
  end
19
19
 
20
20
  def to_mathml
21
- <<~MATHML
22
- <math xmlns='http://www.w3.org/1998/Math/MathML'>
23
- <mstyle displaystyle='true'>
24
- #{mathml_content}
25
- </mstyle>
26
- </math>
27
- MATHML
21
+ math_attrs = {
22
+ xmlns: "http://www.w3.org/1998/Math/MathML",
23
+ display: "block",
24
+ }
25
+ style_attrs = { displaystyle: "true" }
26
+ math = Utility.ox_element("math", attributes: math_attrs)
27
+ style = Utility.ox_element("mstyle", attributes: style_attrs)
28
+ Utility.update_nodes(style, mathml_content)
29
+ Utility.update_nodes(math, [style])
30
+ Ox.dump(math, indent: 2).gsub("&amp;", "&")
28
31
  end
29
32
 
30
33
  def to_mathml_without_math_tag
31
- "<mrow>#{mathml_content}</mrow>"
34
+ Utility.update_nodes(
35
+ Utility.ox_element("mrow"),
36
+ mathml_content,
37
+ )
32
38
  end
33
39
 
34
40
  def mathml_content
35
- value.map(&:to_mathml_without_math_tag).join
41
+ value.map(&:to_mathml_without_math_tag)
36
42
  end
37
43
 
38
44
  def to_latex
39
- value.map(&:to_latex).join
45
+ value.map(&:to_latex).join(" ")
46
+ end
47
+
48
+ def to_html
49
+ value.map(&:to_html).join(" ")
50
+ end
51
+
52
+ def omml_math_attrs
53
+ {
54
+ "xmlns:m": "http://schemas.openxmlformats.org/officeDocument/2006/math",
55
+ "xmlns:mc": "http://schemas.openxmlformats.org/markup-compatibility/2006",
56
+ "xmlns:mo": "http://schemas.microsoft.com/office/mac/office/2008/main",
57
+ "xmlns:mv": "urn:schemas-microsoft-com:mac:vml",
58
+ "xmlns:o": "urn:schemas-microsoft-com:office:office",
59
+ "xmlns:r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships",
60
+ "xmlns:v": "urn:schemas-microsoft-com:vml",
61
+ "xmlns:w": "http://schemas.openxmlformats.org/wordprocessingml/2006/main",
62
+ "xmlns:w10": "urn:schemas-microsoft-com:office:word",
63
+ "xmlns:w14": "http://schemas.microsoft.com/office/word/2010/wordml",
64
+ "xmlns:w15": "http://schemas.microsoft.com/office/word/2012/wordml",
65
+ "xmlns:wne": "http://schemas.microsoft.com/office/word/2006/wordml",
66
+ "xmlns:wp": "http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing",
67
+ "xmlns:wp14": "http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing",
68
+ "xmlns:wpc": "http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas",
69
+ "xmlns:wpg": "http://schemas.microsoft.com/office/word/2010/wordprocessingGroup",
70
+ "xmlns:wpi": "http://schemas.microsoft.com/office/word/2010/wordprocessingInk",
71
+ "xmlns:wps": "http://schemas.microsoft.com/office/word/2010/wordprocessingShape",
72
+ }
73
+ end
74
+
75
+ def to_omml
76
+ para_element = Utility.ox_element(
77
+ "oMathPara",
78
+ attributes: omml_math_attrs,
79
+ namespace: "m",
80
+ )
81
+ math_element = Utility.ox_element("oMath", namespace: "m")
82
+ Utility.update_nodes(math_element, omml_content)
83
+ para_element << math_element
84
+ Ox.dump(para_element, indent: 2).gsub("&amp;", "&")
85
+ end
86
+
87
+ def omml_content
88
+ value.map do |object|
89
+ if object.is_a?(Symbol)
90
+ mt = Utility.ox_element("t", namespace: "m")
91
+ mt << object.value
92
+ else
93
+ object.to_omml_without_math_tag
94
+ end
95
+ end
96
+ end
97
+
98
+ def to_omml_without_math_tag
99
+ if value.length == 2 && ["underover", "powerbase"].include?(
100
+ value&.first&.class_name,
101
+ )
102
+ nary_tag
103
+ else
104
+ r_element = Utility.ox_element("r", namespace: "m")
105
+ r_element << Utility.rpr_element if ["symbol", "number", "text"].include?(value&.first&.class_name)
106
+ Utility.update_nodes(r_element, omml_content)
107
+ end
108
+ end
109
+
110
+ def nary_tag
111
+ nary_tag = Utility.ox_element("nary", namespace: "m")
112
+ e_tag = Utility.ox_element("e", namespace: "m")
113
+ e_tag << value&.last&.to_omml_without_math_tag
114
+ Utility.update_nodes(
115
+ nary_tag,
116
+ [
117
+ value.first.omml_nary_tag,
118
+ e_tag,
119
+ ].flatten.compact,
120
+ )
121
+ end
122
+
123
+ def class_name
124
+ self.class.name.split("::").last.downcase
40
125
  end
41
126
  end
42
127
  end
@@ -10,6 +10,40 @@ module Plurimath
10
10
  first_value = "{#{parameter_one.to_latex}}" if parameter_one
11
11
  "\\overline#{first_value}"
12
12
  end
13
+
14
+ def to_mathml_without_math_tag
15
+ first_value = parameter_one&.to_mathml_without_math_tag
16
+ Utility.update_nodes(
17
+ Utility.ox_element("mover"),
18
+ [
19
+ first_value,
20
+ Utility.ox_element("mo") << "&#xaf;",
21
+ ],
22
+ )
23
+ end
24
+
25
+ def to_omml_without_math_tag
26
+ bar = Utility.ox_element("bar", namespace: "m")
27
+ me = Utility.ox_element("e", namespace: "m")
28
+ me << parameter_one.to_omml_without_math_tag if parameter_one
29
+ Utility.update_nodes(
30
+ bar,
31
+ [
32
+ bar_pr,
33
+ me,
34
+ ],
35
+ )
36
+ end
37
+
38
+ protected
39
+
40
+ def bar_pr
41
+ attrs = { "m:val": "top" }
42
+ barpr = Utility.ox_element("barPr", namespace: "m")
43
+ pos = Utility.ox_element("pos", namespace: "m", attributes: attrs)
44
+ ctrlp = Utility.pr_element("ctrl", true, namespace: "m")
45
+ Utility.update_nodes(barpr, [pos, ctrlp])
46
+ end
13
47
  end
14
48
  end
15
49
  end
@@ -8,22 +8,49 @@ module Plurimath
8
8
  class Base < BinaryFunction
9
9
  def to_asciimath
10
10
  first_value = parameter_one.to_asciimath if parameter_one
11
- second_value = "_#{parameter_two.to_asciimath}" if parameter_two
11
+ second_value = "_#{wrapped(parameter_two)}" if parameter_two
12
12
  "#{first_value}#{second_value}"
13
13
  end
14
14
 
15
15
  def to_mathml_without_math_tag
16
- first_value = parameter_one.to_mathml_without_math_tag
17
- second_value = parameter_two.to_mathml_without_math_tag
18
- "<msub>#{first_value}#{second_value}</msub>"
16
+ under_classes = ["ubrace", "obrace"] + Utility::UNARY_CLASSES
17
+ tag_name = (under_classes.include?(parameter_one&.class_name) ? "under" : "sub")
18
+ sub_tag = Utility.ox_element("m#{tag_name}")
19
+ mathml_value = []
20
+ mathml_value << parameter_one&.to_mathml_without_math_tag
21
+ mathml_value << parameter_two&.to_mathml_without_math_tag if parameter_two
22
+ Utility.update_nodes(sub_tag, mathml_value)
19
23
  end
20
24
 
21
25
  def to_latex
22
26
  first_value = parameter_one.to_latex if parameter_one
23
- first_value = "{#{first_value}}" if parameter_one.is_a?(Math::Formula)
27
+ first_value = "{#{first_value}}" if parameter_one.is_a?(Formula)
24
28
  second_value = parameter_two.to_latex if parameter_two
25
29
  "#{first_value}_{#{second_value}}"
26
30
  end
31
+
32
+ def to_html
33
+ first_value = "<i>#{parameter_one.to_html}</i>" if parameter_one
34
+ second_value = "<sub>#{parameter_two.to_html}</sub>" if parameter_two
35
+ "#{first_value}#{second_value}"
36
+ end
37
+
38
+ def to_omml_without_math_tag
39
+ ssub_element = Utility.ox_element("sSub", namespace: "m")
40
+ subpr_element = Utility.ox_element("sSubPr", namespace: "m")
41
+ e_element = Utility.ox_element("e", namespace: "m")
42
+ sub_element = Utility.ox_element("sub", namespace: "m")
43
+ sub_element << parameter_two.to_omml_without_math_tag if parameter_two
44
+ Utility.update_nodes(
45
+ ssub_element,
46
+ [
47
+ subpr_element << Utility.pr_element("ctrl", true, namespace: "m"),
48
+ e_element << parameter_one.to_omml_without_math_tag,
49
+ sub_element,
50
+ ],
51
+ )
52
+ ssub_element
53
+ end
27
54
  end
28
55
  end
29
56
  end
@@ -12,13 +12,7 @@ module Plurimath
12
12
  end
13
13
 
14
14
  def to_asciimath
15
- "#{class_name}#{value_to_asciimath}"
16
- end
17
-
18
- def value_to_asciimath
19
- first_value = "(#{parameter_one.to_asciimath})" if parameter_one
20
- second_value = "(#{parameter_two.to_asciimath})" if parameter_two
21
- "#{first_value}#{second_value}"
15
+ "#{class_name}#{wrapped(parameter_one)}#{wrapped(parameter_two)}"
22
16
  end
23
17
 
24
18
  def ==(object)
@@ -28,25 +22,118 @@ module Plurimath
28
22
  end
29
23
 
30
24
  def to_mathml_without_math_tag
31
- first_value = parameter_one.to_mathml_without_math_tag if parameter_one
32
- second_value = parameter_two.to_mathml_without_math_tag if parameter_two
33
- third_value = invert_unicode_symbols.empty? ? class_name : invert_unicode_symbols
34
- "<mo>#{third_value}</mo>#{first_value}#{second_value}"
35
- end
36
-
37
- def invert_unicode_symbols
38
- Mathml::Constants::UNICODE_SYMBOLS.invert[class_name].to_s
25
+ mrow_tag = Utility.ox_element("mrow")
26
+ mo_tag = Utility.ox_element("mo") << invert_unicode_symbols.to_s
27
+ first_value = parameter_one&.to_mathml_without_math_tag if parameter_one
28
+ second_value = parameter_two&.to_mathml_without_math_tag if parameter_two
29
+ Utility.update_nodes(
30
+ mrow_tag,
31
+ [
32
+ first_value,
33
+ mo_tag,
34
+ second_value,
35
+ ],
36
+ )
39
37
  end
40
38
 
41
39
  def to_latex
42
- first_value = "{#{parameter_one.to_latex}}" if parameter_one
43
- second_value = "{#{parameter_two.to_latex}}" if parameter_two
40
+ first_value = latex_wrapped(parameter_one) if parameter_one
41
+ second_value = latex_wrapped(parameter_two) if parameter_two
44
42
  "\\#{class_name}#{first_value}#{second_value}"
45
43
  end
46
44
 
45
+ def to_html
46
+ first_value = "<i>#{parameter_one.to_html}</i>" if parameter_one
47
+ second_value = "<i>#{parameter_two.to_html}</i>" if parameter_two
48
+ "#{first_value}#{second_value}"
49
+ end
50
+
51
+ def to_omml_without_math_tag
52
+ r_tag = Utility.ox_element("r", namespace: "m")
53
+ update_row_tag(r_tag, parameter_one) if parameter_one
54
+ update_row_tag(r_tag, parameter_two) if parameter_two
55
+ r_tag
56
+ end
57
+
47
58
  def class_name
48
59
  self.class.name.split("::").last.downcase
49
60
  end
61
+
62
+ protected
63
+
64
+ def update_row_tag(tag, field)
65
+ first_value = field.to_omml_without_math_tag
66
+ first_value = if first_value.is_a?(String)
67
+ Utility.ox_element("t", namespace: "m") << first_value
68
+ end
69
+ Utility.update_nodes(
70
+ tag,
71
+ first_value.is_a?(Array) ? first_value : [first_value],
72
+ )
73
+ end
74
+
75
+ def latex_wrapped(field)
76
+ if validate_function_formula(field)
77
+ "{ \\left ( #{field.to_latex} \\right ) }"
78
+ else
79
+ "{#{field.to_latex}}"
80
+ end
81
+ end
82
+
83
+ def validate_function_formula(field)
84
+ unary_classes = (Utility::UNARY_CLASSES + ["obrace", "ubrace", "hat"])
85
+ if field.is_a?(Formula)
86
+ !(field.value.any?(Left) || field.value.any?(Right))
87
+ elsif unary_classes.include?(field.class_name)
88
+ false
89
+ elsif field.class.name.include?("Function") || field.is_a?(FontStyle)
90
+ !field.is_a?(Text)
91
+ end
92
+ end
93
+
94
+ def mathml_wrapped(field)
95
+ if validate_function_formula(field)
96
+ mrow_tag = Utility.ox_element("mrow")
97
+ open_paren = (Utility.ox_element("mo") << "(")
98
+ close_paren = (Utility.ox_element("mo") << ")")
99
+ Utility.update_nodes(
100
+ mrow_tag,
101
+ [
102
+ open_paren,
103
+ field.to_mathml_without_math_tag,
104
+ close_paren,
105
+ ],
106
+ )
107
+ else
108
+ field.to_mathml_without_math_tag
109
+ end
110
+ end
111
+
112
+ def wrapped(field)
113
+ return "" unless field
114
+
115
+ "(#{field.to_asciimath})"
116
+ end
117
+
118
+ def asciimath_base_value(field)
119
+ if validate_function_formula(field)
120
+ "(#{field.to_asciimath})"
121
+ else
122
+ field.to_asciimath
123
+ end
124
+ end
125
+
126
+ def latex_base_value(field)
127
+ if validate_function_formula(field)
128
+ "{ \\left ( #{field.to_latex} \\right ) }"
129
+ else
130
+ field.to_latex
131
+ end
132
+ end
133
+
134
+ def invert_unicode_symbols
135
+ Mathml::Constants::UNICODE_SYMBOLS.invert[class_name] || class_name
136
+ end
50
137
  end
51
138
  end
52
139
  end
@@ -6,6 +6,14 @@ module Plurimath
6
6
  module Math
7
7
  module Function
8
8
  class Cancel < UnaryFunction
9
+ def to_mathml_without_math_tag
10
+ cancel_tag = Utility.ox_element(
11
+ "menclose",
12
+ attributes: { notation: "updiagonalstrike" },
13
+ )
14
+ first_value = parameter_one&.to_mathml_without_math_tag
15
+ Utility.update_nodes(cancel_tag, [first_value])
16
+ end
9
17
  end
10
18
  end
11
19
  end
@@ -6,6 +6,9 @@ module Plurimath
6
6
  module Math
7
7
  module Function
8
8
  class Ceil < UnaryFunction
9
+ def to_latex
10
+ "{\\lceil #{latex_value} \\rceil}"
11
+ end
9
12
  end
10
13
  end
11
14
  end
@@ -6,16 +6,26 @@ module Plurimath
6
6
  module Math
7
7
  module Function
8
8
  class Color < BinaryFunction
9
+ def to_asciimath
10
+ first_value = "(#{parameter_one&.to_asciimath&.gsub(/\s/, '')})"
11
+ second_value = "(#{parameter_two&.to_asciimath})"
12
+ "color#{first_value}#{second_value}"
13
+ end
14
+
9
15
  def to_mathml_without_math_tag
10
- first_value = parameter_one.value if parameter_one
11
- second_value = parameter_two.to_mathml_without_math_tag if parameter_two
12
- "<mstyle mathcolor='#{first_value}'>#{second_value}</mstyle>"
16
+ Utility.update_nodes(
17
+ Utility.ox_element(
18
+ "mstyle",
19
+ attributes: { mathcolor: parameter_one&.to_asciimath&.gsub(/\s/, "")&.gsub(/"/, "") },
20
+ ),
21
+ [parameter_two&.to_mathml_without_math_tag],
22
+ )
13
23
  end
14
24
 
15
25
  def to_latex
16
- first_value = parameter_one.to_latex if parameter_one
17
- second_value = parameter_two.to_latex if parameter_two
18
- "\\#{class_name}{#{first_value}}#{second_value}"
26
+ first_value = parameter_one&.to_asciimath&.gsub(/\s/, "")
27
+ second_value = parameter_two&.to_latex
28
+ "{\\#{class_name}{#{first_value}} #{second_value}}"
19
29
  end
20
30
  end
21
31
  end
@@ -6,6 +6,14 @@ module Plurimath
6
6
  module Math
7
7
  module Function
8
8
  class F < UnaryFunction
9
+ def to_asciimath
10
+ "f#{parameter_one&.to_asciimath}"
11
+ end
12
+
13
+ def to_latex
14
+ first_value = latex_value if parameter_one
15
+ "f#{first_value}"
16
+ end
9
17
  end
10
18
  end
11
19
  end
@@ -8,13 +8,105 @@ module Plurimath
8
8
  class Fenced < TernaryFunction
9
9
  def to_asciimath
10
10
  first_value = parameter_one ? parameter_one.to_asciimath : "("
11
- second_value = parameter_two.map(&:to_asciimath).join(",") if parameter_two
12
11
  third_value = parameter_three ? parameter_three.to_asciimath : ")"
13
- "#{first_value}#{second_value}#{third_value}"
12
+ "#{first_value}#{parameter_two&.map(&:to_asciimath)&.join(' ')}#{third_value}"
14
13
  end
15
14
 
16
15
  def to_mathml_without_math_tag
17
- "<mfenced open='#{parameter_one.value}' close='#{parameter_three.value}'>#{parameter_two.map(&:to_mathml_without_math_tag).join}</mfenced>"
16
+ first_value = Utility.ox_element("mo") << (mathml_paren(parameter_one) || "")
17
+ second_value = parameter_two&.map(&:to_mathml_without_math_tag) || []
18
+ third_value = Utility.ox_element("mo") << (mathml_paren(parameter_three) || "")
19
+ Utility.update_nodes(
20
+ Utility.ox_element("mrow"),
21
+ (second_value.insert(0, first_value) << third_value),
22
+ )
23
+ end
24
+
25
+ def to_html
26
+ first_value = "<i>#{parameter_one.value}</i>" if parameter_one
27
+ second_value = parameter_two.map(&:to_html).join if parameter_two
28
+ third_value = "<i>#{parameter_three.value}</i>" if parameter_three
29
+ "#{first_value}#{second_value}#{third_value}"
30
+ end
31
+
32
+ def to_latex
33
+ open_paren = parameter_one ? parameter_one.value : "("
34
+ fenced_value = parameter_two&.map(&:to_latex)&.join(" ")
35
+ close_paren = parameter_three ? parameter_three.value : ")"
36
+ first_value = latex_paren(open_paren, false)
37
+ second_value = latex_paren(close_paren, true)
38
+ "#{first_value}#{fenced_value}#{second_value}"
39
+ end
40
+
41
+ def to_omml_without_math_tag
42
+ d = Utility.ox_element("d", namespace: "m")
43
+ dpr = Utility.ox_element("dPr", namespace: "m")
44
+ first_value(dpr)
45
+ third_value(dpr)
46
+ dpr << Utility.pr_element("ctrl", true, namespace: "m")
47
+ Utility.update_nodes(
48
+ d,
49
+ [dpr] + second_value,
50
+ )
51
+ end
52
+
53
+ protected
54
+
55
+ def second_value
56
+ class_names = ["number", "symbol"].freeze
57
+ parameter_two.map do |object|
58
+ e_tag = Utility.ox_element("e", namespace: "m")
59
+ e_tag << if class_names.include?(object.class_name)
60
+ fenced_omml_value(object)
61
+ else
62
+ object.to_omml_without_math_tag
63
+ end
64
+ end
65
+ end
66
+
67
+ def fenced_omml_value(object)
68
+ r_tag = Utility.ox_element("r", namespace: "m")
69
+ t_tag = Utility.ox_element("t", namespace: "m")
70
+ t_tag << object.value
71
+ r_tag << t_tag
72
+ end
73
+
74
+ def first_value(dpr)
75
+ first_value = parameter_one&.value
76
+ return dpr if first_value.nil? || first_value.empty?
77
+
78
+ attributes = { "m:val": first_value }
79
+ dpr << Utility.ox_element(
80
+ "begChr",
81
+ namespace: "m",
82
+ attributes: attributes,
83
+ )
84
+ end
85
+
86
+ def third_value(dpr)
87
+ third_value = parameter_three&.value
88
+ return dpr if third_value.nil? || third_value.empty?
89
+
90
+ attributes = { "m:val": third_value }
91
+ dpr << Utility.ox_element(
92
+ "endChr",
93
+ namespace: "m",
94
+ attributes: attributes,
95
+ )
96
+ end
97
+
98
+ def latex_paren(paren, right)
99
+ paren_side = right ? "\\right" : "\\left"
100
+ return "#{paren_side} ." if paren.include?(":")
101
+
102
+ paren = %w[{ }].include?(paren) ? "\\#{paren}" : paren
103
+ " #{paren_side} #{paren} "
104
+ end
105
+
106
+ def mathml_paren(field)
107
+ return "" if field.value.include?(":")
108
+
109
+ field.value
18
110
  end
19
111
  end
20
112
  end
@@ -6,6 +6,21 @@ module Plurimath
6
6
  module Math
7
7
  module Function
8
8
  class Floor < UnaryFunction
9
+ def to_latex
10
+ "\\lfloor #{parameter_one.to_latex} \\rfloor"
11
+ end
12
+
13
+ def to_mathml_without_math_tag
14
+ first_value = parameter_one&.to_mathml_without_math_tag
15
+ Utility.update_nodes(
16
+ Utility.ox_element("mrow"),
17
+ [
18
+ Utility.ox_element("mo") << "&#x230a;",
19
+ first_value,
20
+ Utility.ox_element("mo") << "&#x230b;",
21
+ ],
22
+ )
23
+ end
9
24
  end
10
25
  end
11
26
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../font_style"
4
+
5
+ module Plurimath
6
+ module Math
7
+ module Function
8
+ class FontStyle
9
+ class Bold < FontStyle
10
+ def initialize(parameter_one,
11
+ parameter_two = "bold")
12
+ super
13
+ end
14
+
15
+ def to_asciimath
16
+ "mathbf(#{parameter_one.to_asciimath})"
17
+ end
18
+
19
+ def to_latex
20
+ first_value = parameter_one.to_latex if parameter_one
21
+ "\\mathbf{#{first_value}}"
22
+ end
23
+
24
+ def to_mathml_without_math_tag
25
+ Utility.update_nodes(
26
+ Utility.ox_element(
27
+ "mstyle",
28
+ attributes: { mathvariant: "bold" },
29
+ ),
30
+ [parameter_one.to_mathml_without_math_tag],
31
+ )
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../font_style"
4
+
5
+ module Plurimath
6
+ module Math
7
+ module Function
8
+ class FontStyle
9
+ class DoubleStruck < FontStyle
10
+ def initialize(parameter_one,
11
+ parameter_two = "double-struck")
12
+ super
13
+ end
14
+
15
+ def to_asciimath
16
+ "mathbb(#{parameter_one.to_asciimath})"
17
+ end
18
+
19
+ def to_latex
20
+ first_value = parameter_one.to_latex if parameter_one
21
+ "\\mathbb{#{first_value}}"
22
+ end
23
+
24
+ def to_mathml_without_math_tag
25
+ Utility.update_nodes(
26
+ Utility.ox_element(
27
+ "mstyle",
28
+ attributes: { mathvariant: "double-struck" },
29
+ ),
30
+ [parameter_one.to_mathml_without_math_tag],
31
+ )
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end