plurimath 0.8.26 → 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 567734f98d50c880712b5f5e044b44698872fddb28f9a42d6db7f9be2c3d14f4
4
- data.tar.gz: f070e5341608e4135e83c5715e2af06c849a56c28deeda6a3d69a6892af83d86
3
+ metadata.gz: 54bd34437d295276f1d97598c8f61725e92d5c23a2a8bfc8351f0552c0235043
4
+ data.tar.gz: 54fd5e1e5bf685a91e1811ab794ab17b0a6be1ad468147f6b524bef37be972d3
5
5
  SHA512:
6
- metadata.gz: cf74d549f20a7d2af98744ebbf10e1c68c78b550e11a2b56c7a66f1fb4ea68313044987144b26403f91658201c3a0892520434b7b6d2b10745dc2962cd9b96c4
7
- data.tar.gz: a11b93465a74fd63518cb9c2336fcf7a843f067e32018f701a8d838276881b54a214453d1d4c5568ac82b7bd06f471de83961a8dfebe15eacc610f8bb0879c38
6
+ metadata.gz: 137b2eee3b725d8d2b76ae1e30c5a4e65e346e446b90e5745a327deffe1a00254f47150049fcb1cc35b54e326064debc1ad9a74cdf67566fe221bc0a67714dc5
7
+ data.tar.gz: 653339b6aa420cd9e6e578805073bf504a0684f3154c5107c46f7481027e92a7bcb990825252f602292beb3a4bea5bfac5891b21be4751a445e1fdcc82153f47
data/Gemfile CHANGED
@@ -11,5 +11,4 @@ gem 'equivalent-xml'
11
11
  gem 'opal-rspec', "~> 1.1.0a"
12
12
  gem 'oga'
13
13
  gem 'ox'
14
- gem "unitsml"
15
14
  gem "debug"
@@ -345,6 +345,7 @@ module Plurimath
345
345
  :@using_default,
346
346
  :@displaystyle,
347
347
  :@__ordered,
348
+ :@unitsml,
348
349
  :@__mixed,
349
350
  :@is_mrow,
350
351
  :@values,
@@ -10,8 +10,7 @@ module Plurimath
10
10
  value = [],
11
11
  left_right_wrapper = true,
12
12
  display_style: true,
13
- input_string: nil,
14
- unitsml: false
13
+ input_string: nil
15
14
  )
16
15
  super
17
16
  @is_mrow = true
@@ -7,7 +7,7 @@ module Plurimath
7
7
  class Formula < Core
8
8
  include Mathml::Utility
9
9
 
10
- attr_accessor :value, :left_right_wrapper, :displaystyle, :input_string, :unitsml, :unitsml_xml, :display
10
+ attr_accessor :value, :left_right_wrapper, :displaystyle, :input_string, :display
11
11
 
12
12
  MATH_ZONE_TYPES = %i[
13
13
  omml
@@ -28,14 +28,12 @@ module Plurimath
28
28
  value = [],
29
29
  left_right_wrapper = true,
30
30
  display_style: true,
31
- input_string: nil,
32
- unitsml: false
31
+ input_string: nil
33
32
  )
34
33
  @value = value.is_a?(Array) ? value : [value]
35
34
  left_right_wrapper = false if @value.first.is_a?(Function::Left)
36
35
  @left_right_wrapper = left_right_wrapper
37
36
  @displaystyle = boolean_display_style(display_style)
38
- @unitsml = unitsml if unitsml
39
37
  end
40
38
 
41
39
  def ==(object)
@@ -45,8 +43,8 @@ module Plurimath
45
43
  object.left_right_wrapper == left_right_wrapper
46
44
  end
47
45
 
48
- def to_asciimath(formatter: nil, options: nil)
49
- options ||= { formatter: formatter }
46
+ def to_asciimath(formatter: nil, unitsml: {}, options: nil)
47
+ options ||= { formatter: formatter, unitsml: unitsml }.compact
50
48
  value.map { |val| val.to_asciimath(options: options) }.join(" ")
51
49
  rescue
52
50
  parse_error!(:asciimath)
@@ -55,11 +53,11 @@ module Plurimath
55
53
  def to_mathml(
56
54
  intent: false,
57
55
  formatter: nil,
58
- unitsml_xml: nil,
56
+ unitsml: {},
59
57
  split_on_linebreak: false,
60
58
  display_style: displaystyle
61
59
  )
62
- options = { formatter: formatter, unitsml_xml: unitsml_xml }
60
+ options = { formatter: formatter, unitsml: unitsml }.compact
63
61
  return line_breaked_mathml(display_style, intent, options: options) if split_on_linebreak
64
62
 
65
63
  math_attrs = {
@@ -89,8 +87,6 @@ module Plurimath
89
87
  mathml_value = mathml_content(intent, options: options)
90
88
  attributes = intent_attribute(mathml_value) if intent
91
89
  mrow = ox_element("mrow", attributes: attributes)
92
- mrow[:unitsml] = true if unitsml
93
- mathml_value += wrapped_unitsml_xml(mrow) if unitsml_xml && options[:unitsml_xml]
94
90
  Utility.update_nodes(mrow, mathml_value)
95
91
  end
96
92
 
@@ -100,15 +96,15 @@ module Plurimath
100
96
  nodes
101
97
  end
102
98
 
103
- def to_latex(formatter: nil, options: nil)
104
- options ||= { formatter: formatter }
99
+ def to_latex(formatter: nil, unitsml: {}, options: nil)
100
+ options ||= { formatter: formatter, unitsml: unitsml }.compact
105
101
  value.map { |val| val.to_latex(options: options) }.join(" ")
106
102
  rescue
107
103
  parse_error!(:latex)
108
104
  end
109
105
 
110
- def to_html(formatter: nil, options: nil)
111
- options ||= { formatter: formatter }
106
+ def to_html(formatter: nil, unitsml: {}, options: nil)
107
+ options ||= { formatter: formatter, unitsml: unitsml }.compact
112
108
  value&.map { |val| val.to_html(options: options) }&.join(" ")
113
109
  rescue
114
110
  parse_error!(:html)
@@ -137,9 +133,9 @@ module Plurimath
137
133
  }
138
134
  end
139
135
 
140
- def to_omml(display_style: displaystyle, split_on_linebreak: false, formatter: nil)
136
+ def to_omml(display_style: displaystyle, split_on_linebreak: false, formatter: nil, unitsml: {})
141
137
  objects = split_on_linebreak ? new_line_support : [self]
142
- options = { formatter: formatter }
138
+ options = { formatter: formatter, unitsml: unitsml }.compact
143
139
  para_element = Utility.ox_element("oMathPara", attributes: omml_attrs, namespace: "m")
144
140
  objects.each.with_index(1) do |object, index|
145
141
  para_element << Utility.update_nodes(
@@ -163,15 +159,15 @@ module Plurimath
163
159
  omml_content(display_style, options: options)
164
160
  end
165
161
 
166
- def to_unicodemath(formatter: nil, options: nil)
167
- options ||= { formatter: formatter }
162
+ def to_unicodemath(formatter: nil, unitsml: {}, options: nil)
163
+ options ||= { formatter: formatter, unitsml: unitsml }.compact
168
164
  Utility.html_entity_to_unicode(unicodemath_value(options: options)).gsub(/\s\/\s/, "/")
169
165
  rescue
170
166
  parse_error!(:unicodemath)
171
167
  end
172
168
 
173
- def to_display(type = nil, formatter: nil)
174
- options = { formatter: formatter }
169
+ def to_display(type = nil, formatter: nil, unitsml: {})
170
+ options = { formatter: formatter, unitsml: unitsml }
175
171
  return type_error!(type) unless MATH_ZONE_TYPES.include?(type.downcase.to_sym)
176
172
 
177
173
  math_zone = case type
@@ -640,12 +636,6 @@ module Plurimath
640
636
  end
641
637
  end
642
638
 
643
- def wrapped_unitsml_xml(mrow)
644
- node = Plurimath.xml_engine.load("<mrow>#{unitsml_xml}</mrow>")
645
- mrow.attributes[:xref] = node.locate("*/@id").first if node.locate("*/@id").any?
646
- node.nodes
647
- end
648
-
649
639
  def space_element(node)
650
640
  element = (ox_element("mo") << "&#x2062;")
651
641
  element[:rspace] = "thickmathspace"
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "unitsml"
4
+
5
+ module Plurimath
6
+ module Math
7
+ module Function
8
+ class Unitsml < Core
9
+ attr_accessor :text
10
+
11
+ def initialize(text)
12
+ @text = text
13
+ @unitsml = ::Unitsml.parse(text)
14
+ end
15
+
16
+ def ==(other)
17
+ other.is_a?(Unitsml) &&
18
+ other.text == @text
19
+ end
20
+
21
+ def to_asciimath(options:)
22
+ formula(options).to_asciimath(options: options)
23
+ end
24
+
25
+ def to_latex(options:)
26
+ formula(options).to_latex(options: options)
27
+ end
28
+
29
+ def to_unicodemath(options:)
30
+ formula(options).to_unicodemath(options: options)
31
+ end
32
+
33
+ def to_html(options:)
34
+ formula(options).to_html(options: options)
35
+ end
36
+
37
+ def to_mathml_without_math_tag(intent, options:)
38
+ mathml = formula(options).to_mathml_without_math_tag(intent, options: options)
39
+ mathml["unitsml"] = true
40
+ options&.dig(:unitsml, :xml) ? wrapped_unitsml_xml(mathml, @unitsml, options) : mathml
41
+ end
42
+
43
+ def to_omml_without_math_tag(display_style, options:)
44
+ formula(options).to_omml_without_math_tag(display_style, options: options)
45
+ end
46
+
47
+ def to_asciimath_math_zone(spacing = "", last = false, indent = true, options:)
48
+ formula(options).to_asciimath_math_zone(spacing, last, indent, options: options)
49
+ end
50
+
51
+ def to_latex_math_zone(spacing = "", last = false, indent = true, options:)
52
+ formula(options).to_latex_math_zone(spacing, last, indent, options: options)
53
+ end
54
+
55
+ def to_mathml_math_zone(spacing = "", last = false, indent = true, options:)
56
+ formula(options).to_mathml_math_zone(spacing, last, indent, options: options)
57
+ end
58
+
59
+ def to_omml_math_zone(spacing = "", last = false, indent = true, options:)
60
+ formula(options).to_omml_math_zone(spacing, last, indent, options: options)
61
+ end
62
+
63
+ def to_unicodemath_math_zone(spacing = "", last = false, indent = true, options:)
64
+ formula(options).to_unicodemath_math_zone(spacing, last, indent, options: options)
65
+ end
66
+
67
+ private
68
+
69
+ def formula(options)
70
+ @unitsml.to_plurimath(options.fetch(:unitsml, {}))
71
+ end
72
+
73
+ def wrapped_unitsml_xml(mathml, unitsml, options)
74
+ xml = Plurimath.xml_engine.load("<mrow>#{unitsml.to_xml(options.fetch(:unitsml, {}))}</mrow>")
75
+ mathml.attributes[:xref] = xml.locate("*/@id").first if xml.locate("*/@id").any?
76
+ Utility.update_nodes(mathml, xml.nodes)
77
+ end
78
+ end
79
+ end
80
+ end
81
+ end
@@ -45,11 +45,11 @@ module Plurimath
45
45
  end
46
46
 
47
47
  def to_mathml_without_math_tag(intent, **)
48
+ attributes = {}
48
49
  if value&.include?("&#x2147;")
49
- attributes = {
50
- intent: Utility.html_entity_to_unicode(value),
51
- }
50
+ attributes[:intent] = Utility.html_entity_to_unicode(value)
52
51
  end
52
+ attributes[:rspace] = @options[:rspace] if @options&.key?(:rspace)
53
53
  mi_tag = ox_element("mi", attributes: attributes)
54
54
  return mi_tag if ["{:", ":}"].include?(value)
55
55
 
@@ -430,8 +430,6 @@ module Plurimath
430
430
 
431
431
  def rquote=(_); end
432
432
 
433
- def rspace=(_); end
434
-
435
433
  def selection=(_); end
436
434
 
437
435
  def separator=(_); end
@@ -106,7 +106,11 @@ module Plurimath
106
106
  when Math::Symbols::Symbol
107
107
  return value if value.value.nil?
108
108
 
109
- mathml_symbol_to_class(value.value)
109
+ instance = mathml_symbol_to_class(value.value)
110
+ if value&.options&.any? && instance.is_a?(Math::Symbols::Symbol)
111
+ instance.options = value.options
112
+ end
113
+ instance
110
114
  when String
111
115
  mathml_symbol_to_class(value)
112
116
  end
@@ -358,6 +358,12 @@ module Plurimath
358
358
 
359
359
  update_temp_order(value, "mglyph")
360
360
  end
361
+
362
+ def rspace=(value)
363
+ return if value.nil? || value.empty?
364
+
365
+ @options = Hash(@options).merge(rspace: value)
366
+ end
361
367
  end
362
368
  end
363
369
  end
@@ -5,18 +5,15 @@ module Plurimath
5
5
  class Unitsml
6
6
  attr_accessor :text
7
7
 
8
+ VALID_UNITSML = %r{\^(([^\s][^*\/,"]*?[a-z]+)|(\([^-\d]+\)|[^\(\d-]+))}
9
+
8
10
  def initialize(text)
9
11
  @text = text
10
- raise Math::ParseError.new(error_message) if text.match?(/\^(([^\s][^*\/,"]*?[a-z]+)|(\([^-\d]+\)|[^\(\d-]+))/)
12
+ raise Math::ParseError.new(error_message) if text.match?(VALID_UNITSML)
11
13
  end
12
14
 
13
15
  def to_formula
14
- unitsml = ::Unitsml.parse(text)
15
- formula = unitsml.to_plurimath
16
- formula.unitsml = true
17
- formula.input_string = text
18
- formula.unitsml_xml = unitsml.to_xml
19
- formula
16
+ Math::Function::Unitsml.new(text)
20
17
  end
21
18
 
22
19
  def error_message
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Plurimath
4
- VERSION = "0.8.26"
4
+ VERSION = "0.9.0"
5
5
  end
data/plurimath.gemspec CHANGED
@@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
28
28
  spec.add_dependency 'mml'
29
29
  spec.add_dependency 'thor'
30
30
  spec.add_dependency 'parslet'
31
+ spec.add_dependency 'unitsml'
31
32
  spec.add_dependency 'bigdecimal'
32
33
  spec.add_dependency 'lutaml-model'
33
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: plurimath
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.26
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-02-13 00:00:00.000000000 Z
11
+ date: 2025-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ox
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: unitsml
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: bigdecimal
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -277,6 +291,7 @@ files:
277
291
  - lib/plurimath/math/function/unary_function.rb
278
292
  - lib/plurimath/math/function/underover.rb
279
293
  - lib/plurimath/math/function/underset.rb
294
+ - lib/plurimath/math/function/unitsml.rb
280
295
  - lib/plurimath/math/function/vec.rb
281
296
  - lib/plurimath/math/number.rb
282
297
  - lib/plurimath/math/symbols.rb
@@ -1801,7 +1816,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
1801
1816
  - !ruby/object:Gem::Version
1802
1817
  version: '0'
1803
1818
  requirements: []
1804
- rubygems_version: 3.3.27
1819
+ rubygems_version: 3.5.22
1805
1820
  signing_key:
1806
1821
  specification_version: 4
1807
1822
  summary: Converts LaTeX math into MathML.