Class: Jekyll::CompressImages

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll-compress-images.rb

Instance Method Summary collapse

Instance Method Details

#analyze(image) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/jekyll-compress-images.rb', line 34

def analyze(image)
  if @last_update.has_key? image
    optimize image if @last_update[image] != File.basename(image)
  else
    optimize image
  end
end

#default_optionsObject



27
28
29
30
31
32
# File 'lib/jekyll-compress-images.rb', line 27

def default_options
  {
    "cache_file" => "_compress_images_cache.yml",
    "images_path" => "assets/img/**/*.{gif,png,jpg,jpeg,svg}",
  }
end

#generate(site) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jekyll-compress-images.rb', line 9

def generate(site)

  config = YAML::load_file "_config.yml"

  config = config["compress_images"] || {}

  @config = default_options.merge! config

  @image_optim = ImageOptim.new pngout: false, svgo: true, verbose: false

  @last_update = YAML::load_file @config["cache_file"] if File.file? @config["cache_file"]
  @last_update ||= {}

  Dir.glob(@config["images_path"]) { |image| analyze image }

  File.open(@config["cache_file"], "w") { |file| file.write @last_update.to_yaml }
end

#optimize(image) ⇒ Object



42
43
44
45
46
# File 'lib/jekyll-compress-images.rb', line 42

def optimize(image)
  puts "Optimizing #{image}".green
  @image_optim.optimize_image! image
  @last_update[image] = File.basename image
end