From 93bb666bffbb357968ce669000e08b6e2ee0ffa4 Mon Sep 17 00:00:00 2001 From: Jeric Bryle Sy Dy Date: Tue, 20 Jun 2017 16:55:58 +0800 Subject: [PATCH 1/3] option to directly save resized images to destination directory (#49) --- README.md | 4 ++++ lib/jekyll-responsive-image/config.rb | 3 ++- lib/jekyll-responsive-image/resize_handler.rb | 24 ++++++++++++------- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index a81d10c..60d600b 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,10 @@ responsive_image: # output_path_format: assets/resized/%{width}/%{basename} + # [Optional, Default: true] + # Whether or not to save the generated assets into the source folder. + save_to_source: false + # [Optional, Default: []] # By default, only images referenced by the responsive_image and responsive_image_block # tags are resized. Here you can set a list of paths or path globs to resize other diff --git a/lib/jekyll-responsive-image/config.rb b/lib/jekyll-responsive-image/config.rb index fcaea4d..261035e 100644 --- a/lib/jekyll-responsive-image/config.rb +++ b/lib/jekyll-responsive-image/config.rb @@ -7,7 +7,8 @@ module Jekyll 'output_path_format' => 'assets/resized/%{filename}-%{width}x%{height}.%{extension}', 'sizes' => [], 'extra_images' => [], - 'auto_rotate' => false + 'auto_rotate' => false, + 'save_to_source' => true } def initialize(site) diff --git a/lib/jekyll-responsive-image/resize_handler.rb b/lib/jekyll-responsive-image/resize_handler.rb index d1bb383..bc86b68 100644 --- a/lib/jekyll-responsive-image/resize_handler.rb +++ b/lib/jekyll-responsive-image/resize_handler.rb @@ -22,23 +22,31 @@ module Jekyll site_source_filepath = File.expand_path(filepath, config[:site_source]) site_dest_filepath = File.expand_path(filepath, config[:site_dest]) - # Don't resize images more than once - next if File.exist?(site_source_filepath) + if config['save_to_source'] + target_filepath = site_source_filepath + else + target_filepath = site_dest_filepath + end - ensure_output_dir_exists!(site_source_filepath) + # Don't resize images more than once + next if File.exist?(target_filepath) + + ensure_output_dir_exists!(target_filepath) ensure_output_dir_exists!(site_dest_filepath) - Jekyll.logger.info "Generating #{site_source_filepath}" + Jekyll.logger.info "Generating #{target_filepath}" i = img.scale(ratio) - i.write(site_source_filepath) do |f| + i.write(target_filepath) do |f| f.interlace = i.interlace f.quality = size['quality'] || config['default_quality'] end - # Ensure the generated file is copied to the _site directory - Jekyll.logger.info "Copying resized image to #{site_dest_filepath}" - FileUtils.copy_file(site_source_filepath, site_dest_filepath) + if config['save_to_source'] + # Ensure the generated file is copied to the _site directory + Jekyll.logger.info "Copying resized image to #{site_dest_filepath}" + FileUtils.copy_file(site_source_filepath, site_dest_filepath) + end i.destroy! end From 2ad615952e332603f861e689ad89baa3ef2fe34e Mon Sep 17 00:00:00 2001 From: Jeric Bryle Sy Dy Date: Tue, 20 Jun 2017 17:22:40 +0800 Subject: [PATCH 2/3] added test to save_to_source --- features/save-to-source.feature | 34 +++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 features/save-to-source.feature diff --git a/features/save-to-source.feature b/features/save-to-source.feature new file mode 100644 index 0000000..7f45c3a --- /dev/null +++ b/features/save-to-source.feature @@ -0,0 +1,34 @@ +Feature: Save to source + Scenario: Save to source + Given I have a responsive_image configuration with: + """ + save_to_source: true + sizes: + - width: 100 + extra_images: + - assets/everybody-loves-jalapeño-pineapple-cornbread.png + - assets/*.jpeg + """ + And I have a file "index.html" with "Hello, world!" + When I run Jekyll + And the file "assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should exist + And the file "assets/resized/progressive-100x50.jpeg" should exist + And the file "_site/assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should exist + And the file "_site/assets/resized/progressive-100x50.jpeg" should exist + + Scenario: Do not save to source + Given I have a responsive_image configuration with: + """ + save_to_source: false + sizes: + - width: 100 + extra_images: + - assets/everybody-loves-jalapeño-pineapple-cornbread.png + - assets/*.jpeg + """ + And I have a file "index.html" with "Hello, world!" + When I run Jekyll + And the file "assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should not exist + And the file "assets/resized/progressive-100x50.jpeg" should not exist + And the file "_site/assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should exist + And the file "_site/assets/resized/progressive-100x50.jpeg" should exist \ No newline at end of file From e3349308254568b7b875185bf5a663663deb99e8 Mon Sep 17 00:00:00 2001 From: Joseph Wynn Date: Wed, 21 Jun 2017 19:34:18 +0100 Subject: [PATCH 3/3] Update tests to be more descriptive; reduce test cases I've updated the `Scenario` for these to be more descriptive about what they're actually testing. I've also removed some redundant test cases (I think it's enough to test this with just one image) and used the Given-When-Then format to improve readability. --- features/save-to-source.feature | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/features/save-to-source.feature b/features/save-to-source.feature index 7f45c3a..4c0c94a 100644 --- a/features/save-to-source.feature +++ b/features/save-to-source.feature @@ -1,22 +1,18 @@ Feature: Save to source - Scenario: Save to source + Scenario: Resized images are saved to the source directory by default Given I have a responsive_image configuration with: """ - save_to_source: true sizes: - width: 100 extra_images: - assets/everybody-loves-jalapeño-pineapple-cornbread.png - - assets/*.jpeg """ And I have a file "index.html" with "Hello, world!" When I run Jekyll - And the file "assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should exist - And the file "assets/resized/progressive-100x50.jpeg" should exist + Then the file "assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should exist And the file "_site/assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should exist - And the file "_site/assets/resized/progressive-100x50.jpeg" should exist - Scenario: Do not save to source + Scenario: Resized images can be saved to the destination directory only with save_to_source: false Given I have a responsive_image configuration with: """ save_to_source: false @@ -28,7 +24,5 @@ Feature: Save to source """ And I have a file "index.html" with "Hello, world!" When I run Jekyll - And the file "assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should not exist - And the file "assets/resized/progressive-100x50.jpeg" should not exist - And the file "_site/assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should exist - And the file "_site/assets/resized/progressive-100x50.jpeg" should exist \ No newline at end of file + Then the file "assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should not exist + But the file "_site/assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should exist