Class: Bridgetown::Webp::WebpGenerator
- Inherits:
-
Generator
- Object
- Generator
- Bridgetown::Webp::WebpGenerator
- Defined in:
- lib/bridgetown-webp/webpGenerator.rb
Overview
class WebpFile
Instance Method Summary collapse
-
#generate(site) ⇒ Object
Generate paginated pages if necessary (Default entry point) site - The Site.
Instance Method Details
#generate(site) ⇒ Object
Generate paginated pages if necessary (Default entry point) site - The Site.
Returns nothing.
27 28 29 30 31 32 33 34 35 36 37 38 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 105 106 107 108 109 110 |
# File 'lib/bridgetown-webp/webpGenerator.rb', line 27 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'] Bridgetown.logger.info "WebP:","Disabled in site.config." return end Bridgetown.logger.debug "WebP:","Starting" # 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 # If nesting is enabled, get all the nested directories too if @config['nested'] newdir = [] for imgdir in @config['img_dir'] # Get every directory below (and including) imgdir, recursively newdir.concat(Dir.glob(imgdir + "/**/")) end @config['img_dir'] = newdir end # Counting the number of files generated file_count = 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'] imgdir_source = File.join(site.source, imgdir) imgdir_destination = File.join(site.dest, imgdir) FileUtils::mkdir_p(imgdir_destination) Bridgetown.logger.info "WebP:","Processing #{imgdir_source}" # handle only jpg, jpeg, png and gif for imgfile in Dir[imgdir_source + "**/*.*"] imgfile_relative_path = File.dirname(imgfile.sub(imgdir_source, "")) # Skip empty stuff file_ext = File.extname(imgfile).downcase # If the file is not one of the supported formats, exit early next if !@config['formats'].include? file_ext # TODO: Do an exclude check # Create the output file path outfile_filename = if @config['append_ext'] File.basename(imgfile) + '.webp' else file_noext = File.basename(imgfile, file_ext) file_noext + ".webp" end FileUtils::mkdir_p(imgdir_destination + imgfile_relative_path) outfile_fullpath_webp = File.join(imgdir_destination + imgfile_relative_path, outfile_filename) # 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 @config['regenerate'] || !File.file?(outfile_fullpath_webp) || File.mtime(outfile_fullpath_webp) <= File.mtime(imgfile) Bridgetown.logger.info "WebP:", "Change to source image file #{imgfile} detected, regenerating WebP" # Generate the file WebpExec.run(@config['quality'], @config['flags'], imgfile, outfile_fullpath_webp) file_count += 1 end if File.file?(outfile_fullpath_webp) # Keep the webp file from being cleaned by Bridgetown site.static_files << WebpFile.new(site, site.dest, File.join(imgdir, imgfile_relative_path), outfile_filename) end end # dir.foreach end # img_dir Bridgetown.logger.info "WebP:","Generator Complete: #{file_count} file(s) generated" end |