openproject-octicons 19.4.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 +7 -0
- data/LICENSE +21 -0
- data/README.md +103 -0
- data/lib/octicons/octicon.rb +107 -0
- data/lib/octicons/version.rb +3 -0
- data/lib/octicons.rb +8 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e3e6ad82bfb63428f4640dbc2f8840e0b5e6dc76150a21e0b3a30e018f8b64ac
|
4
|
+
data.tar.gz: d062d7494a785370d24eff3b2f9b51089a934fdda0ce4fe24c0fbd5abcefc090
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 292197e9ae1dde0bab169cc4d0cc531f46f8dfbd10b0a4f68b1849d7b0cb5347fcd81842e04c0087a90a47c0a54c163ed64af34f1fff3ce3bef97222e11fc4d1
|
7
|
+
data.tar.gz: a51fb17d2c0274db7e4a6befe710ffd84b96ca4cadba9985064badd5b31bd01df695cd91c8ed90a39f7fad80829400d55e50bde1fe371d86a841681973dbb67f
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 GitHub Inc.
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# octicons
|
2
|
+
|
3
|
+
[](https://rubygems.org/gems/octicons)
|
4
|
+
|
5
|
+
## Install
|
6
|
+
|
7
|
+
Add this to your `Gemfile`
|
8
|
+
|
9
|
+
```rb
|
10
|
+
gem 'octicons'
|
11
|
+
```
|
12
|
+
|
13
|
+
Then `bundle install`.
|
14
|
+
|
15
|
+
If using a framework like Rails, please follow the [installation instructions](https://primer.style/view-components/#installation) in the [Primer ViewComponents](https://primer.style/view-components) documentation.
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
```rb
|
20
|
+
require 'octicons'
|
21
|
+
icon = Octicons::Octicon.new("x")
|
22
|
+
icon.to_svg
|
23
|
+
# <svg class="octicon octicon-x" viewBox="0 0 16 16" width="16" height="16" version="1.1" "aria-hidden"="true"><path d="M7.48 8l3.75 3.75-1.48 1.48L6 9.48l-3.75 3.75-1.48-1.48L4.52 8 .77 4.25l1.48-1.48L6 6.52l3.75-3.75 1.48 1.48z"></path></svg>
|
24
|
+
```
|
25
|
+
|
26
|
+
## Documentation
|
27
|
+
|
28
|
+
The `Octicon` class takes two arguments. The first is the symbol of the icon, and the second is a hash of arguments representing html attributes
|
29
|
+
|
30
|
+
### `symbol` _(required)_
|
31
|
+
|
32
|
+
This is the name of the octicon you want to use. For example `alert`. [Full list of icons](/)
|
33
|
+
|
34
|
+
### Options
|
35
|
+
|
36
|
+
* `:height` - When setting the height to a number, the icon will scale to that size. For example, passing `32`, will calculate the width based on the icon's natural size.
|
37
|
+
* `:width` - When setting the width to a number, the icon will scale to that size. For example, passing `32`, will calculate the width based on the icon's natural size.
|
38
|
+
|
39
|
+
If both `:width, :height` are passed into the options hash, then the icon will be sized exactly at those dimensions.
|
40
|
+
|
41
|
+
### Attributes
|
42
|
+
|
43
|
+
Once initialized, you can read a few properties from the icon.
|
44
|
+
|
45
|
+
#### `symbol`
|
46
|
+
|
47
|
+
Returns the string of the symbol name
|
48
|
+
|
49
|
+
```rb
|
50
|
+
icon = Octicons::Octicon.new("x")
|
51
|
+
icon.symbol
|
52
|
+
# "x"
|
53
|
+
```
|
54
|
+
|
55
|
+
#### `path`
|
56
|
+
|
57
|
+
Path returns the string representation of the path of the icon.
|
58
|
+
|
59
|
+
```rb
|
60
|
+
icon = Octicons::Octicon.new("x")
|
61
|
+
icon.path
|
62
|
+
# <path d="M7.48 8l3.75 3.75-1.48 1.48L6 9.48l-3.75 3.75-1.48-1.48L4.52 8 .77 4.25l1.48-1.48L6 6.52l3.75-3.75 1.48 1.48z"></path>
|
63
|
+
```
|
64
|
+
|
65
|
+
#### `options`
|
66
|
+
|
67
|
+
This is a hash of all the `options` that will be added to the output tag.
|
68
|
+
|
69
|
+
```rb
|
70
|
+
icon = Octicons::Octicon.new("x")
|
71
|
+
icon.options
|
72
|
+
# {:class=>"octicon octicon-x", :viewBox=>"0 0 12 16", :version=>"1.1", :width=>12, :height=>16, :"aria-hidden"=>"true"}
|
73
|
+
```
|
74
|
+
|
75
|
+
#### `width`
|
76
|
+
|
77
|
+
Width is the icon's true width. Based on the svg view box width. _Note, this doesn't change if you scale it up with size options, it only is the natural width of the icon_
|
78
|
+
|
79
|
+
#### `height`
|
80
|
+
|
81
|
+
Height is the icon's true height. Based on the svg view box height. _Note, this doesn't change if you scale it up with size options, it only is the natural height of the icon_
|
82
|
+
|
83
|
+
#### `keywords`
|
84
|
+
|
85
|
+
Returns an array of keywords for the icon. The data comes from the [data file in lib](../data.json). Consider contributing more aliases for the icons.
|
86
|
+
|
87
|
+
```rb
|
88
|
+
icon = Octicons::Octicon.new("x")
|
89
|
+
icon.keywords
|
90
|
+
# ["remove", "close", "delete"]
|
91
|
+
```
|
92
|
+
|
93
|
+
### Methods
|
94
|
+
|
95
|
+
#### `to_svg`
|
96
|
+
|
97
|
+
Returns a string of the svg tag
|
98
|
+
|
99
|
+
```rb
|
100
|
+
icon = Octicons::Octicon.new("x")
|
101
|
+
icon.to_svg
|
102
|
+
# <svg class="octicon octicon-x" viewBox="0 0 16 16" width="16" height="16" version="1.1" "aria-hidden"="true"><path d="M7.48 8l3.75 3.75-1.48 1.48L6 9.48l-3.75 3.75-1.48-1.48L4.52 8 .77 4.25l1.48-1.48L6 6.52l3.75-3.75 1.48 1.48z"></path></svg>
|
103
|
+
```
|
@@ -0,0 +1,107 @@
|
|
1
|
+
module Octicons
|
2
|
+
class Octicon
|
3
|
+
DEFAULT_HEIGHT = 16
|
4
|
+
|
5
|
+
attr_reader :path, :options, :width, :height, :symbol, :keywords
|
6
|
+
|
7
|
+
def initialize(symbol, options = {})
|
8
|
+
@symbol = symbol.to_s
|
9
|
+
if octicon = get_octicon(@symbol, options)
|
10
|
+
@path = octicon["path"]
|
11
|
+
@width = octicon["width"]
|
12
|
+
@height = octicon["height"]
|
13
|
+
@keywords = octicon["keywords"]
|
14
|
+
@options = options.dup
|
15
|
+
@options.merge!({
|
16
|
+
class: classes,
|
17
|
+
viewBox: viewbox,
|
18
|
+
version: "1.1"
|
19
|
+
})
|
20
|
+
@options.merge!(size)
|
21
|
+
@options.merge!(a11y)
|
22
|
+
else
|
23
|
+
raise "Couldn't find octicon symbol for #{@symbol.inspect}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns an string representing a <svg> tag
|
28
|
+
def to_svg
|
29
|
+
"<svg #{html_attributes}>#{@path}</svg>"
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def html_attributes
|
35
|
+
attrs = ""
|
36
|
+
@options.each { |attr, value| attrs += "#{attr}=\"#{value}\" " }
|
37
|
+
attrs.strip
|
38
|
+
end
|
39
|
+
|
40
|
+
# add some accessibility features to svg
|
41
|
+
def a11y
|
42
|
+
accessible = {}
|
43
|
+
|
44
|
+
if @options[:"aria-label"].nil? && @options["aria-label"].nil?
|
45
|
+
accessible[:"aria-hidden"] = "true"
|
46
|
+
else
|
47
|
+
accessible[:role] = "img"
|
48
|
+
end
|
49
|
+
|
50
|
+
accessible
|
51
|
+
end
|
52
|
+
|
53
|
+
# prepare the octicon class
|
54
|
+
def classes
|
55
|
+
"octicon octicon-#{@symbol} #{@options[:class]} ".strip
|
56
|
+
end
|
57
|
+
|
58
|
+
def viewbox
|
59
|
+
"0 0 #{@width} #{@height}"
|
60
|
+
end
|
61
|
+
|
62
|
+
# determine the height and width of the octicon based on :size option
|
63
|
+
def size
|
64
|
+
size = {
|
65
|
+
width: @width,
|
66
|
+
height: @height
|
67
|
+
}
|
68
|
+
|
69
|
+
# Specific size
|
70
|
+
unless @options[:width].nil? && @options[:height].nil?
|
71
|
+
size[:width] = @options[:width].nil? ? calculate_width(@options[:height]) : @options[:width]
|
72
|
+
size[:height] = @options[:height].nil? ? calculate_height(@options[:width]) : @options[:height]
|
73
|
+
end
|
74
|
+
|
75
|
+
size
|
76
|
+
end
|
77
|
+
|
78
|
+
def calculate_width(height)
|
79
|
+
(height.to_i * @width) / @height
|
80
|
+
end
|
81
|
+
|
82
|
+
def calculate_height(width)
|
83
|
+
(width.to_i * @height) / @width
|
84
|
+
end
|
85
|
+
|
86
|
+
def get_octicon(symbol, options = {})
|
87
|
+
if octicon = Octicons::OCTICON_SYMBOLS[symbol]
|
88
|
+
# We're using width as an approximation for height if the height option is not passed in
|
89
|
+
height = options[:height] || options[:width] || DEFAULT_HEIGHT
|
90
|
+
natural_height = closest_natural_height(octicon["heights"].keys, height)
|
91
|
+
return {
|
92
|
+
"name" => octicon["name"],
|
93
|
+
"keywords" => octicon["keywords"],
|
94
|
+
"width" => octicon["heights"][natural_height.to_s]["width"].to_i,
|
95
|
+
"height" => natural_height,
|
96
|
+
"path" => octicon["heights"][natural_height.to_s]["path"]
|
97
|
+
}
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def closest_natural_height(natural_heights, height)
|
102
|
+
return natural_heights.reduce(natural_heights[0].to_i) do |acc, natural_height|
|
103
|
+
natural_height.to_i <= height.to_i ? natural_height.to_i : acc
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
data/lib/octicons.rb
ADDED
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: openproject-octicons
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 19.4.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- GitHub Inc.
|
8
|
+
- OpenProject GmbH
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2023-07-17 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: A package that distributes Octicons in a gem
|
15
|
+
email:
|
16
|
+
- support@openproject.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- LICENSE
|
22
|
+
- README.md
|
23
|
+
- lib/octicons.rb
|
24
|
+
- lib/octicons/octicon.rb
|
25
|
+
- lib/octicons/version.rb
|
26
|
+
homepage: https://github.com/opf/openproject-octicons
|
27
|
+
licenses:
|
28
|
+
- MIT
|
29
|
+
metadata:
|
30
|
+
rubygems_mfa_required: 'false'
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubygems_version: 3.3.7
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: GitHub's octicons gem
|
50
|
+
test_files: []
|