Class: Middleman::ImageOptim::Extension

Inherits:
Extension
  • Object
show all
Defined in:
lib/middleman/image_optim/extension.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeExtension

Returns a new instance of Extension.



21
22
23
24
# File 'lib/middleman/image_optim/extension.rb', line 21

def initialize(*)
  super
  @image_optim = ::ImageOptim.new(options.to_h.merge(verbose: logger.level == 0))
end

Instance Attribute Details

#image_optimObject (readonly)

Returns the value of attribute image_optim.



19
20
21
# File 'lib/middleman/image_optim/extension.rb', line 19

def image_optim
  @image_optim
end

Instance Method Details

#manipulate_resource_list(resources) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/middleman/image_optim/extension.rb', line 26

def manipulate_resource_list(resources)
  optimized_files = image_optim.optimize_images(resources.map(&:source_file))

  resources.zip(optimized_files).map do |resource, (_, optimized_file)|
    if optimized_file
      reduction = optimized_file.original_size - optimized_file.size
      if reduction > 0
        logger.debug "#{resource.path} reduced #{reduction / 1024}KB (#{'%.2f' % (100.0 * reduction / optimized_file.original_size)}%)"
        Middleman::Sitemap::Resource.new(app.sitemap, resource.path, optimized_file.to_s)
      end
    end or resource
  end
end