Sha256: 0a8bb9c190408317a63edf85e943b6496dbd258660d60bcf34fbf3ac6c70ce49

Contents?: true

Size: 1.98 KB

Versions: 49

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true
module Dato
  module Utils
    class FaviconTagsBuilder
      attr_reader :theme_color, :site

      APPLE_TOUCH_ICON_SIZES = [57, 60, 72, 76, 114, 120, 144, 152, 180].freeze
      ICON_SIZES = [16, 32, 96, 192].freeze
      WINDOWS_SIZES = [[70, 70], [150, 150], [310, 310], [310, 150]].freeze

      def initialize(site, theme_color)
        @site = site
        @theme_color = theme_color
      end

      def meta_tags
        [
          build_icon_tags,
          build_apple_icon_tags,
          build_windows_tags,
          build_color_tags,
          build_app_name_tag
        ].flatten.compact
      end

      def build_apple_icon_tags
        return unless site.favicon

        APPLE_TOUCH_ICON_SIZES.map do |size|
          link_tag(
            'apple-touch-icon',
            url(size),
            sizes: "#{size}x#{size}"
          )
        end
      end

      def build_icon_tags
        return unless site.favicon

        ICON_SIZES.map do |size|
          link_tag(
            'icon',
            url(size),
            sizes: "#{size}x#{size}",
            type: "image/#{site.favicon.format}"
          )
        end
      end

      def build_windows_tags
        return unless site.favicon

        WINDOWS_SIZES.map do |(w, h)|
          meta_tag("msapplication-square#{w}x#{h}logo", url(w, h))
        end
      end

      def build_app_name_tag
        meta_tag('application-name', site.name)
      end

      def build_color_tags
        return unless theme_color

        [
          meta_tag('theme-color', theme_color),
          meta_tag('msapplication-TileColor', theme_color)
        ]
      end

      def url(width, height = width)
        site.favicon.url(w: width, h: height)
      end

      def meta_tag(name, value)
        { tag_name: 'meta', attributes: { name: name, content: value } }
      end

      def link_tag(rel, href, attrs = {})
        { tag_name: 'link', attributes: attrs.merge(rel: rel, href: href) }
      end
    end
  end
end

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
dato-0.3.28 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.27 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.26 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.25 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.24 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.23 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.22 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.21 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.20 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.19 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.18 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.17 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.16 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.15 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.14 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.13 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.12 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.9.1 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.11 lib/dato/utils/favicon_tags_builder.rb
dato-0.3.10 lib/dato/utils/favicon_tags_builder.rb