plurimath 0.3.8 → 0.3.9

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 78fffd236ecb53260b07fa02ce5cc2254d3835a6491229a7acc50c2a2ed80dbb
4
- data.tar.gz: e17acb1342c8b5a7f5de3b8496962a12264404f9dd8902a42c47069a6ad0a157
3
+ metadata.gz: 1d50026941bd556b94051c14a61a081ad96dbf839ed1aa324a9451382ef512e8
4
+ data.tar.gz: 40bf633bf5c0286daa011bf80d90baa2d522b18b9e545035d5dd6c32593792db
5
5
  SHA512:
6
- metadata.gz: a66739db5ef92081d8c73af2bbebaf77aa03bab5f8befbd14a9a80bdb4eea4d8c03e996dbc0aedbb5cf16c0bd90189a32dde1d67925c646f02cec6423e532f3c
7
- data.tar.gz: 81e43972fead184a05ebc04948bba6433fef1ce6824223bd59b8a89d08c2ac41f4adbcb41e00426dd2d9dad6d6ff04a0ec2fc34ba7f51a7457f258b5087e0e72
6
+ metadata.gz: 8cf6f394801e567d2a7b482a14fc4da5e3b4c19515ce549a00fc9c247ce4d26c565dc5ff72586648cd30c51c0361f40808fb1117ed408bd9c319156622b34640
7
+ data.tar.gz: 65d1590db64f33f620ad438ae2d328eb1fe9a6bffeee9a427dba9f782bdc9b429ed5c54e9892ade8c59894ddd57d2bcc6680a4b7cb1b9cee26c67c0cab1b05d2
@@ -766,6 +766,18 @@ module Plurimath
766
766
  )
767
767
  end
768
768
 
769
+ rule(ternary_class: simple(:function),
770
+ base_value: simple(:base),
771
+ power_value: simple(:power),
772
+ third_value: sequence(:third)) do
773
+ third_value = third.is_a?(Slice) ? nil : third
774
+ Utility.get_class(function).new(
775
+ Utility.unfenced_value(base),
776
+ Utility.unfenced_value(power),
777
+ Utility.filter_values(third_value),
778
+ )
779
+ end
780
+
769
781
  rule(unary_class: simple(:function),
770
782
  intermediate_exp: simple(:int_exp)) do
771
783
  first_value = if Utility::UNARY_CLASSES.include?(function)
@@ -821,6 +833,11 @@ module Plurimath
821
833
  expr.insert(0, ternary)
822
834
  end
823
835
 
836
+ rule(ternary: simple(:ternary),
837
+ left_right: simple(:left_right)) do
838
+ [ternary, left_right]
839
+ end
840
+
824
841
  rule(unary_class: simple(:function),
825
842
  text: simple(:text)) do
826
843
  Utility.get_class(function).new(
@@ -15,6 +15,10 @@ module Plurimath
15
15
  "subsup"
16
16
  end
17
17
 
18
+ def omml_tag_name
19
+ "subSup"
20
+ end
21
+
18
22
  def nary_attr_value
19
23
  ""
20
24
  end
@@ -3,12 +3,13 @@
3
3
  module Plurimath
4
4
  module Math
5
5
  class Formula < Core
6
- attr_accessor :value, :left_right_wrapper, :displaystyle
6
+ attr_accessor :value, :left_right_wrapper, :displaystyle, :input_string
7
7
 
8
8
  def initialize(
9
9
  value = [],
10
10
  left_right_wrapper = true,
11
- displaystyle: true
11
+ displaystyle: true,
12
+ input_string: nil
12
13
  )
13
14
  @value = value.is_a?(Array) ? value : [value]
14
15
  left_right_wrapper = false if @value.first.is_a?(Function::Left)
@@ -23,6 +24,8 @@ module Plurimath
23
24
 
24
25
  def to_asciimath
25
26
  value.map(&:to_asciimath).join(" ")
27
+ rescue
28
+ parse_error!(:asciimath)
26
29
  end
27
30
 
28
31
  def to_mathml(display_style: displaystyle)
@@ -36,6 +39,8 @@ module Plurimath
36
39
  Utility.update_nodes(style, mathml_content)
37
40
  Utility.update_nodes(math, [style])
38
41
  Ox.dump(math, indent: 2).gsub("&amp;", "&")
42
+ rescue
43
+ parse_error!(:mathml)
39
44
  end
40
45
 
41
46
  def to_mathml_without_math_tag
@@ -53,10 +58,14 @@ module Plurimath
53
58
 
54
59
  def to_latex
55
60
  value&.map(&:to_latex)&.join(" ")
61
+ rescue
62
+ parse_error!(:latex)
56
63
  end
57
64
 
58
65
  def to_html
59
66
  value&.map(&:to_html)&.join(" ")
67
+ rescue
68
+ parse_error!(:html)
60
69
  end
61
70
 
62
71
  def omml_math_attrs
@@ -92,6 +101,8 @@ module Plurimath
92
101
  Utility.update_nodes(math_element, omml_content)
93
102
  Utility.update_nodes(para_element, Array(math_element))
94
103
  Ox.dump(para_element, indent: 2).gsub("&amp;", "&").lstrip
104
+ rescue
105
+ parse_error!(:omml)
95
106
  end
96
107
 
97
108
  def omml_content
@@ -115,10 +126,6 @@ module Plurimath
115
126
  [nary_tag]
116
127
  end
117
128
 
118
- def class_name
119
- self.class.name.split("::").last.downcase
120
- end
121
-
122
129
  def extract_class_from_text
123
130
  return false unless (value.length < 2 && value&.first&.is_a?(Function::Text))
124
131
 
@@ -141,6 +148,12 @@ module Plurimath
141
148
  def validate_function_formula
142
149
  (value.none?(Function::Left) || value.none?(Function::Right))
143
150
  end
151
+
152
+ protected
153
+
154
+ def parse_error!(type)
155
+ Math.parse_error!(input_string, type)
156
+ end
144
157
  end
145
158
  end
146
159
  end
@@ -83,7 +83,17 @@ module Plurimath
83
83
  end
84
84
 
85
85
  def all_values_exist?
86
- !parameter_one.nil? && !parameter_two.nil?
86
+ !(parameter_one.nil? && parameter_two.nil?)
87
+ end
88
+
89
+ def underover
90
+ return r_element(class_name, rpr_tag: false) unless all_values_exist?
91
+
92
+ first_value = Symbol.new(class_name)
93
+ overset = Overset.new(first_value, parameter_two)
94
+ return Array(overset.to_omml_without_math_tag) unless parameter_one
95
+
96
+ Array(Underset.new(overset, parameter_one)&.to_omml_without_math_tag)
87
97
  end
88
98
  end
89
99
  end
@@ -14,7 +14,7 @@ module Plurimath
14
14
 
15
15
  def to_mathml_without_math_tag
16
16
  first_value = Utility.ox_element("mo") << class_name
17
- return first_value if all_values_exist?
17
+ return first_value unless all_values_exist?
18
18
 
19
19
  value_array = [first_value]
20
20
  value_array << parameter_one&.to_mathml_without_math_tag
@@ -31,13 +31,7 @@ module Plurimath
31
31
  end
32
32
 
33
33
  def to_omml_without_math_tag
34
- return r_element("inf", rpr_tag: false) unless all_values_exist?
35
-
36
- inf = Symbol.new("inf")
37
- overset = Overset.new(inf, parameter_two)
38
- return Array(overset.to_omml_without_math_tag) unless parameter_one
39
-
40
- Array(Underset.new(overset, parameter_one)&.to_omml_without_math_tag)
34
+ underover
41
35
  end
42
36
  end
43
37
  end
@@ -40,13 +40,7 @@ module Plurimath
40
40
  end
41
41
 
42
42
  def to_omml_without_math_tag
43
- return r_element("lim", rpr_tag: false) unless all_values_exist?
44
-
45
- lim = Symbol.new("lim")
46
- overset = Overset.new(lim, parameter_two)
47
- return overset.to_omml_without_math_tag unless parameter_one
48
-
49
- Underset.new(overset, parameter_one)&.to_omml_without_math_tag
43
+ underover
50
44
  end
51
45
  end
52
46
  end
@@ -22,17 +22,16 @@ module Plurimath
22
22
  mo_tag = Utility.ox_element("mo") << invert_unicode_symbols.to_s
23
23
  return mo_tag unless all_values_exist?
24
24
 
25
- msubsup_tag = Utility.ox_element("msubsup")
26
- first_value = parameter_one&.to_mathml_without_math_tag if parameter_one
27
- second_value = parameter_two&.to_mathml_without_math_tag if parameter_two
28
- Utility.update_nodes(
29
- msubsup_tag,
30
- [
31
- mo_tag,
32
- first_value,
33
- second_value,
34
- ],
35
- )
25
+ value_array = [mo_tag]
26
+ value_array << parameter_one&.to_mathml_without_math_tag
27
+ value_array << parameter_two&.to_mathml_without_math_tag
28
+ tag_name = if parameter_one && parameter_two
29
+ "subsup"
30
+ else
31
+ parameter_one ? "sub" : "sup"
32
+ end
33
+ msubsup_tag = Utility.ox_element("m#{tag_name}")
34
+ Utility.update_nodes(msubsup_tag, value_array)
36
35
  return msubsup_tag if parameter_three.nil?
37
36
 
38
37
  Utility.update_nodes(
@@ -40,7 +39,7 @@ module Plurimath
40
39
  [
41
40
  msubsup_tag,
42
41
  parameter_three&.to_mathml_without_math_tag,
43
- ].flatten.compact,
42
+ ].compact,
44
43
  )
45
44
  end
46
45
 
@@ -35,7 +35,7 @@ module Plurimath
35
35
  narypr << Utility.ox_element(
36
36
  "limLoc",
37
37
  namespace: "m",
38
- attributes: { "m:val": "subSup" },
38
+ attributes: { "m:val": parameter_one.omml_tag_name },
39
39
  )
40
40
  hide_tags(narypr)
41
41
  narypr << Utility.pr_element("ctrl", true, namespace: "m")
@@ -47,6 +47,8 @@ module Plurimath
47
47
  end
48
48
 
49
49
  def to_omml_without_math_tag
50
+ return underover if parameter_one.omml_tag_name == "undOvr"
51
+
50
52
  ssubsup = Utility.ox_element("sSubSup", namespace: "m")
51
53
  ssubsuppr = Utility.ox_element("sSubSupPr", namespace: "m")
52
54
  ssubsuppr << hide_tags(
@@ -74,6 +74,10 @@ module Plurimath
74
74
  [r_tag]
75
75
  end
76
76
  end
77
+
78
+ def nary_attr_value
79
+ "∏"
80
+ end
77
81
  end
78
82
  end
79
83
  end
@@ -74,6 +74,14 @@ module Plurimath
74
74
  [r_tag]
75
75
  end
76
76
  end
77
+
78
+ def omml_tag_name
79
+ "undOvr"
80
+ end
81
+
82
+ def nary_attr_value
83
+ "∑"
84
+ end
77
85
  end
78
86
  end
79
87
  end
@@ -77,10 +77,6 @@ module Plurimath
77
77
  end
78
78
  end
79
79
 
80
- def class_name
81
- self.class.name.split("::").last.downcase
82
- end
83
-
84
80
  protected
85
81
 
86
82
  def present?(field)
@@ -131,6 +131,13 @@ module Plurimath
131
131
 
132
132
  Array(parameter.to_mathml_without_math_tag)
133
133
  end
134
+
135
+ def underover
136
+ overset = Overset.new(parameter_one, parameter_three)
137
+ return overset unless parameter_two
138
+
139
+ Underset.new(overset, parameter_two)&.to_omml_without_math_tag
140
+ end
134
141
  end
135
142
  end
136
143
  end
@@ -26,6 +26,10 @@ module Plurimath
26
26
  "underover"
27
27
  end
28
28
 
29
+ def omml_tag_name
30
+ "undOvr"
31
+ end
32
+
29
33
  def validate_function_formula
30
34
  false
31
35
  end
@@ -36,10 +36,7 @@ module Plurimath
36
36
  end
37
37
 
38
38
  def to_omml_without_math_tag
39
- overset = Overset.new(parameter_one, parameter_three)
40
- return overset unless parameter_two
41
-
42
- Underset.new(overset, parameter_two)&.to_omml_without_math_tag
39
+ underover
43
40
  end
44
41
 
45
42
  def omml_nary_tag
@@ -86,7 +83,7 @@ module Plurimath
86
83
  end
87
84
 
88
85
  def first_value(pr_element)
89
- first_value = parameter_one.is_a?(Number) ? parameter_one.value : parameter_one.to_omml_without_math_tag
86
+ first_value = parameter_one.nary_attr_value
90
87
  first_value = Utility.html_entity_to_unicode(first_value)
91
88
  unless first_value == "∫"
92
89
  pr_element << Utility.ox_element(
@@ -72,6 +72,14 @@ module Plurimath
72
72
  ["&#x22c0;", "&#x22c1;", "&#x22c2;", "&#x22c3;"].include?(value) ? "underover" : "subsup"
73
73
  end
74
74
 
75
+ def omml_tag_name
76
+ if ["&#x22c0;", "&#x22c1;", "&#x22c2;", "&#x22c3;", "&#x22c3;", "&#x2211;", "&#x220f;"].include?(value)
77
+ return "undOvr"
78
+ end
79
+
80
+ "subSup"
81
+ end
82
+
75
83
  def nary_attr_value
76
84
  value
77
85
  end
@@ -42,7 +42,9 @@ module Plurimath
42
42
 
43
43
  begin
44
44
  klass = klass_from_type(type)
45
- klass.new(text).to_formula
45
+ formula = klass.new(text).to_formula
46
+ formula.input_string = text
47
+ formula
46
48
  rescue => ee
47
49
  parse_error!(text, type.to_sym)
48
50
  end
@@ -22,11 +22,11 @@ module Plurimath
22
22
 
23
23
  def parse
24
24
  ox_nodes = Ox.load(text, strip_namespace: true)
25
- display_style = ox_nodes&.locate("*/mstyle/@displaystyle")&.first
25
+ display_style = ox_nodes&.locate("*/mstyle/@displaystyle")&.first || true
26
26
  nodes = parse_nodes(ox_nodes.nodes)
27
27
  Math::Formula.new(
28
28
  Transform.new.apply(nodes).flatten.compact,
29
- displaystyle: display_style
29
+ displaystyle: display_style,
30
30
  )
31
31
  end
32
32
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Plurimath
4
- VERSION = "0.3.8"
4
+ VERSION = "0.3.9"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plurimath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.8
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-08-03 00:00:00.000000000 Z
11
+ date: 2023-08-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parslet