Resize extra images (#23)

* Switch deprecated {File,Dir}#exists? for #exist?

* Implement extra_images configuration.

Closes #21.
This commit is contained in:
Joseph Wynn 2016-06-05 22:32:13 +01:00
parent f63b5002dd
commit 08bdd3b1f6
8 changed files with 76 additions and 5 deletions

View File

@ -64,6 +64,13 @@ responsive_image:
# %{height} Height of the resized image # %{height} Height of the resized image
# #
output_path_format: assets/resized/%{width}/%{basename} 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 ## Usage

View File

@ -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

View File

@ -33,7 +33,11 @@ Then /^I should see "(.+)" in "(.*)"$/ do |text, file|
end end
Then /^the file "(.+)" should exist$/ do |path| 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 end
Then /^the image "(.+)" should have the dimensions "(\d+)x(\d+)"$/ do |path, width, height| Then /^the image "(.+)" should have the dimensions "(\d+)x(\d+)"$/ do |path, width, height|

View File

@ -13,6 +13,7 @@ require 'jekyll/responsive_image/resize_handler'
require 'jekyll/responsive_image/common' require 'jekyll/responsive_image/common'
require 'jekyll/responsive_image/tag' require 'jekyll/responsive_image/tag'
require 'jekyll/responsive_image/block' 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', Jekyll::ResponsiveImage::Tag)
Liquid::Template.register_tag('responsive_image_block', Jekyll::ResponsiveImage::Block) Liquid::Template.register_tag('responsive_image_block', Jekyll::ResponsiveImage::Block)

View File

@ -5,6 +5,7 @@ module Jekyll
'base_path' => 'assets', 'base_path' => 'assets',
'output_path_format' => 'assets/resized/%{filename}-%{width}x%{height}.%{extension}', 'output_path_format' => 'assets/resized/%{filename}-%{width}x%{height}.%{extension}',
'sizes' => [], 'sizes' => [],
'extra_images' => []
}.freeze }.freeze
class << self class << self

View File

@ -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

View File

@ -4,7 +4,7 @@ module Jekyll
include ResponsiveImage::Utils include ResponsiveImage::Utils
def process(image_path, config) 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 resize_handler = ResizeHandler.new
img = Magick::Image::read(image_path).first img = Magick::Image::read(image_path).first

View File

@ -17,7 +17,7 @@ module Jekyll
resized.push(image_hash(config['base_path'], filepath, width, height)) resized.push(image_hash(config['base_path'], filepath, width, height))
# Don't resize images more than once # Don't resize images more than once
next if File.exists?(filepath) next if File.exist?(filepath)
ensure_output_dir_exists!(File.dirname(filepath)) ensure_output_dir_exists!(File.dirname(filepath))
@ -35,7 +35,7 @@ module Jekyll
i.destroy! i.destroy!
end end
img.destroy! img.destroy!
resized resized
@ -46,7 +46,7 @@ module Jekyll
end end
def ensure_output_dir_exists!(dir) def ensure_output_dir_exists!(dir)
unless Dir.exists?(dir) unless Dir.exist?(dir)
Jekyll.logger.info "Creating output directory #{dir}" Jekyll.logger.info "Creating output directory #{dir}"
FileUtils.mkdir_p(dir) FileUtils.mkdir_p(dir)
end end