plurimath 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (87) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/test.yml +8 -11
  3. data/.gitignore +1 -0
  4. data/AsciiMath-Supported-Data.adoc +280 -0
  5. data/Gemfile +1 -0
  6. data/Latex-Supported-Data.adoc +1872 -0
  7. data/MathML-Supported-Data.adoc +270 -0
  8. data/README.adoc +94 -0
  9. data/lib/plurimath/asciimath/constants.rb +215 -222
  10. data/lib/plurimath/asciimath/parse.rb +45 -11
  11. data/lib/plurimath/asciimath/parser.rb +4 -3
  12. data/lib/plurimath/asciimath/transform.rb +222 -131
  13. data/lib/plurimath/asciimath.rb +1 -1
  14. data/lib/plurimath/html/constants.rb +50 -0
  15. data/lib/plurimath/html/parse.rb +149 -0
  16. data/lib/plurimath/html/parser.rb +26 -0
  17. data/lib/plurimath/html/transform.rb +363 -0
  18. data/lib/plurimath/html.rb +1 -1
  19. data/lib/plurimath/latex/constants.rb +1885 -1858
  20. data/lib/plurimath/latex/parse.rb +127 -34
  21. data/lib/plurimath/latex/parser.rb +5 -4
  22. data/lib/plurimath/latex/transform.rb +324 -164
  23. data/lib/plurimath/math/formula.rb +9 -1
  24. data/lib/plurimath/math/function/bar.rb +1 -1
  25. data/lib/plurimath/math/function/base.rb +7 -1
  26. data/lib/plurimath/math/function/binary_function.rb +10 -5
  27. data/lib/plurimath/math/function/color.rb +4 -4
  28. data/lib/plurimath/math/function/fenced.rb +7 -2
  29. data/lib/plurimath/math/function/font_style/bold.rb +18 -0
  30. data/lib/plurimath/math/function/font_style/double_struck.rb +18 -0
  31. data/lib/plurimath/math/function/font_style/fraktur.rb +18 -0
  32. data/lib/plurimath/math/function/font_style/monospace.rb +18 -0
  33. data/lib/plurimath/math/function/font_style/sans-serif.rb +18 -0
  34. data/lib/plurimath/math/function/font_style/script.rb +18 -0
  35. data/lib/plurimath/math/function/font_style.rb +2 -22
  36. data/lib/plurimath/math/function/frac.rb +4 -4
  37. data/lib/plurimath/math/function/inf.rb +0 -1
  38. data/lib/plurimath/math/function/left.rb +3 -6
  39. data/lib/plurimath/math/function/limits.rb +0 -1
  40. data/lib/plurimath/math/function/log.rb +6 -0
  41. data/lib/plurimath/math/function/mod.rb +6 -0
  42. data/lib/plurimath/math/function/multiscript.rb +11 -0
  43. data/lib/plurimath/math/function/norm.rb +2 -1
  44. data/lib/plurimath/math/function/over.rb +29 -0
  45. data/lib/plurimath/math/function/overset.rb +2 -2
  46. data/lib/plurimath/math/function/power.rb +7 -1
  47. data/lib/plurimath/math/function/power_base.rb +14 -7
  48. data/lib/plurimath/math/function/prod.rb +6 -0
  49. data/lib/plurimath/math/function/right.rb +24 -0
  50. data/lib/plurimath/math/function/root.rb +5 -4
  51. data/lib/plurimath/math/function/sqrt.rb +1 -1
  52. data/lib/plurimath/math/function/substack.rb +0 -1
  53. data/lib/plurimath/math/function/sum.rb +6 -0
  54. data/lib/plurimath/math/function/table/align.rb +24 -0
  55. data/lib/plurimath/math/function/table/array.rb +25 -0
  56. data/lib/plurimath/math/function/table/bmatrix.rb +26 -0
  57. data/lib/plurimath/math/function/table/matrix.rb +24 -0
  58. data/lib/plurimath/math/function/table/multline.rb +24 -0
  59. data/lib/plurimath/math/function/table/pmatrix.rb +24 -0
  60. data/lib/plurimath/math/function/table/split.rb +24 -0
  61. data/lib/plurimath/math/function/table/vmatrix.rb +25 -0
  62. data/lib/plurimath/math/function/table.rb +17 -5
  63. data/lib/plurimath/math/function/td.rb +6 -1
  64. data/lib/plurimath/math/function/ternary_function.rb +16 -6
  65. data/lib/plurimath/math/function/text.rb +19 -7
  66. data/lib/plurimath/math/function/tr.rb +6 -1
  67. data/lib/plurimath/math/function/unary_function.rb +5 -0
  68. data/lib/plurimath/math/function/vec.rb +4 -0
  69. data/lib/plurimath/math/function.rb +13 -2
  70. data/lib/plurimath/math/number.rb +8 -0
  71. data/lib/plurimath/math/symbol.rb +12 -3
  72. data/lib/plurimath/math.rb +9 -4
  73. data/lib/plurimath/mathml/constants.rb +2 -34
  74. data/lib/plurimath/mathml/parse.rb +7 -2
  75. data/lib/plurimath/mathml/parser.rb +2 -1
  76. data/lib/plurimath/mathml/transform.rb +73 -68
  77. data/lib/plurimath/mathml.rb +1 -1
  78. data/lib/plurimath/omml/constants.rb +154 -0
  79. data/lib/plurimath/omml/parser.rb +22 -0
  80. data/lib/plurimath/omml/transform.rb +216 -0
  81. data/lib/plurimath/omml.rb +1 -1
  82. data/lib/plurimath/unitsml.rb +4 -0
  83. data/lib/plurimath/utility.rb +73 -0
  84. data/lib/plurimath/version.rb +1 -1
  85. data/plurimath.gemspec +1 -0
  86. metadata +49 -7
  87. 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) { str("_") }
8
- rule(:power) { str("^") }
9
- rule(:slash) { str("\\") }
10
- rule(:double_slash) { str("\\\\").as("\\\\") }
11
- rule(:array_args) { str("{") >> expression >> str("}") }
12
- rule(:ending) { slash >> str("end") >> intermediate_exp }
13
- rule(:begining) { slash >> str("begin") >> intermediate_exp }
14
- rule(:lparen) { arr_to_expression(Constants::PARENTHESIS.keys, :lparen) }
15
- rule(:rparen) { arr_to_expression(Constants::PARENTHESIS.values, :rparen) }
16
- rule(:subscript) { intermediate_exp >> base >> intermediate_exp.as(:subscript) }
17
- rule(:supscript) { intermediate_exp >> power >> intermediate_exp.as(:supscript) }
18
- rule(:environment) { arr_to_expression(Constants::ENVIRONMENTS.keys, :environment) }
19
- rule(:binary) { slash >> arr_to_expression(Constants::BINARY_CLASSES, :binary) }
20
- rule(:under_over) { slash >> arr_to_expression(Constants::UNDEROVER_CLASSES, :binary) }
21
- rule(:left_right) { str("\\left") >> lparen >> expression >> str("\\right") >> rparen }
22
- rule(:math_operators) { slash >> arr_to_expression(Constants::MATH_OPERATORS, :unary_functions) >> str("\\limits") }
23
- rule(:unary) { slash >> arr_to_expression(Constants::UNARY_CLASSES, :unary) >> intermediate_exp.as(:first_value) }
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 >> arr_to_expression(Constants::SYMBOLS, :symbols)) |
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
- (slash >> arr_to_expression(Constants::FONT_STYLES, :fonts) >> expression.as(:intermediate_exp)) |
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)
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
- double_slash
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.as(:begining) >> array_args.as(:args) >> expression.as(:table_data) >> ending.as(:ending)).as(:environment) |
77
- (begining.as(:begining) >> expression.as(:table_data) >> ending.as(:ending)).as(:environment) |
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(:iteration) { (sequence.as(:sequence) >> expression.as(:expression)) | sequence }
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
- expression = str(expression).as(name || expression.to_sym) if expression.is_a?(String)
93
- expression | str(expr_string).as(name || expr_string.to_sym)
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
- rparen = Constants::PARENTHESIS[context.captures[:paren][:lparen].to_s]
100
- match("[^#{rparen}]").repeat.as(:text)
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(" ", "").gsub("\n", "")
12
+ @text = text.gsub(/\s/, "")
13
13
  end
14
14
 
15
15
  def parse
16
- tree_t = Plurimath::Latex::Parse.new.parse(text)
17
- formula = Plurimath::Latex::Transform.new.apply(tree_t)
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
- Plurimath::Math::Formula.new(formula)
21
+ Math::Formula.new(formula)
21
22
  end
22
23
  end
23
24
  end