<commit_before>module FaviconMaker
  require "mini_magick"
  require 'fileutils'

  class Generator

    ICON_VERSIONS = {
      :apple_144 => {:filename => "apple-touch-icon-144x144-precomposed.png", :dimensions => "144x144", :format => "png"},
      :apple_114 => {:filename => "apple-touch-icon-114x114-precomposed.png", :dimensions => "114x114", :format => "png"},
      :apple_72 => {:filename => "apple-touch-icon-72x72-precomposed.png", :dimensions => "72x72", :format => "png"},
      :apple_57 => {:filename => "apple-touch-icon-57x57-precomposed.png", :dimensions => "57x57", :format => "png"},
      :apple_pre => {:filename => "apple-touch-icon-precomposed.png", :dimensions => "57x57", :format => "png"},
      :apple => {:filename => "apple-touch-icon.png", :dimensions => "57x57", :format => "png"},
      :fav_png => {:filename => "favicon.png", :dimensions => "16x16", :format => "png"},
      :fav_ico => {:filename => "favicon.ico", :dimensions => "16x16", :format => "ico"}
    }

    class << self

      def create_versions(options={}, &block)
        options = {
          :versions => ICON_VERSIONS.keys,
          :custom_versions => {},
          :root_dir => File.dirname(__FILE__),
          :input_dir => "favicons",
          :base_image => "favicon_base.png",
          :output_dir => "favicons_output",
          :copy => false
        }.merge(options)

        raise ArgumentError unless options[:versions].is_a? Array
        base_path = File.join(options[:root_dir], options[:input_dir])
        input_path = File.join(base_path, options[:base_image])

        icon_versions = ICON_VERSIONS.merge(options[:custom_versions])
        (options[:versions] + options[:custom_versions].keys).uniq.each do |version|
          version = icon_versions[version]
          composed_path = File.join(base_path, version[:filename])
          output_path = File.join(options[:root_dir], options[:output_dir], version[:filename])

          build_mode = nil
          # check for self composed icon file
          if File.exist?(composed_path)
            if options[:copy]
              FileUtils.cp composed_path, output_path
              build_mode = :copied
            end
          else
            image = MiniMagick::Image.open(input_path)
            image.define "png:include-chunk=none,trns,gama"
            image.resize version[:dimensions]
            image.format version[:format]
            image.flatten
            image.write output_path
            build_mode = :generated
          end

          if block_given?
            yield output_path, build_mode
          end
        end
      end

    end
  end
end<commit_msg>flatten removes transparency so use it only for ico format<commit_after>module FaviconMaker
  require "mini_magick"
  require 'fileutils'

  class Generator

    ICON_VERSIONS = {
      :apple_144 => {:filename => "apple-touch-icon-144x144-precomposed.png", :dimensions => "144x144", :format => "png"},
      :apple_114 => {:filename => "apple-touch-icon-114x114-precomposed.png", :dimensions => "114x114", :format => "png"},
      :apple_72 => {:filename => "apple-touch-icon-72x72-precomposed.png", :dimensions => "72x72", :format => "png"},
      :apple_57 => {:filename => "apple-touch-icon-57x57-precomposed.png", :dimensions => "57x57", :format => "png"},
      :apple_pre => {:filename => "apple-touch-icon-precomposed.png", :dimensions => "57x57", :format => "png"},
      :apple => {:filename => "apple-touch-icon.png", :dimensions => "57x57", :format => "png"},
      :fav_png => {:filename => "favicon.png", :dimensions => "16x16", :format => "png"},
      :fav_ico => {:filename => "favicon.ico", :dimensions => "16x16", :format => "ico"}
    }

    class << self

      def create_versions(options={}, &block)
        options = {
          :versions => ICON_VERSIONS.keys,
          :custom_versions => {},
          :root_dir => File.dirname(__FILE__),
          :input_dir => "favicons",
          :base_image => "favicon_base.png",
          :output_dir => "favicons_output",
          :copy => false
        }.merge(options)

        raise ArgumentError unless options[:versions].is_a? Array
        base_path = File.join(options[:root_dir], options[:input_dir])
        input_path = File.join(base_path, options[:base_image])

        icon_versions = ICON_VERSIONS.merge(options[:custom_versions])
        (options[:versions] + options[:custom_versions].keys).uniq.each do |version|
          version = icon_versions[version]
          composed_path = File.join(base_path, version[:filename])
          output_path = File.join(options[:root_dir], options[:output_dir], version[:filename])

          build_mode = nil
          # check for self composed icon file
          if File.exist?(composed_path)
            if options[:copy]
              FileUtils.cp composed_path, output_path
              build_mode = :copied
            end
          else
            image = MiniMagick::Image.open(input_path)
            image.define "png:include-chunk=none,trns,gama"
            image.resize version[:dimensions]
            image.format version[:format]
            image.flatten if version[:format] == 'ico'
            image.write output_path
            build_mode = :generated
          end

          if block_given?
            yield output_path, build_mode
          end
        end
      end

    end
  end
end
{'fraction_non_alphanumeric': 0.14531654942613847, 'fraction_numerical': 0.024435394298407995, 'mean_word_length': 2.6261744966442953, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 81, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1184806', 'n_tokens_mistral': 1731, 'n_tokens_neox': 1602, 'n_words': 321}