diff --git a/README.md b/README.md index 472b952..e502755 100644 --- a/README.md +++ b/README.md @@ -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`). | diff --git a/features/fixtures/assets/subdir/test.png b/features/fixtures/assets/subdir/test.png new file mode 100644 index 0000000..e53ebca Binary files /dev/null and b/features/fixtures/assets/subdir/test.png differ diff --git a/features/image-generation.feature b/features/image-generation.feature new file mode 100644 index 0000000..0193bd7 --- /dev/null +++ b/features/image-generation.feature @@ -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 diff --git a/features/step_definitions/jekyll_steps.rb b/features/step_definitions/jekyll_steps.rb index 56f4f9e..0990a53 100644 --- a/features/step_definitions/jekyll_steps.rb +++ b/features/step_definitions/jekyll_steps.rb @@ -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) diff --git a/lib/jekyll/responsive_image/utils.rb b/lib/jekyll/responsive_image/utils.rb index bceb0de..35d569a 100644 --- a/lib/jekyll/responsive_image/utils.rb +++ b/lib/jekyll/responsive_image/utils.rb @@ -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('.'),