11require 'jekyll/document'
2+ require 'fileutils'
23
34module Jekyll
45 module Webp
56
67 #
7- # A static file to hold the generated webp image after generation
8+ # A static file to hold the generated webp image after generation
89 # so that Jekyll will copy it into the site output directory
910 class WebpFile < StaticFile
1011 def write ( dest )
@@ -30,11 +31,11 @@ def generate(site)
3031
3132 # If disabled then simply quit
3233 if !@config [ 'enabled' ]
33- Jekyll . logger . info "WebP:" , "Disabled in site.config."
34+ Jekyll . logger . info "WebP:" , "Disabled in site.config."
3435 return
3536 end
3637
37- Jekyll . logger . debug "WebP:" , "Starting"
38+ Jekyll . logger . debug "WebP:" , "Starting"
3839
3940 # If the site destination directory has not yet been created then create it now. Otherwise, we cannot write our file there.
4041 Dir ::mkdir ( site . dest ) if !File . directory? site . dest
@@ -44,57 +45,55 @@ def generate(site)
4445
4546 # Iterate through every image in each of the image folders and create a webp image
4647 # if one has not been created already for that image.
47- for imgdir in @config [ 'img_dir' ]
48- imgdir_source = File . join ( site . source , imgdir )
49- Jekyll . logger . info "WebP:" , "Processing #{ imgdir_source } "
50-
51- # handle only jpg, jpeg, png and gif
52- Dir . foreach ( imgdir_source ) do |imgfile |
53-
54- # Skip empty stuff
55- next if imgfile == '.' or imgfile == '..'
56- file_ext = File . extname ( imgfile ) . downcase
57-
58- # If the file is not one of the supported formats, exit early
59- next if !@config [ 'formats' ] . include? file_ext
60-
61- # TODO: Do an exclude check
62-
63- # Create the output file path
64- file_noext = File . basename ( imgfile , file_ext )
65- outfile_filename = file_noext + ".webp"
66- outfile_fullpath_webp = File . join ( imgdir_source , outfile_filename )
67-
68- # Create the full input file name
69- infile_fullpath = File . join ( imgdir_source , imgfile )
70-
71- # Check if the file already has a webp alternative?
72- # If we're force rebuilding all webp files then ignore the check
73- # also check the modified time on the files to ensure that the webp file
74- # is newer than the source file, if not then regenerate
75- next if !@config [ 'regenerate' ] && File . file? ( outfile_fullpath_webp ) &&
76- File . mtime ( outfile_fullpath_webp ) > File . mtime ( infile_fullpath )
77-
78- if ( File . file? ( outfile_fullpath_webp ) &&
79- File . mtime ( outfile_fullpath_webp ) <= File . mtime ( infile_fullpath ) )
80- Jekyll . logger . info "WebP:" , "Change to source image file #{ imgfile } detected, regenerating WebP"
81- end
82-
83- # Generate the file
84- WebpExec . run ( @config [ 'quality' ] , infile_fullpath , outfile_fullpath_webp )
85-
86- # Keep the webp file from being cleaned by Jekyll
87- site . static_files << WebpFile . new ( site , site . dest , imgdir , outfile_filename )
88- file_count += 1
89-
90- end # dir.foreach
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
9190 end # img_dir
9291
93- Jekyll . logger . info "WebP:" , "Generator Complete: #{ file_count } file(s) generated"
92+ Jekyll . logger . info "WebP:" , "Generator Complete: #{ file_count } file(s) generated"
9493
9594 end #function generate
9695
9796 end #class WebPGenerator
98-
97+
9998 end #module Webp
100- end #module Jekyll
99+ end #module Jekyll
0 commit comments