plurimath 0.2.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 +8 -11
- data/.gitignore +1 -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 +215 -222
- data/lib/plurimath/asciimath/parse.rb +45 -11
- data/lib/plurimath/asciimath/parser.rb +4 -3
- data/lib/plurimath/asciimath/transform.rb +222 -131
- 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 +1885 -1858
- data/lib/plurimath/latex/parse.rb +127 -34
- data/lib/plurimath/latex/parser.rb +5 -4
- data/lib/plurimath/latex/transform.rb +324 -164
- data/lib/plurimath/math/formula.rb +9 -1
- data/lib/plurimath/math/function/bar.rb +1 -1
- data/lib/plurimath/math/function/base.rb +7 -1
- data/lib/plurimath/math/function/binary_function.rb +10 -5
- data/lib/plurimath/math/function/color.rb +4 -4
- data/lib/plurimath/math/function/fenced.rb +7 -2
- 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 +2 -22
- data/lib/plurimath/math/function/frac.rb +4 -4
- data/lib/plurimath/math/function/inf.rb +0 -1
- data/lib/plurimath/math/function/left.rb +3 -6
- data/lib/plurimath/math/function/limits.rb +0 -1
- data/lib/plurimath/math/function/log.rb +6 -0
- data/lib/plurimath/math/function/mod.rb +6 -0
- data/lib/plurimath/math/function/multiscript.rb +11 -0
- data/lib/plurimath/math/function/norm.rb +2 -1
- data/lib/plurimath/math/function/over.rb +29 -0
- data/lib/plurimath/math/function/overset.rb +2 -2
- data/lib/plurimath/math/function/power.rb +7 -1
- data/lib/plurimath/math/function/power_base.rb +14 -7
- data/lib/plurimath/math/function/prod.rb +6 -0
- data/lib/plurimath/math/function/right.rb +24 -0
- data/lib/plurimath/math/function/root.rb +5 -4
- data/lib/plurimath/math/function/sqrt.rb +1 -1
- data/lib/plurimath/math/function/substack.rb +0 -1
- data/lib/plurimath/math/function/sum.rb +6 -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 +17 -5
- data/lib/plurimath/math/function/td.rb +6 -1
- data/lib/plurimath/math/function/ternary_function.rb +16 -6
- data/lib/plurimath/math/function/text.rb +19 -7
- data/lib/plurimath/math/function/tr.rb +6 -1
- data/lib/plurimath/math/function/unary_function.rb +5 -0
- data/lib/plurimath/math/function/vec.rb +4 -0
- data/lib/plurimath/math/function.rb +13 -2
- data/lib/plurimath/math/number.rb +8 -0
- data/lib/plurimath/math/symbol.rb +12 -3
- data/lib/plurimath/math.rb +9 -4
- data/lib/plurimath/mathml/constants.rb +2 -34
- data/lib/plurimath/mathml/parse.rb +7 -2
- data/lib/plurimath/mathml/parser.rb +2 -1
- data/lib/plurimath/mathml/transform.rb +73 -68
- data/lib/plurimath/mathml.rb +1 -1
- 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 +1 -1
- data/lib/plurimath/unitsml.rb +4 -0
- data/lib/plurimath/utility.rb +73 -0
- data/lib/plurimath/version.rb +1 -1
- data/plurimath.gemspec +1 -0
- metadata +49 -7
- data/README.md +0 -40
@@ -4,46 +4,112 @@ 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(:
|
12
|
-
rule(:
|
13
|
-
|
14
|
-
rule(:
|
15
|
-
rule(:
|
16
|
-
|
17
|
-
rule(:
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
rule(:
|
22
|
-
|
23
|
-
|
7
|
+
rule(:base) { str("_") }
|
8
|
+
rule(:power) { str("^") }
|
9
|
+
rule(:slash) { str("\\") }
|
10
|
+
rule(:unary) { slash >> unary_classes }
|
11
|
+
rule(:ending) { (slash >> str("end") >> intermediate_exp).as(:ending) }
|
12
|
+
rule(:binary) { slash >> binary_classes }
|
13
|
+
|
14
|
+
rule(:array_args) { (str("{") >> expression >> str("}")).as(:args) }
|
15
|
+
rule(:under_over) { slash >> underover_classes }
|
16
|
+
|
17
|
+
rule(:begining) do
|
18
|
+
(slash >> str("begin") >> intermediate_exp).as(:begining)
|
19
|
+
end
|
20
|
+
|
21
|
+
rule(:symbols) do
|
22
|
+
arr_to_expression(Constants::SYMBOLS.keys, :symbols)
|
23
|
+
end
|
24
|
+
|
25
|
+
rule(:operators) do
|
26
|
+
arr_to_expression(Constants::OPERATORS, :operant)
|
27
|
+
end
|
28
|
+
|
29
|
+
rule(:numeric_values) do
|
30
|
+
arr_to_expression(Constants::NUMERIC_VALUES, :numeric_values)
|
31
|
+
end
|
32
|
+
|
33
|
+
rule(:unary_classes) do
|
34
|
+
arr_to_expression(Constants::UNARY_CLASSES, :unary)
|
35
|
+
end
|
36
|
+
|
37
|
+
rule(:binary_classes) do
|
38
|
+
arr_to_expression(Constants::BINARY_CLASSES, :binary)
|
39
|
+
end
|
40
|
+
|
41
|
+
rule(:underover_classes) do
|
42
|
+
arr_to_expression(Constants::UNDEROVER_CLASSES, :binary)
|
43
|
+
end
|
44
|
+
|
45
|
+
rule(:power_base_classes) do
|
46
|
+
arr_to_expression(Constants::POWER_BASE_CLASSES, :binary)
|
47
|
+
end
|
48
|
+
|
49
|
+
rule(:math_operators_classes) do
|
50
|
+
arr_to_expression(Constants::MATH_OPERATORS, :unary_functions)
|
51
|
+
end
|
52
|
+
|
53
|
+
rule(:lparen) do
|
54
|
+
arr_to_expression(Constants::PARENTHESIS.keys, :lparen)
|
55
|
+
end
|
56
|
+
|
57
|
+
rule(:rparen) do
|
58
|
+
arr_to_expression(Constants::PARENTHESIS.values, :rparen)
|
59
|
+
end
|
60
|
+
|
61
|
+
rule(:subscript) do
|
62
|
+
intermediate_exp >> base >> intermediate_exp.as(:subscript)
|
63
|
+
end
|
64
|
+
|
65
|
+
rule(:supscript) do
|
66
|
+
intermediate_exp >> power >> intermediate_exp.as(:supscript)
|
67
|
+
end
|
68
|
+
|
69
|
+
rule(:environment) do
|
70
|
+
arr_to_expression(Constants::MATRICES.keys, :environment)
|
71
|
+
end
|
72
|
+
|
73
|
+
rule(:fonts) do
|
74
|
+
arr_to_expression(Constants::FONT_STYLES, :fonts)
|
75
|
+
end
|
76
|
+
|
77
|
+
rule(:math_operators) do
|
78
|
+
slash >> math_operators_classes >> str("\\limits")
|
79
|
+
end
|
80
|
+
|
81
|
+
rule(:left_right) do
|
82
|
+
(str("\\left").as(:left) >> lparen >> expression.as(:expression) >> str("\\right").as(:right) >> rparen)
|
83
|
+
end
|
84
|
+
|
85
|
+
rule(:sqrt_arg) do
|
86
|
+
str("[").as(:lparen) >> (expression | str("")) >> str("]").as(:rparen)
|
87
|
+
end
|
24
88
|
|
25
89
|
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)
|
90
|
+
(math_operators >> base >> intermediate_exp.as(:base) >> power >> intermediate_exp.as(:power)) |
|
91
|
+
(math_operators >> power >> intermediate_exp.as(:power) >> base >> intermediate_exp.as(:base))
|
28
92
|
end
|
29
93
|
|
30
94
|
rule(:symbol_class_commands) do
|
31
|
-
(slash >>
|
95
|
+
(slash >> symbols) |
|
96
|
+
(unary >> intermediate_exp.as(:first_value)).as(:unary_functions) |
|
32
97
|
unary.as(:unary_functions) |
|
33
98
|
binary |
|
34
99
|
under_over |
|
35
100
|
environment |
|
36
|
-
|
37
|
-
|
38
|
-
(slash >>
|
39
|
-
|
101
|
+
operators |
|
102
|
+
numeric_values |
|
103
|
+
(slash >> power_base_classes) |
|
104
|
+
(slash >> fonts >> (binary_functions | intermediate_exp).as(:intermediate_exp))
|
40
105
|
end
|
41
106
|
|
42
107
|
rule(:symbol_text_or_integer) do
|
43
108
|
symbol_class_commands |
|
44
109
|
match["a-zA-Z"].repeat(1).as(:text) |
|
110
|
+
(str('"') >> match("[^\"]").repeat >> str('"')).as(:text) |
|
45
111
|
match(/\d+(\.[0-9]+)|\d/).repeat(1).as(:number) |
|
46
|
-
|
112
|
+
str("\\\\").as("\\\\")
|
47
113
|
end
|
48
114
|
|
49
115
|
rule(:intermediate_exp) do
|
@@ -59,8 +125,6 @@ module Plurimath
|
|
59
125
|
subscript.as(:base)
|
60
126
|
end
|
61
127
|
|
62
|
-
rule(:sqrt_arg) { str("[").as(:lparen) >> (expression | str("")) >> str("]").as(:rparen) }
|
63
|
-
|
64
128
|
rule(:binary_functions) do
|
65
129
|
(slash >> str("sqrt").as(:root) >> sqrt_arg.as(:first_value) >> intermediate_exp.as(:second_value)).as(:binary) |
|
66
130
|
(slash >> str("sqrt").as(:sqrt) >> intermediate_exp.as(:intermediate_exp)).as(:binary) |
|
@@ -70,34 +134,63 @@ module Plurimath
|
|
70
134
|
|
71
135
|
rule(:sequence) do
|
72
136
|
limits.as(:limits) |
|
137
|
+
(left_right.as(:left_right) >> power >> intermediate_exp.as(:supscript)) |
|
138
|
+
(left_right.as(:left_right) >> base >> intermediate_exp.as(:subscript)) |
|
73
139
|
left_right.as(:left_right) |
|
140
|
+
(over_class >> power >> intermediate_exp.as(:supscript)) |
|
141
|
+
(over_class >> base >> intermediate_exp.as(:subscript)) |
|
142
|
+
over_class |
|
74
143
|
(slash >> str("mbox") >> lparen.capture(:paren) >> read_text >> rparen).as(:unary_functions) |
|
75
144
|
(slash >> str("substack").as(:substack) >> lparen >> expression.as(:substack_value) >> rparen) |
|
76
|
-
(begining
|
77
|
-
(begining
|
145
|
+
(begining >> array_args >> expression.as(:table_data) >> ending).as(:environment) |
|
146
|
+
(begining >> expression.as(:table_data) >> ending).as(:environment) |
|
78
147
|
(slash >> environment >> intermediate_exp).as(:table_data) |
|
79
148
|
power_base |
|
80
149
|
binary_functions |
|
81
150
|
intermediate_exp
|
82
151
|
end
|
83
152
|
|
84
|
-
rule(:
|
153
|
+
rule(:left_right_over) do
|
154
|
+
(str("\\left").as(:left) >>
|
155
|
+
lparen >>
|
156
|
+
expression.repeat.as(:dividend) >>
|
157
|
+
str("\\over") >>
|
158
|
+
expression.repeat.as(:divisor) >>
|
159
|
+
str("\\right").as(:right) >>
|
160
|
+
rparen)
|
161
|
+
end
|
162
|
+
|
163
|
+
rule(:over_class) do
|
164
|
+
(
|
165
|
+
(str("{") >> expression.repeat.as(:dividend) >> str("\\over") >> expression.repeat.as(:divisor) >> str("}")) |
|
166
|
+
(left_right_over.as(:left_right).as(:power) >> power >> intermediate_exp) |
|
167
|
+
(left_right_over.as(:left_right).as(:base) >> base >> intermediate_exp) |
|
168
|
+
left_right_over.as(:left_right)
|
169
|
+
).as(:over)
|
170
|
+
end
|
171
|
+
|
172
|
+
rule(:iteration) do
|
173
|
+
(sequence.as(:sequence) >> expression.as(:expression)) | sequence
|
174
|
+
end
|
85
175
|
|
86
176
|
rule(:expression) { (iteration >> expression) | iteration }
|
87
177
|
|
88
178
|
root :expression
|
89
179
|
|
90
180
|
def arr_to_expression(array, name = nil)
|
181
|
+
type = array.first.class
|
91
182
|
array.reduce do |expression, expr_string|
|
92
|
-
|
93
|
-
expression
|
183
|
+
name = name || expr_string.to_sym || expression.to_sym
|
184
|
+
expression = str(expression).as(name) if expression.is_a?(type)
|
185
|
+
expression | str(expr_string).as(name)
|
94
186
|
end
|
95
187
|
end
|
96
188
|
|
97
189
|
def read_text
|
98
190
|
dynamic do |_sour, context|
|
99
|
-
|
100
|
-
|
191
|
+
lparen = context.captures[:paren][:lparen].to_s
|
192
|
+
rparen = Constants::PARENTHESIS[lparen]
|
193
|
+
match("[^#{rparen}]").repeat.as(:mbox)
|
101
194
|
end
|
102
195
|
end
|
103
196
|
end
|
@@ -9,15 +9,16 @@ module Plurimath
|
|
9
9
|
attr_accessor :text
|
10
10
|
|
11
11
|
def initialize(text)
|
12
|
-
@text = text.gsub(
|
12
|
+
@text = text.gsub(/\s/, "")
|
13
13
|
end
|
14
14
|
|
15
15
|
def parse
|
16
|
-
tree_t =
|
17
|
-
|
16
|
+
tree_t = Latex::Parse.new.parse(text)
|
17
|
+
tree_t = JSON.parse(tree_t.to_json, symbolize_names: true)
|
18
|
+
formula = Latex::Transform.new.apply(tree_t)
|
18
19
|
formula = [formula] unless formula.is_a?(Array)
|
19
20
|
|
20
|
-
|
21
|
+
Math::Formula.new(formula)
|
21
22
|
end
|
22
23
|
end
|
23
24
|
end
|