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
@@ -4,51 +4,92 @@ require "parslet"
|
|
4
4
|
module Plurimath
|
5
5
|
class Latex
|
6
6
|
class Parse < Parslet::Parser
|
7
|
-
rule(:base)
|
8
|
-
rule(:power)
|
9
|
-
rule(:slash)
|
10
|
-
rule(:
|
11
|
-
rule(:array_args)
|
12
|
-
|
13
|
-
rule(:
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
rule(:
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
rule(:
|
22
|
-
|
23
|
-
|
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
|
-
(
|
32
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
(
|
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
|
-
(
|
66
|
-
(slash >> str("sqrt").as(:
|
67
|
-
(
|
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("
|
75
|
-
(
|
76
|
-
(begining
|
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
|
-
|
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(:
|
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)
|
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
|
91
|
-
|
92
|
-
|
93
|
-
|
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
|
98
|
-
|
99
|
-
|
100
|
-
|
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
|
-
|
12
|
+
enti = HTMLEntities.new
|
13
|
+
text = enti.encode(enti.decode(text), :hexadecimal)
|
14
|
+
text = text
|
15
|
+
.gsub(/((?<!\\) )|\n+/, "")
|
16
|
+
.gsub(/\\\\ /, "\\\\\\\\")
|
17
|
+
.gsub(/&/, "&")
|
18
|
+
.gsub(/"/, "\"")
|
19
|
+
.gsub(/
/, "")
|
20
|
+
@text = text
|
13
21
|
end
|
14
22
|
|
15
23
|
def parse
|
16
|
-
tree_t =
|
17
|
-
formula =
|
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
|
-
|
28
|
+
Math::Formula.new(formula)
|
21
29
|
end
|
22
30
|
end
|
23
31
|
end
|