plurimath 0.2.9 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c97fcf3901a7da18bbac329563b358490117e3ec3103f6c5c9e4e8ad7b5d3c8
4
- data.tar.gz: abe2849aa2d81cb246b2b703576c6ae0df283b323e27433a9af95d07c1dd9932
3
+ metadata.gz: 80f221bdd7b1327b1773cb34581837b8e1dce2dd869d8908ce6da6c70cd6af33
4
+ data.tar.gz: abf97a179d3b56a42a36cdeedd5323caf9873125c88cbc1109ecbd32c13be70e
5
5
  SHA512:
6
- metadata.gz: 2ac0c52f647ba6ff70bbf2fd0d9af2a33df8176c287699e8e99aa3478eaa44fe9b8fabcb54d115de5116f6823f2cdcda0f53965bbde806f47b948f3ca5d2a81c
7
- data.tar.gz: ba854ed8ecd87cd3fb30e2c80508abfaa0d900a22f3492d531f0b592272d692d9886203db73d81da1dbe0b3402831c2164f8c32472d69926f8976f069eab3718
6
+ metadata.gz: efa17041d13cb3107a5364777b0cf9cff7e20daf37a885ecf43680a9e9a01d47df2610416042316068fc85a92145aeb888635800994d917dbcb67b91333c2b59
7
+ data.tar.gz: 9f2af614455c09f6475692431db914ac750dfe405cdf0af4ed38a9dc8e0d6adc7b5d5cf3f2e3e331818b4532c9b4a200f88cdee0ac03e907b2c6fcb26ca994c9
@@ -197,7 +197,7 @@ module Plurimath
197
197
  neg: :"¬",
198
198
  not: :"¬",
199
199
  "*": :"⋅",
200
- "@": :"@",
200
+ "@": :"∘",
201
201
  "<": :"&#x3c;",
202
202
  ">": :"&#x3e;",
203
203
  "/": :"&#x2f;",
@@ -352,7 +352,7 @@ module Plurimath
352
352
  font_style.to_s,
353
353
  )
354
354
  Math::Function::Base.new(
355
- Utility.unfenced_value(font_object),
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
- Utility.unfenced_value(power_base),
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
- Utility.unfenced_value(power_base),
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
- Utility.unfenced_value(power_base),
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
- Utility.unfenced_value(power_base),
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
- Utility.unfenced_value(power_base),
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
- Utility.unfenced_value(power_base),
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
- Utility.unfenced_value(int_exp),
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,
@@ -6,6 +6,14 @@ module Plurimath
6
6
  module Math
7
7
  module Function
8
8
  class Abs < UnaryFunction
9
+ def to_mathml_without_math_tag
10
+ symbol = Utility.ox_element("mo") << "|"
11
+ first_value = mathml_value&.insert(0, symbol)
12
+ Utility.update_nodes(
13
+ Utility.ox_element("mrow"),
14
+ first_value << symbol,
15
+ )
16
+ end
9
17
  end
10
18
  end
11
19
  end
@@ -9,6 +9,7 @@ module Plurimath
9
9
  def initialize(parameter_one = nil, parameter_two = nil)
10
10
  @parameter_one = parameter_one
11
11
  @parameter_two = parameter_two
12
+ Utility.validate_left_right([parameter_one, parameter_two])
12
13
  end
13
14
 
14
15
  def to_asciimath
@@ -9,6 +9,16 @@ module Plurimath
9
9
  def to_latex
10
10
  "{\\lceil #{latex_value} \\rceil}"
11
11
  end
12
+
13
+ def to_mathml_without_math_tag
14
+ left_value = Utility.ox_element("mo") << "&#x2308;"
15
+ first_value = mathml_value&.insert(0, left_value)
16
+ right_value = Utility.ox_element("mo") << "&#x2309;"
17
+ Utility.update_nodes(
18
+ Utility.ox_element("mrow"),
19
+ first_value << right_value,
20
+ )
21
+ end
12
22
  end
13
23
  end
14
24
  end
@@ -6,6 +6,13 @@ module Plurimath
6
6
  module Math
7
7
  module Function
8
8
  class Ddot < UnaryFunction
9
+ def to_mathml_without_math_tag
10
+ second_value = Utility.ox_element("mo") << ".."
11
+ Utility.update_nodes(
12
+ Utility.ox_element("mover", attributes: { accent: "true" }),
13
+ mathml_value << second_value,
14
+ )
15
+ end
9
16
  end
10
17
  end
11
18
  end
@@ -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&.is_a?(Math::Symbol)
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&.is_a?(Math::Symbol)
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
@@ -12,6 +12,7 @@ module Plurimath
12
12
  @parameter_one = parameter_one
13
13
  @parameter_two = parameter_two
14
14
  @parameter_three = parameter_three
15
+ Utility.validate_left_right([parameter_one, parameter_two, parameter_three])
15
16
  end
16
17
 
17
18
  def to_asciimath
@@ -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)
@@ -104,10 +105,10 @@ module Plurimath
104
105
 
105
106
  def omml_value
106
107
  if parameter_one.is_a?(Array)
107
- return parameter_one.compact.map(&:to_omml_without_math_tag)
108
+ return parameter_one&.compact&.map(&:to_omml_without_math_tag)
108
109
  end
109
110
 
110
- first_value = parameter_one.to_omml_without_math_tag
111
+ first_value = parameter_one&.to_omml_without_math_tag
111
112
  if parameter_one.is_a?(Symbol)
112
113
  first_value = Utility.ox_element("t", namespace: "m") << first_value
113
114
  end
@@ -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
 
@@ -24,7 +24,8 @@ Ox.default_options = { encoding: "UTF-8" }
24
24
 
25
25
  module Plurimath
26
26
  module Math
27
- class Error < StandardError; end
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
- raise_error! unless valid_type?(type)
41
+ type_error! unless valid_type?(type)
41
42
 
42
- klass = VALID_TYPES[type.to_sym]
43
- klass.new(text).to_formula
44
- rescue => ee
45
- message = <<~MESSAGE
46
- An error occurred while processing the input. Please check your input to ensure it is valid or open an issue on Github If you believe the input is correct.
47
- ---- INPUT START ----
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 raise_error!
57
- raise Plurimath::Math::Error, Error.new("Type is not valid, "\
58
- "please enter string or symbol")
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, :raise_error!, :valid_type?
81
+ module_function :parse, :klass_from_type, :parse_error!, :type_error!, :valid_type?
67
82
  end
68
83
  end
@@ -190,7 +190,10 @@ module Plurimath
190
190
  end
191
191
 
192
192
  def update_nodes(element, nodes)
193
- nodes&.each { |node| element << node unless node.nil? }
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
- value = filter_values(object.parameter_two)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Plurimath
4
- VERSION = "0.2.9"
4
+ VERSION = "0.3.1"
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.2.9
4
+ version: 0.3.1
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-04-19 00:00:00.000000000 Z
11
+ date: 2023-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: parslet