From ae14f210bf24b2bf5f761e027363ecc9f7c1b5f4 Mon Sep 17 00:00:00 2001 From: Jetroid Date: Fri, 20 Sep 2019 17:38:16 +0100 Subject: [PATCH] Allow nested search with option --- README.md | 5 ++++- lib/jekyll-webp/defaults.rb | 5 ++++- lib/jekyll-webp/webpGenerator.rb | 10 ++++++++++ 3 files changed, 18 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2b507c8..88314de 100644 --- a/README.md +++ b/README.md @@ -35,10 +35,13 @@ webp: # The quality of the webp conversion 0 to 100 (where 100 is least lossy) quality: 75 - # List of directories containing images to optimize, nested directories will not be checked + # List of directories containing images to optimize, nested directories will only be checked if `nested` is true # By default the generator will search for a folder called `/img` under the site root and process all jpg, png and tiff image files found there. img_dir: ["/img"] + # Whether to search in nested directories or not + nested: false + # add ".gif" to the format list to generate webp for animated gifs as well formats: [".jpeg", ".jpg", ".png", ".tiff"] diff --git a/lib/jekyll-webp/defaults.rb b/lib/jekyll-webp/defaults.rb index 43c11e8..cf282ad 100644 --- a/lib/jekyll-webp/defaults.rb +++ b/lib/jekyll-webp/defaults.rb @@ -13,9 +13,12 @@ module Webp # https://developers.google.com/speed/webp/docs/cwebp#options 'flags' => "-m 4 -pass 4 -af", - # List of directories containing images to optimize, Nested directories will not be checked + # List of directories containing images to optimize, Nested directories only be checked if `nested` is true 'img_dir' => ["/img"], + # Whether to search in nested directories or not + 'nested' => false, + # add ".gif" to the format list to generate webp for animated gifs as well 'formats' => [".jpeg", ".jpg", ".png", ".tiff"], diff --git a/lib/jekyll-webp/webpGenerator.rb b/lib/jekyll-webp/webpGenerator.rb index c17c432..261a514 100644 --- a/lib/jekyll-webp/webpGenerator.rb +++ b/lib/jekyll-webp/webpGenerator.rb @@ -40,6 +40,16 @@ def generate(site) # If the site destination directory has not yet been created then create it now. Otherwise, we cannot write our file there. Dir::mkdir(site.dest) if !File.directory? site.dest + # If nesting is enabled, get all the nested directories too + if @config['nested'] + newdir = [] + for imgdir in @config['img_dir'] + # Get every directory below (and including) imgdir, recursively + newdir.concat(Dir.glob(imgdir + "/**/")) + end + @config['img_dir'] = newdir + end + # Counting the number of files generated file_count = 0