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:
|
||||
- width: 100
|
||||
|
||||
extra_images:
|
||||
- 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!"
|
||||
When I run Jekyll
|
||||
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
|
||||
Given I have a responsive_image configuration with:
|
||||
"""
|
||||
sizes:
|
||||
- width: 100
|
||||
|
||||
extra_images:
|
||||
- assets/*.png
|
||||
"""
|
||||
|
@ -30,6 +29,24 @@ Feature: Extra image generation
|
|||
And I have a file "index.html" with "Hello, world!"
|
||||
When I run Jekyll
|
||||
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
|
||||
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 %}"
|
||||
When I run Jekyll
|
||||
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
|
||||
Given I have a responsive_image configuration with:
|
||||
|
@ -33,4 +34,21 @@ Feature: Responsive image generation
|
|||
|
||||
When I run Jekyll
|
||||
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 "_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
|
||||
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 "_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 }
|
||||
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|
|
||||
write_file('_config.yml', config)
|
||||
end
|
||||
|
|
|
@ -9,9 +9,6 @@ require 'jekyll/responsive_image'
|
|||
TEST_DIR = File.join('/', 'tmp', 'jekyll')
|
||||
|
||||
def run_jekyll(options = {})
|
||||
options['source'] ||= TEST_DIR
|
||||
options['destination'] ||= File.join(TEST_DIR, '_site')
|
||||
|
||||
options = Jekyll.configuration(options)
|
||||
|
||||
site = Jekyll::Site.new(options)
|
||||
|
|
|
@ -2,8 +2,8 @@ Before do
|
|||
FileUtils.rm_rf(TEST_DIR) if File.exist?(TEST_DIR)
|
||||
FileUtils.mkdir_p(TEST_DIR)
|
||||
|
||||
fixtures = File.expand_path('../../fixtures', __FILE__)
|
||||
FileUtils.cp_r(Dir.glob("#{fixtures}/*"), TEST_DIR)
|
||||
test_site = File.expand_path('../../test-site', __FILE__)
|
||||
FileUtils.cp_r(Dir.glob("#{test_site}/*"), TEST_DIR)
|
||||
|
||||
Dir.chdir(TEST_DIR)
|
||||
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
|
||||
class ResponsiveImage
|
||||
class Block < Liquid::Block
|
||||
|
@ -5,29 +14,7 @@ module Jekyll
|
|||
|
||||
def render(context)
|
||||
attributes = YAML.load(super)
|
||||
|
||||
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
|
||||
render_responsive_image(context, attributes)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,15 +4,42 @@ module Jekyll
|
|||
include Jekyll::ResponsiveImage::Utils
|
||||
|
||||
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
|
||||
output_dir = format_output_path(config['output_path_format'], config['base_path'], '*', '*', '*')
|
||||
output_dir = "#{File.dirname(output_dir)}/*"
|
||||
def keep_resized_image!(site, image)
|
||||
keep_dir = File.dirname(image['path'])
|
||||
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
|
||||
|
|
|
@ -7,7 +7,10 @@ module Jekyll
|
|||
config = make_config(site)
|
||||
|
||||
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
|
||||
|
|
|
@ -4,7 +4,7 @@ module Jekyll
|
|||
include ResponsiveImage::Utils
|
||||
|
||||
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
|
||||
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)
|
||||
resized.push(image_hash(config['base_path'], filepath, width, height))
|
||||
|
||||
# Don't resize images more than once
|
||||
next if File.exist?(filepath)
|
||||
site_source_filepath = File.expand_path(filepath, config[:site_source])
|
||||
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}"
|
||||
|
||||
i = img.scale(ratio)
|
||||
i.write(filepath) do |f|
|
||||
i.write(site_source_filepath) do |f|
|
||||
f.quality = size['quality'] || config['default_quality']
|
||||
end
|
||||
|
||||
# Ensure the generated file is copied to the _site directory
|
||||
site_dest_filepath = File.expand_path(filepath, config[:site_dest])
|
||||
ensure_output_dir_exists!(File.dirname(site_dest_filepath))
|
||||
FileUtils.copy(filepath, site_dest_filepath)
|
||||
FileUtils.copy_file(site_source_filepath, site_dest_filepath)
|
||||
|
||||
i.destroy!
|
||||
end
|
||||
|
|
|
@ -15,28 +15,7 @@ module Jekyll
|
|||
end
|
||||
|
||||
def render(context)
|
||||
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 = 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
|
||||
render_responsive_image(context, @attributes)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue