Treat paths as relative to Jekyll `source` directory (#34)
* Move 'fixtures' to 'test-site' because, like, that's what it is * Refactor to make reasoning about file paths easier * Write a failing test * Conventions, conventions * Always treat paths as relative to the Jekyll site source
This commit is contained in:
parent
6c8e02bb11
commit
efeb87a032
|
@ -8,7 +8,6 @@ Feature: Extra image generation
|
||||||
"""
|
"""
|
||||||
sizes:
|
sizes:
|
||||||
- width: 100
|
- width: 100
|
||||||
|
|
||||||
extra_images:
|
extra_images:
|
||||||
- assets/everybody-loves-jalapeño-pineapple-cornbread.png
|
- assets/everybody-loves-jalapeño-pineapple-cornbread.png
|
||||||
"""
|
"""
|
||||||
|
@ -16,13 +15,13 @@ Feature: Extra image generation
|
||||||
And I have a file "index.html" with "Hello, world!"
|
And I have a file "index.html" with "Hello, world!"
|
||||||
When I run Jekyll
|
When I run Jekyll
|
||||||
Then the image "assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should have the dimensions "100x50"
|
Then the image "assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should have the dimensions "100x50"
|
||||||
|
And the file "_site/assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should exist
|
||||||
|
|
||||||
Scenario: Using glob patterns
|
Scenario: Using glob patterns
|
||||||
Given I have a responsive_image configuration with:
|
Given I have a responsive_image configuration with:
|
||||||
"""
|
"""
|
||||||
sizes:
|
sizes:
|
||||||
- width: 100
|
- width: 100
|
||||||
|
|
||||||
extra_images:
|
extra_images:
|
||||||
- assets/*.png
|
- assets/*.png
|
||||||
"""
|
"""
|
||||||
|
@ -30,6 +29,24 @@ Feature: Extra image generation
|
||||||
And I have a file "index.html" with "Hello, world!"
|
And I have a file "index.html" with "Hello, world!"
|
||||||
When I run Jekyll
|
When I run Jekyll
|
||||||
Then the image "assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should have the dimensions "100x50"
|
Then the image "assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should have the dimensions "100x50"
|
||||||
|
And the file "_site/assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should exist
|
||||||
|
|
||||||
|
Scenario: Honouring Jekyll 'source' configuration
|
||||||
|
Given I have copied my site to "sub-dir/my-site-copy"
|
||||||
|
And I have a configuration with:
|
||||||
|
"""
|
||||||
|
source: sub-dir/my-site-copy
|
||||||
|
responsive_image:
|
||||||
|
sizes:
|
||||||
|
- width: 100
|
||||||
|
extra_images:
|
||||||
|
- assets/*.png
|
||||||
|
"""
|
||||||
|
|
||||||
|
And I have a file "index.html" with "Hello, world!"
|
||||||
|
When I run Jekyll
|
||||||
|
Then the image "sub-dir/my-site-copy/assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should have the dimensions "100x50"
|
||||||
|
And the file "_site/assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should exist
|
||||||
|
|
||||||
Scenario: No extra images
|
Scenario: No extra images
|
||||||
Given I have a responsive_image configuration with:
|
Given I have a responsive_image configuration with:
|
||||||
|
|
|
@ -14,6 +14,7 @@ Feature: Responsive image generation
|
||||||
And I have a file "index.html" with "{% responsive_image path: assets/everybody-loves-jalapeño-pineapple-cornbread.png alt: Foobar %}"
|
And I have a file "index.html" with "{% responsive_image path: assets/everybody-loves-jalapeño-pineapple-cornbread.png alt: Foobar %}"
|
||||||
When I run Jekyll
|
When I run Jekyll
|
||||||
Then the image "assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should have the dimensions "100x50"
|
Then the image "assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should have the dimensions "100x50"
|
||||||
|
And the file "_site/assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should exist
|
||||||
|
|
||||||
Scenario: Handling subdirectories
|
Scenario: Handling subdirectories
|
||||||
Given I have a responsive_image configuration with:
|
Given I have a responsive_image configuration with:
|
||||||
|
@ -33,4 +34,21 @@ Feature: Responsive image generation
|
||||||
|
|
||||||
When I run Jekyll
|
When I run Jekyll
|
||||||
Then the file "assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100.png" should exist
|
Then the file "assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100.png" should exist
|
||||||
|
And the file "_site/assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100.png" should exist
|
||||||
And the file "assets/resized/subdir/test-100.png" should exist
|
And the file "assets/resized/subdir/test-100.png" should exist
|
||||||
|
And the file "_site/assets/resized/subdir/test-100.png" should exist
|
||||||
|
|
||||||
|
Scenario: Honouring Jekyll 'source' configuration
|
||||||
|
Given I have copied my site to "sub-dir/my-site-copy"
|
||||||
|
And I have a configuration with:
|
||||||
|
"""
|
||||||
|
source: sub-dir/my-site-copy
|
||||||
|
responsive_image:
|
||||||
|
template: _includes/responsive-image.html
|
||||||
|
sizes:
|
||||||
|
- width: 100
|
||||||
|
"""
|
||||||
|
And I have a file "sub-dir/my-site-copy/index.html" with "{% responsive_image path: assets/everybody-loves-jalapeño-pineapple-cornbread.png %}"
|
||||||
|
When I run Jekyll
|
||||||
|
Then the image "sub-dir/my-site-copy/assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should have the dimensions "100x50"
|
||||||
|
And the file "_site/assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should exist
|
||||||
|
|
|
@ -77,3 +77,4 @@ Feature: Jekyll responsive_image tag
|
||||||
When I run Jekyll
|
When I run Jekyll
|
||||||
Then I should see "/assets/everybody-loves-jalapeño-pineapple-cornbread.png-resized/100/everybody-loves-jalapeño-pineapple-cornbread-50.png 100w" in "_site/index.html"
|
Then I should see "/assets/everybody-loves-jalapeño-pineapple-cornbread.png-resized/100/everybody-loves-jalapeño-pineapple-cornbread-50.png 100w" in "_site/index.html"
|
||||||
And the file "assets/everybody-loves-jalapeño-pineapple-cornbread.png-resized/100/everybody-loves-jalapeño-pineapple-cornbread-50.png" should exist
|
And the file "assets/everybody-loves-jalapeño-pineapple-cornbread.png-resized/100/everybody-loves-jalapeño-pineapple-cornbread-50.png" should exist
|
||||||
|
And the file "_site/assets/everybody-loves-jalapeño-pineapple-cornbread.png-resized/100/everybody-loves-jalapeño-pineapple-cornbread-50.png" should exist
|
||||||
|
|
|
@ -8,6 +8,16 @@ Then /^Jekyll should throw a "(.+)"$/ do |error_class|
|
||||||
assert_raise(Object.const_get(error_class)) { run_jekyll }
|
assert_raise(Object.const_get(error_class)) { run_jekyll }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Given /^I have copied my site to "(.+)"$/ do |path|
|
||||||
|
new_site_dir = File.join(TEST_DIR, path)
|
||||||
|
|
||||||
|
FileUtils.mkdir_p(new_site_dir)
|
||||||
|
|
||||||
|
Dir.glob(File.join(TEST_DIR, '*'))
|
||||||
|
.reject { |f| File.basename(f) == File.dirname(path) }
|
||||||
|
.each { |f| FileUtils.mv(f, new_site_dir) }
|
||||||
|
end
|
||||||
|
|
||||||
Given /^I have a configuration with:$/ do |config|
|
Given /^I have a configuration with:$/ do |config|
|
||||||
write_file('_config.yml', config)
|
write_file('_config.yml', config)
|
||||||
end
|
end
|
||||||
|
|
|
@ -9,9 +9,6 @@ require 'jekyll/responsive_image'
|
||||||
TEST_DIR = File.join('/', 'tmp', 'jekyll')
|
TEST_DIR = File.join('/', 'tmp', 'jekyll')
|
||||||
|
|
||||||
def run_jekyll(options = {})
|
def run_jekyll(options = {})
|
||||||
options['source'] ||= TEST_DIR
|
|
||||||
options['destination'] ||= File.join(TEST_DIR, '_site')
|
|
||||||
|
|
||||||
options = Jekyll.configuration(options)
|
options = Jekyll.configuration(options)
|
||||||
|
|
||||||
site = Jekyll::Site.new(options)
|
site = Jekyll::Site.new(options)
|
||||||
|
|
|
@ -2,8 +2,8 @@ Before do
|
||||||
FileUtils.rm_rf(TEST_DIR) if File.exist?(TEST_DIR)
|
FileUtils.rm_rf(TEST_DIR) if File.exist?(TEST_DIR)
|
||||||
FileUtils.mkdir_p(TEST_DIR)
|
FileUtils.mkdir_p(TEST_DIR)
|
||||||
|
|
||||||
fixtures = File.expand_path('../../fixtures', __FILE__)
|
test_site = File.expand_path('../../test-site', __FILE__)
|
||||||
FileUtils.cp_r(Dir.glob("#{fixtures}/*"), TEST_DIR)
|
FileUtils.cp_r(Dir.glob("#{test_site}/*"), TEST_DIR)
|
||||||
|
|
||||||
Dir.chdir(TEST_DIR)
|
Dir.chdir(TEST_DIR)
|
||||||
end
|
end
|
||||||
|
|
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 18 KiB After Width: | Height: | Size: 18 KiB |
|
@ -1,3 +1,12 @@
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
module Jekyll
|
module Jekyll
|
||||||
class ResponsiveImage
|
class ResponsiveImage
|
||||||
class Block < Liquid::Block
|
class Block < Liquid::Block
|
||||||
|
@ -5,29 +14,7 @@ module Jekyll
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
attributes = YAML.load(super)
|
attributes = YAML.load(super)
|
||||||
|
render_responsive_image(context, attributes)
|
||||||
cache_key = attributes.to_s
|
|
||||||
result = attributes['cache'] ? RenderCache.get(cache_key) : nil
|
|
||||||
|
|
||||||
if result.nil?
|
|
||||||
site = context.registers[:site]
|
|
||||||
config = make_config(site)
|
|
||||||
|
|
||||||
image_template = attributes['template'] || config['template']
|
|
||||||
|
|
||||||
image = ImageProcessor.process(attributes['path'], config)
|
|
||||||
attributes['original'] = image[:original]
|
|
||||||
attributes['resized'] = image[:resized]
|
|
||||||
|
|
||||||
partial = File.read(image_template)
|
|
||||||
template = Liquid::Template.parse(partial)
|
|
||||||
|
|
||||||
result = template.render!(attributes.merge(site.site_payload))
|
|
||||||
|
|
||||||
RenderCache.set(cache_key, result)
|
|
||||||
end
|
|
||||||
|
|
||||||
result
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,15 +4,42 @@ module Jekyll
|
||||||
include Jekyll::ResponsiveImage::Utils
|
include Jekyll::ResponsiveImage::Utils
|
||||||
|
|
||||||
def make_config(site)
|
def make_config(site)
|
||||||
config = ResponsiveImage.defaults.dup.merge(site.config['responsive_image']).merge(:site_dest => site.dest)
|
ResponsiveImage.defaults.dup
|
||||||
|
.merge(site.config['responsive_image'])
|
||||||
|
.merge(:site_source => site.source, :site_dest => site.dest)
|
||||||
|
end
|
||||||
|
|
||||||
# Not very nice, but this is needed to create a clean path to add to keep_files
|
def keep_resized_image!(site, image)
|
||||||
output_dir = format_output_path(config['output_path_format'], config['base_path'], '*', '*', '*')
|
keep_dir = File.dirname(image['path'])
|
||||||
output_dir = "#{File.dirname(output_dir)}/*"
|
site.config['keep_files'] << keep_dir unless site.config['keep_files'].include?(keep_dir)
|
||||||
|
end
|
||||||
|
|
||||||
site.config['keep_files'] << output_dir unless site.config['keep_files'].include?(output_dir)
|
def render_responsive_image(context, attributes)
|
||||||
|
cache_key = attributes.to_s
|
||||||
|
result = attributes['cache'] ? RenderCache.get(cache_key) : nil
|
||||||
|
|
||||||
config
|
if result.nil?
|
||||||
|
site = context.registers[:site]
|
||||||
|
config = make_config(site)
|
||||||
|
|
||||||
|
source_image_path = site.in_source_dir(attributes['path'].to_s)
|
||||||
|
image = ImageProcessor.process(source_image_path, config)
|
||||||
|
attributes['original'] = image[:original]
|
||||||
|
attributes['resized'] = image[:resized]
|
||||||
|
|
||||||
|
attributes['resized'].each { |resized| keep_resized_image!(site, resized) }
|
||||||
|
|
||||||
|
image_template = site.in_source_dir(attributes['template'] || config['template'])
|
||||||
|
partial = File.read(image_template)
|
||||||
|
template = Liquid::Template.parse(partial)
|
||||||
|
|
||||||
|
result = template.render!(attributes.merge(site.site_payload))
|
||||||
|
|
||||||
|
|
||||||
|
RenderCache.set(cache_key, result)
|
||||||
|
end
|
||||||
|
|
||||||
|
result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -7,7 +7,10 @@ module Jekyll
|
||||||
config = make_config(site)
|
config = make_config(site)
|
||||||
|
|
||||||
config['extra_images'].each do |pathspec|
|
config['extra_images'].each do |pathspec|
|
||||||
Dir.glob(pathspec) { |path| ImageProcessor.process(path, config) }
|
Dir.glob(site.in_source_dir(pathspec)) do |path|
|
||||||
|
result = ImageProcessor.process(path, config)
|
||||||
|
result[:resized].each { |image| keep_resized_image!(site, image) }
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -4,7 +4,7 @@ module Jekyll
|
||||||
include ResponsiveImage::Utils
|
include ResponsiveImage::Utils
|
||||||
|
|
||||||
def process(image_path, config)
|
def process(image_path, config)
|
||||||
raise SyntaxError.new("Invalid image path specified: #{image_path}") unless File.exist?(image_path.to_s)
|
raise SyntaxError.new("Invalid image path specified: #{image_path}") unless File.file?(image_path)
|
||||||
|
|
||||||
resize_handler = ResizeHandler.new
|
resize_handler = ResizeHandler.new
|
||||||
img = Magick::Image::read(image_path).first
|
img = Magick::Image::read(image_path).first
|
||||||
|
|
|
@ -17,22 +17,24 @@ module Jekyll
|
||||||
filepath = format_output_path(config['output_path_format'], config['base_path'], image_path, width, height)
|
filepath = format_output_path(config['output_path_format'], config['base_path'], image_path, width, height)
|
||||||
resized.push(image_hash(config['base_path'], filepath, width, height))
|
resized.push(image_hash(config['base_path'], filepath, width, height))
|
||||||
|
|
||||||
# Don't resize images more than once
|
site_source_filepath = File.expand_path(filepath, config[:site_source])
|
||||||
next if File.exist?(filepath)
|
site_dest_filepath = File.expand_path(filepath, config[:site_dest])
|
||||||
|
|
||||||
ensure_output_dir_exists!(File.dirname(filepath))
|
# Don't resize images more than once
|
||||||
|
next if File.exist?(site_source_filepath)
|
||||||
|
|
||||||
|
ensure_output_dir_exists!(File.dirname(site_source_filepath))
|
||||||
|
ensure_output_dir_exists!(File.dirname(site_dest_filepath))
|
||||||
|
|
||||||
Jekyll.logger.info "Generating #{filepath}"
|
Jekyll.logger.info "Generating #{filepath}"
|
||||||
|
|
||||||
i = img.scale(ratio)
|
i = img.scale(ratio)
|
||||||
i.write(filepath) do |f|
|
i.write(site_source_filepath) do |f|
|
||||||
f.quality = size['quality'] || config['default_quality']
|
f.quality = size['quality'] || config['default_quality']
|
||||||
end
|
end
|
||||||
|
|
||||||
# Ensure the generated file is copied to the _site directory
|
# Ensure the generated file is copied to the _site directory
|
||||||
site_dest_filepath = File.expand_path(filepath, config[:site_dest])
|
FileUtils.copy_file(site_source_filepath, site_dest_filepath)
|
||||||
ensure_output_dir_exists!(File.dirname(site_dest_filepath))
|
|
||||||
FileUtils.copy(filepath, site_dest_filepath)
|
|
||||||
|
|
||||||
i.destroy!
|
i.destroy!
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,28 +15,7 @@ module Jekyll
|
||||||
end
|
end
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
cache_key = @attributes.to_s
|
render_responsive_image(context, @attributes)
|
||||||
result = @attributes['cache'] ? RenderCache.get(cache_key) : nil
|
|
||||||
|
|
||||||
if result.nil?
|
|
||||||
site = context.registers[:site]
|
|
||||||
config = make_config(site)
|
|
||||||
|
|
||||||
image = ImageProcessor.process(@attributes['path'], config)
|
|
||||||
@attributes['original'] = image[:original]
|
|
||||||
@attributes['resized'] = image[:resized]
|
|
||||||
|
|
||||||
image_template = @attributes['template'] || config['template']
|
|
||||||
|
|
||||||
partial = File.read(image_template)
|
|
||||||
template = Liquid::Template.parse(partial)
|
|
||||||
|
|
||||||
result = template.render!(@attributes.merge(site.site_payload))
|
|
||||||
|
|
||||||
RenderCache.set(cache_key, result)
|
|
||||||
end
|
|
||||||
|
|
||||||
result
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue