Modify `dirname` to be relative to `base_path`

This commit is contained in:
Joseph Wynn 2015-12-27 22:41:04 +00:00
parent 64f86bd6da
commit c523b100cc
8 changed files with 44 additions and 26 deletions

View File

@ -45,12 +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 path of the file (assets/foo/some-file.jpg => assets/foo) # %{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)
@ -163,12 +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`). |
| `dirname` | String | Directory name of the file (`assets/some/file.jpg` => `assets/some`). | | `dirname` | String | Directory of the file relative to `base_path` (`assets/sub/dir/some-file.jpg` => `sub/dir`). |
| `filename` | String | Basename without the extension (`assets/some-file.jpg` => `some-file`). | | `filename` | String | Basename without the extension (`assets/some-file.jpg` => `some-file`). |
| `extension` | String | Extension of the file (`assets/some-file.jpg` => `jpg`). | | `extension` | String | Extension of the file (`assets/some-file.jpg` => `jpg`). |

View File

@ -19,7 +19,7 @@ Feature: Responsive image generation
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: "%{dirname}/resized/%{filename}-%{width}.%{extension}" output_path_format: assets/resized/%{dirname}/%{filename}-%{width}.%{extension}
sizes: sizes:
- width: 100 - width: 100
""" """
@ -32,4 +32,4 @@ Feature: Responsive image generation
When I run Jekyll When I run Jekyll
Then the file "assets/resized/test-100.png" should exist Then the file "assets/resized/test-100.png" should exist
And the file "assets/subdir/resized/test-100.png" should exist And the file "assets/resized/subdir/test-100.png" should exist

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,3 +1,5 @@
require 'pathname'
module Jekyll module Jekyll
class ResponsiveImage class ResponsiveImage
module Utils module Utils
@ -9,23 +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,
'dirname' => File.dirname(path), 'dirname' => relative_dirname(base_path, image_path),
'basename' => File.basename(path), 'basename' => File.basename(image_path),
'filename' => File.basename(path, '.*'), 'filename' => File.basename(image_path, '.*'),
'extension' => File.extname(path).delete('.'), '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