class Octicons::Octicon

Constants

DEFAULT_HEIGHT

Attributes

height[R]
keywords[R]
options[R]
path[R]
symbol[R]
width[R]

Public Class Methods

new(symbol, options = {}) click to toggle source
# File lib/octicons/octicon.rb, line 9
def initialize(symbol, options = {})
  @symbol = symbol.to_s
  if octicon = get_octicon(@symbol, options)
    @path = octicon["path"]
    @width = octicon["width"]
    @height = octicon["height"]
    @keywords = octicon["keywords"]
    @options = options.dup
    @options.merge!({
      class:   classes,
      viewBox: viewbox,
      version: "1.1"
    })
    @options.merge!(size)
    @options.merge!(a11y)
  else
    raise "Couldn't find octicon symbol for #{@symbol.inspect}"
  end
end

Public Instance Methods

to_svg() click to toggle source

Returns an string representing a <svg> tag

# File lib/octicons/octicon.rb, line 30
def to_svg
  "<svg #{html_attributes}>#{@path}</svg>"
end

Private Instance Methods

a11y() click to toggle source

add some accessibility features to svg

# File lib/octicons/octicon.rb, line 43
def a11y
  accessible = {}

  if @options[:"aria-label"].nil? && @options["aria-label"].nil?
    accessible[:"aria-hidden"] = "true"
  else
    accessible[:role] = "img"
  end

  accessible
end
calculate_height(width) click to toggle source
# File lib/octicons/octicon.rb, line 84
def calculate_height(width)
  (width.to_i * @height) / @width
end
calculate_width(height) click to toggle source
# File lib/octicons/octicon.rb, line 80
def calculate_width(height)
  (height.to_i * @width) / @height
end
classes() click to toggle source

prepare the octicon class

# File lib/octicons/octicon.rb, line 56
def classes
  "octicon octicon-#{@symbol} #{@options[:class]} ".strip
end
closest_natural_height(natural_heights, height) click to toggle source
# File lib/octicons/octicon.rb, line 103
def closest_natural_height(natural_heights, height)
  return natural_heights.reduce(natural_heights[0].to_i) do |acc, natural_height|
    natural_height.to_i <= height.to_i ? natural_height.to_i : acc
  end
end
get_octicon(symbol, options = {}) click to toggle source
# File lib/octicons/octicon.rb, line 88
def get_octicon(symbol, options = {})
  if octicon = Octicons::OCTICON_SYMBOLS[symbol]
    # We're using width as an approximation for height if the height option is not passed in
    height = options[:height] || options[:width] || DEFAULT_HEIGHT
    natural_height = closest_natural_height(octicon["heights"].keys, height)
    return {
      "name" => octicon["name"],
      "keywords" => octicon["keywords"],
      "width" => octicon["heights"][natural_height.to_s]["width"].to_i,
      "height" => natural_height,
      "path" => octicon["heights"][natural_height.to_s]["path"]
    }
  end
end
html_attributes() click to toggle source
# File lib/octicons/octicon.rb, line 36
def html_attributes
  attrs = ""
  @options.each { |attr, value| attrs += "#{attr}=\"#{value}\" " }
  attrs.strip
end
size() click to toggle source

determine the height and width of the octicon based on :size option

# File lib/octicons/octicon.rb, line 65
def size
  size = {
    width:  @width,
    height: @height
  }

  # Specific size
  unless @options[:width].nil? && @options[:height].nil?
    size[:width]  = @options[:width].nil?  ? calculate_width(@options[:height]) : @options[:width]
    size[:height] = @options[:height].nil? ? calculate_height(@options[:width]) : @options[:height]
  end

  size
end
viewbox() click to toggle source
# File lib/octicons/octicon.rb, line 60
def viewbox
  "0 0 #{@width} #{@height}"
end