Fix for using a custom `source` directory
Paths are still passed around as relative. Code which needs to read files (images, templates) and code which needs to manipulate paths (Jekyll::ResponsiveImage::Utils#image_hash) has to be aware of the site source path. This got a bit messy in the end, but I couldn't find a better way without doing a big refactor of the entire plugin.
This commit is contained in:
parent
83358e79b5
commit
0d3f099057
|
@ -44,6 +44,7 @@ Feature: Responsive image generation
|
||||||
"""
|
"""
|
||||||
source: my-site-copy/src
|
source: my-site-copy/src
|
||||||
responsive_image:
|
responsive_image:
|
||||||
|
base_path: assets
|
||||||
template: _includes/responsive-image.html
|
template: _includes/responsive-image.html
|
||||||
output_path_format: assets/resized/%{dirname}/%{width}/%{basename}
|
output_path_format: assets/resized/%{dirname}/%{width}/%{basename}
|
||||||
sizes:
|
sizes:
|
||||||
|
@ -51,5 +52,5 @@ Feature: Responsive image generation
|
||||||
"""
|
"""
|
||||||
And I have a file "my-site-copy/src/index.html" with "{% responsive_image path: assets/subdir/test.png %}"
|
And I have a file "my-site-copy/src/index.html" with "{% responsive_image path: assets/subdir/test.png %}"
|
||||||
When I run Jekyll
|
When I run Jekyll
|
||||||
Then the image "my-site-copy/src/assets/resized/subdir/test-100x50.png" should have the dimensions "100x50"
|
Then the image "my-site-copy/src/assets/resized/subdir/100/test.png" should have the dimensions "100x50"
|
||||||
And the file "_site/assets/resized/subdir/test-100x50.png" should exist
|
And the file "_site/assets/resized/subdir/100/test.png" should exist
|
||||||
|
|
|
@ -0,0 +1,75 @@
|
||||||
|
Feature: Image hashes inside responsive image templates
|
||||||
|
As a Jekyll user
|
||||||
|
I want to have access to image hashes
|
||||||
|
In order to create custom responsive image templates
|
||||||
|
|
||||||
|
Scenario: Using the {% responsive_image %} tag
|
||||||
|
Given I have a configuration with:
|
||||||
|
"""
|
||||||
|
responsive_image:
|
||||||
|
template: _includes/hash.html
|
||||||
|
output_path_format: assets/resized/%{dirname}/%{width}/%{basename}
|
||||||
|
sizes:
|
||||||
|
- width: 100
|
||||||
|
- width: 120
|
||||||
|
"""
|
||||||
|
And I have a file "index.html" with "{% responsive_image path: assets/subdir/test.png %}"
|
||||||
|
When I run Jekyll
|
||||||
|
Then the file "_site/index.html" should contain:
|
||||||
|
"""
|
||||||
|
path: assets/subdir/test.png
|
||||||
|
width: 300
|
||||||
|
height: 150
|
||||||
|
basename: test.png
|
||||||
|
dirname: subdir
|
||||||
|
filename: test
|
||||||
|
extension: png
|
||||||
|
|
||||||
|
path: assets/resized/subdir/100/test.png
|
||||||
|
width: 100
|
||||||
|
height: 50
|
||||||
|
basename: test.png
|
||||||
|
dirname: resized/subdir/100
|
||||||
|
filename: test
|
||||||
|
extension: png
|
||||||
|
|
||||||
|
path: assets/resized/subdir/120/test.png
|
||||||
|
width: 120
|
||||||
|
height: 60
|
||||||
|
basename: test.png
|
||||||
|
dirname: resized/subdir/120
|
||||||
|
filename: test
|
||||||
|
extension: png
|
||||||
|
"""
|
||||||
|
|
||||||
|
Scenario: Honouring Jekyll 'source' configuration
|
||||||
|
Given I have copied my site to "my-site-copy/src"
|
||||||
|
And I have a configuration with:
|
||||||
|
"""
|
||||||
|
source: my-site-copy/src
|
||||||
|
responsive_image:
|
||||||
|
template: _includes/hash.html
|
||||||
|
output_path_format: assets/resized/%{dirname}/%{width}/%{basename}
|
||||||
|
sizes:
|
||||||
|
- width: 100
|
||||||
|
"""
|
||||||
|
And I have a file "my-site-copy/src/index.html" with "{% responsive_image path: assets/subdir/test.png %}"
|
||||||
|
When I run Jekyll
|
||||||
|
Then the file "_site/index.html" should contain:
|
||||||
|
"""
|
||||||
|
path: assets/subdir/test.png
|
||||||
|
width: 300
|
||||||
|
height: 150
|
||||||
|
basename: test.png
|
||||||
|
dirname: subdir
|
||||||
|
filename: test
|
||||||
|
extension: png
|
||||||
|
|
||||||
|
path: assets/resized/subdir/100/test.png
|
||||||
|
width: 100
|
||||||
|
height: 50
|
||||||
|
basename: test.png
|
||||||
|
dirname: resized/subdir/100
|
||||||
|
filename: test
|
||||||
|
extension: png
|
||||||
|
"""
|
|
@ -41,6 +41,10 @@ Then /^I should see "(.+)" in "(.*)"$/ do |text, file|
|
||||||
assert contents.inspect.include?(text), "Expected to find #{text.inspect} in #{contents.inspect}"
|
assert contents.inspect.include?(text), "Expected to find #{text.inspect} in #{contents.inspect}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Then /^the file "(.+)" should contain:$/ do |file, contents|
|
||||||
|
assert_equal contents.strip, File.open(file).readlines.join.strip
|
||||||
|
end
|
||||||
|
|
||||||
Then /^the file "(.+)" should exist$/ do |path|
|
Then /^the file "(.+)" should exist$/ do |path|
|
||||||
assert File.exist?(path)
|
assert File.exist?(path)
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
path: {{ original.path }}
|
||||||
|
width: {{ original.width }}
|
||||||
|
height: {{ original.height }}
|
||||||
|
basename: {{ original.basename }}
|
||||||
|
dirname: {{ original.dirname }}
|
||||||
|
filename: {{ original.filename }}
|
||||||
|
extension: {{ original.extension }}
|
||||||
|
{% for i in resized %}
|
||||||
|
path: {{ i.path }}
|
||||||
|
width: {{ i.width }}
|
||||||
|
height: {{ i.height }}
|
||||||
|
basename: {{ i.basename }}
|
||||||
|
dirname: {{ i.dirname }}
|
||||||
|
filename: {{ i.filename }}
|
||||||
|
extension: {{ i.extension }}
|
||||||
|
{% endfor %}
|
|
@ -14,12 +14,8 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_h
|
def to_h
|
||||||
config = DEFAULTS.merge(@site.config['responsive_image'])
|
DEFAULTS.merge(@site.config['responsive_image'])
|
||||||
.merge(site_source: @site.source, site_dest: @site.dest)
|
.merge(site_source: @site.source, site_dest: @site.dest)
|
||||||
|
|
||||||
config['base_path'] = @site.in_source_dir(config['base_path'])
|
|
||||||
|
|
||||||
config
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,12 +5,14 @@ module Jekyll
|
||||||
|
|
||||||
def generate(site)
|
def generate(site)
|
||||||
config = Config.new(site).to_h
|
config = Config.new(site).to_h
|
||||||
|
site_source = Pathname.new(site.source)
|
||||||
|
|
||||||
config['extra_images'].each do |pathspec|
|
config['extra_images'].each do |pathspec|
|
||||||
Dir.glob(site.in_source_dir(pathspec)) do |image_path|
|
Dir.glob(site.in_source_dir(pathspec)) do |image_path|
|
||||||
relative_image_path = image_path.sub(/^#{Regexp.escape(image_path)}/, '')
|
path = Pathname.new(image_path)
|
||||||
|
relative_image_path = path.relative_path_from(site_source)
|
||||||
|
|
||||||
result = ImageProcessor.process(image_path, relative_image_path, config)
|
result = ImageProcessor.process(relative_image_path, config)
|
||||||
result[:resized].each { |image| keep_resized_image!(site, image) }
|
result[:resized].each { |image| keep_resized_image!(site, image) }
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,20 +3,22 @@ module Jekyll
|
||||||
class ImageProcessor
|
class ImageProcessor
|
||||||
include ResponsiveImage::Utils
|
include ResponsiveImage::Utils
|
||||||
|
|
||||||
def process(absolute_image_path, relative_image_path, config)
|
def process(image_path, config)
|
||||||
raise SyntaxError.new("Invalid image path specified: #{absolute_image_path}") unless File.file?(absolute_image_path)
|
absolute_image_path = File.expand_path(image_path.to_s, config[:site_source])
|
||||||
|
|
||||||
|
raise SyntaxError.new("Invalid image path specified: #{image_path}") unless File.file?(absolute_image_path)
|
||||||
|
|
||||||
resize_handler = ResizeHandler.new
|
resize_handler = ResizeHandler.new
|
||||||
img = Magick::Image::read(absolute_image_path).first
|
img = Magick::Image::read(absolute_image_path).first
|
||||||
|
|
||||||
{
|
{
|
||||||
original: image_hash(config['base_path'], relative_image_path, img.columns, img.rows),
|
original: image_hash(config, image_path, img.columns, img.rows),
|
||||||
resized: resize_handler.resize_image(img, config),
|
resized: resize_handler.resize_image(img, config),
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.process(absolute_image_path, relative_image_path, config)
|
def self.process(image_path, config)
|
||||||
self.new.process(absolute_image_path, relative_image_path, config)
|
self.new.process(image_path, config)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,8 +15,7 @@ module Jekyll
|
||||||
if result.nil?
|
if result.nil?
|
||||||
config = Config.new(@site).to_h
|
config = Config.new(@site).to_h
|
||||||
|
|
||||||
absolute_image_path = @site.in_source_dir(@attributes['path'].to_s)
|
image = ImageProcessor.process(@attributes['path'], config)
|
||||||
image = ImageProcessor.process(absolute_image_path, @attributes['path'], config)
|
|
||||||
@attributes['original'] = image[:original]
|
@attributes['original'] = image[:original]
|
||||||
@attributes['resized'] = image[:resized]
|
@attributes['resized'] = image[:resized]
|
||||||
|
|
||||||
|
|
|
@ -14,8 +14,8 @@ module Jekyll
|
||||||
next unless needs_resizing?(img, width)
|
next unless needs_resizing?(img, width)
|
||||||
|
|
||||||
image_path = img.filename.force_encoding(Encoding::UTF_8)
|
image_path = img.filename.force_encoding(Encoding::UTF_8)
|
||||||
filepath = format_output_path(config['output_path_format'], config['base_path'], image_path, width, height)
|
filepath = format_output_path(config['output_path_format'], config, image_path, width, height)
|
||||||
resized.push(image_hash(config['base_path'], filepath, width, height))
|
resized.push(image_hash(config, filepath, width, height))
|
||||||
|
|
||||||
site_source_filepath = File.expand_path(filepath, config[:site_source])
|
site_source_filepath = File.expand_path(filepath, config[:site_source])
|
||||||
site_dest_filepath = File.expand_path(filepath, config[:site_dest])
|
site_dest_filepath = File.expand_path(filepath, config[:site_dest])
|
||||||
|
@ -45,8 +45,8 @@ module Jekyll
|
||||||
resized
|
resized
|
||||||
end
|
end
|
||||||
|
|
||||||
def format_output_path(format, base_path, image_path, width, height)
|
def format_output_path(format, config, image_path, width, height)
|
||||||
params = symbolize_keys(image_hash(base_path, image_path, width, height))
|
params = symbolize_keys(image_hash(config, image_path, width, height))
|
||||||
|
|
||||||
Pathname.new(format % params).cleanpath.to_s
|
Pathname.new(format % params).cleanpath.to_s
|
||||||
end
|
end
|
||||||
|
|
|
@ -17,10 +17,10 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
# Build a hash containing image information
|
# Build a hash containing image information
|
||||||
def image_hash(base_path, image_path, width, height)
|
def image_hash(config, image_path, width, height)
|
||||||
{
|
{
|
||||||
'path' => image_path,
|
'path' => image_path,
|
||||||
'dirname' => relative_dirname(base_path, image_path),
|
'dirname' => relative_dirname(config, image_path),
|
||||||
'basename' => File.basename(image_path),
|
'basename' => File.basename(image_path),
|
||||||
'filename' => File.basename(image_path, '.*'),
|
'filename' => File.basename(image_path, '.*'),
|
||||||
'extension' => File.extname(image_path).delete('.'),
|
'extension' => File.extname(image_path).delete('.'),
|
||||||
|
@ -29,9 +29,9 @@ module Jekyll
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
def relative_dirname(base_path, image_path)
|
def relative_dirname(config, image_path)
|
||||||
path = Pathname.new(image_path).expand_path
|
path = Pathname.new(File.expand_path(image_path, config[:site_source]))
|
||||||
base = Pathname.new(base_path).expand_path
|
base = Pathname.new(File.expand_path(config['base_path'], config[:site_source]))
|
||||||
|
|
||||||
path.relative_path_from(base).dirname.to_s.delete('.')
|
path.relative_path_from(base).dirname.to_s.delete('.')
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue