Sha256: 1c6e62a3f7d5fb8a6b11d8f7fce8c4d4da07d79a9867129a0a1b9440bdd00d94

Contents?: true

Size: 1.98 KB

Versions: 171

Compression:

Stored size: 1.98 KB

Contents

module Octicons
  class Octicon

    attr_reader :path, :options, :width, :height, :symbol, :keywords

    def initialize(symbol, options = {})
      @symbol = symbol.to_s
      if octicon = Octicons::OCTICON_SYMBOLS[@symbol]

        @path = octicon["path"]
        @width = octicon["width"].to_i
        @height = octicon["height"].to_i

        @keywords = octicon["keywords"]

        @options = options
        @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

    # Returns an string representing a <svg> tag
    def to_svg
      "<svg #{html_attributes}>#{@path}</svg>"
    end

    private

    def html_attributes
      attrs = ""
      @options.each { |attr, value| attrs += "#{attr}=\"#{value}\" " }
      attrs.strip
    end

    # add some accessibility features to svg
    def a11y
      accessible = {}

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

      accessible
    end

    # prepare the octicon class
    def classes
      "octicon octicon-#{@symbol} #{@options[:class]} ".strip
    end

    def viewbox
      "0 0 #{@width} #{@height}"
    end

    # determine the height and width of the octicon based on :size option
    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

    def calculate_width(height)
      (height.to_i * @width) / @height
    end

    def calculate_height(width)
      (width.to_i * @height) / @width
    end
  end
end

Version data entries

171 entries across 171 versions & 1 rubygems

Version Path
octicons-0.0.0.pre.a9a3db9 lib/octicons/octicon.rb
octicons-8.5.0.pre.7e0ac2b lib/octicons/octicon.rb
octicons-0.0.0.pre.599fde6 lib/octicons/octicon.rb
octicons-8.4.2 lib/octicons/octicon.rb
octicons-8.4.2.pre.c846faf lib/octicons/octicon.rb
octicons-8.4.2.pre.074ae80 lib/octicons/octicon.rb
octicons-0.0.0.pre.6688540 lib/octicons/octicon.rb
octicons-0.0.0.pre.132950f lib/octicons/octicon.rb
octicons-0.0.0.pre.7ec36bd lib/octicons/octicon.rb
octicons-0.0.0.pre.7c22536 lib/octicons/octicon.rb
octicons-0.0.0.pre.6a42072 lib/octicons/octicon.rb
octicons-0.0.0.pre.0962ea2 lib/octicons/octicon.rb
octicons-0.0.0.pre.61f3707 lib/octicons/octicon.rb
octicons-0.0.0.pre.af8bd6a lib/octicons/octicon.rb
octicons-0.0.0.pre.51409f7 lib/octicons/octicon.rb
octicons-0.0.0.pre.8fe30ae lib/octicons/octicon.rb
octicons-0.0.0.pre.13af5f6 lib/octicons/octicon.rb
octicons-0.0.0.pre.3f87998 lib/octicons/octicon.rb
octicons-0.0.0.pre.082eb92 lib/octicons/octicon.rb
octicons-0.0.0.pre.1de7750 lib/octicons/octicon.rb