plurimath 0.3.0 → 0.3.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/plurimath/asciimath/transform.rb +8 -8
- data/lib/plurimath/latex/parse.rb +1 -1
- data/lib/plurimath/math/formula.rb +8 -3
- data/lib/plurimath/math/function/binary_function.rb +1 -0
- data/lib/plurimath/math/function/sum.rb +2 -2
- data/lib/plurimath/math/function/ternary_function.rb +1 -0
- data/lib/plurimath/math/function/tilde.rb +6 -0
- data/lib/plurimath/math/function/unary_function.rb +4 -3
- data/lib/plurimath/math/symbol.rb +5 -1
- data/lib/plurimath/math.rb +31 -16
- data/lib/plurimath/utility.rb +13 -3
- data/lib/plurimath/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 70a043b8a7a5e17b083cf66545b6df58d249f1a09c3666f53090cecc14a273ae
|
4
|
+
data.tar.gz: e9aecbfe1aba0123fac731278428ebdb7b05197c8acbf12f9f64ab1528dea379
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 07fea95bec90f3e4eb135e5fd2246b5460bf0da939111df37140f4cf64462111404558e5a67d5636b66cbf7a72f7d998107ba06273415f13b219c98cddb0df71
|
7
|
+
data.tar.gz: 0a116326448fb66449949741382473549ef806016e808bb9b67ddd68ff47f7a430685bf88ece03e47655199e9e23998bcae41e5e7559f8cb9c862611f899cc3e
|
@@ -352,7 +352,7 @@ module Plurimath
|
|
352
352
|
font_style.to_s,
|
353
353
|
)
|
354
354
|
Math::Function::Base.new(
|
355
|
-
|
355
|
+
font_object,
|
356
356
|
Utility.unfenced_value(base),
|
357
357
|
)
|
358
358
|
end
|
@@ -460,7 +460,7 @@ module Plurimath
|
|
460
460
|
rule(power_base: simple(:power_base),
|
461
461
|
base: simple(:base)) do
|
462
462
|
Math::Function::Base.new(
|
463
|
-
|
463
|
+
power_base,
|
464
464
|
Utility.unfenced_value(base),
|
465
465
|
)
|
466
466
|
end
|
@@ -469,7 +469,7 @@ module Plurimath
|
|
469
469
|
base: simple(:base),
|
470
470
|
expr: sequence(:expr)) do
|
471
471
|
base_object = Math::Function::Base.new(
|
472
|
-
|
472
|
+
power_base,
|
473
473
|
Utility.unfenced_value(base),
|
474
474
|
)
|
475
475
|
Math::Formula.new(
|
@@ -481,7 +481,7 @@ module Plurimath
|
|
481
481
|
base: simple(:base),
|
482
482
|
expr: simple(:expr)) do
|
483
483
|
base_object = Math::Function::Base.new(
|
484
|
-
|
484
|
+
power_base,
|
485
485
|
Utility.unfenced_value(base),
|
486
486
|
)
|
487
487
|
formula_array = [base_object]
|
@@ -494,7 +494,7 @@ module Plurimath
|
|
494
494
|
base_value: simple(:base_value),
|
495
495
|
power_value: simple(:power_value)) do
|
496
496
|
Math::Function::PowerBase.new(
|
497
|
-
|
497
|
+
power_base,
|
498
498
|
Utility.unfenced_value(base_value),
|
499
499
|
Utility.unfenced_value(power_value),
|
500
500
|
)
|
@@ -506,7 +506,7 @@ module Plurimath
|
|
506
506
|
first_value = power_value
|
507
507
|
first_value = power_value.shift if Utility.frac_values(power_value)
|
508
508
|
power_base_object = Math::Function::PowerBase.new(
|
509
|
-
|
509
|
+
power_base,
|
510
510
|
Utility.unfenced_value(base_value),
|
511
511
|
Utility.filter_values(first_value),
|
512
512
|
)
|
@@ -527,7 +527,7 @@ module Plurimath
|
|
527
527
|
power_value: simple(:power_value),
|
528
528
|
expr: sequence(:expr)) do
|
529
529
|
power_base_object = Math::Function::PowerBase.new(
|
530
|
-
|
530
|
+
power_base,
|
531
531
|
Utility.unfenced_value(base_value),
|
532
532
|
Utility.unfenced_value(power_value),
|
533
533
|
)
|
@@ -649,7 +649,7 @@ module Plurimath
|
|
649
649
|
rule(intermediate_exp: simple(:int_exp),
|
650
650
|
base: simple(:base)) do
|
651
651
|
Math::Function::Base.new(
|
652
|
-
|
652
|
+
int_exp,
|
653
653
|
Utility.unfenced_value(base),
|
654
654
|
)
|
655
655
|
end
|
@@ -208,7 +208,7 @@ module Plurimath
|
|
208
208
|
|
209
209
|
def unary_rules(first_value)
|
210
210
|
(slashed_value(first_value, :unary_functions) >> dynamic_power_base) |
|
211
|
-
(slashed_value(first_value, :unary) >> intermediate_exp.as(:first_value)).as(:unary_functions) |
|
211
|
+
(slashed_value(first_value, :unary) >> (left_right | intermediate_exp).as(:first_value)).as(:unary_functions) |
|
212
212
|
(slashed_value(first_value, :unary))
|
213
213
|
end
|
214
214
|
|
@@ -3,14 +3,17 @@
|
|
3
3
|
module Plurimath
|
4
4
|
module Math
|
5
5
|
class Formula
|
6
|
-
attr_accessor :value
|
6
|
+
attr_accessor :value, :left_right_wrapper
|
7
7
|
|
8
|
-
def initialize(value = [])
|
8
|
+
def initialize(value = [], left_right_wrapper = true)
|
9
9
|
@value = value.is_a?(Array) ? value : [value]
|
10
|
+
left_right_wrapper = false if @value.first.is_a?(Function::Left)
|
11
|
+
@left_right_wrapper = left_right_wrapper
|
10
12
|
end
|
11
13
|
|
12
14
|
def ==(object)
|
13
|
-
object.value == value
|
15
|
+
object.value == value &&
|
16
|
+
object.left_right_wrapper == left_right_wrapper
|
14
17
|
end
|
15
18
|
|
16
19
|
def to_asciimath
|
@@ -31,6 +34,8 @@ module Plurimath
|
|
31
34
|
end
|
32
35
|
|
33
36
|
def to_mathml_without_math_tag
|
37
|
+
return mathml_content unless left_right_wrapper
|
38
|
+
|
34
39
|
Utility.update_nodes(
|
35
40
|
Utility.ox_element("mrow"),
|
36
41
|
mathml_content,
|
@@ -56,7 +56,7 @@ module Plurimath
|
|
56
56
|
protected
|
57
57
|
|
58
58
|
def omml_first_value
|
59
|
-
return parameter_one&.to_omml_without_math_tag unless parameter_one
|
59
|
+
return parameter_one&.to_omml_without_math_tag unless parameter_one.is_a?(Math::Symbol)
|
60
60
|
|
61
61
|
mt = Utility.ox_element("t", namespace: "m")
|
62
62
|
mt << parameter_one.to_omml_without_math_tag if parameter_one
|
@@ -64,7 +64,7 @@ module Plurimath
|
|
64
64
|
end
|
65
65
|
|
66
66
|
def omml_second_value
|
67
|
-
return parameter_two&.to_omml_without_math_tag unless parameter_two
|
67
|
+
return parameter_two&.to_omml_without_math_tag unless parameter_two.is_a?(Math::Symbol)
|
68
68
|
|
69
69
|
mt = Utility.ox_element("t", namespace: "m")
|
70
70
|
mt << parameter_two.to_omml_without_math_tag if parameter_two
|
@@ -6,6 +6,12 @@ module Plurimath
|
|
6
6
|
module Math
|
7
7
|
module Function
|
8
8
|
class Tilde < UnaryFunction
|
9
|
+
def to_mathml_without_math_tag
|
10
|
+
mover = Utility.ox_element("mover")
|
11
|
+
first_value = (Utility.ox_element("mo") << "~")
|
12
|
+
second_value = parameter_one.to_mathml_without_math_tag if parameter_one
|
13
|
+
Utility.update_nodes(mover, [second_value, first_value])
|
14
|
+
end
|
9
15
|
end
|
10
16
|
end
|
11
17
|
end
|
@@ -9,6 +9,7 @@ module Plurimath
|
|
9
9
|
def initialize(parameter_one = nil)
|
10
10
|
parameter_one = parameter_one.to_s if parameter_one.is_a?(Parslet::Slice)
|
11
11
|
@parameter_one = parameter_one
|
12
|
+
Utility.validate_left_right([parameter_one])
|
12
13
|
end
|
13
14
|
|
14
15
|
def ==(object)
|
@@ -103,12 +104,12 @@ module Plurimath
|
|
103
104
|
end
|
104
105
|
|
105
106
|
def omml_value
|
106
|
-
if parameter_one
|
107
|
-
return parameter_one&.compact
|
107
|
+
if parameter_one.is_a?(Array)
|
108
|
+
return parameter_one&.compact&.map(&:to_omml_without_math_tag)
|
108
109
|
end
|
109
110
|
|
110
111
|
first_value = parameter_one&.to_omml_without_math_tag
|
111
|
-
if parameter_one
|
112
|
+
if parameter_one.is_a?(Symbol)
|
112
113
|
first_value = Utility.ox_element("t", namespace: "m") << first_value
|
113
114
|
end
|
114
115
|
Array(first_value)
|
@@ -31,7 +31,7 @@ module Plurimath
|
|
31
31
|
|
32
32
|
unicodes = Mathml::Constants::UNICODE_SYMBOLS
|
33
33
|
unicode = unicodes.invert[value]
|
34
|
-
if operator?(unicode) || unicode
|
34
|
+
if operator?(unicode) || unicode || explicit_checks(unicode)
|
35
35
|
mo_value = (unicodes[value] || unicode || value).to_s
|
36
36
|
return Utility.ox_element("mo") << mo_value
|
37
37
|
end
|
@@ -70,6 +70,10 @@ module Plurimath
|
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
73
|
+
def explicit_checks(unicode)
|
74
|
+
return true if [unicode, value].any?{|v| ["∣", "|"].include?(v) }
|
75
|
+
end
|
76
|
+
|
73
77
|
def specific_values
|
74
78
|
return "" if ["{:", ":}"].include?(value)
|
75
79
|
|
data/lib/plurimath/math.rb
CHANGED
@@ -24,7 +24,8 @@ Ox.default_options = { encoding: "UTF-8" }
|
|
24
24
|
|
25
25
|
module Plurimath
|
26
26
|
module Math
|
27
|
-
class
|
27
|
+
class ParseError < StandardError; end
|
28
|
+
class InvalidTypeError < TypeError; end
|
28
29
|
|
29
30
|
VALID_TYPES = {
|
30
31
|
omml: Omml,
|
@@ -37,25 +38,39 @@ module Plurimath
|
|
37
38
|
}.freeze
|
38
39
|
|
39
40
|
def parse(text, type)
|
40
|
-
|
41
|
+
type_error! unless valid_type?(type)
|
41
42
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
#{text}
|
49
|
-
---- INPUT END ----
|
50
|
-
MESSAGE
|
51
|
-
raise Math::Error.new(message), cause: nil
|
43
|
+
begin
|
44
|
+
klass = klass_from_type(type)
|
45
|
+
klass.new(text).to_formula
|
46
|
+
rescue => ee
|
47
|
+
parse_error!(text, type.to_sym)
|
48
|
+
end
|
52
49
|
end
|
53
50
|
|
54
51
|
private
|
55
52
|
|
56
|
-
def
|
57
|
-
|
58
|
-
|
53
|
+
def klass_from_type(type_string_or_sym)
|
54
|
+
VALID_TYPES[type_string_or_sym.to_sym]
|
55
|
+
end
|
56
|
+
|
57
|
+
def parse_error!(text, type)
|
58
|
+
message = <<~MESSAGE
|
59
|
+
[plurimath] Error: Failed to parse the following formula with type `#{type}`.
|
60
|
+
[plurimath] Please first manually validate the formula.
|
61
|
+
[plurimath] If this is a bug, please report the formula at our issue tracker at:
|
62
|
+
[plurimath] https://github.com/plurimath/plurimath/issues
|
63
|
+
---- FORMULA BEGIN ----
|
64
|
+
#{text}
|
65
|
+
---- FORMULA END ----
|
66
|
+
MESSAGE
|
67
|
+
raise ParseError.new(message), cause: nil
|
68
|
+
end
|
69
|
+
|
70
|
+
def type_error!
|
71
|
+
raise InvalidTypeError.new(
|
72
|
+
"`type` must be one of: `#{VALID_TYPES.keys.join('`, `')}`"
|
73
|
+
)
|
59
74
|
end
|
60
75
|
|
61
76
|
def valid_type?(type)
|
@@ -63,6 +78,6 @@ module Plurimath
|
|
63
78
|
VALID_TYPES.key?(type.to_sym)
|
64
79
|
end
|
65
80
|
|
66
|
-
module_function :parse, :
|
81
|
+
module_function :parse, :klass_from_type, :parse_error!, :type_error!, :valid_type?
|
67
82
|
end
|
68
83
|
end
|
data/lib/plurimath/utility.rb
CHANGED
@@ -190,7 +190,10 @@ module Plurimath
|
|
190
190
|
end
|
191
191
|
|
192
192
|
def update_nodes(element, nodes)
|
193
|
-
nodes&.each
|
193
|
+
nodes&.each do |node|
|
194
|
+
next update_nodes(element, node) if node.is_a?(Array)
|
195
|
+
element << node unless node.nil?
|
196
|
+
end
|
194
197
|
element
|
195
198
|
end
|
196
199
|
|
@@ -361,8 +364,7 @@ module Plurimath
|
|
361
364
|
def unfenced_value(object)
|
362
365
|
case object
|
363
366
|
when Math::Function::Fenced
|
364
|
-
|
365
|
-
unfenced_value(value)
|
367
|
+
filter_values(object.parameter_two)
|
366
368
|
when Array
|
367
369
|
filter_values(object)
|
368
370
|
else
|
@@ -397,6 +399,14 @@ module Plurimath
|
|
397
399
|
end
|
398
400
|
Math::Symbol.new(value)
|
399
401
|
end
|
402
|
+
|
403
|
+
def validate_left_right(fields = [])
|
404
|
+
fields.each do |field|
|
405
|
+
if field.is_a?(Math::Formula) && field.value.first.is_a?(Math::Function::Left)
|
406
|
+
field.left_right_wrapper = true
|
407
|
+
end
|
408
|
+
end
|
409
|
+
end
|
400
410
|
end
|
401
411
|
end
|
402
412
|
end
|
data/lib/plurimath/version.rb
CHANGED
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.
|
4
|
+
version: 0.3.1.2
|
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-
|
11
|
+
date: 2023-05-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: parslet
|