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
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../table"
4
+
5
+ module Plurimath
6
+ module Math
7
+ module Function
8
+ class Table
9
+ class Pmatrix < Table
10
+ def initialize(parameter_one,
11
+ parameter_two = "(",
12
+ parameter_three = ")")
13
+ super
14
+ end
15
+
16
+ def to_latex
17
+ first_value = parameter_one&.map(&:to_latex)&.join("\\\\")
18
+ "\\begin{pmatrix}#{first_value}\\end{pmatrix}"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../table"
4
+
5
+ module Plurimath
6
+ module Math
7
+ module Function
8
+ class Table
9
+ class Split < Table
10
+ def initialize(parameter_one,
11
+ parameter_two = "[",
12
+ parameter_three = "]")
13
+ super
14
+ end
15
+
16
+ def to_latex
17
+ first_value = parameter_one&.map(&:to_latex)&.join("\\\\")
18
+ "\\begin{split}#{first_value}\\end{split}"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../table"
4
+
5
+ module Plurimath
6
+ module Math
7
+ module Function
8
+ class Table
9
+ class Vmatrix < Table
10
+ def initialize(parameter_one,
11
+ parameter_two = "|",
12
+ parameter_three = "|")
13
+ parameter_three = nil if parameter_two == "norm["
14
+ super
15
+ end
16
+
17
+ def to_latex
18
+ first_value = parameter_one&.map(&:to_latex)&.join("\\\\")
19
+ "\\begin{vmatrix}#{first_value}\\end{vmatrix}"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -6,6 +6,12 @@ module Plurimath
6
6
  module Math
7
7
  module Function
8
8
  class Table < TernaryFunction
9
+ def initialize(parameter_one = nil,
10
+ parameter_two = nil,
11
+ parameter_three = nil)
12
+ super
13
+ end
14
+
9
15
  def to_asciimath
10
16
  first_value = parameter_one.map(&:to_asciimath).join(",")
11
17
  second_value = parameter_two.nil? ? "[" : parameter_two
@@ -15,7 +21,8 @@ module Plurimath
15
21
 
16
22
  def to_mathml_without_math_tag
17
23
  table_value = parameter_one.map(&:to_mathml_without_math_tag).join
18
- if Latex::Constants::PARENTHESIS.key?(parameter_two) || parameter_two == "|"
24
+ parenthesis = Latex::Constants::PARENTHESIS
25
+ if parenthesis.key?(parameter_two) || parameter_two == "|"
19
26
  "<mfenced open='#{parameter_two}' close='#{parameter_three}'>"\
20
27
  "<mtable>#{table_value}</mtable></mfenced>"
21
28
  elsif parameter_two == "norm["
@@ -28,13 +35,18 @@ module Plurimath
28
35
  def to_latex
29
36
  first_value = parameter_one&.map(&:to_latex)&.join("\\\\")
30
37
  environment = latex_environment
31
- divider = "{#{parameter_three&.map(&:to_latex).join}}" if environment == "array"
32
- "\\begin{#{environment}}#{divider}#{first_value}\\end{#{environment}}"
38
+ "\\begin{#{environment}}#{first_value}\\end{#{environment}}"
33
39
  end
34
40
 
35
41
  def latex_environment
36
- matrices_hash = Latex::Constants::ENVIRONMENTS
37
- matrices_hash.invert[parameter_two] if matrices_hash.value?(parameter_two)
42
+ matrices_hash = Latex::Constants::MATRICES
43
+ matric_value = matrices_hash.value?(parameter_two)
44
+ matrices_hash.invert[parameter_two] if matric_value
45
+ end
46
+
47
+ def to_html
48
+ first_value = parameter_one.map(&:to_html).join
49
+ "<table>#{first_value}</table>"
38
50
  end
39
51
  end
40
52
  end
@@ -10,7 +10,7 @@ module Plurimath
10
10
  if parameter_one.length > 1
11
11
  "[#{parameter_one.map(&:to_asciimath).join(',')}]"
12
12
  else
13
- "#{parameter_one.map(&:to_asciimath).join(',')}"
13
+ parameter_one.map(&:to_asciimath).join(",")
14
14
  end
15
15
  end
16
16
 
@@ -21,6 +21,11 @@ module Plurimath
21
21
  def to_latex
22
22
  parameter_one.map(&:to_latex).join
23
23
  end
24
+
25
+ def to_html
26
+ first_value = parameter_one.map(&:to_html).join
27
+ "<td>#{first_value}</td>"
28
+ end
24
29
  end
25
30
  end
26
31
  end
@@ -6,14 +6,16 @@ module Plurimath
6
6
  class TernaryFunction
7
7
  attr_accessor :parameter_one, :parameter_two, :parameter_three
8
8
 
9
- def initialize(parameter_one = nil, parameter_two = nil, parameter_three = nil)
9
+ def initialize(parameter_one = nil,
10
+ parameter_two = nil,
11
+ parameter_three = nil)
10
12
  @parameter_one = parameter_one
11
13
  @parameter_two = parameter_two
12
14
  @parameter_three = parameter_three
13
15
  end
14
16
 
15
17
  def to_asciimath
16
- first_value = parameter_one.to_asciimath if parameter_one
18
+ first_value = parameter_one&.to_asciimath
17
19
  second_value = "_(#{parameter_two.to_asciimath})" if parameter_two
18
20
  third_value = "^(#{parameter_three.to_asciimath})" if parameter_three
19
21
  "#{first_value}#{second_value}#{third_value}"
@@ -27,10 +29,18 @@ module Plurimath
27
29
  end
28
30
 
29
31
  def to_mathml_without_math_tag
30
- first_value = parameter_one.to_mathml_without_math_tag if parameter_one
31
- second_value = parameter_two.to_mathml_without_math_tag if parameter_two
32
- third_value = parameter_three.to_mathml_without_math_tag if parameter_three
33
- "<m#{class_name}>#{first_value}#{second_value}#{third_value}</m#{class_name}>"
32
+ first_value = parameter_one&.to_mathml_without_math_tag
33
+ second_value = parameter_two&.to_mathml_without_math_tag
34
+ third_value = parameter_three&.to_mathml_without_math_tag
35
+ body_value = "#{first_value}#{second_value}#{third_value}"
36
+ "<m#{class_name}>#{body_value}</m#{class_name}>"
37
+ end
38
+
39
+ def to_html
40
+ first_value = "<i>#{parameter_one.to_html}</i>" if parameter_one
41
+ second_value = "<i>#{parameter_two.to_html}</i>" if parameter_two
42
+ third_value = "<i>#{parameter_three.to_html}</i>" if parameter_three
43
+ first_value + second_value + third_value
34
44
  end
35
45
 
36
46
  def class_name
@@ -7,15 +7,11 @@ module Plurimath
7
7
  module Function
8
8
  class Text < UnaryFunction
9
9
  def to_asciimath
10
- "\"#{parameter_one}\""
10
+ "\"#{parse_text('asciimath') || parameter_one}\""
11
11
  end
12
12
 
13
13
  def to_mathml_without_math_tag
14
- regex_exp = %r{unicode\[:(?<unicode>\w{1,})\]}
15
- parameter_one.gsub!(regex_exp) do |_text|
16
- symbol_value(Regexp.last_match[:unicode]).to_s
17
- end
18
- "<mtext>#{parameter_one}</mtext>"
14
+ "<mtext>#{parse_text('mathml') || parameter_one}</mtext>"
19
15
  end
20
16
 
21
17
  def symbol_value(unicode)
@@ -24,7 +20,23 @@ module Plurimath
24
20
  end
25
21
 
26
22
  def to_latex
27
- parameter_one
23
+ parse_text("latex") || parameter_one
24
+ end
25
+
26
+ def to_html
27
+ parse_text("html") || parameter_one
28
+ end
29
+
30
+ def parse_text(lang)
31
+ regex = %r{\\mbox\{(?<mbox>.{1,})\}|unicode\[:(?<unicode>\w{1,})\]}
32
+ parameter_one.gsub!(regex) do |_text|
33
+ last_match = Regexp.last_match
34
+ if ["mathml", "html"].include?(lang)
35
+ symbol_value(last_match[:unicode]) || last_match[:mbox]
36
+ else
37
+ last_match[:unicode] || last_match[:mbox]
38
+ end
39
+ end
28
40
  end
29
41
  end
30
42
  end
@@ -16,9 +16,14 @@ module Plurimath
16
16
 
17
17
  def to_latex
18
18
  parameter_one.reject do |td|
19
- td if td.is_a?(Symbol) && td.value == "|"
19
+ td if td.is_a?(Math::Symbol) && td.value == "|"
20
20
  end.map(&:to_latex).join("&")
21
21
  end
22
+
23
+ def to_html
24
+ first_value = parameter_one.map(&:to_html).join
25
+ "<tr>#{first_value}</tr>"
26
+ end
22
27
  end
23
28
  end
24
29
  end
@@ -32,6 +32,11 @@ module Plurimath
32
32
  "\\#{class_name}#{first_value}"
33
33
  end
34
34
 
35
+ def to_html
36
+ first_value = "<i>#{parameter_one.to_html}</i>" if parameter_one
37
+ "<i>#{class_name}</i>#{first_value}"
38
+ end
39
+
35
40
  def class_name
36
41
  self.class.name.split("::").last.downcase
37
42
  end
@@ -6,6 +6,10 @@ module Plurimath
6
6
  module Math
7
7
  module Function
8
8
  class Vec < UnaryFunction
9
+ def to_mathml_without_math_tag
10
+ first_value = parameter_one&.to_mathml_without_math_tag
11
+ "<mover>#{first_value}<mo>&#x20D1;</mo></mover>"
12
+ end
9
13
  end
10
14
  end
11
15
  end
@@ -1,3 +1,14 @@
1
- Dir[File.join(__dir__, "function/*.rb")].each do |file|
2
- require_relative file.gsub(".rb", "")
1
+ module Plurimath
2
+ module Math
3
+ module Function
4
+ end
5
+ end
6
+ end
7
+
8
+ # Include the first level files before the next
9
+ (
10
+ Dir.glob(File.join(__dir__, "function", "*.rb")) +
11
+ Dir.glob(File.join(__dir__, "function", "*", "*.rb"))
12
+ ).each do |file|
13
+ require file
3
14
  end
@@ -24,6 +24,14 @@ module Plurimath
24
24
  def to_latex
25
25
  value
26
26
  end
27
+
28
+ def to_html
29
+ value
30
+ end
31
+
32
+ def class_name
33
+ self.class.name.split("::").last.downcase
34
+ end
27
35
  end
28
36
  end
29
37
  end
@@ -14,7 +14,8 @@ module Plurimath
14
14
  end
15
15
 
16
16
  def to_asciimath
17
- value
17
+ symbol = Asciimath::Constants::SYMBOLS.invert[value.to_sym].to_s
18
+ symbol.empty? ? value : symbol
18
19
  end
19
20
 
20
21
  def to_mathml_without_math_tag
@@ -26,8 +27,16 @@ module Plurimath
26
27
  end
27
28
 
28
29
  def to_latex
29
- symbols = Latex::Constants::SYMBOLS
30
- symbols.include?(value) ? "\\#{value}" : value
30
+ symbols = Latex::Constants::SYMBOLS.invert
31
+ symbols.key?(value) ? "\\#{symbols[value]}" : value
32
+ end
33
+
34
+ def to_html
35
+ value
36
+ end
37
+
38
+ def class_name
39
+ self.class.name.split("::").last.downcase
31
40
  end
32
41
  end
33
42
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "ox"
3
4
  require_relative "unicode"
4
5
  require_relative "asciimath"
5
6
  require_relative "omml"
@@ -14,17 +15,21 @@ require_relative "math/symbol"
14
15
  require_relative "asciimath/parser"
15
16
  require_relative "mathml/parser"
16
17
  require_relative "latex/parser"
18
+ require_relative "html/parser"
19
+ require_relative "omml/parser"
20
+ require_relative "utility"
17
21
  module Plurimath
18
22
  module Math
19
23
  class Error < StandardError; end
20
24
 
21
25
  VALID_TYPES = {
22
- unicode: Unicode,
23
- asciimath: Asciimath,
24
26
  omml: Omml,
25
- mathml: Mathml,
26
27
  html: Html,
27
28
  latex: Latex,
29
+ mathml: Mathml,
30
+ unitsml: Unitsml,
31
+ unicode: Unicode,
32
+ asciimath: Asciimath,
28
33
  }.freeze
29
34
 
30
35
  def parse(text, type)
@@ -42,7 +47,7 @@ module Plurimath
42
47
  end
43
48
 
44
49
  def valid_type?(type)
45
- (type.is_a?(Symbol) || type.is_a?(String)) &&
50
+ (type.is_a?(::Symbol) || type.is_a?(String)) &&
46
51
  VALID_TYPES.key?(type.to_sym)
47
52
  end
48
53
 
@@ -283,45 +283,13 @@ module Plurimath
283
283
  log
284
284
  ].freeze
285
285
  FONT_CLASSES = %i[
286
- fraktur
286
+ double-struck
287
287
  sans-serif
288
288
  monospace
289
+ fraktur
289
290
  script
290
- double-struck
291
291
  bold
292
292
  ].freeze
293
- FONT_TYPES = {
294
- :bb=>"bold",
295
- :fr=>"fraktur",
296
- :cc=>"script",
297
- :sf=>"sans-serif",
298
- :tt=>"monospace",
299
- :mbf=>"bold",
300
- :mit=>"italic",
301
- :mtt=>"monospace",
302
- :Bbb=>"double-struck",
303
- :bbb=>"double-struck",
304
- :mscr=>"mathbb",
305
- :mfrak=>"fraktur",
306
- :msans=>"sans-serif",
307
- :mbfit=>"bold italic",
308
- :mathbf=>"bold",
309
- :mathit=>"italic",
310
- :mathds=>"mathbb",
311
- :mathrm=>"mathrm",
312
- :mathtt=>"monospace",
313
- :mathsf=>"sans-serif",
314
- :mathbb=>"double-struck",
315
- :mbfsans=>"bold sans-serif",
316
- :mathcal=>"script",
317
- :mathbold=>"bold",
318
- :mathfrak=>"fraktur",
319
- :mathbfit=>"bold italic",
320
- :mathsfbf=>"sans-serif bold",
321
- :mathsfit=>"sans-serif italic",
322
- :mbfitsans=>"bold italic sans-serif",
323
- :mathsfbfit=>"sans-serif bold italic",
324
- }.freeze
325
293
  end
326
294
  end
327
295
  end
@@ -13,13 +13,18 @@ module Plurimath
13
13
  str("")
14
14
  end
15
15
 
16
- rule(:tag) { (parse_tag(:open) >> iteration.as(:iteration) >> parse_tag(:close)).as(:tag) | parse_text_tag.as(:tag) }
16
+ rule(:tag) do
17
+ (parse_tag(:open) >> iteration.as(:iteration) >> parse_tag(:close)).as(:tag) |
18
+ parse_text_tag.as(:tag)
19
+ end
17
20
 
18
21
  rule(:sequence) { (tag >> sequence.as(:sequence)) | tag }
19
22
 
20
23
  rule(:iteration) { (sequence >> iteration.as(:iteration)) | parse_record }
21
24
 
22
- rule(:expression) { parse_tag(:open) >> iteration.as(:iteration) >> parse_tag(:close) }
25
+ rule(:expression) do
26
+ parse_tag(:open) >> iteration.as(:iteration) >> parse_tag(:close)
27
+ end
23
28
 
24
29
  root :expression
25
30
 
@@ -9,11 +9,12 @@ module Plurimath
9
9
  attr_accessor :text
10
10
 
11
11
  def initialize(text)
12
- @text = text.gsub("\n", "").gsub(" ", "")
12
+ @text = text.gsub(/\s/, "")
13
13
  end
14
14
 
15
15
  def parse
16
16
  tree_t = Plurimath::Mathml::Parse.new.parse(text)
17
+ tree_t = JSON.parse(tree_t.to_json, symbolize_names: true)
17
18
  formula = Plurimath::Mathml::Transform.new.apply(tree_t)
18
19
  formula = [formula] unless formula.is_a?(Array) || formula.nil?
19
20
  return if formula.nil?