plurimath 0.2.0 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/rake.yml +13 -0
- data/.github/workflows/release.yml +22 -0
- data/.gitignore +1 -0
- data/.hound.yml +5 -0
- data/.rubocop.yml +8 -0
- data/AsciiMath-Supported-Data.adoc +2000 -0
- data/Gemfile +3 -0
- data/Latex-Supported-Data.adoc +1879 -0
- data/MathML-Supported-Data.adoc +287 -0
- data/README.adoc +96 -0
- data/lib/plurimath/asciimath/constants.rb +267 -229
- data/lib/plurimath/asciimath/parse.rb +125 -26
- data/lib/plurimath/asciimath/parser.rb +6 -3
- data/lib/plurimath/asciimath/transform.rb +1135 -208
- data/lib/plurimath/asciimath.rb +1 -1
- 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 +1 -1
- data/lib/plurimath/latex/constants.rb +3729 -1906
- data/lib/plurimath/latex/parse.rb +167 -51
- data/lib/plurimath/latex/parser.rb +12 -4
- data/lib/plurimath/latex/transform.rb +598 -183
- data/lib/plurimath/math/base.rb +15 -0
- data/lib/plurimath/math/formula.rb +96 -11
- data/lib/plurimath/math/function/bar.rb +34 -0
- data/lib/plurimath/math/function/base.rb +32 -5
- data/lib/plurimath/math/function/binary_function.rb +104 -17
- data/lib/plurimath/math/function/cancel.rb +8 -0
- data/lib/plurimath/math/function/ceil.rb +3 -0
- data/lib/plurimath/math/function/color.rb +16 -6
- data/lib/plurimath/math/function/f.rb +8 -0
- data/lib/plurimath/math/function/fenced.rb +95 -3
- data/lib/plurimath/math/function/floor.rb +15 -0
- data/lib/plurimath/math/function/font_style/bold.rb +37 -0
- data/lib/plurimath/math/function/font_style/double_struck.rb +37 -0
- data/lib/plurimath/math/function/font_style/fraktur.rb +37 -0
- data/lib/plurimath/math/function/font_style/italic.rb +37 -0
- data/lib/plurimath/math/function/font_style/monospace.rb +37 -0
- data/lib/plurimath/math/function/font_style/normal.rb +37 -0
- data/lib/plurimath/math/function/font_style/sans-serif.rb +37 -0
- data/lib/plurimath/math/function/font_style/script.rb +37 -0
- data/lib/plurimath/math/function/font_style.rb +18 -25
- data/lib/plurimath/math/function/frac.rb +35 -5
- data/lib/plurimath/math/function/g.rb +7 -0
- data/lib/plurimath/math/function/hat.rb +12 -0
- data/lib/plurimath/math/function/inf.rb +21 -1
- data/lib/plurimath/math/function/int.rb +23 -2
- data/lib/plurimath/math/function/left.rb +25 -4
- data/lib/plurimath/math/function/lim.rb +40 -2
- data/lib/plurimath/math/function/limits.rb +8 -0
- data/lib/plurimath/math/function/log.rb +61 -4
- data/lib/plurimath/math/function/longdiv.rb +12 -0
- data/lib/plurimath/math/function/mbox.rb +31 -0
- data/lib/plurimath/math/function/menclose.rb +46 -0
- data/lib/plurimath/math/function/merror.rb +12 -0
- data/lib/plurimath/math/function/mod.rb +25 -4
- data/lib/plurimath/math/function/msgroup.rb +37 -0
- data/lib/plurimath/math/function/msline.rb +12 -0
- data/lib/plurimath/math/function/multiscript.rb +30 -0
- data/lib/plurimath/math/function/norm.rb +18 -1
- data/lib/plurimath/math/function/obrace.rb +17 -0
- data/lib/plurimath/math/function/oint.rb +2 -2
- data/lib/plurimath/math/function/over.rb +36 -0
- data/lib/plurimath/math/function/overset.rb +36 -7
- data/lib/plurimath/math/function/phantom.rb +28 -0
- data/lib/plurimath/math/function/power.rb +33 -9
- data/lib/plurimath/math/function/power_base.rb +110 -5
- data/lib/plurimath/math/function/prod.rb +29 -2
- data/lib/plurimath/math/function/right.rb +44 -0
- data/lib/plurimath/math/function/root.rb +27 -4
- data/lib/plurimath/math/function/rule.rb +33 -0
- data/lib/plurimath/math/function/scarries.rb +12 -0
- data/lib/plurimath/math/function/scarry.rb +12 -0
- data/lib/plurimath/math/function/sqrt.rb +24 -2
- data/lib/plurimath/math/function/stackrel.rb +27 -0
- data/lib/plurimath/math/function/substack.rb +7 -1
- data/lib/plurimath/math/function/sum.rb +56 -2
- data/lib/plurimath/math/function/sup.rb +3 -0
- data/lib/plurimath/math/function/table/align.rb +24 -0
- data/lib/plurimath/math/function/table/array.rb +44 -0
- data/lib/plurimath/math/function/table/bmatrix.rb +37 -0
- data/lib/plurimath/math/function/table/matrix.rb +32 -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 +24 -0
- data/lib/plurimath/math/function/table.rb +190 -20
- data/lib/plurimath/math/function/td.rb +27 -9
- data/lib/plurimath/math/function/ternary_function.rb +83 -8
- data/lib/plurimath/math/function/text.rb +45 -8
- data/lib/plurimath/math/function/tr.rb +28 -4
- data/lib/plurimath/math/function/ubrace.rb +17 -0
- data/lib/plurimath/math/function/ul.rb +29 -0
- data/lib/plurimath/math/function/unary_function.rb +85 -7
- data/lib/plurimath/math/function/underline.rb +12 -0
- data/lib/plurimath/math/function/underover.rb +107 -0
- data/lib/plurimath/math/function/underset.rb +39 -0
- data/lib/plurimath/math/function/vec.rb +10 -0
- data/lib/plurimath/math/function.rb +13 -2
- data/lib/plurimath/math/number.rb +11 -3
- data/lib/plurimath/math/symbol.rb +57 -9
- data/lib/plurimath/math/unicode.rb +11 -0
- data/lib/plurimath/math.rb +15 -6
- data/lib/plurimath/mathml/constants.rb +224 -179
- data/lib/plurimath/mathml/parser.rb +24 -7
- data/lib/plurimath/mathml/transform.rb +249 -148
- data/lib/plurimath/mathml.rb +1 -1
- data/lib/plurimath/omml/parser.rb +42 -0
- data/lib/plurimath/omml/transform.rb +278 -0
- data/lib/plurimath/omml.rb +1 -1
- data/lib/plurimath/unitsml.rb +4 -0
- data/lib/plurimath/utility.rb +395 -0
- data/lib/plurimath/version.rb +1 -1
- data/plurimath.gemspec +1 -0
- metadata +66 -9
- data/.github/workflows/test.yml +0 -36
- data/README.md +0 -40
- data/lib/plurimath/mathml/parse.rb +0 -63
@@ -3,192 +3,293 @@
|
|
3
3
|
module Plurimath
|
4
4
|
class Mathml
|
5
5
|
class Transform < Parslet::Transform
|
6
|
-
rule(
|
7
|
-
rule(
|
8
|
-
rule(
|
9
|
-
rule(
|
10
|
-
rule(
|
11
|
-
rule(
|
12
|
-
rule(
|
13
|
-
rule(
|
14
|
-
|
15
|
-
rule(
|
16
|
-
|
17
|
-
|
18
|
-
|
6
|
+
rule(mi: simple(:mi)) { mi }
|
7
|
+
rule(mo: simple(:mo)) { mo }
|
8
|
+
rule(mo: sequence(:mo)) { Utility.mathml_unary_classes(mo) }
|
9
|
+
rule(xref: simple(:xref)) { nil }
|
10
|
+
rule(mtd: sequence(:mtd)) { Math::Function::Td.new(mtd) }
|
11
|
+
rule(mtr: sequence(:mtr)) { Math::Function::Tr.new(mtr) }
|
12
|
+
rule(accent: simple(:acc)) { nil }
|
13
|
+
rule(none: sequence(:none)) { nil }
|
14
|
+
rule(maxsize: simple(:att)) { nil }
|
15
|
+
rule(minsize: simple(:att)) { nil }
|
16
|
+
rule(notation: simple(:att)) { Math::Function::Menclose.new(att) }
|
17
|
+
rule(mtable: simple(:table)) { table }
|
18
|
+
rule(msqrt: sequence(:sqrt)) { Math::Function::Sqrt.new(sqrt.first) }
|
19
|
+
rule(mstyle: simple(:mstyle)) { mstyle }
|
20
|
+
rule(msline: sequence(:line)) { Math::Function::Msline.new }
|
21
|
+
rule(value: sequence(:value)) { Utility.filter_values(value) }
|
22
|
+
|
23
|
+
rule(mspace: sequence(:space)) { nil }
|
24
|
+
rule(mstyle: sequence(:mstyle)) { Utility.filter_values(mstyle) }
|
25
|
+
rule(mfenced: simple(:mfenced)) { mfenced }
|
26
|
+
rule(mtable: sequence(:mtable)) { Math::Function::Table.new(mtable) }
|
27
|
+
rule(mscarry: sequence(:scarry)) { nil }
|
28
|
+
rule(displaystyle: simple(:att)) { nil }
|
29
|
+
rule(menclose: simple(:enclose)) { enclose }
|
30
|
+
rule(mlabeledtr: sequence(:mtr)) { Math::Function::Tr.new(mtr) }
|
31
|
+
rule(mpadded: sequence(:padded)) { Utility.filter_values(padded) }
|
32
|
+
rule(malignmark: sequence(:mark)) { nil }
|
33
|
+
rule(maligngroup: sequence(:att)) { nil }
|
34
|
+
rule(mprescripts: sequence(:att)) { "mprescripts" }
|
35
|
+
rule(columnlines: simple(:lines)) { lines }
|
36
|
+
|
37
|
+
rule(math: subtree(:math)) do
|
38
|
+
Utility.filter_values(
|
39
|
+
math.flatten.compact,
|
40
|
+
)
|
41
|
+
end
|
42
|
+
|
43
|
+
rule(mphantom: sequence(:phantom)) do
|
44
|
+
Math::Function::Phantom.new(phantom)
|
45
|
+
end
|
46
|
+
|
47
|
+
rule(mn: sequence(:mn)) do
|
48
|
+
Math::Number.new(
|
49
|
+
Utility.string_to_html_entity(
|
50
|
+
mn.join,
|
51
|
+
),
|
52
|
+
)
|
53
|
+
end
|
54
|
+
|
55
|
+
rule(mathvariant: simple(:variant)) do
|
56
|
+
Utility::FONT_STYLES[variant.to_sym]&.new(nil, variant)
|
57
|
+
end
|
58
|
+
|
59
|
+
rule(mi: sequence(:mi)) do
|
60
|
+
mi.any?(String) ? Utility.mathml_unary_classes(mi) : mi
|
61
|
+
end
|
62
|
+
|
63
|
+
rule(open: simple(:lparen)) do
|
64
|
+
Math::Function::Fenced.new(Math::Symbol.new(lparen))
|
65
|
+
end
|
66
|
+
|
67
|
+
rule(msgroup: sequence(:group)) do
|
68
|
+
if group.any?(String)
|
69
|
+
group.each_with_index do |object, ind|
|
70
|
+
group[ind] = Utility.text_classes(object) if object.is_a?(String)
|
71
|
+
end
|
19
72
|
end
|
20
|
-
|
73
|
+
Math::Function::Msgroup.new(group.flatten.compact)
|
74
|
+
end
|
75
|
+
|
76
|
+
rule(mlongdiv: sequence(:long)) do
|
77
|
+
Math::Function::Longdiv.new(long.flatten.compact)
|
21
78
|
end
|
22
79
|
|
23
|
-
rule(
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
80
|
+
rule(menclose: sequence(:close)) do
|
81
|
+
Math::Function::Menclose.new(
|
82
|
+
nil,
|
83
|
+
Utility.filter_values(close),
|
84
|
+
)
|
33
85
|
end
|
34
86
|
|
35
|
-
rule(
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
Plurimath::Math::Formula.new(tag + [sequence])
|
87
|
+
rule(mroot: sequence(:mroot)) do
|
88
|
+
Math::Function::Root.new(
|
89
|
+
mroot[1],
|
90
|
+
mroot[0],
|
91
|
+
)
|
41
92
|
end
|
42
93
|
|
43
|
-
rule(
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
[tag, sequence]
|
94
|
+
rule(merror: sequence(:merror)) do
|
95
|
+
Math::Function::Merror.new(
|
96
|
+
merror[0],
|
97
|
+
merror[1],
|
98
|
+
)
|
49
99
|
end
|
50
100
|
|
51
|
-
rule(
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
101
|
+
rule(mfrac: sequence(:mfrac)) do
|
102
|
+
Math::Function::Frac.new(
|
103
|
+
mfrac[0],
|
104
|
+
mfrac[1],
|
105
|
+
)
|
56
106
|
end
|
57
107
|
|
58
|
-
rule(
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
108
|
+
rule(mfraction: sequence(:mfrac)) do
|
109
|
+
Math::Function::Frac.new(
|
110
|
+
mfrac[0],
|
111
|
+
mfrac[1],
|
112
|
+
)
|
63
113
|
end
|
64
114
|
|
65
|
-
rule(
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
115
|
+
rule(msub: sequence(:msub)) do
|
116
|
+
Math::Function::Base.new(
|
117
|
+
msub[0],
|
118
|
+
msub[1],
|
119
|
+
)
|
70
120
|
end
|
71
121
|
|
72
|
-
rule(
|
73
|
-
|
74
|
-
|
75
|
-
[
|
76
|
-
|
77
|
-
|
122
|
+
rule(msup: sequence(:msup)) do
|
123
|
+
Math::Function::Power.new(
|
124
|
+
msup[0],
|
125
|
+
msup[1],
|
126
|
+
)
|
127
|
+
end
|
128
|
+
|
129
|
+
rule(msubsup: sequence(:msubsup)) do
|
130
|
+
Math::Function::PowerBase.new(
|
131
|
+
msubsup[0],
|
132
|
+
msubsup[1],
|
133
|
+
msubsup[2],
|
134
|
+
)
|
135
|
+
end
|
136
|
+
|
137
|
+
rule(munderover: sequence(:function)) do
|
138
|
+
binary_function = Plurimath::Math::Function::BinaryFunction
|
139
|
+
base_class = function[0]&.value&.first if function[0].is_a?(Math::Formula)
|
140
|
+
if base_class&.class&.ancestors&.include?(binary_function)
|
141
|
+
base_class.parameter_one = function[1]
|
142
|
+
base_class.parameter_two = function[2]
|
143
|
+
base_class
|
78
144
|
else
|
79
|
-
|
145
|
+
Math::Function::Underover.new(
|
146
|
+
function[0],
|
147
|
+
function[1],
|
148
|
+
function[2],
|
149
|
+
)
|
80
150
|
end
|
81
151
|
end
|
82
152
|
|
83
|
-
rule(
|
84
|
-
if
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
elsif name.to_s == "mathvariant"
|
89
|
-
value.to_s
|
153
|
+
rule(mrow: subtree(:mrow)) do
|
154
|
+
if mrow.any?(String)
|
155
|
+
mrow.each_with_index do |object, ind|
|
156
|
+
mrow[ind] = Utility.mathml_unary_classes([object]) if object.is_a?(String)
|
157
|
+
end
|
90
158
|
end
|
159
|
+
Math::Formula.new(mrow.flatten.compact)
|
91
160
|
end
|
92
161
|
|
93
|
-
rule(
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
) do
|
98
|
-
|
99
|
-
|
100
|
-
|
162
|
+
rule(msrow: sequence(:msrow)) do
|
163
|
+
Math::Formula.new(msrow.flatten.compact)
|
164
|
+
end
|
165
|
+
|
166
|
+
rule(mstack: sequence(:stack)) do
|
167
|
+
Math::Function::Stackrel.new(
|
168
|
+
Utility.filter_values(stack),
|
169
|
+
)
|
170
|
+
end
|
171
|
+
|
172
|
+
rule(mover: sequence(:mover)) do
|
173
|
+
if ["ubrace", "obrace"].any?(mover.last.class_name)
|
174
|
+
mover.last.parameter_one = mover.shift if mover.length > 1
|
175
|
+
mover.last
|
101
176
|
else
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
177
|
+
Math::Function::Overset.new(
|
178
|
+
mover[1],
|
179
|
+
mover[0],
|
180
|
+
)
|
106
181
|
end
|
107
182
|
end
|
108
183
|
|
109
|
-
rule(
|
110
|
-
|
111
|
-
|
112
|
-
|
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)
|
184
|
+
rule(munder: sequence(:munder)) do
|
185
|
+
if munder.any?(String)
|
186
|
+
munder.each_with_index do |object, ind|
|
187
|
+
munder[ind] = Utility.mathml_unary_classes([object]) if object.is_a?(String)
|
162
188
|
end
|
189
|
+
end
|
190
|
+
if ["ubrace", "obrace"].any?(munder.last.class_name)
|
191
|
+
munder.last.parameter_one = munder.shift if munder.length > 1
|
192
|
+
munder.last
|
163
193
|
else
|
164
|
-
|
194
|
+
Math::Function::Underset.new(
|
195
|
+
munder[1],
|
196
|
+
munder[0],
|
197
|
+
)
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
rule(mscarries: sequence(:scarries)) do
|
202
|
+
Math::Function::Scarries.new(
|
203
|
+
Utility.filter_values(scarries),
|
204
|
+
)
|
205
|
+
end
|
206
|
+
|
207
|
+
rule(mtext: sequence(:mtext)) do
|
208
|
+
entities = HTMLEntities.new
|
209
|
+
symbols = Constants::UNICODE_SYMBOLS.transform_keys(&:to_s)
|
210
|
+
text = entities.encode(mtext.first, :hexadecimal)
|
211
|
+
symbols.each do |code, string|
|
212
|
+
text.gsub!(code.downcase, "unicode[:#{string}]")
|
213
|
+
end
|
214
|
+
Math::Function::Text.new(text)
|
215
|
+
end
|
216
|
+
|
217
|
+
rule(ms: sequence(:ms)) do
|
218
|
+
entities = HTMLEntities.new
|
219
|
+
symbols = Constants::UNICODE_SYMBOLS.transform_keys(&:to_s)
|
220
|
+
text = entities.encode(ms.first, :hexadecimal)
|
221
|
+
symbols.each do |code, string|
|
222
|
+
text.gsub!(code.downcase, "unicode[:#{string}]")
|
165
223
|
end
|
224
|
+
Math::Function::Text.new(text)
|
225
|
+
end
|
226
|
+
|
227
|
+
rule(mfenced: sequence(:fenced)) do
|
228
|
+
Math::Function::Fenced.new(
|
229
|
+
Math::Symbol.new("("),
|
230
|
+
fenced,
|
231
|
+
Math::Symbol.new(")"),
|
232
|
+
)
|
166
233
|
end
|
167
234
|
|
168
|
-
rule(
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
235
|
+
rule(mmultiscripts: subtree(:script)) do
|
236
|
+
multi = Utility.multiscript(script.compact)
|
237
|
+
prescripts = multi[1]
|
238
|
+
Math::Function::Multiscript.new(
|
239
|
+
multi[0],
|
240
|
+
(prescripts[0] if prescripts),
|
241
|
+
(prescripts[1] if prescripts),
|
242
|
+
)
|
243
|
+
end
|
244
|
+
|
245
|
+
rule(mathcolor: simple(:color)) do
|
246
|
+
Math::Function::Color.new(
|
247
|
+
Math::Function::Text.new(color),
|
248
|
+
)
|
249
|
+
end
|
250
|
+
|
251
|
+
rule(open: simple(:lparen),
|
252
|
+
close: simple(:rparen)) do
|
253
|
+
Math::Function::Fenced.new(
|
254
|
+
Math::Symbol.new(lparen),
|
255
|
+
nil,
|
256
|
+
Math::Symbol.new(rparen),
|
257
|
+
)
|
258
|
+
end
|
175
259
|
|
176
|
-
|
177
|
-
|
260
|
+
rule(mathcolor: simple(:color),
|
261
|
+
mathvariant: simple(:variant)) do
|
262
|
+
variant_class = Utility::FONT_STYLES[variant.to_sym]
|
263
|
+
if variant_class
|
264
|
+
Math::Function::Color.new(
|
265
|
+
Math::Function::Text.new(
|
266
|
+
color,
|
267
|
+
),
|
268
|
+
variant_class.new(
|
269
|
+
nil,
|
270
|
+
variant,
|
271
|
+
),
|
272
|
+
)
|
178
273
|
else
|
179
|
-
|
274
|
+
Math::Function::Color.new(
|
275
|
+
Math::Function::Text.new(
|
276
|
+
color,
|
277
|
+
),
|
278
|
+
)
|
180
279
|
end
|
181
280
|
end
|
182
281
|
|
183
|
-
|
184
|
-
|
282
|
+
rule(attributes: simple(:attrs),
|
283
|
+
value: subtree(:value)) do
|
284
|
+
Utility.join_attr_value(attrs, value.flatten)
|
185
285
|
end
|
186
286
|
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
287
|
+
rule(attributes: subtree(:attrs),
|
288
|
+
value: sequence(:value)) do
|
289
|
+
Utility.join_attr_value(
|
290
|
+
attrs.is_a?(Hash) ? nil : attrs,
|
291
|
+
value.flatten,
|
292
|
+
)
|
192
293
|
end
|
193
294
|
end
|
194
295
|
end
|
data/lib/plurimath/mathml.rb
CHANGED
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "transform"
|
4
|
+
module Plurimath
|
5
|
+
class Omml
|
6
|
+
class Parser
|
7
|
+
attr_accessor :text
|
8
|
+
|
9
|
+
def initialize(text)
|
10
|
+
@text = text
|
11
|
+
end
|
12
|
+
|
13
|
+
def parse
|
14
|
+
nodes = Ox.load(text, strip_namespace: true)
|
15
|
+
@hash = { sequence: parse_nodes(nodes.nodes) }
|
16
|
+
nodes = JSON.parse(@hash.to_json, symbolize_names: true)
|
17
|
+
Math::Formula.new(
|
18
|
+
Transform.new.apply(
|
19
|
+
nodes,
|
20
|
+
),
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
def parse_nodes(nodes)
|
25
|
+
nodes.map do |node|
|
26
|
+
if node.is_a?(String)
|
27
|
+
node
|
28
|
+
elsif !node.attributes.empty?
|
29
|
+
{
|
30
|
+
node.name => {
|
31
|
+
attributes: node.attributes,
|
32
|
+
value: parse_nodes(node.nodes),
|
33
|
+
},
|
34
|
+
}
|
35
|
+
else
|
36
|
+
{ node.name => parse_nodes(node.nodes) }
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|