unitsml 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a08ed1b145023452764dc89296ba34e9ca5eb00cdbaa48cdd053bdc0ead1dbb9
4
+ data.tar.gz: a74caaa5c72e9752d97c4c7c753d82f2af0f7ede94463bbc51eb084f1f009275
5
+ SHA512:
6
+ metadata.gz: 8f46a6311181f3cd1e199bfb1478ca7fe5a1aeebc51b6523add6af95dd17b3abfc0ed8aabe68e45d03ded5bcd6f28ce480155f7c6aa94ef7345819f25499b338
7
+ data.tar.gz: 790714452652b58d3e37056a6359193f42b7ff11d490cf6130005dd7059653eb90b61c54f39e712ea7cd92f52452b8c4485181adbb2bebd2e27c752e83ba46d3
@@ -0,0 +1,11 @@
1
+ name: rake
2
+
3
+ on:
4
+ push:
5
+ branches: [ master, main ]
6
+ tags: [ v* ]
7
+ pull_request:
8
+
9
+ jobs:
10
+ rake:
11
+ uses: metanorma/ci/.github/workflows/generic-rake.yml@main
@@ -0,0 +1,20 @@
1
+ name: release
2
+
3
+ on:
4
+ workflow_dispatch:
5
+ inputs:
6
+ next_version:
7
+ description: |
8
+ Next release version. Possible values: x.y.z, major, minor, patch or pre|rc|etc
9
+ required: true
10
+ default: 'skip'
11
+ repository_dispatch:
12
+ types: [ do-release ]
13
+
14
+ jobs:
15
+ release:
16
+ uses: metanorma/ci/.github/workflows/rubygems-release.yml@main
17
+ with:
18
+ next_version: ${{ github.event.inputs.next_version }}
19
+ secrets:
20
+ rubygems-api-key: ${{ secrets.UNITSML_CI_RUBYGEMS_API_KEY }}
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.gitmodules ADDED
@@ -0,0 +1,6 @@
1
+ [submodule "unitsdb"]
2
+ path = unitsdb
3
+ url = git@github.com:unitsml/unitsdb.git
4
+ [submodule "vendor/unitsdb"]
5
+ path = vendor/unitsdb
6
+ url = git@github.com:unitsml/unitsdb.git
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in unitsml.gemspec
4
+ gemspec
5
+
6
+ gem "rake"
data/Gemfile.lock ADDED
@@ -0,0 +1,83 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ unitsml (0.2.0)
5
+ htmlentities
6
+ parslet
7
+ plurimath
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ ast (2.4.2)
13
+ byebug (11.1.3)
14
+ coderay (1.1.3)
15
+ diff-lcs (1.4.4)
16
+ docile (1.3.5)
17
+ equivalent-xml (0.6.0)
18
+ nokogiri (>= 1.4.3)
19
+ htmlentities (4.3.4)
20
+ method_source (0.9.2)
21
+ mini_portile2 (2.8.1)
22
+ nokogiri (1.14.0)
23
+ mini_portile2 (~> 2.8.0)
24
+ racc (~> 1.4)
25
+ ox (2.14.17)
26
+ parallel (1.20.1)
27
+ parser (3.0.0.0)
28
+ ast (~> 2.4.1)
29
+ parslet (2.0.0)
30
+ plurimath (0.3.6)
31
+ ox
32
+ parslet
33
+ powerpack (0.1.3)
34
+ pry (0.12.2)
35
+ coderay (~> 1.1.0)
36
+ method_source (~> 0.9.0)
37
+ racc (1.6.2)
38
+ rainbow (3.0.0)
39
+ rake (12.3.3)
40
+ rspec (3.10.0)
41
+ rspec-core (~> 3.10.0)
42
+ rspec-expectations (~> 3.10.0)
43
+ rspec-mocks (~> 3.10.0)
44
+ rspec-core (3.10.1)
45
+ rspec-support (~> 3.10.0)
46
+ rspec-expectations (3.10.1)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.10.0)
49
+ rspec-mocks (3.10.2)
50
+ diff-lcs (>= 1.2.0, < 2.0)
51
+ rspec-support (~> 3.10.0)
52
+ rspec-support (3.10.2)
53
+ rubocop (0.54.0)
54
+ parallel (~> 1.10)
55
+ parser (>= 2.5)
56
+ powerpack (~> 0.1)
57
+ rainbow (>= 2.2.2, < 4.0)
58
+ ruby-progressbar (~> 1.7)
59
+ unicode-display_width (~> 1.0, >= 1.0.1)
60
+ ruby-progressbar (1.11.0)
61
+ simplecov (0.21.2)
62
+ docile (~> 1.1)
63
+ simplecov-html (~> 0.11)
64
+ simplecov_json_formatter (~> 0.1)
65
+ simplecov-html (0.12.3)
66
+ simplecov_json_formatter (0.1.2)
67
+ unicode-display_width (1.7.0)
68
+
69
+ PLATFORMS
70
+ ruby
71
+
72
+ DEPENDENCIES
73
+ byebug
74
+ equivalent-xml
75
+ pry (~> 0.12.2)
76
+ rake
77
+ rspec (~> 3.6)
78
+ rubocop (= 0.54.0)
79
+ simplecov (~> 0.15)
80
+ unitsml!
81
+
82
+ BUNDLED WITH
83
+ 2.3.25
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Тараскин Михаил
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "unitsml"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ git submodule update --init --recursive
9
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,72 @@
1
+ = Contributor Covenant Code of Conduct
2
+
3
+ == Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ == Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ == Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ == Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ == Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at MITaraskin@domclick.ru. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ == Attribution
69
+
70
+ This Code of Conduct is adapted
71
+ from the https://contributor-covenant.org[Contributor Covenant’s homepage],
72
+ https://contributor-covenant.org/version/1/4[version 1.4].
data/docs/README.adoc ADDED
@@ -0,0 +1,54 @@
1
+ = unitsml-ruby
2
+
3
+ Gem-wrapper for unitsdb
4
+
5
+ == Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ [source,ruby]
10
+ ----
11
+ gem 'unitsml'
12
+ ----
13
+
14
+ And then execute:
15
+
16
+ [source,sh]
17
+ ----
18
+ $ bundle install
19
+ ----
20
+
21
+ Or install it yourself as:
22
+
23
+ [source,sh]
24
+ ----
25
+ $ gem install unitsml
26
+ ----
27
+
28
+ == Usage
29
+
30
+ === Unit lookup
31
+
32
+ [source,ruby]
33
+ ----
34
+ Unitsml.find_unit(ascii: 'mL') # Unitsml::Unit (with unit attributes)
35
+ ----
36
+
37
+ == Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to https://rubygems.org[rubygems.org].
42
+
43
+ == Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at `https://github.com/unitsml/unitsml-ruby`. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the https://github.com/unitsml/unitsml-ruby/blob/master/docs/CODE_OF_CONDUCT.adoc[code of conduct].
46
+
47
+
48
+ == License
49
+
50
+ The gem is available as open source under the terms of the https://opensource.org/licenses/MIT[MIT license].
51
+
52
+ == Code of Conduct
53
+
54
+ Everyone interacting in the Unitsml project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the https://github.com/unitsml/unitsml-ruby/blob/master/docs/CODE_OF_CONDUCT.adoc[code of conduct].
@@ -0,0 +1,9 @@
1
+ ---
2
+ items:
3
+ - title: README
4
+ path: README/
5
+ - title: Code of conduct
6
+ path: CODE_OF_CONDUCT/
7
+ ---
8
+
9
+ = Navigation
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unitsml
4
+ class Dimension
5
+ attr_accessor :dimension_name, :power_numerator
6
+
7
+ def initialize(dimension_name, power_numerator = nil)
8
+ @dimension_name = dimension_name
9
+ @power_numerator = power_numerator
10
+ end
11
+
12
+ def ==(object)
13
+ self.class == object.class &&
14
+ dimension_name == object&.dimension_name &&
15
+ power_numerator == object&.power_numerator
16
+ end
17
+
18
+ def dim_hash
19
+ Unitsdb.parsable_dimensions[dimension_name]
20
+ end
21
+
22
+ def dim_symbols
23
+ dim_hash&.values&.last&.values&.first["dim_symbols"].first
24
+ end
25
+
26
+ def to_mathml
27
+ value = dim_symbols["mathml"]
28
+ value = Ox.parse(value)
29
+ if power_numerator
30
+ msup = Utility.ox_element("msup")
31
+ msup << (Utility.ox_element("mrow") << value)
32
+ msup << (
33
+ Utility.ox_element("mrow") << (
34
+ Utility.ox_element("mn") << power_numerator
35
+ )
36
+ )
37
+ value = msup
38
+ end
39
+ value
40
+ end
41
+
42
+ def to_latex
43
+ value = dim_symbols["latex"]
44
+ if power_numerator
45
+ value = "#{value}^#{power_numerator}"
46
+ end
47
+ value
48
+ end
49
+
50
+ def to_asciimath
51
+ value = dim_symbols["ascii"]
52
+ value = "#{value}^#{power_numerator}" if power_numerator
53
+ value
54
+ end
55
+
56
+ def to_html
57
+ value = dim_symbols["html"]
58
+ value = "#{value}<sup>#{power_numerator}</sup>" if power_numerator
59
+ value
60
+ end
61
+
62
+ def to_unicode
63
+ value = dim_symbols["unicode"]
64
+ value = "#{value}^#{power_numerator}" if power_numerator
65
+ value
66
+ end
67
+
68
+ def generate_id
69
+ "#{dimension_name.split('_').last}#{power_numerator}"
70
+ end
71
+
72
+ def to_xml
73
+ fields = dim_hash[:fields]
74
+ symbol = fields.values.first["symbol"]
75
+ power_numerator_value = power_numerator || 1
76
+ attributes = { symbol: symbol, powerNumerator: power_numerator_value }
77
+ element_name = modelize(fields.keys.first.capitalize)
78
+ Utility.ox_element(element_name, attributes: attributes)
79
+ end
80
+
81
+ def modelize(value)
82
+ value&.split("_")&.map(&:capitalize)&.join
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unitsml
4
+ class Extender
5
+ attr_accessor :symbol
6
+
7
+ def initialize(symbol)
8
+ @symbol = symbol
9
+ end
10
+
11
+ def ==(object)
12
+ self.class == object.class &&
13
+ symbol == object&.symbol
14
+ end
15
+
16
+ def to_mathml
17
+ Utility.ox_element("mo") << "&#x22c5;"
18
+ end
19
+
20
+ def to_latex
21
+ "/"
22
+ end
23
+
24
+ def to_asciimath
25
+ symbol
26
+ end
27
+
28
+ def to_html
29
+ "&#x22c5;"
30
+ end
31
+
32
+ def to_unicode
33
+ symbol == "*" ? "·" : symbol
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,164 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "plurimath"
4
+ require_relative "utility"
5
+ module Unitsml
6
+ class Formula
7
+ attr_accessor :value, :explicit_value, :root
8
+
9
+ def initialize(value = [],
10
+ explicit_value: nil,
11
+ root: false,
12
+ orig_text: nil,
13
+ norm_text: nil)
14
+ @value = value
15
+ @explicit_value = explicit_value
16
+ @root = root
17
+ @orig_text = orig_text
18
+ @norm_text = norm_text
19
+ end
20
+
21
+ def ==(object)
22
+ self.class == object.class &&
23
+ value == object&.value &&
24
+ explicit_value == object&.explicit_value &&
25
+ root == object.root
26
+ end
27
+
28
+ def to_mathml
29
+ if root
30
+ attributes = { xmlns: "http://www.w3.org/1998/Math/MathML", display: "block" }
31
+ math = Utility.ox_element("math", attributes: attributes)
32
+ Ox.dump(
33
+ Utility.update_nodes(
34
+ math,
35
+ value.map(&:to_mathml).flatten,
36
+ ),
37
+ ).gsub(/&amp;(.*?)(?=<\/)/, '&\1')
38
+ else
39
+ value.map(&:to_mathml)
40
+ end
41
+ end
42
+
43
+ def to_latex
44
+ value.map(&:to_latex).join
45
+ end
46
+
47
+ def to_asciimath
48
+ value.map(&:to_asciimath).join
49
+ end
50
+
51
+ def to_html
52
+ value.map(&:to_html).join
53
+ end
54
+
55
+ def to_unicode
56
+ value.map(&:to_unicode).join
57
+ end
58
+
59
+ def to_xml
60
+ dimensions_array = extract_dimensions(value)
61
+ if (dimensions_array).any?
62
+ dimensions(sort_dims(dimensions_array))
63
+ elsif @orig_text.match(/-$/)
64
+ prefixes
65
+ else
66
+ units
67
+ end
68
+ end
69
+
70
+ def to_plurimath
71
+ return Plurimath::Math.parse(to_asciimath, :asciimath) if @orig_text.match?(/-$/)
72
+
73
+ Plurimath::Math.parse(to_mathml, :mathml)
74
+ end
75
+
76
+ private
77
+
78
+ def extract_dimensions(formula)
79
+ dimensions = []
80
+
81
+ formula.each do |term|
82
+ if term.is_a?(Dimension)
83
+ dimensions << term
84
+ elsif term.is_a?(Sqrt) && term.value.is_a?(Dimension)
85
+ sqrt_term = term.value.dup
86
+ sqrt_term.power_numerator = "0.5"
87
+ dimensions << sqrt_term
88
+ elsif term.is_a?(Formula)
89
+ dimensions.concat(extract_dimensions(term.value))
90
+ end
91
+ end
92
+
93
+ dimensions
94
+ end
95
+
96
+ def extract_units(formula)
97
+ units_arr = []
98
+
99
+ formula.each do |term|
100
+ case term
101
+ when Unit
102
+ units_arr << term.dup
103
+ when Formula
104
+ units_arr.concat(extract_units(term.value.dup))
105
+ when Sqrt
106
+ next unless term.value.is_a?(Unit)
107
+
108
+ units_arr << term.value
109
+ end
110
+ end
111
+
112
+ units_arr
113
+ end
114
+
115
+ def units
116
+ all_units = extract_units(value)
117
+ norm_text = Utility.postprocess_normtext(all_units)
118
+ dims = Utility.units2dimensions(extract_units(value))
119
+ dimension, dimension_component = unique_dimensions(dims, norm_text)
120
+ [
121
+ Utility.unit(all_units, self, dims, norm_text, explicit_value&.dig(:name)),
122
+ Utility.prefixes(all_units),
123
+ dimension,
124
+ dimension_component,
125
+ Utility.quantity(norm_text, explicit_value&.dig(:quantity)),
126
+ ].join
127
+ end
128
+
129
+ def unique_dimensions(dims, norm_text)
130
+ [
131
+ Utility.dimension(norm_text),
132
+ Utility.dimension_components(dims),
133
+ ].uniq
134
+ end
135
+
136
+ def dimensions(dims)
137
+ dim_id = dims.map(&:generate_id).join
138
+ attributes = { xmlns: Utility::UNITSML_NS, "xml:id": "D_#{dim_id}" }
139
+ Ox.dump(
140
+ Utility.update_nodes(
141
+ Utility.ox_element("Dimension", attributes: attributes),
142
+ dims.map(&:to_xml),
143
+ ),
144
+ )
145
+ end
146
+
147
+ def sort_dims(values)
148
+ dims_hash = Utility::Dim2D
149
+ values.sort do |first, second|
150
+ dims_hash.dig(first.dimension_name, :order) <=> dims_hash.dig(second.dimension_name, :order)
151
+ end
152
+ end
153
+
154
+ def prefixes
155
+ norm_text = @norm_text&.split("-")&.first
156
+ prefix_object = Unit.new("", prefix: Prefix.new(norm_text))
157
+ [
158
+ Utility.prefixes([prefix_object]),
159
+ Utility.dimension(norm_text),
160
+ Utility.quantity(norm_text, explicit_value&.dig(:quantity)),
161
+ ].join
162
+ end
163
+ end
164
+ end