Skip to content

Commit

Permalink
Merge pull request #4 from kball/enable-appending-extension
Browse files Browse the repository at this point in the history
Enable appending extension
  • Loading branch information
sverrirs authored Oct 28, 2019
2 parents 22a08ec + 686404b commit fbae458
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ webp:
# List of files or directories to exclude
# e.g. custom or hand generated webp conversion files
exclude: []

# append '.webp' to filename after original extension rather than replacing it.
# Default transforms `image.png` to `image.webp`, while changing to true transforms `image.png` to `image.png.webp`
append_ext: false
############################################################
```

Expand Down
9 changes: 7 additions & 2 deletions lib/jekyll-webp/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ module Webp
# add ".gif" to the format list to generate webp for animated gifs as well
'formats' => [".jpeg", ".jpg", ".png", ".tiff"],

# File extensions for animated gif files
# append .webp to existing extension instead of replacing it
# (Enables more efficient nginx rules.
# See http://www.lazutkin.com/blog/2014/02/23/serve-files-with-nginx-conditionally/)
'append_ext' => false,

# File extensions for animated gif files
'gifs' => [".gif"],

# Set to true to always regenerate existing webp files
Expand All @@ -33,7 +38,7 @@ module Webp
# e.g. custom or hand generated webp conversion files
'exclude' => [],

# List of files or directories to explicitly include
# List of files or directories to explicitly include
# e.g. single files outside of the main image directories
'include' => []
}
Expand Down
18 changes: 11 additions & 7 deletions lib/jekyll-webp/webpGenerator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module Jekyll
module Webp

#
# A static file to hold the generated webp image after generation
# A static file to hold the generated webp image after generation
# so that Jekyll will copy it into the site output directory
class WebpFile < StaticFile
def write(dest)
Expand Down Expand Up @@ -50,22 +50,26 @@ def generate(site)
imgdir_destination = File.join(site.dest, imgdir)
FileUtils::mkdir_p(imgdir_destination)
Jekyll.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
file_noext = File.basename(imgfile, file_ext)
outfile_filename = file_noext+ ".webp"
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)

Expand Down Expand Up @@ -96,6 +100,6 @@ def generate(site)
end #function generate

end #class WebPGenerator

end #module Webp
end #module Jekyll

0 comments on commit fbae458

Please sign in to comment.