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,17 +4,38 @@ require "parslet"
|
|
4
4
|
module Plurimath
|
5
5
|
class Asciimath
|
6
6
|
class Parse < Parslet::Parser
|
7
|
-
rule(:
|
7
|
+
rule(:td) { expression.as(:td) }
|
8
|
+
rule(:base) { str("_") }
|
9
|
+
rule(:power) { str("^") }
|
10
|
+
rule(:space) { match(/\s+/) }
|
11
|
+
rule(:comma) { (str(",") >> space.maybe) }
|
12
|
+
rule(:number) do
|
13
|
+
(match("[0-9]").repeat(1) >> str(".") >> match("[0-9]").repeat(1)).as(:number) |
|
14
|
+
match("[0-9]").repeat(1).as(:number) |
|
15
|
+
str(".").as(:symbol)
|
16
|
+
end
|
8
17
|
|
9
|
-
rule(:
|
18
|
+
rule(:controversial_symbols) { power_base | expression }
|
19
|
+
rule(:left_right_open_paren) { str("(") | str("[") }
|
20
|
+
rule(:left_right_close_paren) { str(")") | str("]") }
|
21
|
+
rule(:color_left_parenthesis) { str("(") | str("[") | str("{") }
|
22
|
+
rule(:color_right_parenthesis) { str(")") | str("]") | str("}") }
|
10
23
|
|
11
|
-
rule(:
|
24
|
+
rule(:binary_classes) do
|
25
|
+
arr_to_expression(Constants::BINARY_CLASSES, :binary_class)
|
26
|
+
end
|
12
27
|
|
13
|
-
rule(:
|
28
|
+
rule(:sub_sup_classes) do
|
29
|
+
arr_to_expression(Constants::SUB_SUP_CLASSES, :binary_class)
|
30
|
+
end
|
14
31
|
|
15
|
-
rule(:
|
32
|
+
rule(:open_table) do
|
33
|
+
arr_to_expression(Constants::TABLE_PARENTHESIS.keys, :table_left)
|
34
|
+
end
|
16
35
|
|
17
|
-
rule(:
|
36
|
+
rule(:close_table) do
|
37
|
+
arr_to_expression(Constants::TABLE_PARENTHESIS.values, :table_right)
|
38
|
+
end
|
18
39
|
|
19
40
|
rule(:lparen) do
|
20
41
|
Constants::PARENTHESIS.keys.reduce do |expression, parenthesis|
|
@@ -30,56 +51,134 @@ module Plurimath
|
|
30
51
|
end
|
31
52
|
end
|
32
53
|
|
54
|
+
rule(:left_right) do
|
55
|
+
(str("left") >> left_right_open_paren.as(:left) >> iteration.as(:left_right_value) >> str("right") >> left_right_close_paren.as(:right)) |
|
56
|
+
((table.as(:numerator) >> space.maybe >> match(/(?<!\/)\/(?!\/)/) >> space.maybe >> iteration.as(:denominator)).as(:frac) >> expression) |
|
57
|
+
(table.as(:table) >> expression.maybe)
|
58
|
+
end
|
59
|
+
|
33
60
|
rule(:quoted_text) do
|
34
61
|
str('"') >> match("[^\"]").repeat.as(:text) >> str('"')
|
35
62
|
end
|
36
63
|
|
37
64
|
rule(:symbol_text_or_integer) do
|
38
|
-
|
39
|
-
|
40
|
-
|
65
|
+
sub_sup_classes |
|
66
|
+
binary_classes |
|
67
|
+
hash_to_expression(Constants.precompile_constants) |
|
68
|
+
(match(/[0-9]/).as(:number) >> comma.as(:comma)).repeat(1).as(:comma_separated) |
|
41
69
|
quoted_text |
|
70
|
+
(str("d").as(:d) >> str("x").as(:x)).as(:intermediate_exp) |
|
42
71
|
match["a-zA-Z"].as(:symbol) |
|
43
|
-
match("
|
72
|
+
match(/[^\[{(\\\/@;:.,'"|\]})0-9a-zA-Z\-><$%^&*_=+!`~\s?]/).as(:symbol) |
|
73
|
+
number
|
74
|
+
end
|
75
|
+
|
76
|
+
rule(:power_base) do
|
77
|
+
(base >> space.maybe >> sequence.as(:base_value) >> power >> space.maybe >> sequence.as(:power_value)) |
|
78
|
+
(space.maybe >> base >> space.maybe >> sequence.as(:base_value)).as(:base) |
|
79
|
+
(space.maybe >> power >> space.maybe >> sequence.as(:power_value)).as(:power) |
|
80
|
+
(space.maybe >> base >> space.maybe >> power.as(:symbol).as(:base_value)).as(:base) |
|
81
|
+
(space.maybe >> power >> space.maybe >> base.as(:symbol).as(:power_value)).as(:power)
|
82
|
+
end
|
83
|
+
|
84
|
+
rule(:power_base_rules) do
|
85
|
+
(sub_sup_classes >> power_base).as(:power_base) |
|
86
|
+
(binary_classes >> space.maybe >> sequence.as(:base_value).maybe >> space.maybe >> sequence.as(:power_value).maybe).as(:power_base) |
|
87
|
+
(sequence.as(:power_base) >> power_base).as(:power_base)
|
88
|
+
end
|
89
|
+
|
90
|
+
rule(:table) do
|
91
|
+
(open_table.as(:table_left) >> tr >> close_table.as(:table_right)) |
|
92
|
+
(open_table.as(:table_left) >> tr >> str("}").as(:table_right)) |
|
93
|
+
(str("norm").as(:norm) >> open_table.as(:table_left) >> tr >> close_table.as(:table_right)) |
|
94
|
+
(str("{").as(:table_left) >> tr >> close_table.as(:table_right)) |
|
95
|
+
(str("|").as(:table_left) >> tr >> str("|").as(:table_right)) |
|
96
|
+
(str("left") >> left_right_open_paren.as(:left) >> tr >> str("right") >> left_right_close_paren.as(:right))
|
97
|
+
end
|
98
|
+
|
99
|
+
rule(:tr) do
|
100
|
+
((left_right_open_paren.as(:open_tr) >> td.as(:tds_list) >> left_right_close_paren).as(:table_row) >> comma >> tr.as(:expr)) |
|
101
|
+
(left_right_open_paren.as(:open_tr) >> td.as(:tds_list) >> left_right_close_paren).as(:table_row)
|
102
|
+
end
|
103
|
+
|
104
|
+
rule(:color_value) do
|
105
|
+
(color_left_parenthesis.capture(:paren).as(:lparen) >> expression.as(:rgb_color) >> color_right_parenthesis.maybe.as(:rparen)).as(:intermediate_exp) |
|
106
|
+
iteration
|
44
107
|
end
|
45
108
|
|
46
109
|
rule(:sequence) do
|
47
|
-
(lparen >> expression >> rparen).as(:intermediate_exp) |
|
48
|
-
(
|
49
|
-
(str("text") >> lparen.capture(:paren) >> read_text >> rparen) |
|
50
|
-
(unary_functions >> sequence).as(:unary) |
|
51
|
-
(font_style >> sequence).as(:fonts) |
|
110
|
+
(lparen.as(:lparen) >> space.maybe >> expression.maybe.as(:expr) >> space.maybe >> rparen.maybe.as(:rparen)).as(:intermediate_exp) |
|
111
|
+
(str("text") >> lparen.capture(:paren).as(:lparen) >> read_text.as(:text) >> rparen.maybe.as(:rparen)).as(:intermediate_exp) |
|
52
112
|
symbol_text_or_integer
|
53
113
|
end
|
54
114
|
|
115
|
+
rule(:frac) do
|
116
|
+
(sequence.as(:numerator) >> space.maybe >> match(/(?<!\/)\/(?!\/)/) >> space.maybe >> iteration.as(:denominator)).as(:frac) |
|
117
|
+
((power_base_rules | power_base).as(:numerator) >> match(/(?<!\/)\/(?!\/)/) >> iteration.as(:denominator)).as(:frac)
|
118
|
+
end
|
119
|
+
|
120
|
+
rule(:mod) do
|
121
|
+
(sequence.as(:dividend) >> space.maybe >> str("mod").as(:mod) >> space.maybe >> iteration.as(:divisor)).as(:mod) |
|
122
|
+
((power_base_rules >> power_base).as(:dividend) >> space.maybe >> str("mod").as(:mod) >> space.maybe >> iteration.as(:divisor)).as(:mod) |
|
123
|
+
(power_base_rules.as(:dividend) >> space.maybe >> str("mod").as(:mod) >> space.maybe >> iteration.as(:divisor)).as(:mod)
|
124
|
+
end
|
125
|
+
|
55
126
|
rule(:iteration) do
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
(sequence >>
|
127
|
+
table.as(:table) |
|
128
|
+
comma.as(:comma) |
|
129
|
+
mod |
|
130
|
+
(sequence.as(:sequence) >> space.maybe >> str("//").as(:symbol)) |
|
131
|
+
(str("color") >> color_value.as(:color) >> sequence.as(:color_value)) |
|
132
|
+
frac |
|
133
|
+
(power_base_rules >> power_base) |
|
134
|
+
power_base_rules |
|
60
135
|
sequence.as(:sequence) |
|
61
|
-
|
136
|
+
space
|
62
137
|
end
|
63
138
|
|
64
139
|
rule(:expression) do
|
65
|
-
|
66
|
-
(iteration >>
|
67
|
-
|
140
|
+
left_right.as(:left_right) |
|
141
|
+
(iteration >> space.maybe >> expression).as(:expr) |
|
142
|
+
(base.as(:symbol) >> expression.maybe).as(:expr) |
|
143
|
+
(power.as(:symbol) >> expression.maybe).as(:expr) |
|
144
|
+
str("") |
|
145
|
+
(rparen.as(:rparen) >> space.maybe >> controversial_symbols >> comma.as(:comma).maybe >> expression).repeat(1).as(:expr) |
|
146
|
+
(power.as(:symbol) >> space.maybe >> expression).as(:expr) |
|
147
|
+
comma.as(:comma).maybe
|
68
148
|
end
|
69
149
|
|
70
150
|
root :expression
|
71
151
|
|
72
152
|
def arr_to_expression(arr, name = nil)
|
153
|
+
type = arr.first.class
|
73
154
|
arr.reduce do |expression, expr_string|
|
74
|
-
expression = str(expression).as(name
|
75
|
-
expression | str(expr_string).as(name
|
155
|
+
expression = str(expression).as(name) if expression.is_a?(type)
|
156
|
+
expression | str(expr_string).as(name)
|
76
157
|
end
|
77
158
|
end
|
78
159
|
|
79
160
|
def read_text
|
80
161
|
dynamic do |_sour, context|
|
81
162
|
rparen = Constants::PARENTHESIS[context.captures[:paren].to_sym]
|
82
|
-
match("[^#{rparen}]").repeat
|
163
|
+
match("[^#{rparen}]").repeat
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def hash_to_expression(arr)
|
168
|
+
type = arr.first.class
|
169
|
+
@@expression ||= arr.reduce do |expression, expr_string|
|
170
|
+
expression = dynamic_parser_rules(expression) if expression.is_a?(type)
|
171
|
+
expression | dynamic_parser_rules(expr_string)
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
def dynamic_parser_rules(expr)
|
176
|
+
first_value = str(expr.first.to_s)
|
177
|
+
case expr.last
|
178
|
+
when :symbol then first_value.as(:symbol)
|
179
|
+
when :unary_class then (first_value.as(:unary_class) >> space.maybe >> sequence.maybe).as(:unary)
|
180
|
+
when :fonts then first_value.as(:fonts_class) >> space.maybe >> sequence.as(:fonts_value)
|
181
|
+
when :special_fonts then first_value.as(:bold_fonts)
|
83
182
|
end
|
84
183
|
end
|
85
184
|
end
|
@@ -10,14 +10,17 @@ module Plurimath
|
|
10
10
|
|
11
11
|
def initialize(text)
|
12
12
|
@text = text
|
13
|
+
&.gsub(/(\|:|:\|)/, "|")
|
14
|
+
&.gsub(/(\{:)/, "ℒ")
|
15
|
+
&.gsub(/(:\})/, "ℛ")
|
13
16
|
end
|
14
17
|
|
15
18
|
def parse
|
16
19
|
nodes = Parse.new.parse(text)
|
17
|
-
transformed_tree =
|
18
|
-
return transformed_tree if transformed_tree.is_a?(
|
20
|
+
transformed_tree = Transform.new.apply(nodes)
|
21
|
+
return transformed_tree if transformed_tree.is_a?(Math::Formula)
|
19
22
|
|
20
|
-
|
23
|
+
Math::Formula.new(transformed_tree)
|
21
24
|
end
|
22
25
|
end
|
23
26
|
end
|