Class: Jekyll::Webp::WebpGenerator

Inherits:
Generator
  • Object
show all
Defined in:
lib/jekyll-webp-resize/webpGenerator.rb

Instance Method Summary collapse

Instance Method Details

#compute_file_resizes(output_file, resize = @config['resize']) ⇒ Object

Return a map -> filename



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/jekyll-webp-resize/webpGenerator.rb', line 22

def compute_file_resizes(output_file, resize=@config['resize'])
  output = {0 => output_file}
  if resize.length == 0
    return output
  end

  for resize_entry in resize
    dirname = File.dirname(output_file)
    extension = File.extname(output_file)
    basename = File.basename(output_file, extension)
    new_output_file = dirname + "/" + basename + "-" + resize_entry.to_s + extension
    output[resize_entry] = new_output_file
  end

  return output
end

#generate(site) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/jekyll-webp-resize/webpGenerator.rb', line 39

def generate(site)
  # Retrieve and merge the configuration from the site yml file
  @config = DEFAULT.merge(site.config['webp'] || {})

  # If disabled then simply quit
  if !@config['enabled']
    Jekyll.logger.info("WebP:", "Disabled in site.config.")
    return
  end

  # If the site destination directory has not yet been created then create it now. Otherwise, we cannot write our file there.
  Dir::mkdir(site.dest) if !File.directory? site.dest

  # Counting the number of files generated
  files_generated = 0

  # Iterate through every image in each of the image folders and create a webp image
  # if one has not been created already for that image.
  for imgdir in @config['img_dir']
    source_directory = File.join(site.source, imgdir)
    destination_directory = File.join(site.dest, imgdir)
    FileUtils::mkdir_p(destination_directory)
    Jekyll.logger.info("WebP:", "Processing #{source_directory}")

    for file in Dir[source_directory + "**/*.*"]
      prefix = File.dirname(file.sub(source_directory, ""))

      # If the file is not one of the supported formats, exit early
      extension = File.extname(file).downcase
      next if !@config['formats'].include? extension

      # Create the output file path
      filename = File.basename(file, extension) + ".webp"
      FileUtils::mkdir_p(destination_directory + prefix)
      output_full_path = File.join(destination_directory + prefix, filename)

      compute_file_resizes(output_full_path).each do |size, output|
        # Check if the file already has a webp alternative?
        # If we're force rebuilding all webp files then ignore the check
        # also check the modified time on the files to ensure that the webp file
        # is newer than the source file, if not then regenerate
        if !File.file?(output) ||
           (File.mtime(output) <= File.mtime(file))
          # Jekyll.logger.info("WebP:", "Change to image file #{file} detected, regenerating WebP")

          # Generate the file
          WebpExec.run(file, output, @config['flags'], size)
          files_generated += 1
        end
        if File.file?(output)
          # Keep the webp file from being cleaned by Jekyll
          site.static_files << WebpFile.new(site,
                                            site.dest,
                                            File.join(imgdir, prefix),
                                            File.basename(output))
          site.static_files << WebpFile.new(site,
                                            site.dest,
                                            File.join(imgdir, prefix),
                                            File.basename(output).sub("webp", "jpg"))
        end
      end
    end # dir.foreach
  end # img_dir

  Jekyll.logger.info("WebP:", "Generator Complete: #{files_generated} file(s) generated")
end