plurimath 0.2.0 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -4,51 +4,92 @@ require "parslet"
4
4
  module Plurimath
5
5
  class Latex
6
6
  class Parse < Parslet::Parser
7
- rule(:base) { str("_") }
8
- rule(:power) { str("^") }
9
- rule(:slash) { str("\\") }
10
- rule(:double_slash) { str("\\\\").as("\\\\") }
11
- rule(:array_args) { str("{") >> expression >> str("}") }
12
- rule(:ending) { slash >> str("end") >> intermediate_exp }
13
- rule(:begining) { slash >> str("begin") >> intermediate_exp }
14
- rule(:lparen) { arr_to_expression(Constants::PARENTHESIS.keys, :lparen) }
15
- rule(:rparen) { arr_to_expression(Constants::PARENTHESIS.values, :rparen) }
16
- rule(:subscript) { intermediate_exp >> base >> intermediate_exp.as(:subscript) }
17
- rule(:supscript) { intermediate_exp >> power >> intermediate_exp.as(:supscript) }
18
- rule(:environment) { arr_to_expression(Constants::ENVIRONMENTS.keys, :environment) }
19
- rule(:binary) { slash >> arr_to_expression(Constants::BINARY_CLASSES, :binary) }
20
- rule(:under_over) { slash >> arr_to_expression(Constants::UNDEROVER_CLASSES, :binary) }
21
- rule(:left_right) { str("\\left") >> lparen >> expression >> str("\\right") >> rparen }
22
- rule(:math_operators) { slash >> arr_to_expression(Constants::MATH_OPERATORS, :unary_functions) >> str("\\limits") }
23
- rule(:unary) { slash >> arr_to_expression(Constants::UNARY_CLASSES, :unary) >> intermediate_exp.as(:first_value) }
7
+ rule(:base) { str("_") }
8
+ rule(:power) { str("^") }
9
+ rule(:slash) { str("\\") }
10
+ rule(:under_over) { slash >> underover_classes }
11
+ rule(:array_args) { (str("{") >> expression.as(:args) >> str("}")) }
12
+
13
+ rule(:optional_args) do
14
+ (str("[") >> intermediate_exp.maybe.as(:options) >> str("]")).maybe
15
+ end
16
+
17
+ rule(:begining) do
18
+ (slash >> str("begin") >> (str("{") >> symbol_text_or_integer >> str("*").as(:asterisk).maybe >> str("}")) >> optional_args)
19
+ end
20
+
21
+ rule(:ending) do
22
+ (slash >> str("end") >> (str("{") >> symbol_text_or_integer >> str("*").maybe >> str("}"))).as(:ending)
23
+ end
24
+
25
+ rule(:numeric_values) do
26
+ arr_to_expression(Constants::NUMERIC_VALUES, :numeric_values)
27
+ end
28
+
29
+ rule(:underover_classes) do
30
+ arr_to_expression(Constants::UNDEROVER_CLASSES, :binary)
31
+ end
32
+
33
+ rule(:math_operators_classes) do
34
+ arr_to_expression(Constants::MATH_OPERATORS, :unary_functions)
35
+ end
36
+
37
+ rule(:lparen) do
38
+ arr_to_expression(Constants::PARENTHESIS.keys, :lparen)
39
+ end
40
+
41
+ rule(:rparen) do
42
+ arr_to_expression(Constants::PARENTHESIS.values, :rparen)
43
+ end
44
+
45
+ rule(:environment) do
46
+ arr_to_expression(Constants::MATRICES.keys, :environment)
47
+ end
48
+
49
+ rule(:subscript) do
50
+ intermediate_exp >> base >> intermediate_exp.as(:subscript)
51
+ end
52
+
53
+ rule(:supscript) do
54
+ intermediate_exp >> power >> intermediate_exp.as(:supscript)
55
+ end
56
+
57
+ rule(:math_operators) do
58
+ symbol_text_or_integer.as(:first_value) >> str("\\limits")
59
+ end
60
+
61
+ rule(:sqrt_arg) do
62
+ (str("[").as(:lparen) >> intermediate_exp.repeat(1).as(:expression) >> str("]").as(:rparen)) |
63
+ (str("[").as(:lparen) >> str("]").as(:rparen))
64
+ end
24
65
 
25
66
  rule(:limits) do
26
- math_operators >> base >> intermediate_exp.as(:base) >> power >> intermediate_exp.as(:power) |
27
- math_operators >> power >> intermediate_exp.as(:power) >> base >> intermediate_exp.as(:base)
67
+ (math_operators >> base >> intermediate_exp.as(:base) >> power >> intermediate_exp.as(:power)) |
68
+ (math_operators >> power >> intermediate_exp.as(:power) >> base >> intermediate_exp.as(:base))
28
69
  end
29
70
 
30
71
  rule(:symbol_class_commands) do
31
- (slash >> arr_to_expression(Constants::SYMBOLS, :symbols)) |
32
- unary.as(:unary_functions) |
33
- binary |
72
+ (str("&#x") >> match["0-9a-fA-F"].repeat >> str(";")).as(:unicode_symbols) |
73
+ hash_to_expression(Constants::SYMBOLS) |
34
74
  under_over |
35
75
  environment |
36
- (slash >> arr_to_expression(Constants::FONT_STYLES, :fonts) >> expression.as(:intermediate_exp)) |
37
- arr_to_expression(Constants::OPERATORS, :operant) |
38
- (slash >> arr_to_expression(Constants::POWER_BASE_CLASSES, :binary)) |
39
- arr_to_expression(Constants::NUMERIC_VALUES, :numeric_values)
76
+ numeric_values
40
77
  end
41
78
 
42
79
  rule(:symbol_text_or_integer) do
43
80
  symbol_class_commands |
44
- match["a-zA-Z"].repeat(1).as(:text) |
81
+ (slash >> math_operators_classes) |
82
+ match["a-zA-Z"].as(:symbols) |
83
+ (str('"') >> match("[^\"]").repeat >> str('"')).as(:text) |
45
84
  match(/\d+(\.[0-9]+)|\d/).repeat(1).as(:number) |
46
- double_slash
85
+ str("\\\\").as("\\\\") |
86
+ (slash >> (lparen | rparen).as(:symbols)) |
87
+ lparen |
88
+ str("\\ ").as(:space)
47
89
  end
48
90
 
49
91
  rule(:intermediate_exp) do
50
- (lparen >> expression.as(:expression) >> rparen) |
51
- (lparen >> str("") >> rparen) |
92
+ (str("{") >> expression.maybe.as(:expression) >> str("}")) |
52
93
  symbol_text_or_integer
53
94
  end
54
95
 
@@ -59,47 +100,122 @@ module Plurimath
59
100
  subscript.as(:base)
60
101
  end
61
102
 
62
- rule(:sqrt_arg) { str("[").as(:lparen) >> (expression | str("")) >> str("]").as(:rparen) }
63
-
64
103
  rule(:binary_functions) do
65
- (slash >> str("sqrt").as(:root) >> sqrt_arg.as(:first_value) >> intermediate_exp.as(:second_value)).as(:binary) |
66
- (slash >> str("sqrt").as(:sqrt) >> intermediate_exp.as(:intermediate_exp)).as(:binary) |
67
- (intermediate_exp.as(:first_value) >> under_over >> intermediate_exp.as(:second_value)).as(:under_over) |
68
- (binary >> intermediate_exp.as(:first_value) >> intermediate_exp.as(:second_value)).as(:binary)
104
+ (intermediate_exp.as(:first_value) >> under_over >> intermediate_exp.as(:second_value)).as(:under_over) |
105
+ (slash >> str("sqrt").as(:root) >> sqrt_arg.as(:first_value) >> intermediate_exp.as(:second_value)).as(:binary) |
106
+ (slash >> str("sqrt").as(:sqrt) >> intermediate_exp.as(:intermediate_exp)).as(:binary)
69
107
  end
70
108
 
71
109
  rule(:sequence) do
72
110
  limits.as(:limits) |
111
+ (binary_functions.as(:binary_functions) >> power >> sequence.as(:supscript)).as(:power) |
112
+ (binary_functions.as(:binary_functions) >> base >> sequence.as(:subscript)).as(:base) |
113
+ binary_functions |
114
+ (slash >> str("rule").as(:rule) >> sqrt_arg.maybe.as(:first_value) >> intermediate_exp.maybe.as(:second_value) >> intermediate_exp.maybe.as(:third_value)).as(:binary) |
115
+ (over_class >> power >> intermediate_exp.as(:supscript)) |
116
+ (over_class >> base >> intermediate_exp.as(:subscript)) |
117
+ over_class |
118
+ (left_right.as(:left_right) >> power >> intermediate_exp.as(:supscript)) |
119
+ (left_right.as(:left_right) >> base >> intermediate_exp.as(:subscript)) |
73
120
  left_right.as(:left_right) |
74
- (slash >> str("mbox") >> lparen.capture(:paren) >> read_text >> rparen).as(:unary_functions) |
75
- (slash >> str("substack").as(:substack) >> lparen >> expression.as(:substack_value) >> rparen) |
76
- (begining.as(:begining) >> array_args.as(:args) >> expression.as(:table_data) >> ending.as(:ending)).as(:environment) |
77
- (begining.as(:begining) >> expression.as(:table_data) >> ending.as(:ending)).as(:environment) |
121
+ (slash >> str("substack").as(:substack) >> intermediate_exp) |
122
+ (begining >> array_args >> expression.as(:table_data) >> ending).as(:environment) |
123
+ (begining >> expression.as(:table_data) >> ending).as(:environment) |
78
124
  (slash >> environment >> intermediate_exp).as(:table_data) |
79
125
  power_base |
80
- binary_functions |
126
+ (rparen >> (base >> sequence.as(:subscript)).maybe >> power >> sequence.as(:supscript)).as(:power_base) |
127
+ (rparen >> (power >> sequence.as(:supscript)) >> base >> sequence.as(:subscript)).as(:power_base) |
128
+ rparen |
81
129
  intermediate_exp
82
130
  end
83
131
 
84
- rule(:iteration) { (sequence.as(:sequence) >> expression.as(:expression)) | sequence }
132
+ rule(:left_right) do
133
+ (
134
+ str("\\left").as(:left) >> lparen.maybe >>
135
+ (
136
+ (expression.repeat.as(:dividend) >> str("\\over") >> expression.repeat.as(:divisor)) |
137
+ expression.as(:expression).maybe
138
+ ) >>
139
+ str("\\right").as(:right).maybe >> (rparen | str(".").maybe)
140
+ )
141
+ end
142
+
143
+ rule(:over_class) do
144
+ (
145
+ (str("{") >> expression.repeat.as(:dividend) >> str("\\over") >> expression.repeat.as(:divisor) >> str("}")) |
146
+ (left_right.as(:left_right).as(:power) >> power >> intermediate_exp) |
147
+ (left_right.as(:left_right).as(:base) >> base >> intermediate_exp)
148
+ ).as(:over)
149
+ end
150
+
151
+ rule(:iteration) do
152
+ (sequence.as(:sequence) >> expression.as(:expression)) |
153
+ sequence
154
+ end
85
155
 
86
- rule(:expression) { (iteration >> expression) | iteration }
156
+ rule(:expression) do
157
+ (iteration >> expression) |
158
+ iteration |
159
+ ((iteration.as(:dividend) >> str("\\over") >> iteration.as(:divisor)) >> expression.repeat)
160
+ end
87
161
 
88
162
  root :expression
89
163
 
90
- def arr_to_expression(array, name = nil)
91
- array.reduce do |expression, expr_string|
92
- expression = str(expression).as(name || expression.to_sym) if expression.is_a?(String)
93
- expression | str(expr_string).as(name || expr_string.to_sym)
164
+ def arr_to_expression(array, name)
165
+ @@new_hash ||= {}
166
+ type = array.first.class
167
+ @@new_hash[name] ||= array.reduce do |expression, expr_string|
168
+ expression = str(expression).as(name) if expression.is_a?(type)
169
+ expression | str(expr_string).as(name)
170
+ end
171
+ end
172
+
173
+ def hash_to_expression(hash)
174
+ @@expression ||= hash.reduce do |expression, (key, value)|
175
+ expression = dynamic_rules(expression.first, expression.last) if expression.is_a?(Array)
176
+ expression | dynamic_rules(key, value)
94
177
  end
95
178
  end
96
179
 
97
- def read_text
98
- dynamic do |_sour, context|
99
- rparen = Constants::PARENTHESIS[context.captures[:paren][:lparen].to_s]
100
- match("[^#{rparen}]").repeat.as(:text)
180
+ def dynamic_rules(expr, name)
181
+ first_value = str(expr.to_s)
182
+ case name
183
+ when :operant
184
+ (first_value.as(:operant) | (slashed_value(first_value, :symbols)))
185
+ when :symbols
186
+ slashed_value(first_value, :symbols)
187
+ when :unary
188
+ unary_rules(first_value)
189
+ when :fonts
190
+ (slashed_value(first_value, :fonts) >> (binary_functions | intermediate_exp).as(:intermediate_exp))
191
+ when :power_base
192
+ (slashed_value(first_value, :binary) >> dynamic_power_base).as(:power_base) |
193
+ (slashed_value(first_value, :binary))
194
+ when :underover
195
+ (slashed_value(first_value, :underover) >> dynamic_power_base) |
196
+ (slashed_value(first_value, :underover) >> intermediate_exp.maybe.as(:first_value) >> dynamic_power_base) |
197
+ (slashed_value(first_value, :underover))
198
+ when :binary
199
+ (slashed_value(first_value, :binary) >> intermediate_exp.as(:first_value) >> intermediate_exp.as(:second_value)).as(:binary)
101
200
  end
102
201
  end
202
+
203
+ def slashed_value(first_value, name = nil)
204
+ (slash >> first_value.as(name))
205
+ end
206
+
207
+ def unary_rules(first_value)
208
+ (slashed_value(first_value, :unary_functions) >> dynamic_power_base) |
209
+ (slashed_value(first_value, :unary) >> intermediate_exp.as(:first_value)).as(:unary_functions) |
210
+ (slashed_value(first_value, :unary))
211
+ end
212
+
213
+ def dynamic_power_base
214
+ (base >> intermediate_exp.as(:subscript) >> power >> intermediate_exp.as(:supscript)) |
215
+ (power >> intermediate_exp.as(:supscript) >> base >> intermediate_exp.as(:subscript)) |
216
+ (power >> intermediate_exp.as(:supscript)) |
217
+ (base >> intermediate_exp.as(:subscript))
218
+ end
103
219
  end
104
220
  end
105
221
  end
@@ -9,15 +9,23 @@ module Plurimath
9
9
  attr_accessor :text
10
10
 
11
11
  def initialize(text)
12
- @text = text.gsub(" ", "").gsub("\n", "")
12
+ enti = HTMLEntities.new
13
+ text = enti.encode(enti.decode(text), :hexadecimal)
14
+ text = text
15
+ .gsub(/((?<!\\) )|\n+/, "")
16
+ .gsub(/\\\\ /, "\\\\\\\\")
17
+ .gsub(/&#x26;/, "&")
18
+ .gsub(/&#x22;/, "\"")
19
+ .gsub(/&#xa;/, "")
20
+ @text = text
13
21
  end
14
22
 
15
23
  def parse
16
- tree_t = Plurimath::Latex::Parse.new.parse(text)
17
- formula = Plurimath::Latex::Transform.new.apply(tree_t)
24
+ tree_t = Parse.new.parse(text)
25
+ formula = Transform.new.apply(tree_t)
18
26
  formula = [formula] unless formula.is_a?(Array)
19
27
 
20
- Plurimath::Math::Formula.new(formula)
28
+ Math::Formula.new(formula)
21
29
  end
22
30
  end
23
31
  end