@@ -5,7 +5,7 @@ module Jekyll
55 module Webp
66
77 #
8- # A static file to hold the generated webp image after generation
8+ # A static file to hold the generated webp image after generation
99 # so that Jekyll will copy it into the site output directory
1010 class WebpFile < StaticFile
1111 def write ( dest )
@@ -31,11 +31,11 @@ def generate(site)
3131
3232 # If disabled then simply quit
3333 if !@config [ 'enabled' ]
34- Jekyll . logger . info "WebP:" , "Disabled in site.config."
34+ Jekyll . logger . info "WebP:" , "Disabled in site.config."
3535 return
3636 end
3737
38- Jekyll . logger . debug "WebP:" , "Starting"
38+ Jekyll . logger . debug "WebP:" , "Starting"
3939
4040 # If the site destination directory has not yet been created then create it now. Otherwise, we cannot write our file there.
4141 Dir ::mkdir ( site . dest ) if !File . directory? site . dest
@@ -45,55 +45,57 @@ def generate(site)
4545
4646 # Iterate through every image in each of the image folders and create a webp image
4747 # if one has not been created already for that image.
48- for source in @config [ 'img_dir' ]
49- source_full_path = File . join ( site . source , source )
50- destination_full_path = File . join ( site . dest , source )
51- FileUtils ::mkdir_p destination_full_path
52- Jekyll . logger . info "WebP:" , "Processing #{ source_full_path } "
53-
54- for file_full_path in Dir [ source_full_path + "/**/*.*" ]
55- file_relative_path = file_full_path . sub ( source_full_path , "" )
56-
57- file_extension = File . extname ( file_full_path ) . downcase
58-
59- # If the file is not one of the supported formats, exit early
60- next if !@config [ 'formats' ] . include? file_extension
61-
62- # TODO: Do an exclude check
63-
64- # Create the output file path
65- file_no_extension = File . basename ( file_full_path , file_extension )
66- relative_path = File . dirname ( file_relative_path )
67- FileUtils ::mkdir_p ( destination_full_path + relative_path )
68- outfile_fullpath_webp = File . join ( destination_full_path + relative_path , file_no_extension + ".webp" )
69-
70- # Check if the file already has a webp alternative?
71- # If we're force rebuilding all webp files then ignore the check
72- # also check the modified time on the files to ensure that the webp file
73- # is newer than the source file, if not then regenerate
74- next if !@config [ 'regenerate' ] && File . file? ( outfile_fullpath_webp ) &&
75- File . mtime ( outfile_fullpath_webp ) > File . mtime ( source_full_path )
76-
77- if ( File . file? ( outfile_fullpath_webp ) &&
78- File . mtime ( outfile_fullpath_webp ) <= File . mtime ( source_full_path ) )
79- Jekyll . logger . info "WebP:" , "Change to source image file #{ file } detected, regenerating WebP"
80- end
81-
82- Jekyll . logger . info "WebP:" , "Generating #{ outfile_fullpath_webp } "
83- # Generate the file
84- WebpExec . run ( @config [ 'quality' ] , file_full_path , outfile_fullpath_webp )
85-
86- # Keep the webp file from being cleaned by Jekyll
87- site . static_files << WebpFile . new ( site , site . dest , source , outfile_fullpath_webp )
88- file_count += 1
89- end # file_full_path
48+ for imgdir in @config [ 'img_dir' ]
49+ imgdir_source = File . join ( site . source , imgdir )
50+ imgdir_destination = File . join ( site . dest , imgdir )
51+ FileUtils ::mkdir_p ( imgdir_destination )
52+ Jekyll . logger . info "WebP:" , "Processing #{ imgdir_source } "
53+
54+ # handle only jpg, jpeg, png and gif
55+ for imgfile in Dir [ imgdir_source + "**/*.*" ]
56+ imgfile_relative_path = File . dirname ( imgfile . sub ( imgdir_source , "" ) )
57+
58+ # Skip empty stuff
59+ file_ext = File . extname ( imgfile ) . downcase
60+
61+ # If the file is not one of the supported formats, exit early
62+ next if !@config [ 'formats' ] . include? file_ext
63+
64+ # TODO: Do an exclude check
65+
66+ # Create the output file path
67+ file_noext = File . basename ( imgfile , file_ext )
68+ outfile_filename = file_noext + ".webp"
69+ FileUtils ::mkdir_p ( imgdir_destination + imgfile_relative_path )
70+ outfile_fullpath_webp = File . join ( imgdir_destination + imgfile_relative_path , outfile_filename )
71+
72+ # Check if the file already has a webp alternative?
73+ # If we're force rebuilding all webp files then ignore the check
74+ # also check the modified time on the files to ensure that the webp file
75+ # is newer than the source file, if not then regenerate
76+ next if !@config [ 'regenerate' ] && File . file? ( outfile_fullpath_webp ) &&
77+ File . mtime ( outfile_fullpath_webp ) > File . mtime ( imgfile )
78+
79+ if ( File . file? ( outfile_fullpath_webp ) &&
80+ File . mtime ( outfile_fullpath_webp ) <= File . mtime ( imgfile ) )
81+ Jekyll . logger . info "WebP:" , "Change to source image file #{ imgfile } detected, regenerating WebP"
82+ end
83+
84+ # Generate the file
85+ WebpExec . run ( @config [ 'quality' ] , imgfile , outfile_fullpath_webp )
86+
87+ # Keep the webp file from being cleaned by Jekyll
88+ site . static_files << WebpFile . new ( site , site . dest , imgdir , outfile_fullpath_webp )
89+ file_count += 1
90+
91+ end # dir.foreach
9092 end # img_dir
9193
92- Jekyll . logger . info "WebP:" , "Generator Complete: #{ file_count } file(s) generated"
94+ Jekyll . logger . info "WebP:" , "Generator Complete: #{ file_count } file(s) generated"
9395
9496 end #function generate
9597
9698 end #class WebPGenerator
97-
99+
98100 end #module Webp
99101end #module Jekyll
0 commit comments