From 354f693afce2ef0ab44f649186d28b1c5df4cf66 Mon Sep 17 00:00:00 2001 From: Joseph Wynn Date: Tue, 27 Sep 2016 19:59:24 +0100 Subject: [PATCH 1/7] Another refactor to try and avoid `include` --- lib/jekyll/responsive_image.rb | 2 +- lib/jekyll/responsive_image/block.rb | 15 ++---- lib/jekyll/responsive_image/common.rb | 46 ------------------- lib/jekyll/responsive_image/defaults.rb | 2 +- .../responsive_image/extra_image_generator.rb | 7 +-- .../responsive_image/image_processor.rb | 2 +- lib/jekyll/responsive_image/render_cache.rb | 2 +- lib/jekyll/responsive_image/renderer.rb | 44 ++++++++++++++++++ lib/jekyll/responsive_image/resize_handler.rb | 2 +- lib/jekyll/responsive_image/tag.rb | 6 +-- lib/jekyll/responsive_image/utils.rb | 7 ++- lib/jekyll/responsive_image/version.rb | 2 +- 12 files changed, 65 insertions(+), 72 deletions(-) delete mode 100644 lib/jekyll/responsive_image/common.rb create mode 100644 lib/jekyll/responsive_image/renderer.rb diff --git a/lib/jekyll/responsive_image.rb b/lib/jekyll/responsive_image.rb index 70c3de9..0224bcf 100644 --- a/lib/jekyll/responsive_image.rb +++ b/lib/jekyll/responsive_image.rb @@ -10,7 +10,7 @@ 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' diff --git a/lib/jekyll/responsive_image/block.rb b/lib/jekyll/responsive_image/block.rb index 935635f..c5d07ad 100644 --- a/lib/jekyll/responsive_image/block.rb +++ b/lib/jekyll/responsive_image/block.rb @@ -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 diff --git a/lib/jekyll/responsive_image/common.rb b/lib/jekyll/responsive_image/common.rb deleted file mode 100644 index 60bc103..0000000 --- a/lib/jekyll/responsive_image/common.rb +++ /dev/null @@ -1,46 +0,0 @@ -module Jekyll - class ResponsiveImage - module Common - include Jekyll::ResponsiveImage::Utils - - def make_config(site) - ResponsiveImage.defaults.dup - .merge(site.config['responsive_image']) - .merge(:site_source => site.source, :site_dest => site.dest) - end - - 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 - - def 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) - - 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] - - 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 diff --git a/lib/jekyll/responsive_image/defaults.rb b/lib/jekyll/responsive_image/defaults.rb index c8ddc5f..6665cda 100644 --- a/lib/jekyll/responsive_image/defaults.rb +++ b/lib/jekyll/responsive_image/defaults.rb @@ -1,5 +1,5 @@ module Jekyll - class ResponsiveImage + module ResponsiveImage @defaults = { 'default_quality' => 85, 'base_path' => 'assets', diff --git a/lib/jekyll/responsive_image/extra_image_generator.rb b/lib/jekyll/responsive_image/extra_image_generator.rb index 70932fe..9a15703 100644 --- a/lib/jekyll/responsive_image/extra_image_generator.rb +++ b/lib/jekyll/responsive_image/extra_image_generator.rb @@ -1,10 +1,11 @@ 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) + renderer = Renderer.new(site, {}) + config = renderer.make_config config['extra_images'].each do |pathspec| Dir.glob(site.in_source_dir(pathspec)) do |image_path| diff --git a/lib/jekyll/responsive_image/image_processor.rb b/lib/jekyll/responsive_image/image_processor.rb index 86a2b3a..dcaaada 100644 --- a/lib/jekyll/responsive_image/image_processor.rb +++ b/lib/jekyll/responsive_image/image_processor.rb @@ -1,5 +1,5 @@ module Jekyll - class ResponsiveImage + module ResponsiveImage class ImageProcessor include ResponsiveImage::Utils diff --git a/lib/jekyll/responsive_image/render_cache.rb b/lib/jekyll/responsive_image/render_cache.rb index 6de1768..467a7f2 100644 --- a/lib/jekyll/responsive_image/render_cache.rb +++ b/lib/jekyll/responsive_image/render_cache.rb @@ -1,5 +1,5 @@ module Jekyll - class ResponsiveImage + module ResponsiveImage class RenderCache attr_accessor :store diff --git a/lib/jekyll/responsive_image/renderer.rb b/lib/jekyll/responsive_image/renderer.rb new file mode 100644 index 0000000..a03201c --- /dev/null +++ b/lib/jekyll/responsive_image/renderer.rb @@ -0,0 +1,44 @@ +module Jekyll + module ResponsiveImage + class Renderer + include Jekyll::ResponsiveImage::Utils + + def initialize(site, attributes) + @site = site + @attributes = attributes + end + + def make_config + ResponsiveImage.defaults.dup + .merge(@site.config['responsive_image']) + .merge(:site_source => @site.source, :site_dest => @site.dest) + end + + def render_responsive_image + cache_key = @attributes.to_s + result = @attributes['cache'] ? RenderCache.get(cache_key) : nil + + if result.nil? + config = make_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] + + @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 diff --git a/lib/jekyll/responsive_image/resize_handler.rb b/lib/jekyll/responsive_image/resize_handler.rb index 04b5e8d..3358b72 100644 --- a/lib/jekyll/responsive_image/resize_handler.rb +++ b/lib/jekyll/responsive_image/resize_handler.rb @@ -1,5 +1,5 @@ module Jekyll - class ResponsiveImage + module ResponsiveImage class ResizeHandler include ResponsiveImage::Utils diff --git a/lib/jekyll/responsive_image/tag.rb b/lib/jekyll/responsive_image/tag.rb index 7994c2b..6ba8147 100644 --- a/lib/jekyll/responsive_image/tag.rb +++ b/lib/jekyll/responsive_image/tag.rb @@ -1,8 +1,6 @@ module Jekyll - class ResponsiveImage + module ResponsiveImage class Tag < Liquid::Tag - include Jekyll::ResponsiveImage::Common - def initialize(tag_name, markup, tokens) super @@ -15,7 +13,7 @@ module Jekyll end def render(context) - render_responsive_image(context, @attributes) + Renderer.new(context.registers[:site], @attributes).render_responsive_image end end end diff --git a/lib/jekyll/responsive_image/utils.rb b/lib/jekyll/responsive_image/utils.rb index fbe612e..9aeac88 100644 --- a/lib/jekyll/responsive_image/utils.rb +++ b/lib/jekyll/responsive_image/utils.rb @@ -1,8 +1,13 @@ require 'pathname' module Jekyll - class ResponsiveImage + module ResponsiveImage module Utils + 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 + def symbolize_keys(hash) result = {} hash.each_key do |key| diff --git a/lib/jekyll/responsive_image/version.rb b/lib/jekyll/responsive_image/version.rb index 2a793e9..a094b92 100644 --- a/lib/jekyll/responsive_image/version.rb +++ b/lib/jekyll/responsive_image/version.rb @@ -1,5 +1,5 @@ module Jekyll - class ResponsiveImage + module ResponsiveImage VERSION = '1.0.0.pre3'.freeze end end From b47f12a1db1bd47c07e8df121fa4bcd988b4c73f Mon Sep 17 00:00:00 2001 From: Joseph Wynn Date: Tue, 27 Sep 2016 19:59:35 +0100 Subject: [PATCH 2/7] An actual failing test now --- features/image-generation.feature | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/features/image-generation.feature b/features/image-generation.feature index 4fbfb51..8e640b2 100644 --- a/features/image-generation.feature +++ b/features/image-generation.feature @@ -39,16 +39,17 @@ 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: 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/test-100x50.png" should have the dimensions "100x50" + And the file "_site/assets/resized/subdir/test-100x50.png" should exist From 6d569710b9edceb8aa3cedd1e6080ff5d3a89dcb Mon Sep 17 00:00:00 2001 From: Joseph Wynn Date: Tue, 27 Sep 2016 23:48:24 +0100 Subject: [PATCH 3/7] More test improvements --- features/step_definitions/jekyll_steps.rb | 2 -- features/support/env.rb | 4 +++- features/test-site/_includes/base-url.html | 2 +- features/test-site/_includes/responsive-image.html | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/features/step_definitions/jekyll_steps.rb b/features/step_definitions/jekyll_steps.rb index 5ca4529..6612ba9 100644 --- a/features/step_definitions/jekyll_steps.rb +++ b/features/step_definitions/jekyll_steps.rb @@ -1,5 +1,3 @@ -include Test::Unit::Assertions - When /^I run Jekyll$/ do run_jekyll end diff --git a/features/support/env.rb b/features/support/env.rb index eaed1ae..d686c8e 100644 --- a/features/support/env.rb +++ b/features/support/env.rb @@ -3,7 +3,7 @@ if ENV['CI'] Coveralls.wear! end -require 'test/unit' +require 'test/unit/assertions' require 'jekyll/responsive_image' TEST_DIR = File.join('/', 'tmp', 'jekyll') @@ -14,3 +14,5 @@ def run_jekyll(options = {}) site = Jekyll::Site.new(options) site.process end + +World(Test::Unit::Assertions) diff --git a/features/test-site/_includes/base-url.html b/features/test-site/_includes/base-url.html index e3e6485..1fa4aed 100644 --- a/features/test-site/_includes/base-url.html +++ b/features/test-site/_includes/base-url.html @@ -1 +1 @@ - + diff --git a/features/test-site/_includes/responsive-image.html b/features/test-site/_includes/responsive-image.html index 277fb39..f2df49e 100644 --- a/features/test-site/_includes/responsive-image.html +++ b/features/test-site/_includes/responsive-image.html @@ -1 +1 @@ -{{ alt }} +{{ alt }} From 4de5db835dd2cde58dc9bfbcabc869bae66febb0 Mon Sep 17 00:00:00 2001 From: Joseph Wynn Date: Tue, 27 Sep 2016 23:49:18 +0100 Subject: [PATCH 4/7] Refactor to config class --- lib/jekyll/responsive_image.rb | 2 +- lib/jekyll/responsive_image/config.rb | 26 +++++++++++++++++++ lib/jekyll/responsive_image/defaults.rb | 15 ----------- .../responsive_image/extra_image_generator.rb | 3 +-- lib/jekyll/responsive_image/renderer.rb | 8 +----- 5 files changed, 29 insertions(+), 25 deletions(-) create mode 100644 lib/jekyll/responsive_image/config.rb delete mode 100644 lib/jekyll/responsive_image/defaults.rb diff --git a/lib/jekyll/responsive_image.rb b/lib/jekyll/responsive_image.rb index 0224bcf..1913060 100644 --- a/lib/jekyll/responsive_image.rb +++ b/lib/jekyll/responsive_image.rb @@ -5,7 +5,7 @@ require 'jekyll' 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' diff --git a/lib/jekyll/responsive_image/config.rb b/lib/jekyll/responsive_image/config.rb new file mode 100644 index 0000000..d11a24c --- /dev/null +++ b/lib/jekyll/responsive_image/config.rb @@ -0,0 +1,26 @@ +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 + config = DEFAULTS.merge(@site.config['responsive_image']) + .merge(site_source: @site.source, site_dest: @site.dest) + + config['base_path'] = @site.in_source_dir(config['base_path']) + + config + end + end + end +end diff --git a/lib/jekyll/responsive_image/defaults.rb b/lib/jekyll/responsive_image/defaults.rb deleted file mode 100644 index 6665cda..0000000 --- a/lib/jekyll/responsive_image/defaults.rb +++ /dev/null @@ -1,15 +0,0 @@ -module Jekyll - module ResponsiveImage - @defaults = { - 'default_quality' => 85, - 'base_path' => 'assets', - 'output_path_format' => 'assets/resized/%{filename}-%{width}x%{height}.%{extension}', - 'sizes' => [], - 'extra_images' => [] - }.freeze - - class << self - attr_reader :defaults - end - end -end diff --git a/lib/jekyll/responsive_image/extra_image_generator.rb b/lib/jekyll/responsive_image/extra_image_generator.rb index 9a15703..80ab4e7 100644 --- a/lib/jekyll/responsive_image/extra_image_generator.rb +++ b/lib/jekyll/responsive_image/extra_image_generator.rb @@ -4,8 +4,7 @@ module Jekyll include Jekyll::ResponsiveImage::Utils def generate(site) - renderer = Renderer.new(site, {}) - config = renderer.make_config + config = Config.new(site).to_h config['extra_images'].each do |pathspec| Dir.glob(site.in_source_dir(pathspec)) do |image_path| diff --git a/lib/jekyll/responsive_image/renderer.rb b/lib/jekyll/responsive_image/renderer.rb index a03201c..2c30679 100644 --- a/lib/jekyll/responsive_image/renderer.rb +++ b/lib/jekyll/responsive_image/renderer.rb @@ -8,18 +8,12 @@ module Jekyll @attributes = attributes end - def make_config - ResponsiveImage.defaults.dup - .merge(@site.config['responsive_image']) - .merge(:site_source => @site.source, :site_dest => @site.dest) - end - def render_responsive_image cache_key = @attributes.to_s result = @attributes['cache'] ? RenderCache.get(cache_key) : nil if result.nil? - config = make_config + config = Config.new(@site).to_h absolute_image_path = @site.in_source_dir(@attributes['path'].to_s) image = ImageProcessor.process(absolute_image_path, @attributes['path'], config) From 83358e79b570599b10ff2723c8ca4eaecb241a74 Mon Sep 17 00:00:00 2001 From: Joseph Wynn Date: Tue, 27 Sep 2016 23:50:01 +0100 Subject: [PATCH 5/7] Resize handler tweaks --- lib/jekyll/responsive_image/resize_handler.rb | 17 +++++++++++++---- lib/jekyll/responsive_image/utils.rb | 6 ------ 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/lib/jekyll/responsive_image/resize_handler.rb b/lib/jekyll/responsive_image/resize_handler.rb index 3358b72..cc16f83 100644 --- a/lib/jekyll/responsive_image/resize_handler.rb +++ b/lib/jekyll/responsive_image/resize_handler.rb @@ -23,10 +23,10 @@ module Jekyll # 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)) + ensure_output_dir_exists!(site_source_filepath) + ensure_output_dir_exists!(site_dest_filepath) - Jekyll.logger.info "Generating #{filepath}" + Jekyll.logger.info "Generating #{site_source_filepath}" i = img.scale(ratio) i.write(site_source_filepath) do |f| @@ -34,6 +34,7 @@ module Jekyll end # Ensure the generated file is copied to the _site directory + Jekyll.logger.info "Copying resized image to #{site_dest_filepath}" FileUtils.copy_file(site_source_filepath, site_dest_filepath) i.destroy! @@ -44,11 +45,19 @@ module Jekyll resized end + def format_output_path(format, base_path, image_path, width, height) + params = symbolize_keys(image_hash(base_path, image_path, width, height)) + + Pathname.new(format % params).cleanpath.to_s + end + def needs_resizing?(img, width) img.columns > width end - def ensure_output_dir_exists!(dir) + def ensure_output_dir_exists!(path) + dir = File.dirname(path) + unless Dir.exist?(dir) Jekyll.logger.info "Creating output directory #{dir}" FileUtils.mkdir_p(dir) diff --git a/lib/jekyll/responsive_image/utils.rb b/lib/jekyll/responsive_image/utils.rb index 9aeac88..4688e92 100644 --- a/lib/jekyll/responsive_image/utils.rb +++ b/lib/jekyll/responsive_image/utils.rb @@ -16,12 +16,6 @@ module Jekyll result end - def format_output_path(format, base_path, image_path, width, height) - params = symbolize_keys(image_hash(base_path, image_path, width, height)) - - Pathname.new(format % params).cleanpath.to_s - end - # Build a hash containing image information def image_hash(base_path, image_path, width, height) { From 0d3f0990571a1cf685590be8741ec3f66c101e4a Mon Sep 17 00:00:00 2001 From: Joseph Wynn Date: Tue, 11 Oct 2016 21:21:11 +0100 Subject: [PATCH 6/7] Fix for using a custom `source` directory Paths are still passed around as relative. Code which needs to read files (images, templates) and code which needs to manipulate paths (Jekyll::ResponsiveImage::Utils#image_hash) has to be aware of the site source path. This got a bit messy in the end, but I couldn't find a better way without doing a big refactor of the entire plugin. --- features/image-generation.feature | 5 +- features/image-hashes.feature | 75 +++++++++++++++++++ features/step_definitions/jekyll_steps.rb | 4 + features/test-site/_includes/hash.html | 16 ++++ lib/jekyll/responsive_image/config.rb | 8 +- .../responsive_image/extra_image_generator.rb | 6 +- .../responsive_image/image_processor.rb | 12 +-- lib/jekyll/responsive_image/renderer.rb | 3 +- lib/jekyll/responsive_image/resize_handler.rb | 8 +- lib/jekyll/responsive_image/utils.rb | 10 +-- 10 files changed, 121 insertions(+), 26 deletions(-) create mode 100644 features/image-hashes.feature create mode 100644 features/test-site/_includes/hash.html diff --git a/features/image-generation.feature b/features/image-generation.feature index 8e640b2..c9d511f 100644 --- a/features/image-generation.feature +++ b/features/image-generation.feature @@ -44,6 +44,7 @@ Feature: Responsive image generation """ source: my-site-copy/src responsive_image: + base_path: assets template: _includes/responsive-image.html output_path_format: assets/resized/%{dirname}/%{width}/%{basename} sizes: @@ -51,5 +52,5 @@ Feature: Responsive image generation """ 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 "my-site-copy/src/assets/resized/subdir/test-100x50.png" should have the dimensions "100x50" - And the file "_site/assets/resized/subdir/test-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 diff --git a/features/image-hashes.feature b/features/image-hashes.feature new file mode 100644 index 0000000..fa0a615 --- /dev/null +++ b/features/image-hashes.feature @@ -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 + """ diff --git a/features/step_definitions/jekyll_steps.rb b/features/step_definitions/jekyll_steps.rb index 6612ba9..ca86251 100644 --- a/features/step_definitions/jekyll_steps.rb +++ b/features/step_definitions/jekyll_steps.rb @@ -41,6 +41,10 @@ Then /^I should see "(.+)" in "(.*)"$/ do |text, file| 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 diff --git a/features/test-site/_includes/hash.html b/features/test-site/_includes/hash.html new file mode 100644 index 0000000..2fa786e --- /dev/null +++ b/features/test-site/_includes/hash.html @@ -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 %} diff --git a/lib/jekyll/responsive_image/config.rb b/lib/jekyll/responsive_image/config.rb index d11a24c..72592a2 100644 --- a/lib/jekyll/responsive_image/config.rb +++ b/lib/jekyll/responsive_image/config.rb @@ -14,12 +14,8 @@ module Jekyll end def to_h - config = DEFAULTS.merge(@site.config['responsive_image']) - .merge(site_source: @site.source, site_dest: @site.dest) - - config['base_path'] = @site.in_source_dir(config['base_path']) - - config + DEFAULTS.merge(@site.config['responsive_image']) + .merge(site_source: @site.source, site_dest: @site.dest) end end end diff --git a/lib/jekyll/responsive_image/extra_image_generator.rb b/lib/jekyll/responsive_image/extra_image_generator.rb index 80ab4e7..5ea6c43 100644 --- a/lib/jekyll/responsive_image/extra_image_generator.rb +++ b/lib/jekyll/responsive_image/extra_image_generator.rb @@ -5,12 +5,14 @@ module Jekyll def generate(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 diff --git a/lib/jekyll/responsive_image/image_processor.rb b/lib/jekyll/responsive_image/image_processor.rb index dcaaada..ca102be 100644 --- a/lib/jekyll/responsive_image/image_processor.rb +++ b/lib/jekyll/responsive_image/image_processor.rb @@ -3,20 +3,22 @@ module Jekyll 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 diff --git a/lib/jekyll/responsive_image/renderer.rb b/lib/jekyll/responsive_image/renderer.rb index 2c30679..06a3263 100644 --- a/lib/jekyll/responsive_image/renderer.rb +++ b/lib/jekyll/responsive_image/renderer.rb @@ -15,8 +15,7 @@ module Jekyll if result.nil? config = Config.new(@site).to_h - absolute_image_path = @site.in_source_dir(@attributes['path'].to_s) - image = ImageProcessor.process(absolute_image_path, @attributes['path'], config) + image = ImageProcessor.process(@attributes['path'], config) @attributes['original'] = image[:original] @attributes['resized'] = image[:resized] diff --git a/lib/jekyll/responsive_image/resize_handler.rb b/lib/jekyll/responsive_image/resize_handler.rb index cc16f83..1fc411f 100644 --- a/lib/jekyll/responsive_image/resize_handler.rb +++ b/lib/jekyll/responsive_image/resize_handler.rb @@ -14,8 +14,8 @@ module Jekyll next unless needs_resizing?(img, width) image_path = img.filename.force_encoding(Encoding::UTF_8) - 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)) + filepath = format_output_path(config['output_path_format'], config, image_path, width, height) + resized.push(image_hash(config, filepath, width, height)) site_source_filepath = File.expand_path(filepath, config[:site_source]) site_dest_filepath = File.expand_path(filepath, config[:site_dest]) @@ -45,8 +45,8 @@ module Jekyll resized end - def format_output_path(format, base_path, image_path, width, height) - params = symbolize_keys(image_hash(base_path, image_path, width, height)) + def format_output_path(format, config, image_path, width, height) + params = symbolize_keys(image_hash(config, image_path, width, height)) Pathname.new(format % params).cleanpath.to_s end diff --git a/lib/jekyll/responsive_image/utils.rb b/lib/jekyll/responsive_image/utils.rb index 4688e92..d940806 100644 --- a/lib/jekyll/responsive_image/utils.rb +++ b/lib/jekyll/responsive_image/utils.rb @@ -17,10 +17,10 @@ module Jekyll end # Build a hash containing image information - def image_hash(base_path, image_path, width, height) + def image_hash(config, image_path, width, height) { 'path' => image_path, - 'dirname' => relative_dirname(base_path, image_path), + 'dirname' => relative_dirname(config, image_path), 'basename' => File.basename(image_path), 'filename' => File.basename(image_path, '.*'), 'extension' => File.extname(image_path).delete('.'), @@ -29,9 +29,9 @@ module Jekyll } end - def relative_dirname(base_path, image_path) - path = Pathname.new(image_path).expand_path - base = Pathname.new(base_path).expand_path + def relative_dirname(config, image_path) + path = Pathname.new(File.expand_path(image_path, config[:site_source])) + base = Pathname.new(File.expand_path(config['base_path'], config[:site_source])) path.relative_path_from(base).dirname.to_s.delete('.') end From 854aa4cf00093967002401a9be4adffb7697ba6f Mon Sep 17 00:00:00 2001 From: Joseph Wynn Date: Tue, 11 Oct 2016 22:03:11 +0100 Subject: [PATCH 7/7] pre4 --- lib/jekyll/responsive_image/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/jekyll/responsive_image/version.rb b/lib/jekyll/responsive_image/version.rb index a094b92..fb1cb99 100644 --- a/lib/jekyll/responsive_image/version.rb +++ b/lib/jekyll/responsive_image/version.rb @@ -1,5 +1,5 @@ module Jekyll module ResponsiveImage - VERSION = '1.0.0.pre3'.freeze + VERSION = '1.0.0.pre4'.freeze end end