lib/middleman-webp/converter.rb in middleman-webp-0.4.1 vs lib/middleman-webp/converter.rb in middleman-webp-0.4.2

- old
+ new

@@ -1,45 +1,44 @@ require 'middleman-webp/options' module Middleman module WebP + ## + # Handles finding image assets and running them through the conversion using + # external cwebp and gif2webp commandline tools class Converter SUFFIX_RE = /(jpe?g|png|tiff?|gif)$/i - def initialize(app, options = {}, builder, logger) + def initialize(app, builder, options = {}) @app = app @options = Middleman::WebP::Options.new(options) @builder = builder - @log = logger end def convert @original_size = 0 @converted_size = 0 convert_images(image_files) do |src, dst| - if !!@options.allow_skip && dst.size >= src.size - next reject_file(dst) - end + next reject_file(dst) if @options.allow_skip && dst.size >= src.size @original_size += src.size @converted_size += dst.size - @log.info "#{dst.path} "\ + @builder.trigger :created, "#{dst.path} "\ "(#{change_percentage(src.size, dst.size)} smaller)" end print_summary end - def convert_images(paths, &after_conversion) + def convert_images(paths, &_after_conversion) paths.each do |p| begin dst = destination_path(p) exec_convert_tool(p, dst) yield File.new(p), File.new(dst.to_s) rescue StandardError => e - @log.error "Converting #{p} failed" - @log.debug e.to_s + @builder.trigger :error, "Converting #{p} failed", e.backtrace end end end def exec_convert_tool(src, dst) @@ -53,19 +52,19 @@ def tool_for(file) file.to_s =~ /gif$/i ? 'gif2webp' : 'cwebp' end def reject_file(file) - @log.warn "#{file.path} skipped" + @builder.trigger :deleted, "#{file.path} skipped" File.unlink(file) end def print_summary savings = @original_size - @converted_size - @log.info 'Total conversion savings: '\ - "#{number_to_human_size(savings)} "\ - "(#{change_percentage(@original_size, @converted_size)})", :blue + @builder.trigger :webp, '', 'Total conversion savings: '\ + "#{number_to_human_size(savings)} "\ + "(#{change_percentage(@original_size, @converted_size)})" end # Calculate change percentage of converted file size # # src - File instance of the source @@ -82,26 +81,20 @@ format('%g %%', format('%.2f', 100 - 100.0 * dst_size / src_size)) end def destination_path(src_path) - if @options.append_extension - dst_name = "#{src_path.basename.to_s}.webp" - else - dst_name = src_path.basename.to_s.gsub(SUFFIX_RE, 'webp') - end + dst_name = if @options.append_extension + "#{src_path.basename}.webp" + else + src_path.basename.to_s.gsub(SUFFIX_RE, 'webp') + end src_path.parent.join(dst_name) end def image_files - if @options.run_before_build - app_dir = Pathname(File.join(@app.root, @app.config[:source])) - else - app_dir = Pathname(@app.config[:build_dir]) - end - - all = ::Middleman::Util.all_files_under(app_dir) + all = ::Middleman::Util.all_files_under(site_dir) images = all.select { |p| p.to_s =~ SUFFIX_RE } # Reject files matching possible ignore patterns @options.ignore.reduce(images) do |arr, matcher| arr.select { |path| !matcher.matches? path } @@ -124,9 +117,19 @@ units = %w(B KiB MiB GiB TiB PiB) exponent = (Math.log(n) / Math.log(1024)).to_i format("%g #{units[exponent]}", format('%.2f', n.to_f / 1024**exponent)) + end + + private + + def site_dir + if @options.run_before_build + Pathname(File.join(@app.root, @app.config[:source])) + else + Pathname(@app.config[:build_dir]) + end end end end end