plurimath 0.1.0 → 0.2.1
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 +33 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +2 -0
- data/AsciiMath-Supported-Data.adoc +280 -0
- data/Gemfile +1 -0
- data/Latex-Supported-Data.adoc +1872 -0
- data/MathML-Supported-Data.adoc +270 -0
- data/README.adoc +94 -0
- data/lib/plurimath/asciimath/constants.rb +301 -0
- data/lib/plurimath/asciimath/parse.rb +121 -0
- data/lib/plurimath/asciimath/parser.rb +25 -0
- data/lib/plurimath/asciimath/transform.rb +395 -0
- data/lib/plurimath/asciimath.rb +16 -0
- data/lib/plurimath/html/constants.rb +50 -0
- data/lib/plurimath/html/parse.rb +149 -0
- data/lib/plurimath/html/parser.rb +26 -0
- data/lib/plurimath/html/transform.rb +363 -0
- data/lib/plurimath/html.rb +15 -0
- data/lib/plurimath/latex/constants.rb +1990 -0
- data/lib/plurimath/latex/parse.rb +198 -0
- data/lib/plurimath/latex/parser.rb +25 -0
- data/lib/plurimath/latex/transform.rb +458 -0
- data/lib/plurimath/latex.rb +15 -0
- data/lib/plurimath/math/formula.rb +51 -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 +36 -0
- data/lib/plurimath/math/function/binary_function.rb +58 -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 +27 -0
- data/lib/plurimath/math/function/floor.rb +12 -0
- data/lib/plurimath/math/function/font_style/bold.rb +18 -0
- data/lib/plurimath/math/function/font_style/double_struck.rb +18 -0
- data/lib/plurimath/math/function/font_style/fraktur.rb +18 -0
- data/lib/plurimath/math/function/font_style/monospace.rb +18 -0
- data/lib/plurimath/math/function/font_style/sans-serif.rb +18 -0
- data/lib/plurimath/math/function/font_style/script.rb +18 -0
- data/lib/plurimath/math/function/font_style.rb +25 -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 +17 -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 +20 -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 +18 -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 +29 -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 +29 -0
- data/lib/plurimath/math/function/multiscript.rb +11 -0
- data/lib/plurimath/math/function/norm.rb +16 -0
- data/lib/plurimath/math/function/obrace.rb +12 -0
- data/lib/plurimath/math/function/oint.rb +17 -0
- data/lib/plurimath/math/function/over.rb +29 -0
- data/lib/plurimath/math/function/overset.rb +23 -0
- data/lib/plurimath/math/function/power.rb +36 -0
- data/lib/plurimath/math/function/power_base.rb +33 -0
- data/lib/plurimath/math/function/prod.rb +29 -0
- data/lib/plurimath/math/function/right.rb +24 -0
- data/lib/plurimath/math/function/root.rb +23 -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 +17 -0
- data/lib/plurimath/math/function/sum.rb +29 -0
- data/lib/plurimath/math/function/sup.rb +12 -0
- data/lib/plurimath/math/function/table/align.rb +24 -0
- data/lib/plurimath/math/function/table/array.rb +25 -0
- data/lib/plurimath/math/function/table/bmatrix.rb +26 -0
- data/lib/plurimath/math/function/table/matrix.rb +24 -0
- data/lib/plurimath/math/function/table/multline.rb +24 -0
- data/lib/plurimath/math/function/table/pmatrix.rb +24 -0
- data/lib/plurimath/math/function/table/split.rb +24 -0
- data/lib/plurimath/math/function/table/vmatrix.rb +25 -0
- data/lib/plurimath/math/function/table.rb +54 -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 +32 -0
- data/lib/plurimath/math/function/ternary_function.rb +52 -0
- data/lib/plurimath/math/function/text.rb +44 -0
- data/lib/plurimath/math/function/tilde.rb +12 -0
- data/lib/plurimath/math/function/tr.rb +30 -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 +46 -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 +16 -0
- data/lib/plurimath/math/function.rb +14 -0
- data/lib/plurimath/math/number.rb +37 -0
- data/lib/plurimath/math/symbol.rb +43 -0
- data/lib/plurimath/math.rb +56 -0
- data/lib/plurimath/mathml/constants.rb +295 -0
- data/lib/plurimath/mathml/parse.rb +68 -0
- data/lib/plurimath/mathml/parser.rb +26 -0
- data/lib/plurimath/mathml/transform.rb +200 -0
- data/lib/plurimath/mathml.rb +16 -0
- data/lib/plurimath/omml/constants.rb +154 -0
- data/lib/plurimath/omml/parser.rb +22 -0
- data/lib/plurimath/omml/transform.rb +216 -0
- data/lib/plurimath/omml.rb +15 -0
- data/lib/plurimath/unicode.rb +15 -0
- data/lib/plurimath/unitsml.rb +15 -0
- data/lib/plurimath/utility.rb +73 -0
- data/lib/plurimath/version.rb +3 -1
- data/lib/plurimath.rb +3 -5
- data/plurimath.gemspec +4 -3
- metadata +169 -9
- data/README.md +0 -40
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Plurimath
|
4
|
+
module Math
|
5
|
+
class Number
|
6
|
+
attr_accessor :value
|
7
|
+
|
8
|
+
def initialize(value)
|
9
|
+
@value = value
|
10
|
+
end
|
11
|
+
|
12
|
+
def ==(object)
|
13
|
+
object.value == value
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_asciimath
|
17
|
+
value
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_mathml_without_math_tag
|
21
|
+
"<mn>#{value}</mn>"
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_latex
|
25
|
+
value
|
26
|
+
end
|
27
|
+
|
28
|
+
def to_html
|
29
|
+
value
|
30
|
+
end
|
31
|
+
|
32
|
+
def class_name
|
33
|
+
self.class.name.split("::").last.downcase
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Plurimath
|
4
|
+
module Math
|
5
|
+
class Symbol
|
6
|
+
attr_accessor :value
|
7
|
+
|
8
|
+
def initialize(sym)
|
9
|
+
@value = sym
|
10
|
+
end
|
11
|
+
|
12
|
+
def ==(object)
|
13
|
+
object.value == value
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_asciimath
|
17
|
+
symbol = Asciimath::Constants::SYMBOLS.invert[value.to_sym].to_s
|
18
|
+
symbol.empty? ? value : symbol
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_mathml_without_math_tag
|
22
|
+
"<mo>#{mathml_symbol_value}</mo>"
|
23
|
+
end
|
24
|
+
|
25
|
+
def mathml_symbol_value
|
26
|
+
Mathml::Constants::UNICODE_SYMBOLS.invert[value] || value
|
27
|
+
end
|
28
|
+
|
29
|
+
def to_latex
|
30
|
+
symbols = Latex::Constants::SYMBOLS.invert
|
31
|
+
symbols.key?(value) ? "\\#{symbols[value]}" : value
|
32
|
+
end
|
33
|
+
|
34
|
+
def to_html
|
35
|
+
value
|
36
|
+
end
|
37
|
+
|
38
|
+
def class_name
|
39
|
+
self.class.name.split("::").last.downcase
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "ox"
|
4
|
+
require_relative "unicode"
|
5
|
+
require_relative "asciimath"
|
6
|
+
require_relative "omml"
|
7
|
+
require_relative "mathml"
|
8
|
+
require_relative "html"
|
9
|
+
require_relative "latex"
|
10
|
+
require_relative "unitsml"
|
11
|
+
require_relative "math/formula"
|
12
|
+
require_relative "math/function"
|
13
|
+
require_relative "math/number"
|
14
|
+
require_relative "math/symbol"
|
15
|
+
require_relative "asciimath/parser"
|
16
|
+
require_relative "mathml/parser"
|
17
|
+
require_relative "latex/parser"
|
18
|
+
require_relative "html/parser"
|
19
|
+
require_relative "omml/parser"
|
20
|
+
require_relative "utility"
|
21
|
+
module Plurimath
|
22
|
+
module Math
|
23
|
+
class Error < StandardError; end
|
24
|
+
|
25
|
+
VALID_TYPES = {
|
26
|
+
omml: Omml,
|
27
|
+
html: Html,
|
28
|
+
latex: Latex,
|
29
|
+
mathml: Mathml,
|
30
|
+
unitsml: Unitsml,
|
31
|
+
unicode: Unicode,
|
32
|
+
asciimath: Asciimath,
|
33
|
+
}.freeze
|
34
|
+
|
35
|
+
def parse(text, type)
|
36
|
+
raise_error! unless valid_type?(type)
|
37
|
+
|
38
|
+
klass = VALID_TYPES[type.to_sym]
|
39
|
+
klass.new(text).to_formula
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def raise_error!
|
45
|
+
raise Plurimath::Math::Error, Error.new("Type is not valid, "\
|
46
|
+
"please enter string or symbol")
|
47
|
+
end
|
48
|
+
|
49
|
+
def valid_type?(type)
|
50
|
+
(type.is_a?(::Symbol) || type.is_a?(String)) &&
|
51
|
+
VALID_TYPES.key?(type.to_sym)
|
52
|
+
end
|
53
|
+
|
54
|
+
module_function :parse, :raise_error!, :valid_type?
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,295 @@
|
|
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
|
+
double-struck
|
287
|
+
sans-serif
|
288
|
+
monospace
|
289
|
+
fraktur
|
290
|
+
script
|
291
|
+
bold
|
292
|
+
].freeze
|
293
|
+
end
|
294
|
+
end
|
295
|
+
end
|
@@ -0,0 +1,68 @@
|
|
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) do
|
17
|
+
(parse_tag(:open) >> iteration.as(:iteration) >> parse_tag(:close)).as(:tag) |
|
18
|
+
parse_text_tag.as(:tag)
|
19
|
+
end
|
20
|
+
|
21
|
+
rule(:sequence) { (tag >> sequence.as(:sequence)) | tag }
|
22
|
+
|
23
|
+
rule(:iteration) { (sequence >> iteration.as(:iteration)) | parse_record }
|
24
|
+
|
25
|
+
rule(:expression) do
|
26
|
+
parse_tag(:open) >> iteration.as(:iteration) >> parse_tag(:close)
|
27
|
+
end
|
28
|
+
|
29
|
+
root :expression
|
30
|
+
|
31
|
+
def array_to_expression(array, name = nil)
|
32
|
+
initial_type = array.first.class
|
33
|
+
array.reduce do |expr, tag|
|
34
|
+
expr = str_to_expression(expr, name) if expr.is_a?(initial_type)
|
35
|
+
expr | str_to_expression(tag, name)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def str_to_expression(string, name)
|
40
|
+
return str(string) if name.nil?
|
41
|
+
|
42
|
+
str(string).as(name)
|
43
|
+
end
|
44
|
+
|
45
|
+
def parse_tag(opts)
|
46
|
+
tag = str("<")
|
47
|
+
tag = tag >> str("/") if opts == :close
|
48
|
+
tag = tag >> array_to_expression(Constants::TAGS, opts)
|
49
|
+
tag = tag >> attributes.as(:attributes) if opts == :open
|
50
|
+
tag >> str(">")
|
51
|
+
end
|
52
|
+
|
53
|
+
def attributes
|
54
|
+
(match["a-zA-Z"].repeat.as(:name) >>
|
55
|
+
str("=") >> quoted_string).repeat
|
56
|
+
end
|
57
|
+
|
58
|
+
def quoted_string
|
59
|
+
(str('"') >> match("[^\"]").repeat.as(:value) >> str('"')) |
|
60
|
+
(str("'") >> match("[^\']").repeat.as(:value) >> str("'"))
|
61
|
+
end
|
62
|
+
|
63
|
+
def parse_text_tag
|
64
|
+
str("<mtext>") >> match("[^<]").repeat.as(:quoted_text) >> str("</mtext>")
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,26 @@
|
|
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(/\s/, "")
|
13
|
+
end
|
14
|
+
|
15
|
+
def parse
|
16
|
+
tree_t = Plurimath::Mathml::Parse.new.parse(text)
|
17
|
+
tree_t = JSON.parse(tree_t.to_json, symbolize_names: true)
|
18
|
+
formula = Plurimath::Mathml::Transform.new.apply(tree_t)
|
19
|
+
formula = [formula] unless formula.is_a?(Array) || formula.nil?
|
20
|
+
return if formula.nil?
|
21
|
+
|
22
|
+
Plurimath::Math::Formula.new(formula)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|