Sha256: ee6d12cf3b0d934384f17ca38df8f5be643bb1cb60add22ce14d462b6dbc5ff2

Contents?: true

Size: 687 Bytes

Versions: 2

Compression:

Stored size: 687 Bytes

Contents

# frozen_string_literal: true

module Heroicons
  class Icon
    def initialize(name:, variant:, options: {})
      @name = name
      @variant = variant
      @options = options
    end

    def render
      return if file.nil?

      doc = Nokogiri::XML::Document.parse(file)
      svg = doc.at_css("svg")

      @options.each do |k, v|
        svg[dasherize(k.to_s)] = v
      end

      doc.to_html.strip
    end

    private

    def file
      @file ||= File.read(path).force_encoding("UTF-8")
    rescue StandardError
      nil
    end

    def path
      File.join(ICONS_PATH, @variant.to_s, "#{@name}.svg")
    end

    def dasherize(s)
      s.gsub(/_/, "-")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
heroicons-ruby-1.0.0 lib/heroicons/icon.rb
heroicons-ruby-0.1.0 lib/heroicons/icon.rb