diff --git a/README.md b/README.md index c7c6d2c..061d8c6 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,13 @@ responsive_image: # %{height} Height of the resized image # output_path_format: assets/resized/%{width}/%{basename} + + # 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 + # images. This is useful for resizing images which will be referenced from stylesheets. + extra_images: + - assets/foo/bar.png + - assets/bgs/*.png ``` ## Usage diff --git a/features/extra-image-generation.feature b/features/extra-image-generation.feature new file mode 100644 index 0000000..49f9151 --- /dev/null +++ b/features/extra-image-generation.feature @@ -0,0 +1,43 @@ +Feature: Extra image generation + As a Jekyll user + I want to resize images that aren't used in posts or pages + In order to use them in my stylesheets + + Scenario: Resizing a single image + Given I have a responsive_image configuration with: + """ + sizes: + - width: 100 + + extra_images: + - assets/test.png + """ + + And I have a file "index.html" with "Hello, world!" + When I run Jekyll + Then the image "assets/resized/test-100x50.png" should have the dimensions "100x50" + + Scenario: Using glob patterns + Given I have a responsive_image configuration with: + """ + sizes: + - width: 100 + + extra_images: + - assets/*.png + """ + + And I have a file "index.html" with "Hello, world!" + When I run Jekyll + Then the image "assets/resized/test-100x50.png" should have the dimensions "100x50" + + Scenario: No extra images + Given I have a responsive_image configuration with: + """ + sizes: + - width: 100 + """ + + And I have a file "index.html" with "Hello, world!" + When I run Jekyll + Then the file "assets/resized/test-100x50.png" should not exist diff --git a/features/step_definitions/jekyll_steps.rb b/features/step_definitions/jekyll_steps.rb index 0990a53..07899c9 100644 --- a/features/step_definitions/jekyll_steps.rb +++ b/features/step_definitions/jekyll_steps.rb @@ -33,7 +33,11 @@ Then /^I should see "(.+)" in "(.*)"$/ do |text, file| end Then /^the file "(.+)" should exist$/ do |path| - assert File.exists?(path) + assert File.exist?(path) +end + +Then /^the file "(.+)" should not exist$/ do |path| + assert !File.exist?(path) end Then /^the image "(.+)" should have the dimensions "(\d+)x(\d+)"$/ do |path, width, height| diff --git a/lib/jekyll/responsive_image.rb b/lib/jekyll/responsive_image.rb index 06fd05c..70c3de9 100644 --- a/lib/jekyll/responsive_image.rb +++ b/lib/jekyll/responsive_image.rb @@ -13,6 +13,7 @@ require 'jekyll/responsive_image/resize_handler' require 'jekyll/responsive_image/common' require 'jekyll/responsive_image/tag' require 'jekyll/responsive_image/block' +require 'jekyll/responsive_image/extra_image_generator' Liquid::Template.register_tag('responsive_image', Jekyll::ResponsiveImage::Tag) Liquid::Template.register_tag('responsive_image_block', Jekyll::ResponsiveImage::Block) diff --git a/lib/jekyll/responsive_image/defaults.rb b/lib/jekyll/responsive_image/defaults.rb index 6bd6532..c8ddc5f 100644 --- a/lib/jekyll/responsive_image/defaults.rb +++ b/lib/jekyll/responsive_image/defaults.rb @@ -5,6 +5,7 @@ module Jekyll 'base_path' => 'assets', 'output_path_format' => 'assets/resized/%{filename}-%{width}x%{height}.%{extension}', 'sizes' => [], + 'extra_images' => [] }.freeze class << self diff --git a/lib/jekyll/responsive_image/extra_image_generator.rb b/lib/jekyll/responsive_image/extra_image_generator.rb new file mode 100644 index 0000000..dc72f34 --- /dev/null +++ b/lib/jekyll/responsive_image/extra_image_generator.rb @@ -0,0 +1,15 @@ +module Jekyll + class ResponsiveImage + class ExtraImageGenerator < Jekyll::Generator + include Jekyll::ResponsiveImage::Common + + def generate(site) + config = make_config(site) + + config['extra_images'].each do |pathspec| + Dir.glob(pathspec) { |path| ImageProcessor.process(path, config) } + end + end + end + end +end diff --git a/lib/jekyll/responsive_image/image_processor.rb b/lib/jekyll/responsive_image/image_processor.rb index 7e7632f..cbae275 100644 --- a/lib/jekyll/responsive_image/image_processor.rb +++ b/lib/jekyll/responsive_image/image_processor.rb @@ -4,7 +4,7 @@ module Jekyll include ResponsiveImage::Utils def process(image_path, config) - raise SyntaxError.new("Invalid image path specified: #{image_path}") unless File.exists?(image_path.to_s) + raise SyntaxError.new("Invalid image path specified: #{image_path}") unless File.exist?(image_path.to_s) resize_handler = ResizeHandler.new img = Magick::Image::read(image_path).first diff --git a/lib/jekyll/responsive_image/resize_handler.rb b/lib/jekyll/responsive_image/resize_handler.rb index b63c4db..6fbe8d6 100644 --- a/lib/jekyll/responsive_image/resize_handler.rb +++ b/lib/jekyll/responsive_image/resize_handler.rb @@ -17,7 +17,7 @@ module Jekyll resized.push(image_hash(config['base_path'], filepath, width, height)) # Don't resize images more than once - next if File.exists?(filepath) + next if File.exist?(filepath) ensure_output_dir_exists!(File.dirname(filepath)) @@ -35,7 +35,7 @@ module Jekyll i.destroy! end - + img.destroy! resized @@ -46,7 +46,7 @@ module Jekyll end def ensure_output_dir_exists!(dir) - unless Dir.exists?(dir) + unless Dir.exist?(dir) Jekyll.logger.info "Creating output directory #{dir}" FileUtils.mkdir_p(dir) end