Fix absolute path bug (#35)

* Fix file contents test

* Fix tests to catch the bug

* Fix bug where `original.path` was absolute
This commit is contained in:
Joseph Wynn 2016-09-25 21:37:10 +01:00 committed by GitHub
parent 707b1078c5
commit bd336070e1
6 changed files with 24 additions and 25 deletions

View File

@ -43,27 +43,26 @@ Feature: Jekyll responsive_image tag
- width: 100 - width: 100
- width: 200 - width: 200
""" """
And I have a file "index.html" with "{% responsive_image path: assets/everybody-loves-jalapeño-pineapple-cornbread.png %}" And I have a file "index.html" with "{% responsive_image path: assets/subdir/test.png %}"
When I run Jekyll When I run Jekyll
Then I should see "<img alt=\"\" src=\"/assets/everybody-loves-jalapeño-pineapple-cornbread.png\"" in "_site/index.html" Then I should see "<img alt=\"\" src=\"/assets/subdir/test.png\"" in "_site/index.html"
And I should see "/assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png 100w" in "_site/index.html" And I should see "/assets/resized/test-100x50.png 100w,/assets/resized/test-200x100.png 200w,/assets/subdir/test.png 300w" in "_site/index.html"
And I should see "/assets/resized/everybody-loves-jalapeño-pineapple-cornbread-200x100.png 200w" in "_site/index.html"
And I should see "/assets/everybody-loves-jalapeño-pineapple-cornbread.png 300w" in "_site/index.html" And the file "assets/resized/test-100x50.png" should exist
And the file "assets/resized/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should exist And the file "assets/resized/test-200x100.png" should exist
And the file "assets/resized/everybody-loves-jalapeño-pineapple-cornbread-200x100.png" should exist
Scenario: Overriding the template Scenario: Overriding the template
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
sizes: sizes:
- width: 50
- width: 100 - width: 100
- width: 200 - width: 150
- width: 300
""" """
And I have a file "index.html" with "{% responsive_image path: assets/everybody-loves-jalapeño-pineapple-cornbread.png template: _includes/custom-template.html %}" And I have a file "index.html" with "{% responsive_image path: assets/everybody-loves-jalapeño-pineapple-cornbread.png template: _includes/custom-template.html %}"
When I run Jekyll When I run Jekyll
Then I should see "[100, 200, 300]" in "_site/index.html" Then I should see "[50, 100, 150]" in "_site/index.html"
Scenario: Overriding the generated filenames Scenario: Overriding the generated filenames
Given I have a responsive_image configuration with: Given I have a responsive_image configuration with:

View File

@ -39,7 +39,8 @@ Given /^I have a file "(.+)" with "(.+)"$/ do |path, contents|
end end
Then /^I should see "(.+)" in "(.*)"$/ do |text, file| Then /^I should see "(.+)" in "(.*)"$/ do |text, file|
assert_match(Regexp.new(text), File.open(file).readlines.join) contents = File.open(file).readlines.join
assert contents.inspect.include?(text), "Expected to find #{text.inspect} in #{contents.inspect}"
end end
Then /^the file "(.+)" should exist$/ do |path| Then /^the file "(.+)" should exist$/ do |path|

View File

@ -1,4 +1 @@
<img alt="{{ alt }}" src="/{{ path }}" title="{{ title }}" srcset=" <img alt="{{ alt }}" src="/{{ path }}" title="{{ title }}" srcset="{% for i in resized %}/{{ i.path }} {{ i.width }}w,{% endfor %}/{{ original.path }} {{ original.width }}w">
{% for i in resized %}/{{ i.path }} {{ i.width }}w,{% endfor %}
/{{ original.path }} {{ original.width }}w
">

View File

@ -22,8 +22,8 @@ module Jekyll
site = context.registers[:site] site = context.registers[:site]
config = make_config(site) config = make_config(site)
source_image_path = site.in_source_dir(attributes['path'].to_s) absolute_image_path = site.in_source_dir(attributes['path'].to_s)
image = ImageProcessor.process(source_image_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]

View File

@ -7,8 +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(site.in_source_dir(pathspec)) do |path| Dir.glob(site.in_source_dir(pathspec)) do |image_path|
result = ImageProcessor.process(path, config) relative_image_path = image_path.sub(/^#{Regexp.escape(image_path)}/, '')
result = ImageProcessor.process(image_path, 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

View File

@ -3,20 +3,20 @@ module Jekyll
class ImageProcessor class ImageProcessor
include ResponsiveImage::Utils include ResponsiveImage::Utils
def process(image_path, config) def process(absolute_image_path, relative_image_path, config)
raise SyntaxError.new("Invalid image path specified: #{image_path}") unless File.file?(image_path) raise SyntaxError.new("Invalid image path specified: #{absolute_image_path}") unless File.file?(absolute_image_path)
resize_handler = ResizeHandler.new resize_handler = ResizeHandler.new
img = Magick::Image::read(image_path).first img = Magick::Image::read(absolute_image_path).first
{ {
original: image_hash(config['base_path'], image_path, img.columns, img.rows), original: image_hash(config['base_path'], relative_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(image_path, config) def self.process(absolute_image_path, relative_image_path, config)
self.new.process(image_path, config) self.new.process(absolute_image_path, relative_image_path, config)
end end
end end
end end