plurimath 0.8.17 → 0.8.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +4 -4
  2. data/lib/plurimath/asciimath/parse.rb +1 -0
  3. data/lib/plurimath/asciimath/transform.rb +12 -0
  4. data/lib/plurimath/math/core.rb +63 -4
  5. data/lib/plurimath/math/formula/mrow.rb +193 -0
  6. data/lib/plurimath/math/formula/mstyle.rb +17 -0
  7. data/lib/plurimath/math/formula.rb +311 -32
  8. data/lib/plurimath/math/function/base.rb +4 -0
  9. data/lib/plurimath/math/function/color.rb +17 -4
  10. data/lib/plurimath/math/function/fenced.rb +219 -0
  11. data/lib/plurimath/math/function/frac.rb +4 -0
  12. data/lib/plurimath/math/function/linebreak.rb +2 -2
  13. data/lib/plurimath/math/function/longdiv.rb +3 -0
  14. data/lib/plurimath/math/function/menclose.rb +3 -0
  15. data/lib/plurimath/math/function/merror.rb +5 -2
  16. data/lib/plurimath/math/function/mglyph.rb +27 -0
  17. data/lib/plurimath/math/function/mlabeledtr.rb +19 -0
  18. data/lib/plurimath/math/function/mpadded.rb +28 -1
  19. data/lib/plurimath/math/function/ms.rb +80 -0
  20. data/lib/plurimath/math/function/msgroup.rb +15 -0
  21. data/lib/plurimath/math/function/msline.rb +5 -2
  22. data/lib/plurimath/math/function/multiscript.rb +14 -0
  23. data/lib/plurimath/math/function/over.rb +3 -0
  24. data/lib/plurimath/math/function/overset.rb +11 -0
  25. data/lib/plurimath/math/function/phantom.rb +3 -0
  26. data/lib/plurimath/math/function/power.rb +3 -0
  27. data/lib/plurimath/math/function/power_base.rb +3 -0
  28. data/lib/plurimath/math/function/root.rb +3 -0
  29. data/lib/plurimath/math/function/scarries.rb +3 -0
  30. data/lib/plurimath/math/function/semantics.rb +14 -0
  31. data/lib/plurimath/math/function/sqrt.rb +3 -0
  32. data/lib/plurimath/math/function/stackrel.rb +3 -0
  33. data/lib/plurimath/math/function/table.rb +53 -0
  34. data/lib/plurimath/math/function/td.rb +4 -1
  35. data/lib/plurimath/math/function/text.rb +22 -2
  36. data/lib/plurimath/math/function/tr.rb +13 -0
  37. data/lib/plurimath/math/function/underover.rb +3 -0
  38. data/lib/plurimath/math/function/underset.rb +44 -0
  39. data/lib/plurimath/math/number.rb +10 -1
  40. data/lib/plurimath/math/symbols/gg.rb +4 -4
  41. data/lib/plurimath/math/symbols/ll.rb +4 -4
  42. data/lib/plurimath/math/symbols/minus.rb +1 -1
  43. data/lib/plurimath/math/symbols/symbol.rb +12 -4
  44. data/lib/plurimath/math.rb +2 -0
  45. data/lib/plurimath/mathml/parser.rb +45 -86
  46. data/lib/plurimath/mathml/utility/empty_defined_methods.rb +477 -0
  47. data/lib/plurimath/mathml/utility/formula_transformation.rb +472 -0
  48. data/lib/plurimath/mathml/utility.rb +363 -0
  49. data/lib/plurimath/mathml.rb +1 -0
  50. data/lib/plurimath/unicode_math/transform.rb +2 -2
  51. data/lib/plurimath/utility.rb +5 -23
  52. data/lib/plurimath/version.rb +1 -1
  53. data/lib/plurimath.rb +9 -0
  54. data/plurimath.gemspec +4 -2
  55. metadata +37 -5
  56. data/lib/plurimath/mathml/transform.rb +0 -413
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "unary_function"
4
+ require_relative "../../mathml/utility"
4
5
 
5
6
  module Plurimath
6
7
  module Math
7
8
  module Function
8
9
  class Phantom < UnaryFunction
10
+ include Mathml::Utility
11
+
9
12
  def to_asciimath(options:)
10
13
  "#{Array.new(asciimath_value(options: options)&.length, '\ ').join}"
11
14
  end
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "binary_function"
4
+ require_relative "../../mathml/utility"
4
5
 
5
6
  module Plurimath
6
7
  module Math
7
8
  module Function
8
9
  class Power < BinaryFunction
10
+ include Mathml::Utility
11
+
9
12
  FUNCTION = {
10
13
  name: "superscript",
11
14
  first_value: "base",
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "ternary_function"
4
+ require_relative "../../mathml/utility"
4
5
 
5
6
  module Plurimath
6
7
  module Math
7
8
  module Function
8
9
  class PowerBase < TernaryFunction
10
+ include Mathml::Utility
11
+
9
12
  FUNCTION = {
10
13
  name: "subsup",
11
14
  first_value: "base",
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "binary_function"
4
+ require_relative "../../mathml/utility"
4
5
 
5
6
  module Plurimath
6
7
  module Math
7
8
  module Function
8
9
  class Root < BinaryFunction
10
+ include Mathml::Utility
11
+
9
12
  FUNCTION = {
10
13
  name: "root",
11
14
  first_value: "radicand",
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "unary_function"
4
+ require_relative "../../mathml/utility"
4
5
 
5
6
  module Plurimath
6
7
  module Math
7
8
  module Function
8
9
  class Scarries < UnaryFunction
10
+ include Mathml::Utility
11
+
9
12
  def to_asciimath(options:)
10
13
  asciimath_value(options: options)
11
14
  end
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "binary_function"
4
+ require_relative "../../mathml/utility"
4
5
 
5
6
  module Plurimath
6
7
  module Math
7
8
  module Function
8
9
  class Semantics < BinaryFunction
10
+ include Mathml::Utility
11
+
9
12
  FUNCTION = {
10
13
  name: "semantics",
11
14
  first_value: "first argument",
@@ -41,6 +44,17 @@ module Plurimath
41
44
  end
42
45
  end
43
46
 
47
+ def annotation=(value)
48
+ return unless value
49
+
50
+ @parameter_two = [
51
+ {
52
+ annotation: value,
53
+ }
54
+ ]
55
+ @temp_mathml_order.delete("annotation")
56
+ end
57
+
44
58
  protected
45
59
 
46
60
  def other_tags(array, intent, options:)
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "unary_function"
4
+ require_relative "../../mathml/utility"
4
5
 
5
6
  module Plurimath
6
7
  module Math
7
8
  module Function
8
9
  class Sqrt < UnaryFunction
10
+ include Mathml::Utility
11
+
9
12
  attr_accessor :options
10
13
 
11
14
  def to_mathml_without_math_tag(intent, options:)
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "binary_function"
4
+ require_relative "../../mathml/utility"
4
5
 
5
6
  module Plurimath
6
7
  module Math
7
8
  module Function
8
9
  class Stackrel < BinaryFunction
10
+ include Mathml::Utility
11
+
9
12
  FUNCTION = {
10
13
  name: "stackrel",
11
14
  first_value: "above",
@@ -1,9 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../../mathml/utility"
4
+
3
5
  module Plurimath
4
6
  module Math
5
7
  module Function
6
8
  class Table < Core
9
+ include Mathml::Utility
10
+
7
11
  attr_accessor :value, :open_paren, :close_paren, :options
8
12
 
9
13
  SIMPLE_TABLES = %w[array align split].freeze
@@ -137,10 +141,59 @@ module Plurimath
137
141
  }
138
142
  end
139
143
 
144
+ def mtr_value=(value)
145
+ return if value.nil? || value.empty?
146
+
147
+ self.value = replace_order_with_value(
148
+ clear_temp_order,
149
+ update_temp_mathml_values(value),
150
+ "mtr"
151
+ )
152
+ end
153
+
154
+ def mlabeledtr_value=(value)
155
+ return if value.nil? || value.empty?
156
+
157
+ self.value = replace_order_with_value(
158
+ clear_temp_order,
159
+ update_temp_mathml_values(value),
160
+ "mlabeledtr"
161
+ )
162
+ end
163
+
164
+ def frame=(value)
165
+ return if value.nil? || value.empty?
166
+
167
+ set_option(:frame, value)
168
+ end
169
+
170
+ def rowlines=(value)
171
+ return if value.nil? || value.empty?
172
+
173
+ set_option(:rowlines, value)
174
+ end
175
+
176
+ def columnlines=(value)
177
+ return if value.nil? || value.empty?
178
+ return if value.split.all? { |val| val.include?("none") }
179
+
180
+ Plurimath::Utility.table_separator(
181
+ value.split,
182
+ @value,
183
+ )
184
+ set_option(:columnlines, value)
185
+ end
186
+
140
187
  protected
141
188
 
189
+ def set_option(option, value)
190
+ @options ||= {}
191
+ @options[option] = value
192
+ end
193
+
142
194
  def mathml_parenthesis(field, intent, options:)
143
195
  return "" unless field
196
+
144
197
  if field&.class_name == "symbol"
145
198
  paren = field&.to_mathml_without_math_tag(intent, options: options)&.nodes&.first
146
199
  return invisible_paren?(paren) ? "" : paren.to_s
@@ -1,14 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "binary_function"
4
+ require_relative "../../mathml/utility"
4
5
 
5
6
  module Plurimath
6
7
  module Math
7
8
  module Function
8
9
  class Td < BinaryFunction
10
+ include Mathml::Utility
11
+
9
12
  def initialize(parameter_one = nil, parameter_two = nil)
10
13
  parameter_one&.delete_if { |td| td == "&" }
11
- super(parameter_one, parameter_two)
14
+ super(Array(parameter_one), parameter_two)
12
15
  end
13
16
 
14
17
  def to_asciimath(options:)
@@ -2,13 +2,20 @@
2
2
 
3
3
  require "htmlentities"
4
4
  require_relative "unary_function"
5
+ require_relative "../../mathml/utility"
5
6
 
6
7
  module Plurimath
7
8
  module Math
8
9
  module Function
9
10
  class Text < UnaryFunction
11
+ include Mathml::Utility
12
+
10
13
  PARSER_REGEX = %r{unicode\[:(?<unicode>\w{1,})\]}.freeze
11
14
 
15
+ def initialize(parameter_one = "")
16
+ super(parameter_one)
17
+ end
18
+
12
19
  def to_asciimath(**)
13
20
  "\"#{parse_text('asciimath') || parameter_one}\""
14
21
  end
@@ -67,12 +74,25 @@ module Plurimath
67
74
  "#{spacing}\"#{dump_omml(self, display_style, options: options)}\" text\n"
68
75
  end
69
76
 
77
+ def to_unicodemath_math_zone(spacing, _, _, options:)
78
+ "#{spacing}#{to_unicodemath(options: options)} text\n"
79
+ end
80
+
70
81
  def value
71
82
  parameter_one
72
83
  end
73
84
 
74
- def to_unicodemath_math_zone(spacing, _, _, options:)
75
- "#{spacing}#{to_unicodemath(options: options)} text\n"
85
+ def element_order=(*); end
86
+
87
+ def value=(text)
88
+ text = text.join if text.is_a?(Array)
89
+ entities = HTMLEntities.new
90
+ symbols = Mathml::Constants::UNICODE_SYMBOLS.transform_keys(&:to_s)
91
+ text = entities.encode(text, :hexadecimal)
92
+ symbols.each do |code, string|
93
+ text = text.gsub(code.downcase, "unicode[:#{string}]")
94
+ end
95
+ self.parameter_one = text
76
96
  end
77
97
 
78
98
  protected
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "unary_function"
4
+ require_relative "../../mathml/utility"
4
5
 
5
6
  module Plurimath
6
7
  module Math
7
8
  module Function
8
9
  class Tr < UnaryFunction
10
+ include Mathml::Utility
11
+
9
12
  def initialize(parameter_one = [])
10
13
  parameter_one.map!.with_index { |_, index| Td.new([]) } if parameter_one&.all?("@")
11
14
  super(parameter_one)
@@ -93,6 +96,16 @@ module Plurimath
93
96
  row_lines.shift if row_lines.first.is_a?(Math::Symbols::Hline)
94
97
  first_value
95
98
  end
99
+
100
+ def mtd_value=(value)
101
+ return if value.nil? || value.empty?
102
+
103
+ self.parameter_one = replace_order_with_value(
104
+ clear_temp_order,
105
+ update_temp_mathml_values(value),
106
+ "mtd"
107
+ )
108
+ end
96
109
  end
97
110
  end
98
111
  end
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../../mathml/utility"
3
4
  require_relative "ternary_function"
4
5
 
5
6
  module Plurimath
6
7
  module Math
7
8
  module Function
8
9
  class Underover < TernaryFunction
10
+ include Mathml::Utility
11
+
9
12
  FUNCTION = {
10
13
  name: "UnderOver",
11
14
  first_value: "base",
@@ -1,12 +1,16 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "binary_function"
4
+ require_relative "../../mathml/utility"
4
5
 
5
6
  module Plurimath
6
7
  module Math
7
8
  module Function
8
9
  class Underset < BinaryFunction
10
+ include Mathml::Utility
11
+
9
12
  attr_accessor :options
13
+
10
14
  FUNCTION = {
11
15
  name: "underscript",
12
16
  first_value: "underscript value",
@@ -21,6 +25,13 @@ module Plurimath
21
25
  @options = options unless options.empty?
22
26
  end
23
27
 
28
+ def element_order=(value)
29
+ @temp_mathml_order = validated_order(
30
+ value,
31
+ rejectable_array: ["comment"]
32
+ )
33
+ end
34
+
24
35
  def to_mathml_without_math_tag(intent, options:)
25
36
  value_array = [
26
37
  validate_mathml_fields(parameter_two, intent, options: options),
@@ -79,8 +90,41 @@ module Plurimath
79
90
  parameter_two.is_nary_function? || parameter_two.is_nary_symbol?
80
91
  end
81
92
 
93
+ def content=(value)
94
+ if no_content_in?(Array(value))
95
+ delete_all_text
96
+ else
97
+ new_val = Array(value).map do |val|
98
+ validate_symbols(val) unless val.strip.empty?
99
+ end
100
+ validate_text_order(new_val)
101
+ end
102
+ update_temp_mathml_values(@temp_mathml_order)
103
+ end
104
+
82
105
  protected
83
106
 
107
+ def validate_text_order(value)
108
+ @temp_mathml_order.each_with_index do |item, index|
109
+ next unless item == "text"
110
+
111
+ shifted_value = value.shift
112
+ next @temp_mathml_order[index] = shifted_value if shifted_value
113
+
114
+ @temp_mathml_order.delete_at(index)
115
+ end
116
+ end
117
+
118
+ def delete_all_text
119
+ @temp_mathml_order.delete("text")
120
+ end
121
+
122
+ def no_content_in?(value)
123
+ value.nil? ||
124
+ value.empty? ||
125
+ value&.all? { |val| val.strip.empty? }
126
+ end
127
+
84
128
  def unicode_accent?(field)
85
129
  return unless field.is_a?(Math::Symbols::Symbol)
86
130
 
@@ -1,11 +1,14 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../mathml/utility"
4
+
3
5
  module Plurimath
4
6
  module Math
5
7
  class Number < Core
6
8
  attr_accessor :value, :mini_sub_sized, :mini_sup_sized
9
+ include Mathml::Utility
7
10
 
8
- def initialize(value, mini_sub_sized: false, mini_sup_sized: false)
11
+ def initialize(value = nil, mini_sub_sized: false, mini_sup_sized: false)
9
12
  @value = value.is_a?(Parslet::Slice) ? value.to_s : value
10
13
  @mini_sub_sized = mini_sub_sized if mini_sub_sized
11
14
  @mini_sup_sized = mini_sup_sized if mini_sup_sized
@@ -18,6 +21,8 @@ module Plurimath
18
21
  object.mini_sup_sized == mini_sup_sized
19
22
  end
20
23
 
24
+ def element_order=(*); end
25
+
21
26
  def to_asciimath(options:)
22
27
  format_value_with_options(options)
23
28
  end
@@ -71,6 +76,10 @@ module Plurimath
71
76
  mini_sub_sized || mini_sup_sized
72
77
  end
73
78
 
79
+ def value=(value)
80
+ @value = value.is_a?(Array) ? value.join : value
81
+ end
82
+
74
83
  protected
75
84
 
76
85
  def mini_sub
@@ -3,10 +3,10 @@ module Plurimath
3
3
  module Symbols
4
4
  class Gg < Symbol
5
5
  INPUT = {
6
- unicodemath: [["gg", "&#x226b;"]],
7
- asciimath: [["&#x226b;"], parsing_wrapper(["gg"], lang: :asciimath)],
6
+ unicodemath: ["gg", "&#x226b;", parsing_wrapper(["mgt"], lang: :unicode)],
7
+ asciimath: ["&#x226b;", "gg", "mgt"],
8
8
  mathml: ["&#x226b;"],
9
- latex: [["gg", "&#x226b;"]],
9
+ latex: ["gg", "&#x226b;", parsing_wrapper(["mgt"], lang: :latex)],
10
10
  omml: ["&#x226b;"],
11
11
  html: ["&#x226b;"],
12
12
  }.freeze
@@ -17,7 +17,7 @@ module Plurimath
17
17
  end
18
18
 
19
19
  def to_asciimath(**)
20
- parsing_wrapper("gg", lang: :asciimath)
20
+ "gg"
21
21
  end
22
22
 
23
23
  def to_unicodemath(**)
@@ -3,10 +3,10 @@ module Plurimath
3
3
  module Symbols
4
4
  class Ll < Symbol
5
5
  INPUT = {
6
- unicodemath: [["ll", "&#x226a;"]],
7
- asciimath: [["&#x226a;"], parsing_wrapper(["ll"], lang: :asciimath)],
6
+ unicodemath: ["ll", "&#x226a;", parsing_wrapper(["mlt"], lang: :unicode)],
7
+ asciimath: ["&#x226a;", "ll", "mlt"],
8
8
  mathml: ["&#x226a;"],
9
- latex: [["ll", "&#x226a;"]],
9
+ latex: ["ll", "&#x226a;", parsing_wrapper(["mlt"], lang: :latex)],
10
10
  omml: ["&#x226a;"],
11
11
  html: ["&#x226a;"],
12
12
  }.freeze
@@ -17,7 +17,7 @@ module Plurimath
17
17
  end
18
18
 
19
19
  def to_asciimath(**)
20
- parsing_wrapper("ll", lang: :asciimath)
20
+ "ll"
21
21
  end
22
22
 
23
23
  def to_unicodemath(**)
@@ -5,7 +5,7 @@ module Plurimath
5
5
  INPUT = {
6
6
  unicodemath: [["&#x2212;", "-"], parsing_wrapper(["minus"], lang: :unicode)],
7
7
  asciimath: [["-", "&#x2212;"], parsing_wrapper(["minus"], lang: :asciimath)],
8
- mathml: ["&#x2212;"],
8
+ mathml: ["&#x2212;", "-"],
9
9
  latex: [["minus", "-", "&#x2212;"]],
10
10
  omml: ["&#x2212;"],
11
11
  html: ["&#x2212;"],
@@ -1,9 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "../../mathml/utility"
4
+
3
5
  module Plurimath
4
6
  module Math
5
7
  module Symbols
6
8
  class Symbol < Core
9
+ include Mathml::Utility
10
+
7
11
  attr_accessor :value, :slashed, :mini_sub_sized, :mini_sup_sized, :options
8
12
 
9
13
  INPUT = {}.freeze
@@ -13,7 +17,7 @@ module Plurimath
13
17
  mini_sub_sized: false,
14
18
  mini_sup_sized: false,
15
19
  options: {})
16
- @value = sym.is_a?(Parslet::Slice) ? sym.to_s : sym
20
+ @value = sym.is_a?(Array) ? sym.join : sym&.to_s
17
21
  @slashed = slashed if slashed
18
22
  @mini_sub_sized = mini_sub_sized if mini_sub_sized
19
23
  @mini_sup_sized = mini_sup_sized if mini_sup_sized
@@ -22,7 +26,7 @@ module Plurimath
22
26
 
23
27
  def ==(object)
24
28
  object.respond_to?(:value) &&
25
- object.class == object.class &&
29
+ object.class == self.class &&
26
30
  object.value == value &&
27
31
  object.slashed == slashed &&
28
32
  object.mini_sub_sized == mini_sub_sized &&
@@ -36,6 +40,10 @@ module Plurimath
36
40
  value
37
41
  end
38
42
 
43
+ def value=(value)
44
+ @value = value.is_a?(Array) ? value.join : value.to_s
45
+ end
46
+
39
47
  def to_mathml_without_math_tag(intent, **)
40
48
  if value&.include?("&#x2147;")
41
49
  attributes = {
@@ -45,7 +53,7 @@ module Plurimath
45
53
  mi_tag = ox_element("mi", attributes: attributes)
46
54
  return mi_tag if ["{:", ":}"].include?(value)
47
55
 
48
- mi_tag << value
56
+ value ? mi_tag << value : mi_tag
49
57
  end
50
58
 
51
59
  def to_latex(**)
@@ -108,7 +116,7 @@ module Plurimath
108
116
  self.is_a?(Math::Symbols::Ampersand)
109
117
  end
110
118
 
111
- def linebreak
119
+ def linebreak?
112
120
  value == "\\\\"
113
121
  end
114
122
 
@@ -13,6 +13,8 @@ require_relative "math/core"
13
13
  require_relative "math/number"
14
14
  require_relative "math/symbols"
15
15
  require_relative "math/formula"
16
+ require_relative "math/formula/mrow"
17
+ require_relative "math/formula/mstyle"
16
18
  require_relative "math/function"
17
19
  require_relative "asciimath/parser"
18
20
  require_relative "unicode_math/parser"