lib/jekyll/favicon/static_file/convertible.rb in jekyll-favicon-1.0.0.pre.1 vs lib/jekyll/favicon/static_file/convertible.rb in jekyll-favicon-1.0.0.pre.2

- old
+ new

@@ -1,9 +1,9 @@ # frozen_string_literal: true -require 'jekyll/favicon/configuration/defaults' -require 'jekyll/favicon/utils' +require "jekyll/favicon/configuration/defaults" +require "jekyll/favicon/utils" module Jekyll module Favicon class StaticFile < Jekyll::StaticFile # Create static file based on a source file @@ -22,92 +22,95 @@ end def convertible_patch(configuration) Utils.patch configuration do |value| case value - when :sizes then sizes.join ' ' + when :sizes then sizes.join " " else value end end end def sizes - if (match = name.match(/^.*-(\d+x\d+)\..*$/)) then [match[1]] - elsif (define = convert_spec['define']) - define.split('=').last.split(',').collect { |size| [size, size].join 'x' } - elsif (resize = convert_spec['resize']) then [resize] - elsif (scale = convert_spec['scale']) then [scale] + if (match = Utils.name_to_size(name)) then [match[1]] + elsif (define = Utils.define_to_size(convert_spec["define"])) then define + elsif (resize = convert_spec["resize"]) then [resize] + elsif (scale = convert_spec["scale"]) then [scale] end end # Jekyll::StaticFile method # asks if dest mtime is older than source mtime after original modified? def modified? - super || self.class.mtimes[href] < mtime + super || self.class.mtimes.fetch(href, -1) < mtime end # Jekyll::StaticFile method # adds dest mtime to list after original write - # :reek:ControlParameter def write(dest) super(dest) && self.class.mtimes[href] = mtime end private # Jekyll::StaticFile method # add file creation instead of copying def copy_file(dest_path) case @extname - when '.svg' then super(dest_path) - when '.ico', '.png' + when ".svg" then super(dest_path) + when ".ico", ".png" Utils.convert path, dest_path, convert else Jekyll.logger.warn "Jekyll::Favicon: Can't generate " \ " #{dest_path}. Extension not supported." end end def convert_allow_empty? - @extname == '.svg' && @extname == File.extname(path) + @extname == ".svg" && @extname == File.extname(path) end def convert_spec - spec.fetch 'convert', {} + spec.fetch "convert", {} end + def convertible_keys + convertible_defaults["defaults"].keys + end + def convert_normalize(options) return {} unless options - Utils.compact options.slice(*convertible_defaults['defaults'].keys) + Utils.slice_and_compact options, convertible_keys end - def convert_defaults - convertible_defaults.dig File.extname(path), @extname + def convert_patch(options) + patched_options = convert_patch_options options + Utils.slice_and_compact patched_options, convertible_keys end - # :reek:FeatureEnvy - def convert_patch(options) - %w[density extent].each do |name| - method = "convert_patch_#{name}".to_sym - options.merge! name => send(method, options[name]) + def convert_patch_options(options) + %w[density extent].each_with_object(options) do |name, memo| + method = "convert_patch_option_#{name}".to_sym + memo[name] = send(method, options[name]) end - Utils.compact options.slice(*convertible_defaults['defaults'].keys) end - def convert_patch_density(density) + def convert_patch_option_density(density) case density when :max - length = sizes.collect { |size| size.split('x').max }.max.to_i + length = sizes.collect { |size| size.split("x").collect(&:to_i) } + .flatten + .max length * 3 else density end end - def convert_patch_extent(extent) + def convert_patch_option_extent(extent) case extent when :auto if (size = sizes.first) - width, height = size.split 'x' + width, height = size.split "x" size if width != height end else extent end end