Skip to content

Commit fbae458

Browse files
authored
Merge pull request #4 from kball/enable-appending-extension
Enable appending extension
2 parents 22a08ec + 686404b commit fbae458

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ webp:
5555
# List of files or directories to exclude
5656
# e.g. custom or hand generated webp conversion files
5757
exclude: []
58+
59+
# append '.webp' to filename after original extension rather than replacing it.
60+
# Default transforms `image.png` to `image.webp`, while changing to true transforms `image.png` to `image.png.webp`
61+
append_ext: false
5862
############################################################
5963
```
6064

lib/jekyll-webp/defaults.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ module Webp
1919
# add ".gif" to the format list to generate webp for animated gifs as well
2020
'formats' => [".jpeg", ".jpg", ".png", ".tiff"],
2121

22-
# File extensions for animated gif files
22+
# append .webp to existing extension instead of replacing it
23+
# (Enables more efficient nginx rules.
24+
# See http://www.lazutkin.com/blog/2014/02/23/serve-files-with-nginx-conditionally/)
25+
'append_ext' => false,
26+
27+
# File extensions for animated gif files
2328
'gifs' => [".gif"],
2429

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

36-
# List of files or directories to explicitly include
41+
# List of files or directories to explicitly include
3742
# e.g. single files outside of the main image directories
3843
'include' => []
3944
}

lib/jekyll-webp/webpGenerator.rb

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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)
@@ -50,22 +50,26 @@ def generate(site)
5050
imgdir_destination = File.join(site.dest, imgdir)
5151
FileUtils::mkdir_p(imgdir_destination)
5252
Jekyll.logger.info "WebP:","Processing #{imgdir_source}"
53-
53+
5454
# handle only jpg, jpeg, png and gif
5555
for imgfile in Dir[imgdir_source + "**/*.*"]
5656
imgfile_relative_path = File.dirname(imgfile.sub(imgdir_source, ""))
57-
57+
5858
# Skip empty stuff
5959
file_ext = File.extname(imgfile).downcase
6060

6161
# If the file is not one of the supported formats, exit early
6262
next if !@config['formats'].include? file_ext
6363

6464
# TODO: Do an exclude check
65-
65+
6666
# Create the output file path
67-
file_noext = File.basename(imgfile, file_ext)
68-
outfile_filename = file_noext+ ".webp"
67+
outfile_filename = if @config['append_ext']
68+
File.basename(imgfile) + '.webp'
69+
else
70+
file_noext = File.basename(imgfile, file_ext)
71+
file_noext + ".webp"
72+
end
6973
FileUtils::mkdir_p(imgdir_destination + imgfile_relative_path)
7074
outfile_fullpath_webp = File.join(imgdir_destination + imgfile_relative_path, outfile_filename)
7175

@@ -96,6 +100,6 @@ def generate(site)
96100
end #function generate
97101

98102
end #class WebPGenerator
99-
103+
100104
end #module Webp
101105
end #module Jekyll

0 commit comments

Comments
 (0)