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/features/save-to-source.feature b/features/save-to-source.feature new file mode 100644 index 0000000..4c0c94a --- /dev/null +++ b/features/save-to-source.feature @@ -0,0 +1,28 @@ +Feature: Save to source + Scenario: Resized images are saved to the source directory by default + Given I have a responsive_image configuration with: + """ + sizes: + - width: 100 + extra_images: + - assets/everybody-loves-jalapeño-pineapple-cornbread.png + """ + And I have a file "index.html" with "Hello, world!" + When I run Jekyll + 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 + + 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 + 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 + 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 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