Merge pull request #9 from wildlyinaccurate/dirname-in-image-hash
Add `dirname` to image hashes
This commit is contained in:
commit
3a60fcf266
23
README.md
23
README.md
|
@ -45,11 +45,17 @@ responsive_image:
|
||||||
- width: 1400
|
- width: 1400
|
||||||
quality: 90
|
quality: 90
|
||||||
|
|
||||||
|
# [Optional, Default: assets]
|
||||||
|
# The base directory where assets are stored. This is used to determine the
|
||||||
|
# `dirname` value in `output_path_format` below.
|
||||||
|
base_path: assets
|
||||||
|
|
||||||
# [Optional, Default: assets/resized/%{filename}-%{width}x%{height}.%{extension}]
|
# [Optional, Default: assets/resized/%{filename}-%{width}x%{height}.%{extension}]
|
||||||
# The template used when generating filenames for resized images. Must be a
|
# The template used when generating filenames for resized images. Must be a
|
||||||
# relative path.
|
# relative path.
|
||||||
#
|
#
|
||||||
# Parameters available are:
|
# Parameters available are:
|
||||||
|
# %{dirname} Directory of the file relative to `base_path` (assets/sub/dir/some-file.jpg => sub/dir)
|
||||||
# %{basename} Basename of the file (assets/some-file.jpg => some-file.jpg)
|
# %{basename} Basename of the file (assets/some-file.jpg => some-file.jpg)
|
||||||
# %{filename} Basename without the extension (assets/some-file.jpg => some-file)
|
# %{filename} Basename without the extension (assets/some-file.jpg => some-file)
|
||||||
# %{extension} Extension of the file (assets/some-file.jpg => jpg)
|
# %{extension} Extension of the file (assets/some-file.jpg => jpg)
|
||||||
|
@ -162,11 +168,12 @@ The following variables are available in the template:
|
||||||
|
|
||||||
Image objects (like `original` and each object in `resized`) contain the following properties:
|
Image objects (like `original` and each object in `resized`) contain the following properties:
|
||||||
|
|
||||||
| Variable | Type | Description |
|
| Variable | Type | Description |
|
||||||
|-------------|---------|-------------------------------------------------------------------------|
|
|-------------|---------|----------------------------------------------------------------------------------------------|
|
||||||
| `path` | String | The path to the image. |
|
| `path` | String | The path to the image. |
|
||||||
| `width` | Integer | The width of the image. |
|
| `width` | Integer | The width of the image. |
|
||||||
| `height` | Integer | The height of the image. |
|
| `height` | Integer | The height of the image. |
|
||||||
| `basename` | String | Basename of the file (`assets/some-file.jpg` => `some-file.jpg`). |
|
| `basename` | String | Basename of the file (`assets/some-file.jpg` => `some-file.jpg`). |
|
||||||
| `filename` | String | Basename without the extension (`assets/some-file.jpg` => `some-file`). |
|
| `dirname` | String | Directory of the file relative to `base_path` (`assets/sub/dir/some-file.jpg` => `sub/dir`). |
|
||||||
| `extension` | String | Extension of the file (`assets/some-file.jpg` => `jpg`). |
|
| `filename` | String | Basename without the extension (`assets/some-file.jpg` => `some-file`). |
|
||||||
|
| `extension` | String | Extension of the file (`assets/some-file.jpg` => `jpg`). |
|
||||||
|
|
Binary file not shown.
After Width: | Height: | Size: 18 KiB |
|
@ -0,0 +1,35 @@
|
||||||
|
Feature: Responsive image generation
|
||||||
|
As a Jekyll user
|
||||||
|
I want to generate responsive images
|
||||||
|
In order to use them on my pages
|
||||||
|
|
||||||
|
Scenario: Resizing images
|
||||||
|
Given I have a responsive_image configuration with:
|
||||||
|
"""
|
||||||
|
template: _includes/responsive-image.html
|
||||||
|
sizes:
|
||||||
|
- width: 100
|
||||||
|
"""
|
||||||
|
|
||||||
|
And I have a file "index.html" with "{% responsive_image path: assets/test.png alt: Foobar %}"
|
||||||
|
When I run Jekyll
|
||||||
|
Then the image "assets/resized/test-100x50.png" should have the dimensions "100x50"
|
||||||
|
|
||||||
|
Scenario: Handling subdirectories
|
||||||
|
Given I have a responsive_image configuration with:
|
||||||
|
"""
|
||||||
|
template: _includes/responsive-image.html
|
||||||
|
output_path_format: assets/resized/%{dirname}/%{filename}-%{width}.%{extension}
|
||||||
|
sizes:
|
||||||
|
- width: 100
|
||||||
|
"""
|
||||||
|
|
||||||
|
And I have a file "index.html" with:
|
||||||
|
"""
|
||||||
|
{% responsive_image path: assets/test.png %}
|
||||||
|
{% responsive_image path: assets/subdir/test.png %}
|
||||||
|
"""
|
||||||
|
|
||||||
|
When I run Jekyll
|
||||||
|
Then the file "assets/resized/test-100.png" should exist
|
||||||
|
And the file "assets/resized/subdir/test-100.png" should exist
|
|
@ -69,7 +69,7 @@ Feature: Jekyll responsive_image tag
|
||||||
Given I have a responsive_image configuration with:
|
Given I have a responsive_image configuration with:
|
||||||
"""
|
"""
|
||||||
template: _includes/responsive-image.html
|
template: _includes/responsive-image.html
|
||||||
output_path_format: assets/%{basename}-resized/%{width}/%{filename}-%{height}.%{extension}
|
output_path_format: assets/%{dirname}/%{basename}-resized/%{width}/%{filename}-%{height}.%{extension}
|
||||||
sizes:
|
sizes:
|
||||||
- width: 100
|
- width: 100
|
||||||
"""
|
"""
|
||||||
|
|
|
@ -36,6 +36,12 @@ Then /^the file "(.+)" should exist$/ do |path|
|
||||||
assert File.exists?(path)
|
assert File.exists?(path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Then /^the image "(.+)" should have the dimensions "(\d+)x(\d+)"$/ do |path, width, height|
|
||||||
|
img = Magick::Image::read(path).first
|
||||||
|
assert_equal "#{width}x#{height}", "#{img.columns}x#{img.rows}"
|
||||||
|
img.destroy!
|
||||||
|
end
|
||||||
|
|
||||||
def write_file(path, contents)
|
def write_file(path, contents)
|
||||||
File.open(path, 'w') do |f|
|
File.open(path, 'w') do |f|
|
||||||
f.write(contents)
|
f.write(contents)
|
||||||
|
|
|
@ -7,7 +7,9 @@ module Jekyll
|
||||||
config = ResponsiveImage.defaults.dup.merge(site.config['responsive_image']).merge(:site_dest => site.dest)
|
config = ResponsiveImage.defaults.dup.merge(site.config['responsive_image']).merge(:site_dest => site.dest)
|
||||||
|
|
||||||
# Not very nice, but this is needed to create a clean path to add to keep_files
|
# Not very nice, but this is needed to create a clean path to add to keep_files
|
||||||
output_dir = format_output_path(config['output_path_format'], '*', '*', '*')
|
output_dir = format_output_path(config['output_path_format'], config['base_path'], '*', '*', '*')
|
||||||
|
output_dir = "#{File.dirname(output_dir)}/*"
|
||||||
|
|
||||||
site.config['keep_files'] << output_dir unless site.config['keep_files'].include?(output_dir)
|
site.config['keep_files'] << output_dir unless site.config['keep_files'].include?(output_dir)
|
||||||
|
|
||||||
config
|
config
|
||||||
|
|
|
@ -2,6 +2,7 @@ module Jekyll
|
||||||
class ResponsiveImage
|
class ResponsiveImage
|
||||||
@defaults = {
|
@defaults = {
|
||||||
'default_quality' => 85,
|
'default_quality' => 85,
|
||||||
|
'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' => [],
|
||||||
}.freeze
|
}.freeze
|
||||||
|
|
|
@ -10,7 +10,7 @@ module Jekyll
|
||||||
img = Magick::Image::read(image_path).first
|
img = Magick::Image::read(image_path).first
|
||||||
|
|
||||||
{
|
{
|
||||||
original: image_hash(image_path, img.columns, img.rows),
|
original: image_hash(config['base_path'], image_path, img.columns, img.rows),
|
||||||
resized: resize_handler.resize_image(img, config),
|
resized: resize_handler.resize_image(img, config),
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
|
@ -13,8 +13,8 @@ module Jekyll
|
||||||
|
|
||||||
next unless needs_resizing?(img, width)
|
next unless needs_resizing?(img, width)
|
||||||
|
|
||||||
filepath = format_output_path(config['output_path_format'], img.filename, width, height)
|
filepath = format_output_path(config['output_path_format'], config['base_path'], img.filename, width, height)
|
||||||
resized.push(image_hash(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.exists?(filepath)
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
require 'pathname'
|
||||||
|
|
||||||
module Jekyll
|
module Jekyll
|
||||||
class ResponsiveImage
|
class ResponsiveImage
|
||||||
module Utils
|
module Utils
|
||||||
|
@ -9,22 +11,31 @@ module Jekyll
|
||||||
result
|
result
|
||||||
end
|
end
|
||||||
|
|
||||||
def format_output_path(format, path, width, height)
|
def format_output_path(format, base_path, image_path, width, height)
|
||||||
params = symbolize_keys(image_hash(path, width, height))
|
params = symbolize_keys(image_hash(base_path, image_path, width, height))
|
||||||
format % params
|
|
||||||
|
Pathname.new(format % params).cleanpath.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
# Build a hash containing image information
|
# Build a hash containing image information
|
||||||
def image_hash(path, width, height)
|
def image_hash(base_path, image_path, width, height)
|
||||||
{
|
{
|
||||||
'path' => path,
|
'path' => image_path,
|
||||||
'basename' => File.basename(path),
|
'dirname' => relative_dirname(base_path, image_path),
|
||||||
'filename' => File.basename(path, '.*'),
|
'basename' => File.basename(image_path),
|
||||||
'extension' => File.extname(path).delete('.'),
|
'filename' => File.basename(image_path, '.*'),
|
||||||
|
'extension' => File.extname(image_path).delete('.'),
|
||||||
'width' => width,
|
'width' => width,
|
||||||
'height' => height,
|
'height' => height,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def relative_dirname(base_path, image_path)
|
||||||
|
path = Pathname.new(image_path).expand_path
|
||||||
|
base = Pathname.new(base_path).expand_path
|
||||||
|
|
||||||
|
path.relative_path_from(base).dirname.to_s.delete('.')
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue