Skip to content

Commit

Permalink
Fix absolute path bug (#35)
Browse files Browse the repository at this point in the history
* Fix file contents test

* Fix tests to catch the bug

* Fix bug where `original.path` was absolute
  • Loading branch information
wildlyinaccurate authored Sep 25, 2016
1 parent 707b107 commit bd33607
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 25 deletions.
19 changes: 9 additions & 10 deletions features/responsive-image-tag.feature
Original file line number Diff line number Diff line change
Expand Up @@ -43,27 +43,26 @@ Feature: Jekyll responsive_image tag
- width: 100
- 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
Then I should see "<img alt=\"\" src=\"/assets/everybody-loves-jalapeño-pineapple-cornbread.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/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/everybody-loves-jalapeño-pineapple-cornbread-100x50.png" should exist
And the file "assets/resized/everybody-loves-jalapeño-pineapple-cornbread-200x100.png" should exist
Then I should see "<img alt=\"\" src=\"/assets/subdir/test.png\"" 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 the file "assets/resized/test-100x50.png" should exist
And the file "assets/resized/test-200x100.png" should exist

Scenario: Overriding the template
Given I have a responsive_image configuration with:
"""
template: _includes/responsive-image.html
sizes:
- width: 50
- width: 100
- width: 200
- width: 300
- width: 150
"""
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
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
Given I have a responsive_image configuration with:
Expand Down
3 changes: 2 additions & 1 deletion features/step_definitions/jekyll_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
end

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

Then /^the file "(.+)" should exist$/ do |path|
Expand Down
5 changes: 1 addition & 4 deletions features/test-site/_includes/responsive-image.html
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
<img alt="{{ alt }}" src="/{{ path }}" title="{{ title }}" srcset="
{% for i in resized %}/{{ i.path }} {{ i.width }}w,{% endfor %}
/{{ original.path }} {{ original.width }}w
">
<img alt="{{ alt }}" src="/{{ path }}" title="{{ title }}" srcset="{% for i in resized %}/{{ i.path }} {{ i.width }}w,{% endfor %}/{{ original.path }} {{ original.width }}w">
4 changes: 2 additions & 2 deletions lib/jekyll/responsive_image/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ def render_responsive_image(context, attributes)
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)
absolute_image_path = site.in_source_dir(attributes['path'].to_s)
image = ImageProcessor.process(absolute_image_path, attributes['path'], config)
attributes['original'] = image[:original]
attributes['resized'] = image[:resized]

Expand Down
6 changes: 4 additions & 2 deletions lib/jekyll/responsive_image/extra_image_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ def generate(site)
config = make_config(site)

config['extra_images'].each do |pathspec|
Dir.glob(site.in_source_dir(pathspec)) do |path|
result = ImageProcessor.process(path, config)
Dir.glob(site.in_source_dir(pathspec)) do |image_path|
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) }
end
end
Expand Down
12 changes: 6 additions & 6 deletions lib/jekyll/responsive_image/image_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ class ResponsiveImage
class ImageProcessor
include ResponsiveImage::Utils

def process(image_path, config)
raise SyntaxError.new("Invalid image path specified: #{image_path}") unless File.file?(image_path)
def process(absolute_image_path, relative_image_path, config)
raise SyntaxError.new("Invalid image path specified: #{absolute_image_path}") unless File.file?(absolute_image_path)

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),
}
end

def self.process(image_path, config)
self.new.process(image_path, config)
def self.process(absolute_image_path, relative_image_path, config)
self.new.process(absolute_image_path, relative_image_path, config)
end
end
end
Expand Down

0 comments on commit bd33607

Please sign in to comment.