plurimath 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +36 -0
- data/.rubocop.yml +2 -0
- data/lib/plurimath/asciimath/constants.rb +308 -0
- data/lib/plurimath/asciimath/parse.rb +87 -0
- data/lib/plurimath/asciimath/parser.rb +24 -0
- data/lib/plurimath/asciimath/transform.rb +304 -0
- data/lib/plurimath/asciimath.rb +16 -0
- data/lib/plurimath/html.rb +15 -0
- data/lib/plurimath/latex/constants.rb +1963 -0
- data/lib/plurimath/latex/parse.rb +105 -0
- data/lib/plurimath/latex/parser.rb +24 -0
- data/lib/plurimath/latex/transform.rb +298 -0
- data/lib/plurimath/latex.rb +15 -0
- data/lib/plurimath/math/formula.rb +43 -0
- data/lib/plurimath/math/function/abs.rb +12 -0
- data/lib/plurimath/math/function/arccos.rb +12 -0
- data/lib/plurimath/math/function/arcsin.rb +12 -0
- data/lib/plurimath/math/function/arctan.rb +12 -0
- data/lib/plurimath/math/function/bar.rb +16 -0
- data/lib/plurimath/math/function/base.rb +30 -0
- data/lib/plurimath/math/function/binary_function.rb +53 -0
- data/lib/plurimath/math/function/cancel.rb +12 -0
- data/lib/plurimath/math/function/ceil.rb +12 -0
- data/lib/plurimath/math/function/color.rb +23 -0
- data/lib/plurimath/math/function/cos.rb +12 -0
- data/lib/plurimath/math/function/cosh.rb +12 -0
- data/lib/plurimath/math/function/cot.rb +12 -0
- data/lib/plurimath/math/function/coth.rb +12 -0
- data/lib/plurimath/math/function/csc.rb +12 -0
- data/lib/plurimath/math/function/csch.rb +12 -0
- data/lib/plurimath/math/function/ddot.rb +12 -0
- data/lib/plurimath/math/function/deg.rb +12 -0
- data/lib/plurimath/math/function/det.rb +12 -0
- data/lib/plurimath/math/function/dim.rb +12 -0
- data/lib/plurimath/math/function/dot.rb +12 -0
- data/lib/plurimath/math/function/exp.rb +12 -0
- data/lib/plurimath/math/function/f.rb +12 -0
- data/lib/plurimath/math/function/fenced.rb +22 -0
- data/lib/plurimath/math/function/floor.rb +12 -0
- data/lib/plurimath/math/function/font_style.rb +45 -0
- data/lib/plurimath/math/function/frac.rb +23 -0
- data/lib/plurimath/math/function/g.rb +12 -0
- data/lib/plurimath/math/function/gcd.rb +12 -0
- data/lib/plurimath/math/function/glb.rb +12 -0
- data/lib/plurimath/math/function/hat.rb +12 -0
- data/lib/plurimath/math/function/hom.rb +12 -0
- data/lib/plurimath/math/function/inf.rb +18 -0
- data/lib/plurimath/math/function/int.rb +17 -0
- data/lib/plurimath/math/function/ker.rb +12 -0
- data/lib/plurimath/math/function/lcm.rb +12 -0
- data/lib/plurimath/math/function/left.rb +23 -0
- data/lib/plurimath/math/function/lg.rb +12 -0
- data/lib/plurimath/math/function/lim.rb +23 -0
- data/lib/plurimath/math/function/liminf.rb +12 -0
- data/lib/plurimath/math/function/limits.rb +19 -0
- data/lib/plurimath/math/function/limsup.rb +12 -0
- data/lib/plurimath/math/function/ln.rb +12 -0
- data/lib/plurimath/math/function/log.rb +23 -0
- data/lib/plurimath/math/function/lub.rb +12 -0
- data/lib/plurimath/math/function/max.rb +12 -0
- data/lib/plurimath/math/function/min.rb +12 -0
- data/lib/plurimath/math/function/mod.rb +23 -0
- data/lib/plurimath/math/function/norm.rb +15 -0
- data/lib/plurimath/math/function/obrace.rb +12 -0
- data/lib/plurimath/math/function/oint.rb +17 -0
- data/lib/plurimath/math/function/overset.rb +23 -0
- data/lib/plurimath/math/function/power.rb +30 -0
- data/lib/plurimath/math/function/power_base.rb +26 -0
- data/lib/plurimath/math/function/prod.rb +23 -0
- data/lib/plurimath/math/function/root.rb +22 -0
- data/lib/plurimath/math/function/sec.rb +12 -0
- data/lib/plurimath/math/function/sech.rb +12 -0
- data/lib/plurimath/math/function/sin.rb +12 -0
- data/lib/plurimath/math/function/sinh.rb +12 -0
- data/lib/plurimath/math/function/sqrt.rb +16 -0
- data/lib/plurimath/math/function/stackrel.rb +12 -0
- data/lib/plurimath/math/function/substack.rb +18 -0
- data/lib/plurimath/math/function/sum.rb +23 -0
- data/lib/plurimath/math/function/sup.rb +12 -0
- data/lib/plurimath/math/function/table.rb +42 -0
- data/lib/plurimath/math/function/tan.rb +12 -0
- data/lib/plurimath/math/function/tanh.rb +12 -0
- data/lib/plurimath/math/function/td.rb +27 -0
- data/lib/plurimath/math/function/ternary_function.rb +42 -0
- data/lib/plurimath/math/function/text.rb +32 -0
- data/lib/plurimath/math/function/tilde.rb +12 -0
- data/lib/plurimath/math/function/tr.rb +25 -0
- data/lib/plurimath/math/function/ubrace.rb +12 -0
- data/lib/plurimath/math/function/ul.rb +12 -0
- data/lib/plurimath/math/function/unary_function.rb +41 -0
- data/lib/plurimath/math/function/underover.rb +12 -0
- data/lib/plurimath/math/function/underset.rb +12 -0
- data/lib/plurimath/math/function/vec.rb +12 -0
- data/lib/plurimath/math/function.rb +3 -0
- data/lib/plurimath/math/number.rb +29 -0
- data/lib/plurimath/math/symbol.rb +34 -0
- data/lib/plurimath/math.rb +51 -0
- data/lib/plurimath/mathml/constants.rb +327 -0
- data/lib/plurimath/mathml/parse.rb +63 -0
- data/lib/plurimath/mathml/parser.rb +25 -0
- data/lib/plurimath/mathml/transform.rb +195 -0
- data/lib/plurimath/mathml.rb +16 -0
- data/lib/plurimath/omml.rb +15 -0
- data/lib/plurimath/unicode.rb +15 -0
- data/lib/plurimath/unitsml.rb +11 -0
- data/lib/plurimath/version.rb +3 -1
- data/lib/plurimath.rb +3 -5
- data/plurimath.gemspec +3 -3
- metadata +129 -11
@@ -0,0 +1,304 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Plurimath
|
4
|
+
class Asciimath
|
5
|
+
class Transform < Parslet::Transform
|
6
|
+
rule(expr: simple(:expr)) { expr }
|
7
|
+
rule(base: simple(:base)) { base }
|
8
|
+
rule(base: sequence(:base)) { base }
|
9
|
+
rule(fonts: simple(:fonts)) { fonts }
|
10
|
+
rule(power: simple(:power)) { power }
|
11
|
+
rule(unary: simple(:unary)) { unary }
|
12
|
+
rule(power: sequence(:power)) { power }
|
13
|
+
rule(binary: simple(:binary)) { binary }
|
14
|
+
rule(sequence: simple(:sequence)) { sequence }
|
15
|
+
rule(power_base: simple(:power_base)) { power_base }
|
16
|
+
rule(intermediate_exp: simple(:int_exp)) { int_exp }
|
17
|
+
rule(mod: simple(:mod), expr: simple(:expr)) { [mod, expr] }
|
18
|
+
rule(base: sequence(:base), expr: simple(:exp)) { base + [exp] }
|
19
|
+
|
20
|
+
rule(expr: sequence(:expr)) do
|
21
|
+
Plurimath::Math::Formula.new(expr)
|
22
|
+
end
|
23
|
+
|
24
|
+
rule(power: sequence(:power), expr: simple(:expr)) do
|
25
|
+
power + [expr]
|
26
|
+
end
|
27
|
+
|
28
|
+
rule(power: sequence(:power), expr: sequence(:expr)) do
|
29
|
+
power + expr
|
30
|
+
end
|
31
|
+
|
32
|
+
rule(number: simple(:number)) do
|
33
|
+
Plurimath::Math::Number.new(number.to_s)
|
34
|
+
end
|
35
|
+
|
36
|
+
rule(symbol: simple(:symbol)) do
|
37
|
+
Plurimath::Math::Symbol.new(symbol.to_s)
|
38
|
+
end
|
39
|
+
|
40
|
+
rule(text: simple(:text)) do
|
41
|
+
text.is_a?(Slice) ? Transform.get_class("text").new(text.to_s) : text
|
42
|
+
end
|
43
|
+
|
44
|
+
rule(base: simple(:base),
|
45
|
+
expr: simple(:expr)) do
|
46
|
+
[base, expr]
|
47
|
+
end
|
48
|
+
|
49
|
+
rule(fonts: simple(:font_style), intermediate_exp: simple(:int_exp)) do
|
50
|
+
Plurimath::Math::Function::FontStyle.new(int_exp, font_style.to_s)
|
51
|
+
end
|
52
|
+
|
53
|
+
rule(fonts: simple(:font_style), text: simple(:text)) do
|
54
|
+
parameter_one = Plurimath::Math::Function::Text.new(text.to_s)
|
55
|
+
Plurimath::Math::Function::FontStyle.new(parameter_one, font_style.to_s)
|
56
|
+
end
|
57
|
+
|
58
|
+
rule(power_base: simple(:power_base), expr: sequence(:expr)) do
|
59
|
+
expr.insert(0, power_base)
|
60
|
+
end
|
61
|
+
|
62
|
+
rule(power_base: simple(:power_base), expr: simple(:expr)) do
|
63
|
+
[power_base, expr]
|
64
|
+
end
|
65
|
+
|
66
|
+
rule(power_base: sequence(:power_base), expr: simple(:expr)) do
|
67
|
+
power_base + [expr]
|
68
|
+
end
|
69
|
+
|
70
|
+
rule(power_base: sequence(:power_base), expr: sequence(:expr)) do
|
71
|
+
power_base + expr
|
72
|
+
end
|
73
|
+
|
74
|
+
rule(sequence: simple(:sequence), expr: sequence(:expr)) do
|
75
|
+
expr.insert(0, sequence)
|
76
|
+
end
|
77
|
+
|
78
|
+
rule(dividend: simple(:dividend), mod: simple(:mod),
|
79
|
+
divisor: simple(:divisor)) do
|
80
|
+
Plurimath::Math::Function::Mod.new(dividend, divisor)
|
81
|
+
end
|
82
|
+
|
83
|
+
rule(unary: simple(:unary), "^": simple(:exponent),
|
84
|
+
number: simple(:number)) do
|
85
|
+
[
|
86
|
+
unary,
|
87
|
+
Plurimath::Math::Symbol.new(exponent.to_s),
|
88
|
+
Plurimath::Math::Number.new(number.to_s),
|
89
|
+
]
|
90
|
+
end
|
91
|
+
|
92
|
+
rule(unary: simple(:unary), "^": simple(:exponent),
|
93
|
+
intermediate_exp: simple(:intermediate_exp)) do
|
94
|
+
[
|
95
|
+
unary,
|
96
|
+
Plurimath::Math::Symbol.new(exponent.to_s),
|
97
|
+
intermediate_exp,
|
98
|
+
]
|
99
|
+
end
|
100
|
+
|
101
|
+
rule(unary: simple(:unary), "^": simple(:exponent),
|
102
|
+
text: simple(:text)) do
|
103
|
+
[
|
104
|
+
unary,
|
105
|
+
Plurimath::Math::Symbol.new(exponent.to_s),
|
106
|
+
text.is_a?(Slice) ? Transform.get_class("text").new(text.to_s) : text,
|
107
|
+
]
|
108
|
+
end
|
109
|
+
|
110
|
+
rule(symbol: simple(:symbol), "^": simple(:exponent),
|
111
|
+
number: simple(:number)) do
|
112
|
+
[
|
113
|
+
Plurimath::Math::Symbol.new(symbol.to_s),
|
114
|
+
Plurimath::Math::Symbol.new(exponent.to_s),
|
115
|
+
Plurimath::Math::Number.new(number.to_s),
|
116
|
+
]
|
117
|
+
end
|
118
|
+
|
119
|
+
rule(binary: simple(:binary),
|
120
|
+
"^": simple(:under_score),
|
121
|
+
intermediate_exp: simple(:int_exp)) do
|
122
|
+
[
|
123
|
+
binary,
|
124
|
+
Plurimath::Math::Symbol.new("^"),
|
125
|
+
int_exp,
|
126
|
+
]
|
127
|
+
end
|
128
|
+
|
129
|
+
rule(binary: simple(:function), "^": simple(:exponent),
|
130
|
+
number: simple(:number)) do
|
131
|
+
[
|
132
|
+
function,
|
133
|
+
Plurimath::Math::Symbol.new("^"),
|
134
|
+
Plurimath::Math::Number.new(number.to_s),
|
135
|
+
]
|
136
|
+
end
|
137
|
+
|
138
|
+
Constants::UNARY_CLASSES.each do |unary_class|
|
139
|
+
rule(unary_class => simple(:function),
|
140
|
+
intermediate_exp: simple(:int_exp)) do
|
141
|
+
Transform.get_class(function).new(int_exp)
|
142
|
+
end
|
143
|
+
|
144
|
+
rule(unary_class => simple(:function),
|
145
|
+
_: simple(:under_score),
|
146
|
+
intermediate_exp: simple(:int_exp)) do
|
147
|
+
[
|
148
|
+
Transform.get_class(function).new,
|
149
|
+
Plurimath::Math::Symbol.new("_"),
|
150
|
+
int_exp,
|
151
|
+
]
|
152
|
+
end
|
153
|
+
|
154
|
+
rule(unary_class => simple(:function),
|
155
|
+
symbol: simple(:new_symbol)) do
|
156
|
+
symbol = Plurimath::Math::Symbol.new(new_symbol.to_s)
|
157
|
+
Transform.get_class(function).new(symbol)
|
158
|
+
end
|
159
|
+
|
160
|
+
rule(unary_class => simple(:function),
|
161
|
+
number: simple(:new_number)) do
|
162
|
+
number = Plurimath::Math::Number.new(new_number.to_s)
|
163
|
+
Transform.get_class(function).new(number)
|
164
|
+
end
|
165
|
+
|
166
|
+
rule(unary_class => simple(:function),
|
167
|
+
"^": simple(:base),
|
168
|
+
intermediate_exp: simple(:unary)) do
|
169
|
+
[
|
170
|
+
Transform.get_class(unary_class).new,
|
171
|
+
Plurimath::Math::Symbol.new("^"),
|
172
|
+
unary,
|
173
|
+
]
|
174
|
+
end
|
175
|
+
|
176
|
+
rule(unary_class => simple(:function),
|
177
|
+
_: simple(:base),
|
178
|
+
symbol: simple(:symbol)) do
|
179
|
+
[
|
180
|
+
Transform.get_class(unary_class).new,
|
181
|
+
Plurimath::Math::Symbol.new("_"),
|
182
|
+
Plurimath::Math::Symbol.new(symbol.to_s),
|
183
|
+
]
|
184
|
+
end
|
185
|
+
|
186
|
+
rule(unary_class => simple(:function),
|
187
|
+
_: simple(:under_score),
|
188
|
+
base: simple(:base),
|
189
|
+
"^": simple(:power),
|
190
|
+
exponent: simple(:exponent)) do
|
191
|
+
[
|
192
|
+
Transform.get_class(function).new,
|
193
|
+
Plurimath::Math::Symbol.new("_"),
|
194
|
+
base,
|
195
|
+
Plurimath::Math::Symbol.new("^"),
|
196
|
+
exponent,
|
197
|
+
]
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
Constants::BINARY_CLASSES.each do |binary_class|
|
202
|
+
rule(binary_class => simple(:function)) do
|
203
|
+
Transform.get_class(function).new
|
204
|
+
end
|
205
|
+
|
206
|
+
rule(binary_class => simple(:function), _: simple(:under_score),
|
207
|
+
base: simple(:int_exp), "^": simple(:power),
|
208
|
+
exponent: simple(:exponent)) do
|
209
|
+
Transform.get_class(function).new(int_exp, exponent)
|
210
|
+
end
|
211
|
+
|
212
|
+
rule(binary_class => simple(:function), _: simple(:under_score),
|
213
|
+
intermediate_exp: simple(:int_exp)) do
|
214
|
+
Transform.get_class(function).new(int_exp, nil)
|
215
|
+
end
|
216
|
+
|
217
|
+
rule(binary_class => simple(:function), "^": simple(:exponent),
|
218
|
+
intermediate_exp: simple(:int_exp)) do
|
219
|
+
Transform.get_class(function).new(nil, int_exp)
|
220
|
+
end
|
221
|
+
|
222
|
+
rule(binary_class => simple(:function), _: simple(:under_score),
|
223
|
+
number: simple(:number)) do
|
224
|
+
Transform.get_class(function).new(
|
225
|
+
Plurimath::Math::Number.new(number.to_s), nil
|
226
|
+
)
|
227
|
+
end
|
228
|
+
|
229
|
+
rule(binary_class => simple(:function), _: simple(:under_score),
|
230
|
+
symbol: simple(:symbol)) do
|
231
|
+
Transform.get_class(function).new(
|
232
|
+
Plurimath::Math::Symbol.new(symbol.to_s), nil
|
233
|
+
)
|
234
|
+
end
|
235
|
+
|
236
|
+
rule(binary_class => simple(:function), _: simple(:under_score),
|
237
|
+
unary: simple(:unary)) do
|
238
|
+
Transform.get_class(function).new(unary, nil)
|
239
|
+
end
|
240
|
+
|
241
|
+
rule(binary_class => simple(:function), "^": simple(:power),
|
242
|
+
number: simple(:number)) do
|
243
|
+
Transform.get_class(function).new(
|
244
|
+
nil,
|
245
|
+
Plurimath::Math::Number.new(number.to_s),
|
246
|
+
)
|
247
|
+
end
|
248
|
+
|
249
|
+
rule(binary_class => simple(:function), "^": simple(:power),
|
250
|
+
symbol: simple(:symbol)) do
|
251
|
+
Transform.get_class(function).new(
|
252
|
+
nil,
|
253
|
+
Plurimath::Math::Symbol.new(symbol.to_s),
|
254
|
+
)
|
255
|
+
end
|
256
|
+
|
257
|
+
rule(binary_class => simple(:function), base: simple(:base),
|
258
|
+
exponent: simple(:exponent)) do
|
259
|
+
Transform.get_class(function).new(base, exponent)
|
260
|
+
end
|
261
|
+
|
262
|
+
rule(binary: simple(:function), "^": simple(:exponent),
|
263
|
+
text: simple(:text)) do
|
264
|
+
[
|
265
|
+
function,
|
266
|
+
Plurimath::Math::Symbol.new("^"),
|
267
|
+
text.is_a?(Slice) ? Transform.get_class("text").new(text.to_s) : text,
|
268
|
+
]
|
269
|
+
end
|
270
|
+
|
271
|
+
rule(binary: simple(:function), "^": simple(:exponent),
|
272
|
+
unary: simple(:unary)) do
|
273
|
+
[
|
274
|
+
function,
|
275
|
+
Plurimath::Math::Symbol.new("^"),
|
276
|
+
unary,
|
277
|
+
]
|
278
|
+
end
|
279
|
+
|
280
|
+
Constants::UNARY_CLASSES.each do |unary_class|
|
281
|
+
rule(binary_class => simple(:function),
|
282
|
+
_: simple(:base),
|
283
|
+
unary_class => simple(:unary)) do
|
284
|
+
unary_class = Transform.get_class(unary).new
|
285
|
+
Transform.get_class(binary_class).new(unary_class, nil)
|
286
|
+
end
|
287
|
+
|
288
|
+
rule(binary_class => simple(:function),
|
289
|
+
"^": simple(:base),
|
290
|
+
unary_class => simple(:unary)) do
|
291
|
+
unary_class = Transform.get_class(unary).new
|
292
|
+
Transform.get_class(binary_class).new(nil, unary_class)
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|
296
|
+
|
297
|
+
rule(sequence: simple(:sequence), expr: simple(:exp)) { [sequence, exp] }
|
298
|
+
|
299
|
+
def self.get_class(text)
|
300
|
+
Object.const_get("Plurimath::Math::Function::#{text.to_s.capitalize}")
|
301
|
+
end
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "math"
|
4
|
+
module Plurimath
|
5
|
+
class Asciimath
|
6
|
+
attr_accessor :text
|
7
|
+
|
8
|
+
def initialize(text)
|
9
|
+
@text = text
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_formula
|
13
|
+
@text = Plurimath::Asciimath::Parser.new(text).parse
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|