plurimath 0.5.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +4 -0
  3. data/.rspec +3 -0
  4. data/.rspec-opal +11 -0
  5. data/Gemfile +4 -0
  6. data/Rakefile +11 -0
  7. data/lib/plurimath/asciimath/parse.rb +3 -2
  8. data/lib/plurimath/asciimath/transform.rb +47 -5
  9. data/lib/plurimath/latex/constants.rb +5 -1
  10. data/lib/plurimath/latex/parse.rb +20 -11
  11. data/lib/plurimath/latex/transform.rb +32 -14
  12. data/lib/plurimath/math/core.rb +212 -10
  13. data/lib/plurimath/math/formula.rb +154 -40
  14. data/lib/plurimath/math/function/abs.rb +28 -15
  15. data/lib/plurimath/math/function/bar.rb +5 -0
  16. data/lib/plurimath/math/function/base.rb +22 -2
  17. data/lib/plurimath/math/function/binary_function.rb +38 -12
  18. data/lib/plurimath/math/function/ceil.rb +24 -9
  19. data/lib/plurimath/math/function/color.rb +14 -0
  20. data/lib/plurimath/math/function/ddot.rb +6 -1
  21. data/lib/plurimath/math/function/deg.rb +4 -1
  22. data/lib/plurimath/math/function/det.rb +4 -1
  23. data/lib/plurimath/math/function/dim.rb +4 -1
  24. data/lib/plurimath/math/function/dot.rb +5 -0
  25. data/lib/plurimath/math/function/exp.rb +4 -1
  26. data/lib/plurimath/math/function/fenced.rb +51 -15
  27. data/lib/plurimath/math/function/floor.rb +24 -14
  28. data/lib/plurimath/math/function/font_style.rb +63 -8
  29. data/lib/plurimath/math/function/frac.rb +38 -18
  30. data/lib/plurimath/math/function/gcd.rb +4 -1
  31. data/lib/plurimath/math/function/glb.rb +8 -1
  32. data/lib/plurimath/math/function/hat.rb +16 -5
  33. data/lib/plurimath/math/function/hom.rb +4 -1
  34. data/lib/plurimath/math/function/inf.rb +16 -0
  35. data/lib/plurimath/math/function/int.rb +35 -9
  36. data/lib/plurimath/math/function/ker.rb +4 -1
  37. data/lib/plurimath/math/function/lcm.rb +17 -1
  38. data/lib/plurimath/math/function/left.rb +20 -2
  39. data/lib/plurimath/math/function/lg.rb +4 -1
  40. data/lib/plurimath/math/function/lim.rb +16 -0
  41. data/lib/plurimath/math/function/liminf.rb +4 -1
  42. data/lib/plurimath/math/function/limits.rb +29 -9
  43. data/lib/plurimath/math/function/limsup.rb +4 -1
  44. data/lib/plurimath/math/function/linebreak.rb +95 -0
  45. data/lib/plurimath/math/function/ln.rb +4 -1
  46. data/lib/plurimath/math/function/log.rb +27 -12
  47. data/lib/plurimath/math/function/longdiv.rb +19 -0
  48. data/lib/plurimath/math/function/lub.rb +4 -1
  49. data/lib/plurimath/math/function/max.rb +4 -1
  50. data/lib/plurimath/math/function/mbox.rb +8 -7
  51. data/lib/plurimath/math/function/menclose.rb +6 -0
  52. data/lib/plurimath/math/function/merror.rb +12 -3
  53. data/lib/plurimath/math/function/min.rb +4 -1
  54. data/lib/plurimath/math/function/mod.rb +48 -15
  55. data/lib/plurimath/math/function/msgroup.rb +32 -0
  56. data/lib/plurimath/math/function/msline.rb +10 -0
  57. data/lib/plurimath/math/function/multiscript.rb +27 -0
  58. data/lib/plurimath/math/function/nary.rb +128 -0
  59. data/lib/plurimath/math/function/norm.rb +24 -15
  60. data/lib/plurimath/math/function/obrace.rb +5 -0
  61. data/lib/plurimath/math/function/oint.rb +48 -20
  62. data/lib/plurimath/math/function/over.rb +29 -10
  63. data/lib/plurimath/math/function/overset.rb +19 -10
  64. data/lib/plurimath/math/function/phantom.rb +24 -6
  65. data/lib/plurimath/math/function/power.rb +19 -4
  66. data/lib/plurimath/math/function/power_base.rb +34 -33
  67. data/lib/plurimath/math/function/prod.rb +43 -23
  68. data/lib/plurimath/math/function/right.rb +20 -2
  69. data/lib/plurimath/math/function/root.rb +6 -0
  70. data/lib/plurimath/math/function/rule.rb +8 -1
  71. data/lib/plurimath/math/function/scarries.rb +14 -0
  72. data/lib/plurimath/math/function/semantics.rb +15 -0
  73. data/lib/plurimath/math/function/sqrt.rb +5 -0
  74. data/lib/plurimath/math/function/stackrel.rb +14 -0
  75. data/lib/plurimath/math/function/substack.rb +18 -51
  76. data/lib/plurimath/math/function/sum.rb +36 -12
  77. data/lib/plurimath/math/function/sup.rb +11 -2
  78. data/lib/plurimath/math/function/table/array.rb +1 -1
  79. data/lib/plurimath/math/function/table/matrix.rb +4 -0
  80. data/lib/plurimath/math/function/table.rb +62 -26
  81. data/lib/plurimath/math/function/td.rb +42 -4
  82. data/lib/plurimath/math/function/ternary_function.rb +48 -10
  83. data/lib/plurimath/math/function/text.rb +25 -3
  84. data/lib/plurimath/math/function/tilde.rb +5 -0
  85. data/lib/plurimath/math/function/tr.rb +29 -1
  86. data/lib/plurimath/math/function/ubrace.rb +5 -0
  87. data/lib/plurimath/math/function/unary_function.rb +115 -29
  88. data/lib/plurimath/math/function/underover.rb +27 -58
  89. data/lib/plurimath/math/function/underset.rb +25 -9
  90. data/lib/plurimath/math/function/vec.rb +45 -0
  91. data/lib/plurimath/math/function.rb +7 -5
  92. data/lib/plurimath/math/number.rb +12 -8
  93. data/lib/plurimath/math/symbol.rb +25 -13
  94. data/lib/plurimath/math.rb +1 -3
  95. data/lib/plurimath/mathml/parser.rb +6 -4
  96. data/lib/plurimath/mathml/transform.rb +26 -19
  97. data/lib/plurimath/omml/parser.rb +3 -3
  98. data/lib/plurimath/omml/transform.rb +34 -21
  99. data/lib/plurimath/setup/oga.rb +5 -0
  100. data/lib/plurimath/setup/opal.rb.erb +8 -0
  101. data/lib/plurimath/setup/ox.rb +5 -0
  102. data/lib/plurimath/unitsml.rb +2 -1
  103. data/lib/plurimath/utility.rb +77 -41
  104. data/lib/plurimath/version.rb +1 -1
  105. data/lib/plurimath/xml_engine/oga.rb +246 -0
  106. data/lib/plurimath/xml_engine/ox.rb +29 -0
  107. data/lib/plurimath/xml_engine.rb +6 -0
  108. data/lib/plurimath.rb +12 -2
  109. metadata +12 -3
  110. data/lib/plurimath/math/function/scarry.rb +0 -12
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6fabed12e1b85fa2c5d7b2cacfebaf2bafb8481025f6f0a9b072649ff2991131
4
- data.tar.gz: ec912de1e4e356af0d7458c0b2147ea9aee1fb4676506429ac5e4161cd974c79
3
+ metadata.gz: f58b20082cd94488f247f22399522d777ea751a4eba4f21e45a7a792fc6d30f3
4
+ data.tar.gz: f6e55fa7493ac10ac6e8ded470ea547640b3b5b53f00ac36c8cf941d56d5cc87
5
5
  SHA512:
6
- metadata.gz: d31d2f5b644402ab58c0f19a9c14e88f91fff26d44dff6c89484b66d45d3760087be0827002c87c3b9ca540ac7dd52ad2ceb014f4b0f6b16fc9f94630f823c70
7
- data.tar.gz: 8f5ea828f70451af6ae075d8e06db4055145e096c83fc39b9a31cdcc57ddfff57afdd736e9f696d83e68d7b6c50ada74fa68f46acc00e878e08c1db22050ed2d
6
+ metadata.gz: 133860ffcca6250cb3bef1e9eb186536685b3697a7d26bcda1d287170d74b9ac98083d352143cc6a9fc3649832229c0e1c3c1963c3e787db4d8b6f992b3f5802
7
+ data.tar.gz: 3a2044197aa65f0820961c14a602ca6df69c316e48203b2d448263032837336053715d95c52290b5241486426d000588d632cd820563ddf6466b0f4ab5d46569
data/.gitignore CHANGED
@@ -6,7 +6,11 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ /Gemfile.lock
9
10
 
10
11
  # rspec failure tracking
11
12
  .rspec_status
12
13
  /coverage/
14
+
15
+ # temp files
16
+ *.kate-swp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rspec-opal ADDED
@@ -0,0 +1,11 @@
1
+ --default-path=spec
2
+ -I lib
3
+ -q oga/setup_opal
4
+ -q ll/setup_opal
5
+ --opal-opt=-g,htmlentities
6
+ --opal-opt=-g,equivalent-xml
7
+ --opal-opt=-g,parslet
8
+ --opal-opt=-g,oga
9
+ --format documentation
10
+ --color
11
+ --require spec_helper
data/Gemfile CHANGED
@@ -8,3 +8,7 @@ gem "rspec", "~> 3.0"
8
8
  gem 'simplecov', require: false, group: :test
9
9
  gem 'htmlentities'
10
10
  gem 'equivalent-xml'
11
+ gem 'opal-rspec', "~> 1.1.0a"
12
+ gem 'oga'
13
+ gem 'ox'
14
+ gem "unitsml"
data/Rakefile CHANGED
@@ -1,6 +1,17 @@
1
+ require "erb"
1
2
  require "bundler/gem_tasks"
2
3
  require "rspec/core/rake_task"
4
+ require 'opal/rspec/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
8
+ # Opal testing support
9
+ begin
10
+ Opal::RSpec::RakeTask.new(:"spec-opal")
11
+ rescue LoadError
12
+ # Likely the dependencies haven't been upstreamed yet. Ensure you
13
+ # run those tests via the `plurimath-js` repo's `env/plurimath`
14
+ # script.
15
+ end
16
+
6
17
  task :default => :spec
@@ -69,7 +69,8 @@ module Plurimath
69
69
  end
70
70
 
71
71
  rule(:quoted_text) do
72
- (str('"') >> match("[^\"]").repeat.as(:text) >> str('"')) |
72
+ (str('"') >> str("unitsml(") >> match("[^\)\"]").repeat.as(:unitsml) >> str(')"')) |
73
+ (str('"') >> match("[^\"]").repeat.as(:text) >> str('"')) |
73
74
  (str('"') >> str("").as(:text))
74
75
  end
75
76
 
@@ -188,7 +189,7 @@ module Plurimath
188
189
  def dynamic_parser_rules(expr)
189
190
  first_value = str(expr.first.to_s)
190
191
  case expr.last
191
- when :symbol then first_value.as(:symbol)
192
+ when :symbol then (str("\\").as(:slash) >> match("\s").repeat >> str("\n")) | first_value.as(:symbol)
192
193
  when :unary_class then (first_value.as(:unary_class) >> space? >> sequence.maybe).as(:unary)
193
194
  when :fonts then first_value.as(:fonts_class) >> space? >> sequence.as(:fonts_value)
194
195
  when :special_fonts then first_value.as(:bold_fonts)
@@ -8,6 +8,7 @@ module Plurimath
8
8
  rule(unary: simple(:unary)) { unary }
9
9
  rule(table: simple(:table)) { table }
10
10
  rule(comma: simple(:comma)) { Utility.symbol_object(comma) }
11
+ rule(slash: simple(:slash)) { Math::Function::Linebreak.new }
11
12
  rule(unary: sequence(:unary)) { Utility.filter_values(unary) }
12
13
  rule(rparen: simple(:rparen)) { Utility.symbol_object(rparen) }
13
14
  rule(number: simple(:number)) { Math::Number.new(number) }
@@ -27,6 +28,12 @@ module Plurimath
27
28
  rule(power_value: sequence(:power_value)) { power_value }
28
29
  rule(mod: simple(:mod), expr: simple(:expr)) { [mod, expr] }
29
30
 
31
+ rule(unitsml: simple(:unitsml)) do
32
+ Utility.filter_values(
33
+ Unitsml.new(unitsml.to_s).to_formula.value,
34
+ )
35
+ end
36
+
30
37
  rule(bold_fonts: simple(:font)) do
31
38
  Math::Function::FontStyle::DoubleStruck.new(
32
39
  Utility.symbol_object(font.to_s[0]),
@@ -132,6 +139,13 @@ module Plurimath
132
139
  new_arr
133
140
  end
134
141
 
142
+ rule(sequence: simple(:sequence),
143
+ left_right: sequence(:left_right)) do
144
+ new_arr = [sequence]
145
+ new_arr += left_right unless left_right.empty?
146
+ new_arr
147
+ end
148
+
135
149
  rule(sequence: simple(:sequence),
136
150
  number: simple(:number)) do
137
151
  [sequence, Math::Number.new(number.to_s)]
@@ -296,11 +310,15 @@ module Plurimath
296
310
  end
297
311
 
298
312
  rule(td: simple(:td)) do
299
- Math::Function::Td.new(
300
- [
301
- Utility.td_value(td),
302
- ],
303
- )
313
+ if td.is_a?(Math::Formula) && td.value.any?(Math::Function::Table)
314
+ Utility.td_values(td.value, ",")
315
+ else
316
+ Math::Function::Td.new(
317
+ [
318
+ Utility.td_value(td),
319
+ ],
320
+ )
321
+ end
304
322
  end
305
323
 
306
324
  rule(td: sequence(:td)) do
@@ -624,11 +642,26 @@ module Plurimath
624
642
  new_arr
625
643
  end
626
644
 
645
+ rule(sequence: sequence(:sequence),
646
+ left_right: simple(:left_right)) do
647
+ new_arr = sequence.flatten.compact
648
+ new_arr << left_right unless left_right.to_s.strip.empty?
649
+ new_arr
650
+ end
651
+
627
652
  rule(sequence: sequence(:sequence),
628
653
  expr: sequence(:expr)) do
629
654
  sequence.flatten.compact + expr.flatten.compact
630
655
  end
631
656
 
657
+ rule(sequence: simple(:sequence),
658
+ symbol: simple(:symbol)) do
659
+ [
660
+ sequence,
661
+ Math::Symbol.new(symbol),
662
+ ]
663
+ end
664
+
632
665
  rule(binary_class: simple(:function),
633
666
  base_value: simple(:base)) do
634
667
  Utility.get_class(function).new(
@@ -845,6 +878,15 @@ module Plurimath
845
878
  )
846
879
  end
847
880
 
881
+ rule(unary_class: simple(:function),
882
+ unitsml: simple(:unitsml)) do
883
+ Utility.get_class(function).new(
884
+ Utility.filter_values(
885
+ Unitsml.new(unitsml.to_s).to_formula.value,
886
+ ),
887
+ )
888
+ end
889
+
848
890
  rule(number: simple(:number),
849
891
  comma: simple(:comma)) do
850
892
  [
@@ -1720,6 +1720,7 @@ module Plurimath
1720
1720
  cap: "&#x2229;",
1721
1721
  cup: "&#x222a;",
1722
1722
  sim: "&#x223c;",
1723
+ int: "&#x222b;",
1723
1724
  neq: "&#x2260;",
1724
1725
  leq: "&#x2264;",
1725
1726
  geq: "&#x2265;",
@@ -2853,7 +2854,9 @@ module Plurimath
2853
2854
  mathbfit: :fonts,
2854
2855
  mathfrak: :fonts,
2855
2856
  overline: :unary,
2857
+ underset: :binary,
2856
2858
  overset: :binary,
2859
+ phantom: :unary,
2857
2860
  ddagger: :symbols,
2858
2861
  trprime: :symbols,
2859
2862
  closure: :symbols,
@@ -3245,6 +3248,7 @@ module Plurimath
3245
3248
  textrm: :fonts,
3246
3249
  textbf: :fonts,
3247
3250
  arccos: :unary,
3251
+ cancel: :unary,
3248
3252
  arcsin: :unary,
3249
3253
  arctan: :unary,
3250
3254
  limsup: :unary,
@@ -3559,7 +3563,7 @@ module Plurimath
3559
3563
  tanh: :unary,
3560
3564
  cosh: :unary,
3561
3565
  ddot: :unary,
3562
- mbox: :unary,
3566
+ mbox: :text,
3563
3567
  text: :text,
3564
3568
  '"': :symbols,
3565
3569
  sum: :ternary,
@@ -4,11 +4,18 @@ 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(:under_over) { slash >> underover_classes }
11
- rule(:array_args) { (str("{") >> expression.as(:args) >> str("}")) }
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
+ rule(:array_begin) { (str("\\begin{") >> str("array").as(:environment) >> str("}")) }
13
+ rule(:optional_args) { (str("[") >> intermediate_exp.maybe.as(:options) >> str("]")).maybe }
14
+
15
+ rule(:color) do
16
+ (str("{") >> (str("}").absent? >> any).repeat.as(:symbol) >> str("}")) |
17
+ any.as(:symbol)
18
+ end
12
19
 
13
20
  rule(:optional_args) do
14
21
  (str("[") >> intermediate_exp.maybe.as(:options) >> str("]")).maybe
@@ -16,11 +23,7 @@ module Plurimath
16
23
 
17
24
  rule(:begining) do
18
25
  (slash >> str("begin") >> (str("{") >> symbol_text_or_integer >> str("*").as(:asterisk) >> str("}")) >> optional_args.maybe) |
19
- (slash >> str("begin") >> (str("{") >> symbol_text_or_integer >> str("}")))
20
- end
21
-
22
- rule(:array_begin) do
23
- (str("\\begin{") >> str("array").as(:environment) >> str("}"))
26
+ (slash >> str("begin") >> (str("{") >> symbol_text_or_integer >> str("}")))
24
27
  end
25
28
 
26
29
  rule(:ending) do
@@ -123,7 +126,8 @@ module Plurimath
123
126
  rule(:binary_functions) do
124
127
  (intermediate_exp.as(:first_value) >> under_over >> intermediate_exp.as(:second_value)).as(:under_over) |
125
128
  (slash >> str("sqrt").as(:root) >> sqrt_arg.as(:first_value) >> intermediate_exp.as(:second_value)).as(:binary) |
126
- (slash >> str("sqrt").as(:sqrt) >> intermediate_exp.as(:intermediate_exp)).as(:binary)
129
+ (slash >> str("sqrt").as(:sqrt) >> intermediate_exp.as(:intermediate_exp)).as(:binary) |
130
+ (color_rules)
127
131
  end
128
132
 
129
133
  rule(:sequence) do
@@ -242,6 +246,11 @@ module Plurimath
242
246
  (power >> intermediate_exp.as(:supscript)) |
243
247
  (base >> intermediate_exp.as(:subscript))
244
248
  end
249
+
250
+ def color_rules
251
+ (str("{") >> slash >> str("color").as(:binary) >> color.as(:first_value) >> (sequence >> iteration.maybe).as(:second_value).maybe >> str("}")) |
252
+ (slash >> str("color").as(:binary) >> color.as(:first_value) >> expression.as(:second_value).maybe)
253
+ end
245
254
  end
246
255
  end
247
256
  end
@@ -14,7 +14,7 @@ module Plurimath
14
14
  rule(lparen: simple(:lparen)) { Math::Symbol.new(lparen) }
15
15
  rule(rparen: simple(:rparen)) { Math::Symbol.new(rparen) }
16
16
  rule(limits: simple(:limits)) { limits }
17
- rule("\\\\" => simple(:slash)) { Math::Symbol.new(slash) }
17
+ rule("\\\\" => simple(:slash)) { Math::Function::Linebreak.new }
18
18
  rule(expression: simple(:expr)) { expr }
19
19
  rule(environment: simple(:env)) { env }
20
20
  rule(ternary: simple(:ternary)) { Utility.get_class(ternary).new }
@@ -488,19 +488,19 @@ module Plurimath
488
488
 
489
489
  rule(text: simple(:text),
490
490
  first_value: simple(:first_value)) do
491
- Math::Function::Text.new(first_value)
491
+ Utility.get_class(text).new(first_value)
492
492
  end
493
493
 
494
494
  rule(text: simple(:text),
495
495
  first_value: sequence(:first_value)) do
496
- Math::Function::Text.new(first_value.join)
496
+ Utility.get_class(text).new(first_value.join)
497
497
  end
498
498
 
499
499
  rule(text: simple(:text),
500
500
  first_value: simple(:first_value),
501
501
  supscript: simple(:supscript),) do
502
502
  Math::Function::Power.new(
503
- Math::Function::Text.new(first_value),
503
+ Utility.get_class(text).new(first_value),
504
504
  supscript,
505
505
  )
506
506
  end
@@ -509,26 +509,37 @@ module Plurimath
509
509
  first_value: sequence(:first_value),
510
510
  supscript: simple(:supscript),) do
511
511
  Math::Function::Power.new(
512
- Math::Function::Text.new(first_value.join),
512
+ Utility.get_class(text).new(first_value.join),
513
513
  supscript,
514
514
  )
515
515
  end
516
516
 
517
517
  rule(text: simple(:text),
518
518
  first_value: sequence(:first_value),
519
- subscript: simple(:subscript),) do
519
+ subscript: simple(:subscript)) do
520
520
  Math::Function::Base.new(
521
- Math::Function::Text.new(first_value.join),
521
+ Utility.get_class(text).new(first_value.join),
522
522
  subscript,
523
523
  )
524
524
  end
525
525
 
526
526
  rule(text: simple(:text),
527
527
  first_value: simple(:first_value),
528
- subscript: simple(:subscript),) do
528
+ subscript: simple(:subscript)) do
529
529
  Math::Function::Base.new(
530
- Math::Function::Text.new(first_value),
530
+ Utility.get_class(text).new(first_value),
531
+ subscript,
532
+ )
533
+ end
534
+
535
+ rule(text: simple(:text),
536
+ first_value: simple(:first_value),
537
+ subscript: simple(:subscript),
538
+ supscript: simple(:supscript)) do
539
+ Math::Function::PowerBase.new(
540
+ Utility.get_class(text).new(first_value),
531
541
  subscript,
542
+ supscript,
532
543
  )
533
544
  end
534
545
 
@@ -788,6 +799,17 @@ module Plurimath
788
799
  end
789
800
  end
790
801
 
802
+ rule(binary: simple(:binary),
803
+ first_value: simple(:first_value),
804
+ second_value: sequence(:second_value)) do
805
+ Utility.get_class(
806
+ binary.to_s.include?("mod") ? "mod" : binary,
807
+ ).new(
808
+ first_value,
809
+ Utility.filter_values(second_value),
810
+ )
811
+ end
812
+
791
813
  rule(underover: simple(:function),
792
814
  first_value: simple(:first),
793
815
  subscript: simple(:subscript),
@@ -926,12 +948,8 @@ module Plurimath
926
948
 
927
949
  rule(substack: simple(:substack),
928
950
  expression: sequence(:value)) do
929
- tds = Utility.td_values(value, "\\\\")
930
-
931
- substack_values = tds.map { |td| Math::Function::Tr.new([td]) }
932
951
  Math::Function::Substack.new(
933
- substack_values.shift,
934
- substack_values.shift,
952
+ Utility.organize_table(value),
935
953
  )
936
954
  end
937
955
  end
@@ -3,6 +3,11 @@
3
3
  module Plurimath
4
4
  module Math
5
5
  class Core
6
+ REPLACABLES = {
7
+ /&amp;/ => "&",
8
+ /^\n/ => "",
9
+ }
10
+
6
11
  def class_name
7
12
  self.class.name.split("::").last.downcase
8
13
  end
@@ -24,18 +29,18 @@ module Plurimath
24
29
  end
25
30
 
26
31
  def empty_tag(wrapper_tag)
27
- r_tag = Utility.ox_element("r", namespace: "m")
28
- r_tag << (Utility.ox_element("t", namespace: "m") << "&#8203;")
32
+ r_tag = ox_element("r", namespace: "m")
33
+ r_tag << (ox_element("t", namespace: "m") << "&#8203;")
29
34
  wrapper_tag << r_tag
30
35
  end
31
36
 
32
- def omml_parameter(field, display_style, tag_name: , namespace: "m")
33
- tag = Utility.ox_element(tag_name, namespace: namespace)
37
+ def omml_parameter(field, display_style, tag_name:, namespace: "m")
38
+ tag = ox_element(tag_name, namespace: namespace)
34
39
  return empty_tag(tag) unless field
35
40
 
36
41
  Utility.update_nodes(
37
42
  tag,
38
- field&.insert_t_tag(display_style),
43
+ field.insert_t_tag(display_style),
39
44
  )
40
45
  end
41
46
 
@@ -44,12 +49,13 @@ module Plurimath
44
49
  end
45
50
 
46
51
  def r_element(string, rpr_tag: true)
47
- r_tag = Utility.ox_element("r", namespace: "m")
52
+ r_tag = ox_element("r", namespace: "m")
48
53
  if rpr_tag
49
- sty_tag = Utility.ox_element("sty", namespace: "m", attributes: { "m:val": "p" })
50
- r_tag << (Utility.ox_element("rPr", namespace: "m") << sty_tag)
54
+ attrs = { "m:val": "p" }
55
+ sty_tag = ox_element("sty", namespace: "m", attributes: attrs)
56
+ r_tag << (ox_element("rPr", namespace: "m") << sty_tag)
51
57
  end
52
- r_tag << (Utility.ox_element("t", namespace: "m") << string)
58
+ r_tag << (ox_element("t", namespace: "m") << string)
53
59
  Array(r_tag)
54
60
  end
55
61
 
@@ -57,13 +63,209 @@ module Plurimath
57
63
  false
58
64
  end
59
65
 
60
- def extract_class_from_text
66
+ def extract_class_name_from_text
61
67
  ""
62
68
  end
63
69
 
64
70
  def font_style_t_tag(display_style)
65
71
  to_omml_without_math_tag(display_style)
66
72
  end
73
+
74
+ def ascii_fields_to_print(field, options = {})
75
+ return if field.nil?
76
+
77
+ hashed = common_math_zone_conversion(field, options)
78
+ options[:array] << "#{hashed[:spacing]}|_ \"#{field&.to_asciimath}\"#{hashed[:field_name]}\n"
79
+ return unless Utility.validate_math_zone(field)
80
+
81
+ options[:array] << field&.to_asciimath_math_zone(hashed[:function_spacing], hashed[:last], hashed[:indent])
82
+ end
83
+
84
+ def latex_fields_to_print(field, options = {})
85
+ return if field.nil?
86
+
87
+ hashed = common_math_zone_conversion(field, options)
88
+ options[:array] << "#{hashed[:spacing]}|_ \"#{field&.to_latex}\"#{hashed[:field_name]}\n"
89
+ return unless Utility.validate_math_zone(field)
90
+
91
+ options[:array] << field&.to_latex_math_zone(hashed[:function_spacing], hashed[:last], hashed[:indent])
92
+ end
93
+
94
+ def mathml_fields_to_print(field, options = {})
95
+ return if field.nil?
96
+
97
+ hashed = common_math_zone_conversion(field, options)
98
+ options[:array] << "#{hashed[:spacing]}|_ \"#{dump_mathml(field)}\"#{hashed[:field_name]}\n"
99
+ return unless Utility.validate_math_zone(field)
100
+
101
+ options[:array] << field&.to_mathml_math_zone(hashed[:function_spacing], hashed[:last], hashed[:indent])
102
+ end
103
+
104
+ def omml_fields_to_print(field, options = {})
105
+ return if field.nil?
106
+
107
+ hashed = common_math_zone_conversion(field, options)
108
+ display_style = options[:display_style]
109
+ options[:array] << "#{hashed[:spacing]}|_ \"#{dump_omml(field, display_style)}\"#{hashed[:field_name]}\n"
110
+ return unless Utility.validate_math_zone(field)
111
+
112
+ options[:array] << field&.to_omml_math_zone(hashed[:function_spacing], hashed[:last], hashed[:indent], display_style: display_style)
113
+ end
114
+
115
+ def dump_mathml(field)
116
+ dump_ox_nodes(field.to_mathml_without_math_tag).gsub(/\n\s*/, "")
117
+ end
118
+
119
+ def dump_omml(field, display_style)
120
+ return if field.nil?
121
+
122
+ dump_ox_nodes(field.omml_nodes(display_style)).gsub(/\n\s*/, "")
123
+ end
124
+
125
+ def omml_nodes(display_style)
126
+ to_omml_without_math_tag(display_style)
127
+ end
128
+
129
+ def validate_mathml_fields(field)
130
+ field.nil? ? ox_element("mi") : field.to_mathml_without_math_tag
131
+ end
132
+
133
+ def common_math_zone_conversion(field, options = {})
134
+ {
135
+ spacing: options[:spacing],
136
+ last: options[:last] || true,
137
+ indent: !field.is_a?(Formula),
138
+ function_spacing: "#{options[:spacing]}#{options[:additional_space]}",
139
+ field_name: (options[:field_name] ? " #{options[:field_name]}" : ""),
140
+ }
141
+ end
142
+
143
+ def filtered_values(value)
144
+ @values = Utility.filter_math_zone_values(value)
145
+ end
146
+
147
+ def dump_ox_nodes(nodes)
148
+ return dump_nodes(nodes) unless nodes.is_a?(Array)
149
+
150
+ nodes.flatten.map { |node| dump_nodes(node) }.join
151
+ end
152
+
153
+ def dump_nodes(nodes, indent: nil)
154
+ replacable_values(
155
+ Plurimath.xml_engine.dump(nodes, indent: indent),
156
+ )
157
+ end
158
+
159
+ def replacable_values(string)
160
+ REPLACABLES.each { |regex, str| string.gsub!(regex, str) }
161
+ string
162
+ end
163
+
164
+ def gsub_spacing(spacing, last)
165
+ spacing.gsub(/\|_/, last ? " " : "| ")
166
+ end
167
+
168
+ def invert_unicode_symbols
169
+ Mathml::Constants::UNICODE_SYMBOLS.invert[class_name] || class_name
170
+ end
171
+
172
+ def separate_table
173
+ false
174
+ end
175
+
176
+ def linebreak
177
+ false
178
+ end
179
+
180
+ def cloned_objects
181
+ object = self.class.new rescue self.class.new(nil)
182
+ variables.each { |var| object.set(var, variable_value(get(var))) }
183
+ object
184
+ end
185
+
186
+ def variable_value(value)
187
+ case value
188
+ when Core
189
+ value.cloned_objects
190
+ when Array
191
+ value.map { |object| variable_value(object) }
192
+ else
193
+ value
194
+ end
195
+ end
196
+
197
+ def line_breaking(obj)
198
+ variables.each do |variable|
199
+ field = get(variable)
200
+ case field
201
+ when Core
202
+ field.line_breaking(obj)
203
+ updated_object_values(variable, obj: obj, update_value: true) if obj.value_exist?
204
+ when Array
205
+ if result(field).length > 1
206
+ updated_object_values(variable, obj: obj)
207
+ else
208
+ field.each { |object| object.line_breaking(obj) }
209
+ end
210
+ end
211
+ end
212
+ end
213
+
214
+ def updated_object_values(param, obj:, update_value: false)
215
+ object = self.class.new(nil)
216
+ found = false
217
+ variables.each do |variable|
218
+ value = if param == variable
219
+ found = true
220
+ if update_value
221
+ return_value = obj.value
222
+ obj.value = []
223
+ return_value
224
+ else
225
+ formula = Formula.new(get(variable))
226
+ formula.line_breaking(obj)
227
+ set(variable, obj)
228
+ get(variable)
229
+ end
230
+ else
231
+ return_value = get(variable)
232
+ set(variable, nil) if found
233
+ return_value
234
+ end
235
+ object.set(variable, Utility.filter_values(value))
236
+ end
237
+ object.hide_function_name = true if object.methods.include?(:hide_function_name)
238
+ obj.update(object)
239
+ end
240
+
241
+ def get(variable)
242
+ instance_variable_get(variable)
243
+ end
244
+
245
+ def set(variable, value)
246
+ instance_variable_set(variable, value)
247
+ end
248
+
249
+ def variables
250
+ instance_variables
251
+ end
252
+
253
+ def ox_element(node, attributes: [], namespace: "")
254
+ Utility.ox_element(
255
+ node,
256
+ attributes: attributes,
257
+ namespace: namespace,
258
+ )
259
+ end
260
+
261
+ def result(value = [])
262
+ value = get("@value") || value
263
+ value.slice_after { |d| d.is_a?(Math::Function::Linebreak) }.to_a
264
+ end
265
+
266
+ def is_unary?
267
+ is_a?(Math::Function::UnaryFunction)
268
+ end
67
269
  end
68
270
  end
69
271
  end