asciimath2unitsml 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3d06ecf91cb804e1a5827a46ef0e458a0fe1b444a476bc6e9d99ff2dd6c06d37
4
- data.tar.gz: 615d0c46c6beb12bea4fb427379ba49835679fb3f1efd56692909d049e9e9be7
3
+ metadata.gz: 034e77e5697ed13c58b3b172857fcbb44fcf95ee7d3f34e43772d2b8e4c70be7
4
+ data.tar.gz: 66702bb8b3a6abf29f55f5a4b0beb7b2860e2e19c4b1efa6866d551e6734007f
5
5
  SHA512:
6
- metadata.gz: 270b64a21c8a566907c1d0a3e81e20f126bd48d277631678b65d662eb8d80da92799e127c0d75f0555226762157eefd1cf27135f3daa1b79693199c00758fa22
7
- data.tar.gz: b78b7cc95c55fde702b677e8e0a70126066341afeed726d4e3d00958b4cd4ad4d11850ef694329eca640019b5354bb34fb7efd246d023c065e7c37388c9de87e
6
+ metadata.gz: d2f364ed5703beed771a4cf82c65abb31a6a1e913cb6a88009a9aa93adc7777775aef63384dd3f11344129bf2d7455fa432187dd1ac80f444127600554bd6737
7
+ data.tar.gz: 3af5ec03a1422567ddb0dbcea962bc4d5edcbc24497fbd9446a0bd542562ad82cb777e3542459e8c99c095620b2c4eb58514208234f48342a345bb571616a6ec
@@ -0,0 +1,51 @@
1
+ # Auto-generated by Cimas: Do not edit it manually!
2
+ # See https://github.com/metanorma/cimas
3
+ name: rake
4
+
5
+ on:
6
+ push:
7
+ branches: [ master, main ]
8
+ tags: [ v* ]
9
+ pull_request:
10
+
11
+ jobs:
12
+ rake:
13
+ name: Test on Ruby ${{ matrix.ruby }} ${{ matrix.os }}
14
+ runs-on: ${{ matrix.os }}
15
+ continue-on-error: ${{ matrix.experimental }}
16
+ strategy:
17
+ fail-fast: false
18
+ matrix:
19
+ ruby: [ '2.7', '2.6', '2.5', '2.4' ]
20
+ os: [ ubuntu-latest, windows-latest, macos-latest ]
21
+ experimental: [ false ]
22
+ include:
23
+ - ruby: '3.0'
24
+ os: 'ubuntu-latest'
25
+ experimental: true
26
+ - ruby: '3.0'
27
+ os: 'windows-latest'
28
+ experimental: true
29
+ - ruby: '3.0'
30
+ os: 'macos-latest'
31
+ experimental: true
32
+ steps:
33
+ - uses: actions/checkout@master
34
+
35
+ - uses: ruby/setup-ruby@v1
36
+ with:
37
+ ruby-version: ${{ matrix.ruby }}
38
+ bundler-cache: true
39
+
40
+ - run: bundle exec rake
41
+
42
+ tests-passed:
43
+ needs: rake
44
+ runs-on: ubuntu-latest
45
+ steps:
46
+ - uses: peter-evans/repository-dispatch@v1
47
+ with:
48
+ token: ${{ secrets.METANORMA_CI_PAT_TOKEN || secrets.GITHUB_TOKEN }}
49
+ repository: ${{ github.repository }}
50
+ event-type: notify
51
+ client-payload: '{"ref": "${{ github.ref }}", "sha": "${{ github.sha }}"}'
@@ -17,6 +17,10 @@ module Asciimath2UnitsML
17
17
  @quantities = read_yaml("../unitsdb/quantities.yaml")
18
18
  @units_id = read_yaml("../unitsdb/units.yaml")
19
19
  @units = flip_name_and_id(@units_id)
20
+ #temporary
21
+ @units[:degC][:render] = "°C"
22
+ @units[:degF][:render] = "°F"
23
+ @units[:Ohm][:render] = "Ω"
20
24
  @parser = parser
21
25
  @multiplier = multiplier(options[:multiplier] || "\u00b7")
22
26
  end
@@ -37,6 +41,7 @@ module Asciimath2UnitsML
37
41
  end
38
42
 
39
43
  def unit_id(text)
44
+ text = text.gsub(/[()]/, "")
40
45
  "U_" +
41
46
  (@units[text.to_sym] ? @units[text.to_sym][:id] : text.gsub(/\*/, ".").gsub(/\^/, ""))
42
47
  end
@@ -82,12 +87,17 @@ module Asciimath2UnitsML
82
87
  END
83
88
  end
84
89
 
90
+ def render(unit)
91
+ #require "byebug"; byebug if unit == "degC"
92
+ @units[unit.to_sym][:render] || unit
93
+ end
94
+
85
95
  def htmlsymbol(units)
86
96
  units.map do |u|
87
97
  if u[:multiplier] then u[:multiplier] == "*" ? @multiplier[:html] : u[:multiplier]
88
98
  else
89
99
  u[:display_exponent] and exp = "<sup>#{u[:display_exponent].sub(/-/, "&#x2212;")}</sup>"
90
- "#{u[:prefix]}#{u[:unit]}#{exp}"
100
+ "#{u[:prefix]}#{render(u[:unit])}#{exp}"
91
101
  end
92
102
  end.join("")
93
103
  end
@@ -96,7 +106,7 @@ module Asciimath2UnitsML
96
106
  exp = units.map do |u|
97
107
  if u[:multiplier] then u[:multiplier] == "*" ? @multiplier[:mathml] : "<mo>#{u[:multiplier]}</mo>"
98
108
  else
99
- base = "<mi mathvariant='normal'>#{u[:prefix]}#{u[:unit]}</mi>"
109
+ base = "<mi mathvariant='normal'>#{u[:prefix]}#{render(u[:unit])}</mi>"
100
110
  if u[:display_exponent]
101
111
  exp = "<mn>#{u[:display_exponent]}</mn>".sub(/<mn>-/, "<mo>&#x2212;</mo><mn>")
102
112
  "<msup><mrow>#{base}</mrow><mrow>#{exp}</mrow></msup>"
@@ -37,8 +37,9 @@ module Asciimath2UnitsML
37
37
  @units[k][:type]&.include?("buildable") || /\*|\^/.match(k)
38
38
  end.map { |k| Regexp.escape(k) }
39
39
  unit1 = /#{unit_keys.sort_by(&:length).reverse.join("|")}/.r
40
- exponent = /\^-?\d+/.r.map { |m| m.sub(/\^/, "") }
41
- multiplier = %r{[*/]}.r.map { |x| { multiplier: x } }
40
+ exponent = /\^\(-?\d+\)/.r.map { |m| m.sub(/\^/, "").gsub(/[()]/, "") } |
41
+ /\^-?\d+/.r.map { |m| m.sub(/\^/, "") }
42
+ multiplier = %r{\*|//|/}.r.map { |x| { multiplier: x[0] } }
42
43
  unit = seq(unit1, exponent._?) { |x| { prefix: nil, unit: x[0], display_exponent: (x[1][0] )} } |
43
44
  seq(prefix, unit1, exponent._?) { |x| { prefix: x[0][0], unit: x[1], display_exponent: (x[2][0] ) } }
44
45
  units_tail = seq(multiplier, unit) { |x| [x[0], x[1]] }
@@ -1,3 +1,3 @@
1
1
  module Asciimath2UnitsML
2
- VERSION = '0.1.2'.freeze
2
+ VERSION = '0.1.3'.freeze
3
3
  end
data/spec/conv_spec.rb CHANGED
@@ -3,19 +3,17 @@ require "spec_helper"
3
3
  RSpec.describe Asciimath2UnitsML do
4
4
  it "converts an AsciiMath string to MathML + UnitsML" do
5
5
  expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT)
6
- 32 + 5 xx 7 "unitsml(kg^-2)" xx 9 "unitsml(g)" xx 1 "unitsml(kg*s^-2)" xx 812 "unitsml(m*s^-2)" - 9 "unitsml(C^3*A)" + 7 "unitsml(hp)" + 13 "unitsml(A/C^-3)"
6
+ 1 "unitsml(kg*s^-2)" xx 9 "unitsml(g)"
7
7
  INPUT
8
8
  <math xmlns='http://www.w3.org/1998/Math/MathML'>
9
- <mn>32</mn>
10
- <mo>+</mo>
11
- <mn>5</mn>
12
- <mo>&#xD7;</mo>
13
- <mn>7</mn>
9
+ <mn>1</mn>
14
10
  <mo rspace='thickmathspace'>&#x2062;</mo>
15
- <mrow xref='U_kg-2'>
11
+ <mrow xref='U_kg.s-2'>
12
+ <mi mathvariant='normal'>kg</mi>
13
+ <mo>&#xB7;</mo>
16
14
  <msup>
17
15
  <mrow>
18
- <mi mathvariant='normal'>kg</mi>
16
+ <mi mathvariant='normal'>s</mi>
19
17
  </mrow>
20
18
  <mrow>
21
19
  <mo>&#x2212;</mo>
@@ -23,19 +21,21 @@ RSpec.describe Asciimath2UnitsML do
23
21
  </mrow>
24
22
  </msup>
25
23
  </mrow>
26
- <Unit xmlns='http://unitsml.nist.gov/2005' xml:id='U_kg-2' dimensionURL='#D_M-2'>
27
- <UnitSystem name='SI' type='SI_base' xml:lang='en-US'/>
28
- <UnitName xml:lang='en'>kg^-2</UnitName>
24
+ <Unit xmlns='http://unitsml.nist.gov/2005' xml:id='U_kg.s-2' dimensionURL='#D_MT-2'>
25
+ <UnitSystem name='SI' type='SI_derived' xml:lang='en-US'/>
26
+ <UnitName xml:lang='en'>kg*s^-2</UnitName>
29
27
  <UnitSymbol type='HTML'>
30
- kg
28
+ kg&#xB7;s
31
29
  <sup>&#x2212;2</sup>
32
30
  </UnitSymbol>
33
31
  <UnitSymbol type='MathML'>
34
32
  <math xmlns='http://www.w3.org/1998/Math/MathML'>
35
33
  <mrow>
34
+ <mi mathvariant='normal'>kg</mi>
35
+ <mo>&#xB7;</mo>
36
36
  <msup>
37
37
  <mrow>
38
- <mi mathvariant='normal'>kg</mi>
38
+ <mi mathvariant='normal'>s</mi>
39
39
  </mrow>
40
40
  <mrow>
41
41
  <mo>&#x2212;</mo>
@@ -45,13 +45,18 @@ RSpec.describe Asciimath2UnitsML do
45
45
  </mrow>
46
46
  </math>
47
47
  </UnitSymbol>
48
+ <RootUnits>
49
+ <EnumeratedRootUnit unit='gram' prefix='k'/>
50
+ <EnumeratedRootUnit unit='second' powerNumerator='-2'/>
51
+ </RootUnits>
48
52
  </Unit>
49
53
  <Prefix xmlns='http://unitsml.nist.gov/2005' prefixBase='10' prefixPower='3' xml:id='NISTp10_3'>
50
54
  <PrefixName xml:lang='en'>kilo</PrefixName>
51
55
  <PrefixSymbol type='ASCII'>k</PrefixSymbol>
52
56
  </Prefix>
53
- <Dimension xmlns='http://unitsml.nist.gov/2005' xml:id='D_M-2'>
54
- <Mass symbol='M' powerNumerator='-2'/>
57
+ <Dimension xmlns='http://unitsml.nist.gov/2005' xml:id='D_MT-2'>
58
+ <Mass symbol='M' powerNumerator='1'/>
59
+ <Time symbol='T' powerNumerator='-2'/>
55
60
  </Dimension>
56
61
  <mo>&#xD7;</mo>
57
62
  <mn>9</mn>
@@ -74,7 +79,41 @@ RSpec.describe Asciimath2UnitsML do
74
79
  <Dimension xmlns='http://unitsml.nist.gov/2005' xml:id='D_M'>
75
80
  <Mass symbol='M' powerNumerator='1'/>
76
81
  </Dimension>
77
- <mo>&#xD7;</mo>
82
+ </math>
83
+ OUTPUT
84
+ end
85
+
86
+ it "deals with non-metric" do
87
+ expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT)
88
+ 1 "unitsml(hp)"
89
+ INPUT
90
+ <math xmlns='http://www.w3.org/1998/Math/MathML'>
91
+ <mn>1</mn>
92
+ <mo rspace='thickmathspace'>&#x2062;</mo>
93
+ <mrow xref='U_NISTu284'>
94
+ <mi mathvariant='normal'>hp</mi>
95
+ </mrow>
96
+ <Unit xmlns='http://unitsml.nist.gov/2005' xml:id='U_NISTu284'>
97
+ <UnitSystem name='not_SI' type='not_SI' xml:lang='en-US'/>
98
+ <UnitName xml:lang='en'>horsepower</UnitName>
99
+ <UnitSymbol type='HTML'>hp</UnitSymbol>
100
+ <UnitSymbol type='MathML'>
101
+ <math xmlns='http://www.w3.org/1998/Math/MathML'>
102
+ <mrow>
103
+ <mi mathvariant='normal'>hp</mi>
104
+ </mrow>
105
+ </math>
106
+ </UnitSymbol>
107
+ </Unit>
108
+ </math>
109
+ OUTPUT
110
+ end
111
+
112
+ it "deals with duplicate units" do
113
+ expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT)
114
+ 1 "unitsml(kg*s^-2)" xx 9 "unitsml(kg*s^-2)"
115
+ INPUT
116
+ <math xmlns='http://www.w3.org/1998/Math/MathML'>
78
117
  <mn>1</mn>
79
118
  <mo rspace='thickmathspace'>&#x2062;</mo>
80
119
  <mrow xref='U_kg.s-2'>
@@ -128,10 +167,10 @@ RSpec.describe Asciimath2UnitsML do
128
167
  <Time symbol='T' powerNumerator='-2'/>
129
168
  </Dimension>
130
169
  <mo>&#xD7;</mo>
131
- <mn>812</mn>
170
+ <mn>9</mn>
132
171
  <mo rspace='thickmathspace'>&#x2062;</mo>
133
- <mrow xref='U_NISTu1.u3e-2_1'>
134
- <mi mathvariant='normal'>m</mi>
172
+ <mrow xref='U_kg.s-2'>
173
+ <mi mathvariant='normal'>kg</mi>
135
174
  <mo>&#xB7;</mo>
136
175
  <msup>
137
176
  <mrow>
@@ -143,17 +182,17 @@ RSpec.describe Asciimath2UnitsML do
143
182
  </mrow>
144
183
  </msup>
145
184
  </mrow>
146
- <Unit xmlns='http://unitsml.nist.gov/2005' xml:id='U_NISTu1.u3e-2_1' dimensionURL='#D_LT-2'>
185
+ <Unit xmlns='http://unitsml.nist.gov/2005' xml:id='U_kg.s-2' dimensionURL='#D_MT-2'>
147
186
  <UnitSystem name='SI' type='SI_derived' xml:lang='en-US'/>
148
- <UnitName xml:lang='en'>meter per second squared</UnitName>
187
+ <UnitName xml:lang='en'>kg*s^-2</UnitName>
149
188
  <UnitSymbol type='HTML'>
150
- m&#xB7;s
189
+ kg&#xB7;s
151
190
  <sup>&#x2212;2</sup>
152
191
  </UnitSymbol>
153
192
  <UnitSymbol type='MathML'>
154
193
  <math xmlns='http://www.w3.org/1998/Math/MathML'>
155
194
  <mrow>
156
- <mi mathvariant='normal'>m</mi>
195
+ <mi mathvariant='normal'>kg</mi>
157
196
  <mo>&#xB7;</mo>
158
197
  <msup>
159
198
  <mrow>
@@ -168,15 +207,36 @@ RSpec.describe Asciimath2UnitsML do
168
207
  </math>
169
208
  </UnitSymbol>
170
209
  <RootUnits>
171
- <EnumeratedRootUnit unit='meter'/>
210
+ <EnumeratedRootUnit unit='gram' prefix='k'/>
172
211
  <EnumeratedRootUnit unit='second' powerNumerator='-2'/>
173
212
  </RootUnits>
174
213
  </Unit>
175
- <Dimension xmlns='http://unitsml.nist.gov/2005' xml:id='D_LT-2'>
176
- <Length symbol='L' powerNumerator='1'/>
214
+ <Prefix xmlns='http://unitsml.nist.gov/2005' prefixBase='10' prefixPower='3' xml:id='NISTp10_3'>
215
+ <PrefixName xml:lang='en'>kilo</PrefixName>
216
+ <PrefixSymbol type='ASCII'>k</PrefixSymbol>
217
+ </Prefix>
218
+ <Dimension xmlns='http://unitsml.nist.gov/2005' xml:id='D_MT-2'>
219
+ <Mass symbol='M' powerNumerator='1'/>
177
220
  <Time symbol='T' powerNumerator='-2'/>
178
221
  </Dimension>
179
- <mo>&#x2212;</mo>
222
+ </math>
223
+ OUTPUT
224
+ end
225
+
226
+ =begin
227
+ it "deals with notational variants" do
228
+ expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT)
229
+ 9 "unitsml(degK)" + 10 "unitsml(K)"
230
+ INPUT
231
+ OUTPUT
232
+ end
233
+ =end
234
+
235
+ it "deals with units division" do
236
+ expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT)
237
+ 9 "unitsml(C^3*A)" + 13 "unitsml(A/C^-3)"
238
+ INPUT
239
+ <math xmlns='http://www.w3.org/1998/Math/MathML'>
180
240
  <mn>9</mn>
181
241
  <mo rspace='thickmathspace'>&#x2062;</mo>
182
242
  <mrow xref='U_C3.A'>
@@ -197,7 +257,7 @@ RSpec.describe Asciimath2UnitsML do
197
257
  <UnitSymbol type='HTML'>
198
258
  C
199
259
  <sup>3</sup>
200
- &#xB7;A
260
+ &#xB7;A
201
261
  </UnitSymbol>
202
262
  <UnitSymbol type='MathML'>
203
263
  <math xmlns='http://www.w3.org/1998/Math/MathML'>
@@ -225,25 +285,56 @@ RSpec.describe Asciimath2UnitsML do
225
285
  <ElectricCurrent symbol='I' powerNumerator='4'/>
226
286
  </Dimension>
227
287
  <mo>+</mo>
228
- <mn>7</mn>
288
+ <mn>13</mn>
229
289
  <mo rspace='thickmathspace'>&#x2062;</mo>
230
- <mrow xref='U_NISTu284'>
231
- <mi mathvariant='normal'>hp</mi>
290
+ <mrow xref='U_A/C-3'>
291
+ <mi mathvariant='normal'>A</mi>
292
+ <mo>/</mo>
293
+ <msup>
294
+ <mrow>
295
+ <mi mathvariant='normal'>C</mi>
296
+ </mrow>
297
+ <mrow>
298
+ <mo>&#x2212;</mo>
299
+ <mn>3</mn>
300
+ </mrow>
301
+ </msup>
232
302
  </mrow>
233
- <Unit xmlns='http://unitsml.nist.gov/2005' xml:id='U_NISTu284'>
234
- <UnitSystem name='not_SI' type='not_SI' xml:lang='en-US'/>
235
- <UnitName xml:lang='en'>horsepower</UnitName>
236
- <UnitSymbol type='HTML'>hp</UnitSymbol>
303
+ <Unit xmlns='http://unitsml.nist.gov/2005' xml:id='U_A/C-3' dimensionURL='#D_T3I4'>
304
+ <UnitSystem name='SI' type='SI_derived' xml:lang='en-US'/>
305
+ <UnitName xml:lang='en'>A*C^3</UnitName>
306
+ <UnitSymbol type='HTML'>
307
+ A/C
308
+ <sup>&#x2212;3</sup>
309
+ </UnitSymbol>
237
310
  <UnitSymbol type='MathML'>
238
311
  <math xmlns='http://www.w3.org/1998/Math/MathML'>
239
312
  <mrow>
240
- <mi mathvariant='normal'>hp</mi>
313
+ <mi mathvariant='normal'>A</mi>
314
+ <mo>/</mo>
315
+ <msup>
316
+ <mrow>
317
+ <mi mathvariant='normal'>C</mi>
318
+ </mrow>
319
+ <mrow>
320
+ <mo>&#x2212;</mo>
321
+ <mn>3</mn>
322
+ </mrow>
323
+ </msup>
241
324
  </mrow>
242
325
  </math>
243
326
  </UnitSymbol>
327
+ <RootUnits>
328
+ <EnumeratedRootUnit unit='ampere'/>
329
+ <EnumeratedRootUnit unit='coulomb' powerNumerator='3'/>
330
+ </RootUnits>
244
331
  </Unit>
332
+ <Dimension xmlns='http://unitsml.nist.gov/2005' xml:id='D_T3I4'>
333
+ <Time symbol='T' powerNumerator='3'/>
334
+ <ElectricCurrent symbol='I' powerNumerator='4'/>
335
+ </Dimension>
245
336
  </math>
246
- OUTPUT
337
+ OUTPUT
247
338
  end
248
339
 
249
340
  it "converts MathML to MatML + UnitsML" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: asciimath2unitsml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ribose Inc.
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-20 00:00:00.000000000 Z
11
+ date: 2021-02-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciimath
@@ -215,6 +215,7 @@ executables: []
215
215
  extensions: []
216
216
  extra_rdoc_files: []
217
217
  files:
218
+ - ".github/workflows/rake.yml"
218
219
  - Gemfile
219
220
  - LICENSE
220
221
  - README.adoc