asciimath2unitsml 0.2.4 → 0.2.5

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: a2279898112af9b3602b0f7ed1a713adc4130015a3f616767d364952fb87b8ff
4
- data.tar.gz: 2bf900fe40159a836fed5bbbcdc7f76c7911083242e12962949b269fafe2e92d
3
+ metadata.gz: 9c5ec436a8b058ce5c93dc246a8493d88a724c7d5f1048b8e42f7a1429791d3c
4
+ data.tar.gz: a7106b478a1efa4f48c0133c9b644d77197db0d71031d48152274c9c43dde1f6
5
5
  SHA512:
6
- metadata.gz: 8d3c8c4ca3db1208f5764ddd0cf2ab221548dceb3ac99d69a14778bd05dc4aec07a54064ae408908aacf27f5dbdd456fbca6eb42f29cc626527d518f03ae831f
7
- data.tar.gz: 3b1d4a11f5db9225b20802ab261ba67099f977a66af680a1aaa19ffe2fdb6f6bbf78f0088f5473fe7b1e23083a72a32f9e130a9fe71abe240f9f306610bdef64
6
+ metadata.gz: e9fd0281fea82d844d833ae155875ab1b52d22cf384185a32330ed18709759aee6df93fb281e0b7844fe14bd79200211646d37b0152a0190a32b81d8d0176153
7
+ data.tar.gz: 8cee75e492900a7ac7e622134feb3c1f0e10503af231af87c7f55d01c88aaf9d817ed66f753249ac5fe5c355a1de9963f3e6b3ed21379996335c89efccc9d022
data/README.adoc CHANGED
@@ -53,6 +53,8 @@ from UnitsDB. For example, `unitsml(cal_th/cm^2, name: langley)`.
53
53
  The unit-string gives the canonical representation of the unit, but SYMBOL is what will be rendered.
54
54
  For example, `unitsml(cal_th/cm^2, name: langley, symbol: La)`, or `unitsml(mm*s^-2, symbol: mm cdot s^-2)`.
55
55
  (All variables in SYMBOL are rendered upright, as is the default for units.)
56
+ * `unitsml(unit-string, multiplier: SYMBOL)` provides an alternate symbol for the multiliper of
57
+ units. The options are an XML entity, or the values `space` or `nospace` (for which see discussion under _Usage_).
56
58
 
57
59
  Standalone prefixes can be recognised by replacing the unit with hyphen; so `unitsml(p-)` corresponds
58
60
  to the standalone prefix "pico" (and is rendered as "p").
@@ -230,7 +232,7 @@ This table can be generated (in Asciidoc format) through `Asciimath2UnitsML::Con
230
232
  | ton | ton of TNT (energy equivalent): `ton_TNT` | ton of refrigeration (12 000 Btu_IT/h): `ton_refrigeration` | | | |
231
233
  | tsp | teaspoon: `tsp` | teaspoon (FDA): `tsp_label` | | | |
232
234
  | yd | yard: `yd` | yard (based on US survey foot): `yd_US_survey` | | | |
233
- | º | degree (degree of arc): `deg` | | | | |
235
+ | ° | degree (degree of arc): `deg` | | | | |
234
236
  | γ | gamma: `gamma` | | | | |
235
237
  | μ | micron: `micron` | | | | |
236
238
  | Ω | ohm: `Ohm` | | | | |
@@ -258,3 +260,4 @@ This table can be generated (in Asciidoc format) through `Asciimath2UnitsML::Con
258
260
  | °R | degree Rankine: `degR` | | | | |
259
261
  | ƛ~C~ | natural unit of length: `lambda-bar_C` | | | | |
260
262
  |===
263
+
@@ -63,7 +63,7 @@ module Asciimath2UnitsML
63
63
  END
64
64
  end
65
65
 
66
- U2D = {
66
+ U2D = {
67
67
  "m" => { dimension: "Length", order: 1, symbol: "L" },
68
68
  "g" => { dimension: "Mass", order: 2, symbol: "M" },
69
69
  "kg" => { dimension: "Mass", order: 2, symbol: "M" },
@@ -123,11 +123,12 @@ module Asciimath2UnitsML
123
123
  quantity = text[1..-1]&.select { |x| /^quantity:/.match(x) }&.first&.sub(/^quantity:\s*/, "")
124
124
  name = text[1..-1]&.select { |x| /^name:/.match(x) }&.first&.sub(/^name:\s*/, "")
125
125
  symbol = text[1..-1]&.select { |x| /^symbol:/.match(x) }&.first&.sub(/^symbol:\s*/, "")
126
+ multiplier = text[1..-1]&.select { |x| /^multiplier:/.match(x) }&.first&.sub(/^multiplier:\s*/, "")
126
127
  normtext = units_only(units).each.map do |u|
127
128
  exp = u[:exponent] && u[:exponent] != "1" ? "^#{u[:exponent]}" : ""
128
129
  "#{u[:prefix]}#{u[:unit]}#{exp}"
129
130
  end.join("*")
130
- [units, text[0], normtext, quantity, name, symbol]
131
+ [units, text[0], normtext, quantity, name, symbol, multiplier]
131
132
  end
132
133
 
133
134
  def postprocess1(units)
@@ -154,16 +155,20 @@ module Asciimath2UnitsML
154
155
  xml.xpath(".//m:mtext", "m" => MATHML_NS).each do |x|
155
156
  next unless %r{^unitsml\(.+\)$}.match(x.text)
156
157
  text = x.text.sub(%r{^unitsml\((.+)\)$}m, "\\1")
157
- units, origtext, normtext, quantity, name, symbol = parse(text)
158
- rendering = symbol ? embeddedmathml(asciimath2mathml(symbol)) : mathmlsymbol(units, false)
158
+ units, origtext, normtext, quantity, name, symbol, multiplier = parse(text)
159
+ rendering = symbol ? embeddedmathml(asciimath2mathml(symbol)) :
160
+ mathmlsymbol(units, false, multiplier)
159
161
  x.replace("#{delimspace(rendering, x)}<mrow xref='#{unit_id(origtext)}'>#{rendering}</mrow>\n"\
160
162
  "#{unitsml(units, origtext, normtext, quantity, name)}")
161
163
  end
162
164
  dedup_ids(xml)
163
165
  end
164
166
 
167
+ # if previous sibling's last descendent non-whitespace is MathML and mn or mi, no space
165
168
  def delimspace(rendering, elem)
166
- return "" if elem&.previous_element && elem&.previous_element.name != "mn"
169
+ prec_text_elem = elem.xpath("./preceding-sibling::*[namespace-uri() = '#{MATHML_NS}']/"\
170
+ "descendant::text()[normalize-space()!=''][last()]/parent::*").last
171
+ return "" if prec_text_elem.nil? || !%w(mn mi).include?(prec_text_elem&.name)
167
172
  text = HTMLEntities.new.encode(Nokogiri::XML("<mrow>#{rendering}</mrow>").text.strip)
168
173
  /\p{L}|\p{N}/.match(text) ?
169
174
  "<mo rspace='thickmathspace'>&#x2062;</mo>" : "<mo>&#x2062;</mo>"
@@ -42,9 +42,10 @@ module Asciimath2UnitsML
42
42
  base
43
43
  end
44
44
 
45
- def mathmlsymbol(units, normalise)
45
+ def mathmlsymbol(units, normalise, multiplier = nil)
46
+ multiplier = multiplier ? "<mo>#{multiplier}</mo>" : @multiplier[:mathml]
46
47
  exp = units.map do |u|
47
- if u[:multiplier] then u[:multiplier] == "*" ? @multiplier[:mathml] : "<mo>#{u[:multiplier]}</mo>"
48
+ if u[:multiplier] then u[:multiplier] == "*" ? multiplier : "<mo>#{u[:multiplier]}</mo>"
48
49
  elsif u[:unit].nil? && u[:prefix]
49
50
  %(<mi mathvariant='normal'>#{htmlent(@prefixes[u[:prefix]].html)}</mi>)
50
51
  else
@@ -1,3 +1,3 @@
1
1
  module Asciimath2UnitsML
2
- VERSION = '0.2.4'.freeze
2
+ VERSION = '0.2.5'.freeze
3
3
  end
data/spec/conv_spec.rb CHANGED
@@ -66,7 +66,7 @@ RSpec.describe Asciimath2UnitsML do
66
66
  OUTPUT
67
67
  end
68
68
 
69
- it "deals with non-Ascii units and prefixes" do
69
+ it "deals with non-Ascii units and prefixes" do
70
70
  expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT)
71
71
  1 "unitsml(um)"
72
72
  INPUT
@@ -108,58 +108,116 @@ OUTPUT
108
108
 
109
109
  it "does not insert space before non-alphabetic units" do
110
110
  expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT)
111
- 1 "unitsml(degK)" + 1 "unitsml(prime)"
111
+ 1 "unitsml(degK)" + 1 "unitsml(prime)" + ii(theta) = s//r "unitsml(rad)" + 10^(12) "unitsml(Hz)"
112
112
  INPUT
113
113
  <math xmlns='http://www.w3.org/1998/Math/MathML'>
114
- <mn>1</mn>
115
- <mo rspace='thickmathspace'>&#x2062;</mo>
116
- <mrow xref='U_NISTu5'>
117
- <mi mathvariant='normal'>&#xB0;K</mi>
118
- </mrow>
119
- <Unit xmlns='http://unitsml.nist.gov/2005' xml:id='U_NISTu5' dimensionURL='#NISTd5'>
120
- <UnitSystem name='SI' type='SI_derived' xml:lang='en-US'/>
121
- <UnitName xml:lang='en'>kelvin</UnitName>
122
- <UnitSymbol type='HTML'>K</UnitSymbol>
123
- <UnitSymbol type='MathML'>
124
- <math xmlns='http://www.w3.org/1998/Math/MathML'>
125
- <mrow>
126
- <mi mathvariant='normal'>K</mi>
127
- </mrow>
128
- </math>
129
- </UnitSymbol>
130
- </Unit>
131
- <Dimension xmlns='http://unitsml.nist.gov/2005' xml:id='NISTd5'>
132
- <ThermodynamicTemperature symbol='Theta' powerNumerator='1'/>
133
- </Dimension>
134
- <Quantity xmlns='http://unitsml.nist.gov/2005' xml:id='NISTq5' dimensionURL='#NISTd5' quantityType='base'>
135
- <QuantityName xml:lang='en-US'>thermodynamic temperature</QuantityName>
136
- </Quantity>
137
- <mo>+</mo>
138
- <mn>1</mn>
139
- <mo>&#x2062;</mo>
140
- <mrow xref='U_NISTu147'>
141
- <mi mathvariant='normal'>&#x2032;</mi>
142
- </mrow>
143
- <Unit xmlns='http://unitsml.nist.gov/2005' xml:id='U_NISTu147'>
144
- <UnitSystem name='not_SI' type='not_SI' xml:lang='en-US'/>
145
- <UnitName xml:lang='en'>minute (minute of arc)</UnitName>
146
- <UnitSymbol type='HTML'>&#x2032;</UnitSymbol>
147
- <UnitSymbol type='MathML'>
148
- <math xmlns='http://www.w3.org/1998/Math/MathML'>
149
- <mrow>
150
- <mi mathvariant='normal'>&#x2032;</mi>
151
- </mrow>
152
- </math>
153
- </UnitSymbol>
154
- </Unit>
155
- <Dimension xmlns='http://unitsml.nist.gov/2005' xml:id='NISTd9'>
156
- <PlaneAngle symbol='Phi' powerNumerator='1'/>
157
- </Dimension>
158
- <Quantity xmlns='http://unitsml.nist.gov/2005' xml:id='NISTq9' dimensionURL='#NISTd9' quantityType='base'>
159
- <QuantityName xml:lang='en-US'>plane angle</QuantityName>
160
- <QuantityName xml:lang='en-US'>angle</QuantityName>
161
- </Quantity>
162
- </math>
114
+ <mn>1</mn>
115
+ <mo rspace='thickmathspace'>&#x2062;</mo>
116
+ <mrow xref='U_NISTu5'>
117
+ <mi mathvariant='normal'>&#xB0;K</mi>
118
+ </mrow>
119
+ <Unit xmlns='http://unitsml.nist.gov/2005' xml:id='U_NISTu5' dimensionURL='#NISTd5'>
120
+ <UnitSystem name='SI' type='SI_derived' xml:lang='en-US'/>
121
+ <UnitName xml:lang='en'>kelvin</UnitName>
122
+ <UnitSymbol type='HTML'>K</UnitSymbol>
123
+ <UnitSymbol type='MathML'>
124
+ <math xmlns='http://www.w3.org/1998/Math/MathML'>
125
+ <mrow>
126
+ <mi mathvariant='normal'>K</mi>
127
+ </mrow>
128
+ </math>
129
+ </UnitSymbol>
130
+ </Unit>
131
+ <Dimension xmlns='http://unitsml.nist.gov/2005' xml:id='NISTd5'>
132
+ <ThermodynamicTemperature symbol='Theta' powerNumerator='1'/>
133
+ </Dimension>
134
+ <Quantity xmlns='http://unitsml.nist.gov/2005' xml:id='NISTq5' dimensionURL='#NISTd5' quantityType='base'>
135
+ <QuantityName xml:lang='en-US'>thermodynamic temperature</QuantityName>
136
+ </Quantity>
137
+ <mo>+</mo>
138
+ <mn>1</mn>
139
+ <mo>&#x2062;</mo>
140
+ <mrow xref='U_NISTu147'>
141
+ <mi mathvariant='normal'>&#x2032;</mi>
142
+ </mrow>
143
+ <Unit xmlns='http://unitsml.nist.gov/2005' xml:id='U_NISTu147'>
144
+ <UnitSystem name='not_SI' type='not_SI' xml:lang='en-US'/>
145
+ <UnitName xml:lang='en'>minute (minute of arc)</UnitName>
146
+ <UnitSymbol type='HTML'>&#x2032;</UnitSymbol>
147
+ <UnitSymbol type='MathML'>
148
+ <math xmlns='http://www.w3.org/1998/Math/MathML'>
149
+ <mrow>
150
+ <mi mathvariant='normal'>&#x2032;</mi>
151
+ </mrow>
152
+ </math>
153
+ </UnitSymbol>
154
+ </Unit>
155
+ <Dimension xmlns='http://unitsml.nist.gov/2005' xml:id='NISTd9'>
156
+ <PlaneAngle symbol='Phi' powerNumerator='1'/>
157
+ </Dimension>
158
+ <Quantity xmlns='http://unitsml.nist.gov/2005' xml:id='NISTq9' dimensionURL='#NISTd9' quantityType='base'>
159
+ <QuantityName xml:lang='en-US'>plane angle</QuantityName>
160
+ <QuantityName xml:lang='en-US'>angle</QuantityName>
161
+ </Quantity>
162
+ <mo>+</mo>
163
+ <mstyle mathvariant='italic'>
164
+ <mi>&#x3B8;</mi>
165
+ </mstyle>
166
+ <mo>=</mo>
167
+ <mi>s</mi>
168
+ <mo>/</mo>
169
+ <mi>r</mi>
170
+ <mo rspace='thickmathspace'>&#x2062;</mo>
171
+ <mrow xref='U_NISTu9'>
172
+ <mi mathvariant='normal'>rad</mi>
173
+ </mrow>
174
+ <Unit xmlns='http://unitsml.nist.gov/2005' xml:id='U_NISTu9' dimensionURL='#D_L0'>
175
+ <UnitSystem name='SI' type='SI_derived' xml:lang='en-US'/>
176
+ <UnitName xml:lang='en'>radian</UnitName>
177
+ <UnitSymbol type='HTML'>rad</UnitSymbol>
178
+ <UnitSymbol type='MathML'>
179
+ <math xmlns='http://www.w3.org/1998/Math/MathML'>
180
+ <mrow>
181
+ <mi mathvariant='normal'>rad</mi>
182
+ </mrow>
183
+ </math>
184
+ </UnitSymbol>
185
+ </Unit>
186
+ <Dimension xmlns='http://unitsml.nist.gov/2005' xml:id='D_L0'>
187
+ <Length symbol='L' powerNumerator='0'/>
188
+ </Dimension>
189
+ <mo>+</mo>
190
+ <msup>
191
+ <mrow>
192
+ <mn>10</mn>
193
+ </mrow>
194
+ <mrow>
195
+ <mn>12</mn>
196
+ </mrow>
197
+ </msup>
198
+ <mo rspace='thickmathspace'>&#x2062;</mo>
199
+ <mrow xref='U_NISTu31'>
200
+ <mi mathvariant='normal'>Hz</mi>
201
+ </mrow>
202
+ <Unit xmlns='http://unitsml.nist.gov/2005' xml:id='U_NISTu31' dimensionURL='#NISTd24'>
203
+ <UnitSystem name='SI' type='SI_derived' xml:lang='en-US'/>
204
+ <UnitName xml:lang='en'>hertz</UnitName>
205
+ <UnitSymbol type='HTML'>Hz</UnitSymbol>
206
+ <UnitSymbol type='MathML'>
207
+ <math xmlns='http://www.w3.org/1998/Math/MathML'>
208
+ <mrow>
209
+ <mi mathvariant='normal'>Hz</mi>
210
+ </mrow>
211
+ </math>
212
+ </UnitSymbol>
213
+ </Unit>
214
+ <Dimension xmlns='http://unitsml.nist.gov/2005' xml:id='NISTd24'>
215
+ <Time symbol='T' powerNumerator='-1'/>
216
+ </Dimension>
217
+ <Quantity xmlns='http://unitsml.nist.gov/2005' xml:id='NISTq45' dimensionURL='#NISTd24' quantityType='base'>
218
+ <QuantityName xml:lang='en-US'>frequency</QuantityName>
219
+ </Quantity>
220
+ </math>
163
221
  OUTPUT
164
222
  end
165
223
 
@@ -562,7 +620,6 @@ INPUT
562
620
  <QuantityName xml:lang='en-US'>mass</QuantityName>
563
621
  </Quantity>
564
622
  <mo>+</mo>
565
- <mo rspace='thickmathspace'>&#x2062;</mo>
566
623
  <mrow xref='U_um'>
567
624
  <mi mathvariant='normal'>&#xB5;m</mi>
568
625
  </mrow>
@@ -1189,5 +1246,68 @@ INPUT
1189
1246
  OUTPUT
1190
1247
  end
1191
1248
 
1249
+ it "deals with multiplier input" do
1250
+ expect(xmlpp(Asciimath2UnitsML::Conv.new().Asciimath2UnitsML(<<~INPUT))).to be_equivalent_to xmlpp(<<~OUTPUT)
1251
+ 10 "unitsml(cm*s^-2, multiplier: xx)"
1252
+ INPUT
1253
+ <math xmlns='http://www.w3.org/1998/Math/MathML'>
1254
+ <mn>10</mn>
1255
+ <mo rspace='thickmathspace'>&#x2062;</mo>
1256
+ <mrow xref='U_cm.s-2'>
1257
+ <mi mathvariant='normal'>cm</mi>
1258
+ <mo>xx</mo>
1259
+ <msup>
1260
+ <mrow>
1261
+ <mi mathvariant='normal'>s</mi>
1262
+ </mrow>
1263
+ <mrow>
1264
+ <mo>&#x2212;</mo>
1265
+ <mn>2</mn>
1266
+ </mrow>
1267
+ </msup>
1268
+ </mrow>
1269
+ <Unit xmlns='http://unitsml.nist.gov/2005' xml:id='U_cm.s-2' dimensionURL='#NISTd28'>
1270
+ <UnitSystem name='SI' type='SI_derived' xml:lang='en-US'/>
1271
+ <UnitName xml:lang='en'>cm*s^-2</UnitName>
1272
+ <UnitSymbol type='HTML'>
1273
+ cm&#xB7;s
1274
+ <sup>&#x2212;2</sup>
1275
+ </UnitSymbol>
1276
+ <UnitSymbol type='MathML'>
1277
+ <math xmlns='http://www.w3.org/1998/Math/MathML'>
1278
+ <mrow>
1279
+ <mi mathvariant='normal'>cm</mi>
1280
+ <mo>&#xB7;</mo>
1281
+ <msup>
1282
+ <mrow>
1283
+ <mi mathvariant='normal'>s</mi>
1284
+ </mrow>
1285
+ <mrow>
1286
+ <mo>&#x2212;</mo>
1287
+ <mn>2</mn>
1288
+ </mrow>
1289
+ </msup>
1290
+ </mrow>
1291
+ </math>
1292
+ </UnitSymbol>
1293
+ <RootUnits>
1294
+ <EnumeratedRootUnit unit='meter' prefix='c'/>
1295
+ <EnumeratedRootUnit unit='second' powerNumerator='-2'/>
1296
+ </RootUnits>
1297
+ </Unit>
1298
+ <Prefix xmlns='http://unitsml.nist.gov/2005' prefixBase='10' prefixPower='-2' xml:id='NISTp10_-2'>
1299
+ <PrefixName xml:lang='en'>centi</PrefixName>
1300
+ <PrefixSymbol type='ASCII'>c</PrefixSymbol>
1301
+ <PrefixSymbol type='unicode'>c</PrefixSymbol>
1302
+ <PrefixSymbol type='LaTeX'>c</PrefixSymbol>
1303
+ <PrefixSymbol type='HTML'>c</PrefixSymbol>
1304
+ </Prefix>
1305
+ <Dimension xmlns='http://unitsml.nist.gov/2005' xml:id='NISTd28'>
1306
+ <Length symbol='L' powerNumerator='1'/>
1307
+ <Time symbol='T' powerNumerator='-2'/>
1308
+ </Dimension>
1309
+ </math>
1310
+ OUTPUT
1311
+ end
1192
1312
 
1193
1313
  end
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.2.4
4
+ version: 0.2.5
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-03-17 00:00:00.000000000 Z
11
+ date: 2021-03-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: asciimath