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,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "unicode"
|
4
|
+
require_relative "asciimath"
|
5
|
+
require_relative "omml"
|
6
|
+
require_relative "mathml"
|
7
|
+
require_relative "html"
|
8
|
+
require_relative "latex"
|
9
|
+
require_relative "unitsml"
|
10
|
+
require_relative "math/formula"
|
11
|
+
require_relative "math/function"
|
12
|
+
require_relative "math/number"
|
13
|
+
require_relative "math/symbol"
|
14
|
+
require_relative "asciimath/parser"
|
15
|
+
require_relative "mathml/parser"
|
16
|
+
require_relative "latex/parser"
|
17
|
+
module Plurimath
|
18
|
+
module Math
|
19
|
+
class Error < StandardError; end
|
20
|
+
|
21
|
+
VALID_TYPES = {
|
22
|
+
unicode: Unicode,
|
23
|
+
asciimath: Asciimath,
|
24
|
+
omml: Omml,
|
25
|
+
mathml: Mathml,
|
26
|
+
html: Html,
|
27
|
+
latex: Latex,
|
28
|
+
}.freeze
|
29
|
+
|
30
|
+
def parse(text, type)
|
31
|
+
raise_error! unless valid_type?(type)
|
32
|
+
|
33
|
+
klass = VALID_TYPES[type.to_sym]
|
34
|
+
klass.new(text).to_formula
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def raise_error!
|
40
|
+
raise Plurimath::Math::Error, Error.new("Type is not valid, "\
|
41
|
+
"please enter string or symbol")
|
42
|
+
end
|
43
|
+
|
44
|
+
def valid_type?(type)
|
45
|
+
(type.is_a?(Symbol) || type.is_a?(String)) &&
|
46
|
+
VALID_TYPES.key?(type.to_sym)
|
47
|
+
end
|
48
|
+
|
49
|
+
module_function :parse, :raise_error!, :valid_type?
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,327 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Plurimath
|
4
|
+
class Mathml
|
5
|
+
class Constants
|
6
|
+
UNICODE_SYMBOLS = {
|
7
|
+
"α": "alpha",
|
8
|
+
"β": "beta",
|
9
|
+
"γ": "gamma",
|
10
|
+
"Γ": "Gamma",
|
11
|
+
"δ": "delta",
|
12
|
+
"Δ": "Delta",
|
13
|
+
"∆": "Delta",
|
14
|
+
"ε": "epsilon",
|
15
|
+
"ɛ": "varepsilon",
|
16
|
+
"ζ": "zeta",
|
17
|
+
"η": "eta",
|
18
|
+
"θ": "theta",
|
19
|
+
"Θ": "Theta",
|
20
|
+
"ϑ": "vartheta",
|
21
|
+
"ι": "iota",
|
22
|
+
"κ": "kappa",
|
23
|
+
"λ": "lambda",
|
24
|
+
"Λ": "Lambda",
|
25
|
+
"μ": "mu",
|
26
|
+
"ν": "nu",
|
27
|
+
"ξ": "xi",
|
28
|
+
"Ξ": "Xi",
|
29
|
+
"π": "pi",
|
30
|
+
"Π": "Pi",
|
31
|
+
"ρ": "rho",
|
32
|
+
"ς": "beta",
|
33
|
+
"σ": "sigma",
|
34
|
+
"Σ": "Sigma",
|
35
|
+
"τ": "tau",
|
36
|
+
"υ": "upsilon",
|
37
|
+
"φ": "phi",
|
38
|
+
"Φ": "Phi",
|
39
|
+
"ϕ": "varphi",
|
40
|
+
"χ": "chi",
|
41
|
+
"ψ": "psi",
|
42
|
+
"Ψ": "Psi",
|
43
|
+
"ω": "omega",
|
44
|
+
"Ω": "omega",
|
45
|
+
"⋅": "dot",
|
46
|
+
"∙": "*",
|
47
|
+
"·": ".",
|
48
|
+
"∗": "**",
|
49
|
+
"⋆": "***",
|
50
|
+
"×": "xx",
|
51
|
+
"⋉": "|><",
|
52
|
+
"⋊": "><|",
|
53
|
+
"⋈": "|><|",
|
54
|
+
"÷": "-:",
|
55
|
+
"∘": "@",
|
56
|
+
"⊕": "o+",
|
57
|
+
"⨁": "o+",
|
58
|
+
"⊗": "ox",
|
59
|
+
"⊙": " ",
|
60
|
+
"∑": "sum",
|
61
|
+
"∏": "prod",
|
62
|
+
"∏": "prod",
|
63
|
+
"∧": "^^",
|
64
|
+
"⋀": "^^^",
|
65
|
+
"∨": "vv",
|
66
|
+
"⋁": "vvv",
|
67
|
+
"∩": "nn",
|
68
|
+
"⋂": "nnn",
|
69
|
+
"∪": "cup",
|
70
|
+
"⋃": "uuu",
|
71
|
+
"≠": "!=",
|
72
|
+
"≤": "<=",
|
73
|
+
"≥": ">=",
|
74
|
+
"≺": "-<",
|
75
|
+
"≻": ">-",
|
76
|
+
"⪯": "-<=",
|
77
|
+
"⪰": " >-=",
|
78
|
+
"∈": "in",
|
79
|
+
"∉": "!in",
|
80
|
+
"⊂": "sub",
|
81
|
+
"⊃": "sup",
|
82
|
+
"⊆": "sube",
|
83
|
+
"⊇": "supe",
|
84
|
+
"≡": "-=",
|
85
|
+
"≅": "~=",
|
86
|
+
"≈": "~~",
|
87
|
+
"∝": "prop",
|
88
|
+
"¬": "not",
|
89
|
+
"∀": "AA",
|
90
|
+
"∃": "EE",
|
91
|
+
"⊥": "_|_",
|
92
|
+
"⊤": "TT",
|
93
|
+
"⊢": "|--",
|
94
|
+
"⊨": "|==",
|
95
|
+
"〈": "(:",
|
96
|
+
"〉": ":)",
|
97
|
+
"⟨": "<<",
|
98
|
+
"⟩": ">>",
|
99
|
+
"∫": "int",
|
100
|
+
"∮": "oint",
|
101
|
+
"∂": "del",
|
102
|
+
"∇": "grad",
|
103
|
+
"±": "+-",
|
104
|
+
"∅": "O/",
|
105
|
+
"∞": "oo",
|
106
|
+
"ℵ": "aleph",
|
107
|
+
"∴": ":.",
|
108
|
+
"∵": ":'",
|
109
|
+
"∠": "/_",
|
110
|
+
"△": "/_\\",
|
111
|
+
"′": "'",
|
112
|
+
"  ": "quad",
|
113
|
+
"    ": "qquad",
|
114
|
+
"⌢": "frown",
|
115
|
+
"⋯": "cdots",
|
116
|
+
"⋮": "vdots",
|
117
|
+
"⋱": "ddots",
|
118
|
+
"⋄": "diamond",
|
119
|
+
"□": "square",
|
120
|
+
"⌊": "|__",
|
121
|
+
"⌋": "__|",
|
122
|
+
"⌈": "|~",
|
123
|
+
"⌉": "~|",
|
124
|
+
"ℂ": "CC",
|
125
|
+
"ℕ": "NN",
|
126
|
+
"ℚ": "QQ",
|
127
|
+
"ℝ": "RR",
|
128
|
+
"ℤ": "ZZ",
|
129
|
+
"↑": "uarr",
|
130
|
+
"↓": "darr",
|
131
|
+
"←": "larr",
|
132
|
+
"↔": "harr",
|
133
|
+
"⇒": "rArr",
|
134
|
+
"⇐": "lArr",
|
135
|
+
"⇔": "hArr",
|
136
|
+
"→": "->",
|
137
|
+
"↣": ">->",
|
138
|
+
"↠": "->>",
|
139
|
+
"⤖": ">->>",
|
140
|
+
"↦": "|->",
|
141
|
+
"…": "...",
|
142
|
+
"−": "-",
|
143
|
+
"⁡": "",
|
144
|
+
"⏞": "obrace",
|
145
|
+
"⏟": "ubrace",
|
146
|
+
}.freeze
|
147
|
+
SYMBOLS = {
|
148
|
+
"|": "|",
|
149
|
+
"/": "//",
|
150
|
+
"\\": "\\\\",
|
151
|
+
"~": "tilde",
|
152
|
+
"(": "(",
|
153
|
+
")": ")",
|
154
|
+
"(:": "(:",
|
155
|
+
":)": ":)",
|
156
|
+
"{": "{",
|
157
|
+
"}": "}",
|
158
|
+
"{:": "{:",
|
159
|
+
":}": ":}",
|
160
|
+
"]": "]",
|
161
|
+
"[": "[",
|
162
|
+
"=": "=",
|
163
|
+
"+": "+",
|
164
|
+
"-": "-",
|
165
|
+
}.freeze
|
166
|
+
CLASSES = %w[
|
167
|
+
mathfrak
|
168
|
+
underset
|
169
|
+
stackrel
|
170
|
+
overset
|
171
|
+
mathcal
|
172
|
+
arccos
|
173
|
+
arcsin
|
174
|
+
arctan
|
175
|
+
mathsf
|
176
|
+
mathbb
|
177
|
+
mathbf
|
178
|
+
mathtt
|
179
|
+
ubrace
|
180
|
+
obrace
|
181
|
+
cancel
|
182
|
+
tilde
|
183
|
+
floor
|
184
|
+
color
|
185
|
+
frac
|
186
|
+
root
|
187
|
+
oint
|
188
|
+
ceil
|
189
|
+
ddot
|
190
|
+
coth
|
191
|
+
csch
|
192
|
+
sech
|
193
|
+
sinh
|
194
|
+
tanh
|
195
|
+
cosh
|
196
|
+
sqrt
|
197
|
+
norm
|
198
|
+
text
|
199
|
+
prod
|
200
|
+
sec
|
201
|
+
int
|
202
|
+
sin
|
203
|
+
tan
|
204
|
+
cos
|
205
|
+
sum
|
206
|
+
exp
|
207
|
+
gcd
|
208
|
+
glb
|
209
|
+
lcm
|
210
|
+
lub
|
211
|
+
cot
|
212
|
+
csc
|
213
|
+
det
|
214
|
+
dim
|
215
|
+
max
|
216
|
+
min
|
217
|
+
abs
|
218
|
+
bar
|
219
|
+
dot
|
220
|
+
hat
|
221
|
+
vec
|
222
|
+
mod
|
223
|
+
log
|
224
|
+
ul
|
225
|
+
ln
|
226
|
+
f
|
227
|
+
g
|
228
|
+
].freeze
|
229
|
+
TAGS = %i[
|
230
|
+
annotation-xml
|
231
|
+
annotation_xml
|
232
|
+
mmultiscripts
|
233
|
+
maligngroup
|
234
|
+
malignmark
|
235
|
+
annotation
|
236
|
+
munderover
|
237
|
+
mscarries
|
238
|
+
semantics
|
239
|
+
mphantom
|
240
|
+
mlongdiv
|
241
|
+
menclose
|
242
|
+
mscarry
|
243
|
+
msubsup
|
244
|
+
mpadded
|
245
|
+
maction
|
246
|
+
msgroup
|
247
|
+
mfenced
|
248
|
+
merror
|
249
|
+
munder
|
250
|
+
mtable
|
251
|
+
mstyle
|
252
|
+
mstack
|
253
|
+
mspace
|
254
|
+
msline
|
255
|
+
mfrac
|
256
|
+
mover
|
257
|
+
msrow
|
258
|
+
mroot
|
259
|
+
msqrt
|
260
|
+
msup
|
261
|
+
msub
|
262
|
+
mrow
|
263
|
+
math
|
264
|
+
mtr
|
265
|
+
mtd
|
266
|
+
ms
|
267
|
+
mi
|
268
|
+
mo
|
269
|
+
mn
|
270
|
+
].freeze
|
271
|
+
BINARY_CLASSES = %i[
|
272
|
+
underset
|
273
|
+
stackrel
|
274
|
+
overset
|
275
|
+
color
|
276
|
+
prod
|
277
|
+
frac
|
278
|
+
root
|
279
|
+
oint
|
280
|
+
int
|
281
|
+
sum
|
282
|
+
mod
|
283
|
+
log
|
284
|
+
].freeze
|
285
|
+
FONT_CLASSES = %i[
|
286
|
+
fraktur
|
287
|
+
sans-serif
|
288
|
+
monospace
|
289
|
+
script
|
290
|
+
double-struck
|
291
|
+
bold
|
292
|
+
].freeze
|
293
|
+
FONT_TYPES = {
|
294
|
+
:bb=>"bold",
|
295
|
+
:fr=>"fraktur",
|
296
|
+
:cc=>"script",
|
297
|
+
:sf=>"sans-serif",
|
298
|
+
:tt=>"monospace",
|
299
|
+
:mbf=>"bold",
|
300
|
+
:mit=>"italic",
|
301
|
+
:mtt=>"monospace",
|
302
|
+
:Bbb=>"double-struck",
|
303
|
+
:bbb=>"double-struck",
|
304
|
+
:mscr=>"mathbb",
|
305
|
+
:mfrak=>"fraktur",
|
306
|
+
:msans=>"sans-serif",
|
307
|
+
:mbfit=>"bold italic",
|
308
|
+
:mathbf=>"bold",
|
309
|
+
:mathit=>"italic",
|
310
|
+
:mathds=>"mathbb",
|
311
|
+
:mathrm=>"mathrm",
|
312
|
+
:mathtt=>"monospace",
|
313
|
+
:mathsf=>"sans-serif",
|
314
|
+
:mathbb=>"double-struck",
|
315
|
+
:mbfsans=>"bold sans-serif",
|
316
|
+
:mathcal=>"script",
|
317
|
+
:mathbold=>"bold",
|
318
|
+
:mathfrak=>"fraktur",
|
319
|
+
:mathbfit=>"bold italic",
|
320
|
+
:mathsfbf=>"sans-serif bold",
|
321
|
+
:mathsfit=>"sans-serif italic",
|
322
|
+
:mbfitsans=>"bold italic sans-serif",
|
323
|
+
:mathsfbfit=>"sans-serif bold italic",
|
324
|
+
}.freeze
|
325
|
+
end
|
326
|
+
end
|
327
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "parslet"
|
4
|
+
module Plurimath
|
5
|
+
class Mathml
|
6
|
+
class Parse < Parslet::Parser
|
7
|
+
rule(:parse_record) do
|
8
|
+
array_to_expression(Constants::CLASSES).as(:class) |
|
9
|
+
array_to_expression(Constants::UNICODE_SYMBOLS.keys).as(:symbol) |
|
10
|
+
array_to_expression(Constants::SYMBOLS.keys).as(:symbol) |
|
11
|
+
match["a-zA-Z"].repeat(1).as(:text) |
|
12
|
+
match(/[0-9]/).repeat(1).as(:number) |
|
13
|
+
str("")
|
14
|
+
end
|
15
|
+
|
16
|
+
rule(:tag) { (parse_tag(:open) >> iteration.as(:iteration) >> parse_tag(:close)).as(:tag) | parse_text_tag.as(:tag) }
|
17
|
+
|
18
|
+
rule(:sequence) { (tag >> sequence.as(:sequence)) | tag }
|
19
|
+
|
20
|
+
rule(:iteration) { (sequence >> iteration.as(:iteration)) | parse_record }
|
21
|
+
|
22
|
+
rule(:expression) { parse_tag(:open) >> iteration.as(:iteration) >> parse_tag(:close) }
|
23
|
+
|
24
|
+
root :expression
|
25
|
+
|
26
|
+
def array_to_expression(array, name = nil)
|
27
|
+
initial_type = array.first.class
|
28
|
+
array.reduce do |expr, tag|
|
29
|
+
expr = str_to_expression(expr, name) if expr.is_a?(initial_type)
|
30
|
+
expr | str_to_expression(tag, name)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def str_to_expression(string, name)
|
35
|
+
return str(string) if name.nil?
|
36
|
+
|
37
|
+
str(string).as(name)
|
38
|
+
end
|
39
|
+
|
40
|
+
def parse_tag(opts)
|
41
|
+
tag = str("<")
|
42
|
+
tag = tag >> str("/") if opts == :close
|
43
|
+
tag = tag >> array_to_expression(Constants::TAGS, opts)
|
44
|
+
tag = tag >> attributes.as(:attributes) if opts == :open
|
45
|
+
tag >> str(">")
|
46
|
+
end
|
47
|
+
|
48
|
+
def attributes
|
49
|
+
(match["a-zA-Z"].repeat.as(:name) >>
|
50
|
+
str("=") >> quoted_string).repeat
|
51
|
+
end
|
52
|
+
|
53
|
+
def quoted_string
|
54
|
+
(str('"') >> match("[^\"]").repeat.as(:value) >> str('"')) |
|
55
|
+
(str("'") >> match("[^\']").repeat.as(:value) >> str("'"))
|
56
|
+
end
|
57
|
+
|
58
|
+
def parse_text_tag
|
59
|
+
str("<mtext>") >> match("[^<]").repeat.as(:quoted_text) >> str("</mtext>")
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "parse"
|
4
|
+
require_relative "constants"
|
5
|
+
require_relative "transform"
|
6
|
+
module Plurimath
|
7
|
+
class Mathml
|
8
|
+
class Parser
|
9
|
+
attr_accessor :text
|
10
|
+
|
11
|
+
def initialize(text)
|
12
|
+
@text = text.gsub("\n", "").gsub(" ", "")
|
13
|
+
end
|
14
|
+
|
15
|
+
def parse
|
16
|
+
tree_t = Plurimath::Mathml::Parse.new.parse(text)
|
17
|
+
formula = Plurimath::Mathml::Transform.new.apply(tree_t)
|
18
|
+
formula = [formula] unless formula.is_a?(Array) || formula.nil?
|
19
|
+
return if formula.nil?
|
20
|
+
|
21
|
+
Plurimath::Math::Formula.new(formula)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,195 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Plurimath
|
4
|
+
class Mathml
|
5
|
+
class Transform < Parslet::Transform
|
6
|
+
rule(tag: simple(:tag)) { tag }
|
7
|
+
rule(tag: sequence(:tag)) { tag }
|
8
|
+
rule(text: simple(:text)) { Plurimath::Math::Symbol.new(text.to_s) }
|
9
|
+
rule(class: simple(:string)) { Transform.get_class(string).new }
|
10
|
+
rule(number: simple(:number)) { Plurimath::Math::Number.new(number.to_s) }
|
11
|
+
rule(tag: sequence(:tag), sequence: simple(:sequence)) { tag + [sequence] }
|
12
|
+
rule(tag: simple(:tag), sequence: sequence(:sequence)) { [tag] + sequence }
|
13
|
+
rule(tag: sequence(:tag), sequence: sequence(:sequence)) { tag + sequence }
|
14
|
+
|
15
|
+
rule(quoted_text: simple(:quoted_text)) do
|
16
|
+
text = quoted_text.to_s
|
17
|
+
Constants::UNICODE_SYMBOLS.each do |code, string|
|
18
|
+
text.gsub!(code.to_s, "unicode[:#{string}]")
|
19
|
+
end
|
20
|
+
Plurimath::Math::Function::Text.new(text)
|
21
|
+
end
|
22
|
+
|
23
|
+
rule(
|
24
|
+
tag: sequence(:tag),
|
25
|
+
sequence: sequence(:sequence),
|
26
|
+
iteration: simple(:iteration),
|
27
|
+
) do
|
28
|
+
new_arr = []
|
29
|
+
new_arr = new_arr + tag unless tag.compact.empty?
|
30
|
+
new_arr = new_arr + sequence unless sequence.compact.empty?
|
31
|
+
new_arr << iteration unless iteration.to_s.empty?
|
32
|
+
new_arr
|
33
|
+
end
|
34
|
+
|
35
|
+
rule(
|
36
|
+
tag: sequence(:tag),
|
37
|
+
sequence: simple(:sequence),
|
38
|
+
iteration: simple(:iteration),
|
39
|
+
) do
|
40
|
+
Plurimath::Math::Formula.new(tag + [sequence])
|
41
|
+
end
|
42
|
+
|
43
|
+
rule(
|
44
|
+
tag: simple(:tag),
|
45
|
+
sequence: simple(:sequence),
|
46
|
+
iteration: simple(:iteration),
|
47
|
+
) do
|
48
|
+
[tag, sequence]
|
49
|
+
end
|
50
|
+
|
51
|
+
rule(tag: sequence(:tag), iteration: simple(:iteration)) do
|
52
|
+
new_arr = []
|
53
|
+
new_arr = tag unless tag.compact.empty?
|
54
|
+
new_arr << iteration unless iteration.to_s.empty?
|
55
|
+
new_arr
|
56
|
+
end
|
57
|
+
|
58
|
+
rule(tag: simple(:tag), sequence: simple(:sequence)) do
|
59
|
+
new_arr = []
|
60
|
+
new_arr << tag unless tag.nil?
|
61
|
+
new_arr << sequence unless sequence.nil?
|
62
|
+
new_arr
|
63
|
+
end
|
64
|
+
|
65
|
+
rule(tag: simple(:tag), iteration: simple(:iteration)) do
|
66
|
+
new_arr = []
|
67
|
+
new_arr << tag unless tag.to_s.empty?
|
68
|
+
new_arr << iteration unless iteration.to_s.empty?
|
69
|
+
new_arr
|
70
|
+
end
|
71
|
+
|
72
|
+
rule(symbol: simple(:symbol)) do
|
73
|
+
decoded_symbol = Constants::UNICODE_SYMBOLS[symbol.to_sym]
|
74
|
+
if Constants::CLASSES.include?(decoded_symbol)
|
75
|
+
[Transform.get_class(decoded_symbol).new]
|
76
|
+
elsif decoded_symbol.nil? && Constants::SYMBOLS[symbol.to_sym]
|
77
|
+
Plurimath::Math::Symbol.new(Constants::SYMBOLS[symbol.to_sym])
|
78
|
+
else
|
79
|
+
Plurimath::Math::Symbol.new(decoded_symbol)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
rule(name: simple(:name), value: simple(:value)) do
|
84
|
+
if ["open", "close"].include?(name.to_s)
|
85
|
+
Plurimath::Math::Symbol.new(value.to_s)
|
86
|
+
elsif name.to_s == "mathcolor"
|
87
|
+
Plurimath::Math::Function::Color.new(value.to_s)
|
88
|
+
elsif name.to_s == "mathvariant"
|
89
|
+
value.to_s
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
rule(
|
94
|
+
tag: simple(:tag),
|
95
|
+
sequence: sequence(:sequence),
|
96
|
+
iteration: simple(:iteration),
|
97
|
+
) do
|
98
|
+
tag_str = tag.to_s.split("::").last.downcase.to_sym
|
99
|
+
if Constants::BINARY_CLASSES.include?(tag_str)
|
100
|
+
tag.new(sequence.first, sequence.last)
|
101
|
+
else
|
102
|
+
new_arr = sequence.compact
|
103
|
+
new_arr = [tag] + new_arr unless tag.nil?
|
104
|
+
new_arr << iteration unless iteration.to_s.empty?
|
105
|
+
new_arr
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
rule(
|
110
|
+
open: simple(:open_tag),
|
111
|
+
attributes: sequence(:attributes),
|
112
|
+
iteration: sequence(:iteration),
|
113
|
+
close: simple(:close_tag),
|
114
|
+
) do
|
115
|
+
Transform.raise_error!(open_tag, close_tag) unless open_tag == close_tag
|
116
|
+
|
117
|
+
if open_tag == "mrow"
|
118
|
+
Plurimath::Math::Formula.new(iteration)
|
119
|
+
elsif open_tag == "munder"
|
120
|
+
if Transform.get_class("obrace") == iteration.last.class
|
121
|
+
iteration.last.parameter_one = iteration.first
|
122
|
+
iteration.last
|
123
|
+
else
|
124
|
+
Plurimath::Math::Function::Underset.new(iteration[1], iteration[0])
|
125
|
+
end
|
126
|
+
elsif open_tag == "munderover"
|
127
|
+
Plurimath::Math::Function::Underover.new(iteration[0], iteration[1], iteration[2])
|
128
|
+
elsif open_tag == "mover"
|
129
|
+
if Transform.get_class("ubrace") == iteration.last.class
|
130
|
+
iteration.last.parameter_one = iteration.first
|
131
|
+
iteration.last
|
132
|
+
else
|
133
|
+
Plurimath::Math::Function::Overset.new(iteration[1], iteration[0])
|
134
|
+
end
|
135
|
+
elsif open_tag == "msup"
|
136
|
+
Plurimath::Math::Function::Power.new(iteration[0], iteration[1])
|
137
|
+
elsif open_tag == "msub"
|
138
|
+
Plurimath::Math::Function::Base.new(iteration[0], iteration[1])
|
139
|
+
elsif open_tag == "msubsup"
|
140
|
+
Plurimath::Math::Function::PowerBase.new(iteration[0], iteration[1], iteration[2])
|
141
|
+
elsif open_tag == "mfrac"
|
142
|
+
Plurimath::Math::Function::Frac.new(iteration.first, iteration.last)
|
143
|
+
elsif open_tag == "msqrt"
|
144
|
+
Plurimath::Math::Function::Sqrt.new(iteration.first)
|
145
|
+
elsif open_tag == "mroot"
|
146
|
+
Plurimath::Math::Function::Root.new(iteration[0], iteration[1])
|
147
|
+
elsif open_tag == "mfenced"
|
148
|
+
Plurimath::Math::Function::Fenced.new(attributes[0], iteration, attributes[1])
|
149
|
+
elsif open_tag == "mtable"
|
150
|
+
Plurimath::Math::Function::Table.new(iteration)
|
151
|
+
elsif open_tag == "mtr"
|
152
|
+
Plurimath::Math::Function::Tr.new(iteration)
|
153
|
+
elsif open_tag == "mtd"
|
154
|
+
Plurimath::Math::Function::Td.new(iteration)
|
155
|
+
elsif attributes.first.is_a?(Plurimath::Math::Function::Color)
|
156
|
+
attributes.first.parameter_two = iteration.first
|
157
|
+
attributes.first
|
158
|
+
elsif open_tag == "mstyle" && !attributes.compact.empty?
|
159
|
+
font_type = attributes.compact.last.to_sym
|
160
|
+
if Constants::FONT_CLASSES.include?(font_type)
|
161
|
+
Math::Function::FontStyle.new(iteration.last, font_type.to_s)
|
162
|
+
end
|
163
|
+
else
|
164
|
+
iteration
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
rule(
|
169
|
+
open: simple(:open_tag),
|
170
|
+
attributes: sequence(:attributes),
|
171
|
+
iteration: simple(:iteration),
|
172
|
+
close: simple(:close_tag),
|
173
|
+
) do
|
174
|
+
Transform.raise_error!(open_tag, close_tag) unless open_tag == close_tag
|
175
|
+
|
176
|
+
if iteration.to_s.include?("Function")
|
177
|
+
iteration
|
178
|
+
else
|
179
|
+
[iteration.to_s.empty? ? nil : iteration]
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
def self.get_class(text)
|
184
|
+
Object.const_get("Plurimath::Math::Function::#{text.to_s.capitalize}")
|
185
|
+
end
|
186
|
+
|
187
|
+
def self.raise_error!(open_tag, close_tag)
|
188
|
+
message = "Please check your input."\
|
189
|
+
" Opening tag is \"#{open_tag}\""\
|
190
|
+
" and closing tag is \"#{close_tag}\""
|
191
|
+
raise Plurimath::Math::Error.new(message)
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|