Add `dirname` to image hashes. Closes #5.

This commit is contained in:
Joseph Wynn 2015-12-26 18:11:05 +00:00
parent 9e122233e5
commit 76f834ce09
5 changed files with 44 additions and 0 deletions

View File

@ -50,6 +50,7 @@ responsive_image:
# relative path.
#
# Parameters available are:
# %{dirname} Directory path of the file (assets/foo/some-file.jpg => assets/foo)
# %{basename} Basename of the file (assets/some-file.jpg => some-file.jpg)
# %{filename} Basename without the extension (assets/some-file.jpg => some-file)
# %{extension} Extension of the file (assets/some-file.jpg => jpg)
@ -168,5 +169,6 @@ Image objects (like `original` and each object in `resized`) contain the followi
| `width` | Integer | The width of the image. |
| `height` | Integer | The height of the image. |
| `basename` | String | Basename of the file (`assets/some-file.jpg` => `some-file.jpg`). |
| `dirname` | String | Directory name of the file (`assets/some/file.jpg` => `assets/some`). |
| `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

View File

@ -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: "%{dirname}/resized/%{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/subdir/resized/test-100.png" should exist

View File

@ -36,6 +36,12 @@ Then /^the file "(.+)" should exist$/ do |path|
assert File.exists?(path)
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)
File.open(path, 'w') do |f|
f.write(contents)

View File

@ -18,6 +18,7 @@ module Jekyll
def image_hash(path, width, height)
{
'path' => path,
'dirname' => File.dirname(path),
'basename' => File.basename(path),
'filename' => File.basename(path, '.*'),
'extension' => File.extname(path).delete('.'),