Skip to content

Commit e436d88

Browse files
Emilio Del Tessandorosverrirs
authored andcommitted
recursively select images and don't write to source directory
1 parent b01eda4 commit e436d88

File tree

3 files changed

+55
-56
lines changed

3 files changed

+55
-56
lines changed

lib/jekyll-webp/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module Jekyll
22
module Webp
3-
VERSION = "0.1.0"
3+
VERSION = "0.1.1"
44
# When modifying remember to issue a new tag command in git before committing, then push the new tag
55
# git tag -a v0.1.0 -m "Gem v0.1.0"
66
# git push origin --tags

lib/jekyll-webp/webpExec.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def self.run(quality, input_file, output_file)
1717
# What is the OS and architecture specific executable name?
1818
exe_name = WebpExec.exe_name
1919

20-
# We need to locate the Gems bin path as we're currently running inside the
20+
# We need to locate the Gems bin path as we're currently running inside the
2121
# jekyll site working directory
2222
# http://stackoverflow.com/a/10083594/779521
2323
gem_spec = Gem::Specification.find_by_name("jekyll-webp")
@@ -27,8 +27,8 @@ def self.run(quality, input_file, output_file)
2727
full_path = File.join(gem_root, bin_path, exe_name)
2828

2929
# Construct the full program call
30-
cmd = "\"#{full_path}\" -quiet -mt -q #{quality.to_s} \"#{input_file}\" -o \"#{output_file}\""
31-
30+
cmd = "\"#{full_path}\" -quiet -mt -m 6 -pass 10 -q #{quality.to_s} \"#{input_file}\" -o \"#{output_file}\""
31+
3232
# Execute the command
3333
stdin, stdout, stderr = Open3.popen3(cmd)
3434

@@ -60,7 +60,7 @@ def self.exe_name
6060
end #function exe_name
6161

6262
end #class WebpExec
63-
63+
6464
end #module Webp
6565

6666
module OS
@@ -88,4 +88,4 @@ def OS.x64?
8888
return 1.size == 8
8989
end
9090
end #module OS
91-
end #module Jekyll
91+
end #module Jekyll

lib/jekyll-webp/webpGenerator.rb

Lines changed: 49 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
require 'jekyll/document'
2+
require 'fileutils'
23

34
module 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

Comments
 (0)