does-octicons 1.0.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.
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Octicons
4
+ class Octicon
5
+ DEFAULT_HEIGHT = 16
6
+
7
+ attr_reader :path, :options, :width, :height, :symbol, :keywords
8
+
9
+ def initialize(symbol, options = {})
10
+ @symbol = symbol.to_s
11
+ if octicon = get_octicon(@symbol, options)
12
+ @path = octicon["path"]
13
+ @width = octicon["width"]
14
+ @height = octicon["height"]
15
+ @keywords = octicon["keywords"]
16
+ @options = options.dup
17
+ @options.merge!({
18
+ class: classes,
19
+ viewBox: viewbox,
20
+ version: "1.1"
21
+ })
22
+ @options.merge!(size)
23
+ @options.merge!(a11y)
24
+ else
25
+ raise "Couldn't find octicon symbol for #{@symbol.inspect}"
26
+ end
27
+ end
28
+
29
+ # Returns an string representing a <svg> tag
30
+ def to_svg
31
+ "<svg #{html_attributes}>#{@path}</svg>"
32
+ end
33
+
34
+ private
35
+
36
+ def html_attributes
37
+ attrs = ""
38
+ @options.each { |attr, value| attrs += "#{attr}=\"#{value}\" " }
39
+ attrs.strip
40
+ end
41
+
42
+ # add some accessibility features to svg
43
+ def a11y
44
+ accessible = {}
45
+
46
+ if @options[:"aria-label"].nil? && @options["aria-label"].nil?
47
+ accessible[:"aria-hidden"] = "true"
48
+ else
49
+ accessible[:role] = "img"
50
+ end
51
+
52
+ accessible
53
+ end
54
+
55
+ # prepare the octicon class
56
+ def classes
57
+ "octicon octicon-#{@symbol} #{@options[:class]} ".strip
58
+ end
59
+
60
+ def viewbox
61
+ "0 0 #{@width} #{@height}"
62
+ end
63
+
64
+ # determine the height and width of the octicon based on :size option
65
+ def size
66
+ size = {
67
+ width: @width,
68
+ height: @height
69
+ }
70
+
71
+ # Specific size
72
+ unless @options[:width].nil? && @options[:height].nil?
73
+ size[:width] = @options[:width].nil? ? calculate_width(@options[:height]) : @options[:width]
74
+ size[:height] = @options[:height].nil? ? calculate_height(@options[:width]) : @options[:height]
75
+ end
76
+
77
+ size
78
+ end
79
+
80
+ def calculate_width(height)
81
+ (height.to_i * @width) / @height
82
+ end
83
+
84
+ def calculate_height(width)
85
+ (width.to_i * @height) / @width
86
+ end
87
+
88
+ def get_octicon(symbol, options = {})
89
+ if octicon = Octicons::OCTICON_SYMBOLS[symbol]
90
+ # We're using width as an approximation for height if the height option is not passed in
91
+ height = options[:height] || options[:width] || DEFAULT_HEIGHT
92
+ natural_height = closest_natural_height(octicon["heights"].keys, height)
93
+ return {
94
+ "name" => octicon["name"],
95
+ "keywords" => octicon["keywords"],
96
+ "width" => octicon["heights"][natural_height.to_s]["width"].to_i,
97
+ "height" => natural_height,
98
+ "path" => octicon["heights"][natural_height.to_s]["path"]
99
+ }
100
+ end
101
+ end
102
+
103
+ def closest_natural_height(natural_heights, height)
104
+ return natural_heights.reduce(natural_heights[0].to_i) do |acc, natural_height|
105
+ natural_height.to_i <= height.to_i ? natural_height.to_i : acc
106
+ end
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Octicons
4
+ VERSION = "1.0.0".freeze
5
+ end
data/lib/octicons.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "octicons/version"
4
+ require "octicons/octicon"
5
+ require "json"
6
+
7
+ module Octicons
8
+ file_data = File.read(File.join(File.dirname(__FILE__), "./build/data.json"))
9
+ OCTICON_SYMBOLS = JSON.parse(file_data).freeze
10
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: does-octicons
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Does Work LLC
8
+ - GitHub Inc.
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2025-08-13 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: A fork of GitHub's octicons gem with custom icons added
15
+ email:
16
+ - mark@does.work
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - LICENSE
22
+ - README.md
23
+ - lib/build/data.json
24
+ - lib/octicons.rb
25
+ - lib/octicons/data.json
26
+ - lib/octicons/octicon.rb
27
+ - lib/octicons/version.rb
28
+ homepage: https://github.com/doeswork/octicons
29
+ licenses:
30
+ - MIT
31
+ metadata:
32
+ homepage_uri: https://github.com/doeswork/octicons
33
+ source_code_uri: https://github.com/doeswork/octicons
34
+ changelog_uri: https://github.com/doeswork/octicons/blob/main/CHANGELOG.md
35
+ post_install_message:
36
+ rdoc_options: []
37
+ require_paths:
38
+ - lib
39
+ required_ruby_version: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 2.7.0
44
+ required_rubygems_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubygems_version: 3.5.16
51
+ signing_key:
52
+ specification_version: 4
53
+ summary: Custom octicons gem with additional icons
54
+ test_files: []