Skip to content

Commit

Permalink
Merge pull request #36 from wildlyinaccurate/source-directory-take-2
Browse files Browse the repository at this point in the history
Take 2 of "treat paths as relative to Jekyll `source` directory"
  • Loading branch information
wildlyinaccurate authored Oct 14, 2016
2 parents a1a183c + 854aa4c commit 1d7273e
Show file tree
Hide file tree
Showing 20 changed files with 218 additions and 122 deletions.
12 changes: 7 additions & 5 deletions features/image-generation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,18 @@ Feature: Responsive image generation
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"
Given I have copied my site to "my-site-copy/src"
And I have a configuration with:
"""
source: sub-dir/my-site-copy
source: my-site-copy/src
responsive_image:
base_path: assets
template: _includes/responsive-image.html
output_path_format: assets/resized/%{dirname}/%{width}/%{basename}
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 %}"
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 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
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/100/test.png" should exist
75 changes: 75 additions & 0 deletions features/image-hashes.feature
Original file line number Diff line number Diff line change
@@ -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
"""
6 changes: 4 additions & 2 deletions features/step_definitions/jekyll_steps.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
include Test::Unit::Assertions

When /^I run Jekyll$/ do
run_jekyll
end
Expand Down Expand Up @@ -43,6 +41,10 @@
assert contents.inspect.include?(text), "Expected to find #{text.inspect} in #{contents.inspect}"
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|
assert File.exist?(path)
end
Expand Down
4 changes: 3 additions & 1 deletion features/support/env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Coveralls.wear!
end

require 'test/unit'
require 'test/unit/assertions'
require 'jekyll/responsive_image'

TEST_DIR = File.join('/', 'tmp', 'jekyll')
Expand All @@ -14,3 +14,5 @@ def run_jekyll(options = {})
site = Jekyll::Site.new(options)
site.process
end

World(Test::Unit::Assertions)
2 changes: 1 addition & 1 deletion features/test-site/_includes/base-url.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<img src="{{ site.baseurl }}/{{ path }}">
<img src="{{ site.baseurl }}/{{ original.path }}">
16 changes: 16 additions & 0 deletions features/test-site/_includes/hash.html
Original file line number Diff line number Diff line change
@@ -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 %}
2 changes: 1 addition & 1 deletion features/test-site/_includes/responsive-image.html
Original file line number Diff line number Diff line change
@@ -1 +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="/{{ original.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.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
require 'rmagick'

require 'jekyll/responsive_image/version'
require 'jekyll/responsive_image/defaults'
require 'jekyll/responsive_image/config'
require 'jekyll/responsive_image/utils'
require 'jekyll/responsive_image/render_cache'
require 'jekyll/responsive_image/image_processor'
require 'jekyll/responsive_image/resize_handler'
require 'jekyll/responsive_image/common'
require 'jekyll/responsive_image/renderer'
require 'jekyll/responsive_image/tag'
require 'jekyll/responsive_image/block'
require 'jekyll/responsive_image/extra_image_generator'
Expand Down
15 changes: 3 additions & 12 deletions lib/jekyll/responsive_image/block.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@









module Jekyll
class ResponsiveImage
module ResponsiveImage
class Block < Liquid::Block
include Jekyll::ResponsiveImage::Common
include Jekyll::ResponsiveImage::Utils

def render(context)
attributes = YAML.load(super)
render_responsive_image(context, attributes)
Renderer.new(context.registers[:site], attributes).render_responsive_image
end
end
end
Expand Down
46 changes: 0 additions & 46 deletions lib/jekyll/responsive_image/common.rb

This file was deleted.

22 changes: 22 additions & 0 deletions lib/jekyll/responsive_image/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Jekyll
module ResponsiveImage
class Config
DEFAULTS = {
'default_quality' => 85,
'base_path' => 'assets',
'output_path_format' => 'assets/resized/%{filename}-%{width}x%{height}.%{extension}',
'sizes' => [],
'extra_images' => []
}

def initialize(site)
@site = site
end

def to_h
DEFAULTS.merge(@site.config['responsive_image'])
.merge(site_source: @site.source, site_dest: @site.dest)
end
end
end
end
15 changes: 0 additions & 15 deletions lib/jekyll/responsive_image/defaults.rb

This file was deleted.

12 changes: 7 additions & 5 deletions lib/jekyll/responsive_image/extra_image_generator.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
module Jekyll
class ResponsiveImage
module ResponsiveImage
class ExtraImageGenerator < Jekyll::Generator
include Jekyll::ResponsiveImage::Common
include Jekyll::ResponsiveImage::Utils

def generate(site)
config = make_config(site)
config = Config.new(site).to_h
site_source = Pathname.new(site.source)

config['extra_images'].each do |pathspec|
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) }
end
end
Expand Down
14 changes: 8 additions & 6 deletions lib/jekyll/responsive_image/image_processor.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
module Jekyll
class ResponsiveImage
module ResponsiveImage
class ImageProcessor
include ResponsiveImage::Utils

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)
def process(image_path, config)
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
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),
}
end

def self.process(absolute_image_path, relative_image_path, config)
self.new.process(absolute_image_path, relative_image_path, config)
def self.process(image_path, config)
self.new.process(image_path, config)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/jekyll/responsive_image/render_cache.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Jekyll
class ResponsiveImage
module ResponsiveImage
class RenderCache
attr_accessor :store

Expand Down
37 changes: 37 additions & 0 deletions lib/jekyll/responsive_image/renderer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Jekyll
module ResponsiveImage
class Renderer
include Jekyll::ResponsiveImage::Utils

def initialize(site, attributes)
@site = site
@attributes = attributes
end

def render_responsive_image
cache_key = @attributes.to_s
result = @attributes['cache'] ? RenderCache.get(cache_key) : nil

if result.nil?
config = Config.new(@site).to_h

image = ImageProcessor.process(@attributes['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
Loading

0 comments on commit 1d7273e

Please sign in to comment.