mirror of
https://github.com/wildlyinaccurate/jekyll-responsive-image.git
synced 2025-07-04 00:00:41 -04:00
Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
dda6d748c8 | ||
|
a31c454ed1 | ||
|
f2be2c53be | ||
|
6cc3a082a1 | ||
|
2f7c0124eb | ||
|
25c6695da4 | ||
|
7679f4b8f5 | ||
|
82e868966c | ||
|
fbc35b8ef9 | ||
|
749e6cc068 | ||
|
fa00c4c9cf | ||
|
4b75f67558 |
@ -35,11 +35,15 @@ For more advanced templates, see the [**Templates**](#templates) section below.
|
|||||||
|
|
||||||
### 3. Configure the plugin
|
### 3. Configure the plugin
|
||||||
|
|
||||||
You **must** have a `responsive_image` block in your `_config.yml` for this plugin to work. The minimum required configuration is a `template` path:
|
You **must** have a `responsive_image` block in your `_config.yml` for this plugin to work. At a minimum, your `responsive_image` configuration should have a template path and a list of sizes.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
responsive_image:
|
responsive_image:
|
||||||
template: _includes/responsive-image.html
|
template: _includes/responsive-image.html
|
||||||
|
sizes:
|
||||||
|
- width: 320
|
||||||
|
- width: 480
|
||||||
|
- width: 800
|
||||||
```
|
```
|
||||||
|
|
||||||
For a list of all the available configuration options, see the [**All configuration options**](#all-configuration-options) section below.
|
For a list of all the available configuration options, see the [**All configuration options**](#all-configuration-options) section below.
|
||||||
|
@ -13,6 +13,20 @@ Feature: Jekyll responsive_image_block tag
|
|||||||
When I run Jekyll
|
When I run Jekyll
|
||||||
Then I should see "<img alt=\"Lorem ipsum\" src=\"/assets/everybody-loves-jalapeño-pineapple-cornbread.png\" title=\"Magic rainbow adventure!\"" in "_site/index.html"
|
Then I should see "<img alt=\"Lorem ipsum\" src=\"/assets/everybody-loves-jalapeño-pineapple-cornbread.png\" title=\"Magic rainbow adventure!\"" in "_site/index.html"
|
||||||
|
|
||||||
|
Scenario: Tabs for indentation
|
||||||
|
Given I have a responsive_image configuration with "template" set to "_includes/responsive-image.html"
|
||||||
|
And I have a file "index.html" with:
|
||||||
|
"""
|
||||||
|
{% assign path = 'assets/everybody-loves-jalapeño-pineapple-cornbread.png' %}
|
||||||
|
{% responsive_image_block %}
|
||||||
|
path: {{ path }}
|
||||||
|
title: Magic rainbow adventure!
|
||||||
|
alt: Lorem ipsum
|
||||||
|
{% endresponsive_image_block %}
|
||||||
|
"""
|
||||||
|
When I run Jekyll
|
||||||
|
Then I should see "<img alt=\"Lorem ipsum\" src=\"/assets/everybody-loves-jalapeño-pineapple-cornbread.png\" title=\"Magic rainbow adventure!\"" in "_site/index.html"
|
||||||
|
|
||||||
Scenario: Global variables available in templates
|
Scenario: Global variables available in templates
|
||||||
Given I have a file "index.html" with:
|
Given I have a file "index.html" with:
|
||||||
"""
|
"""
|
||||||
|
@ -1 +1,3 @@
|
|||||||
|
{% assign largest = resized | sort: 'width' | last %}
|
||||||
|
|
||||||
<img alt="{{ alt }}" src="/{{ original.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,7 +4,13 @@ module Jekyll
|
|||||||
include Jekyll::ResponsiveImage::Utils
|
include Jekyll::ResponsiveImage::Utils
|
||||||
|
|
||||||
def render(context)
|
def render(context)
|
||||||
attributes = YAML.load(super)
|
content = super
|
||||||
|
|
||||||
|
if content.include?("\t")
|
||||||
|
content = content.lines.map {|line| line.gsub(/\G[\t ]/, " ")}.join("\n")
|
||||||
|
end
|
||||||
|
|
||||||
|
attributes = YAML.load(content)
|
||||||
Renderer.new(context.registers[:site], attributes).render_responsive_image
|
Renderer.new(context.registers[:site], attributes).render_responsive_image
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -8,12 +8,11 @@ module Jekyll
|
|||||||
|
|
||||||
Jekyll.logger.warn "Invalid image path specified: #{image_path.inspect}" unless File.file?(absolute_image_path)
|
Jekyll.logger.warn "Invalid image path specified: #{image_path.inspect}" unless File.file?(absolute_image_path)
|
||||||
|
|
||||||
resize_handler = ResizeHandler.new
|
resize_handler = ResizeHandler.new(absolute_image_path, config)
|
||||||
img = Magick::Image::read(absolute_image_path).first
|
|
||||||
|
|
||||||
{
|
{
|
||||||
original: image_hash(config, image_path, img.columns, img.rows),
|
original: image_hash(config, image_path, resize_handler.original_image.columns, resize_handler.original_image.rows),
|
||||||
resized: resize_handler.resize_image(img, config),
|
resized: resize_handler.resize_image,
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -25,7 +25,11 @@ module Jekyll
|
|||||||
partial = File.read(image_template)
|
partial = File.read(image_template)
|
||||||
template = Liquid::Template.parse(partial)
|
template = Liquid::Template.parse(partial)
|
||||||
|
|
||||||
result = template.render!(@attributes.merge(@site.site_payload))
|
info = {
|
||||||
|
registers: { site: @site }
|
||||||
|
}
|
||||||
|
|
||||||
|
result = template.render!(@attributes.merge(@site.site_payload), info)
|
||||||
|
|
||||||
RenderCache.set(cache_key, result)
|
RenderCache.set(cache_key, result)
|
||||||
end
|
end
|
||||||
|
@ -3,26 +3,39 @@ module Jekyll
|
|||||||
class ResizeHandler
|
class ResizeHandler
|
||||||
include ResponsiveImage::Utils
|
include ResponsiveImage::Utils
|
||||||
|
|
||||||
def resize_image(img, config)
|
attr_reader :original_image
|
||||||
img.auto_orient! if config['auto_rotate']
|
|
||||||
|
|
||||||
|
def initialize(original_image_absolute_path, config)
|
||||||
|
@config = config
|
||||||
|
|
||||||
|
@original_image_absolute_path = original_image_absolute_path
|
||||||
|
|
||||||
|
if @config['auto_rotate']
|
||||||
|
load_full_image
|
||||||
|
@original_image.auto_orient!
|
||||||
|
else
|
||||||
|
load_image_properties_only
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def resize_image
|
||||||
resized = []
|
resized = []
|
||||||
|
|
||||||
config['sizes'].each do |size|
|
@config['sizes'].each do |size|
|
||||||
width = size['width']
|
width = size['width']
|
||||||
ratio = width.to_f / img.columns.to_f
|
ratio = width.to_f / @original_image.columns.to_f
|
||||||
height = (img.rows.to_f * ratio).round
|
height = (@original_image.rows.to_f * ratio).round
|
||||||
|
|
||||||
next unless needs_resizing?(img, width)
|
next unless needs_resizing?(width)
|
||||||
|
|
||||||
image_path = img.filename.force_encoding(Encoding::UTF_8)
|
image_path = @original_image.filename.force_encoding(Encoding::UTF_8)
|
||||||
filepath = format_output_path(config['output_path_format'], config, image_path, width, height)
|
filepath = format_output_path(@config['output_path_format'], @config, image_path, width, height)
|
||||||
resized.push(image_hash(config, filepath, width, height))
|
resized.push(image_hash(@config, filepath, width, height))
|
||||||
|
|
||||||
site_source_filepath = File.expand_path(filepath, config[:site_source])
|
site_source_filepath = File.expand_path(filepath, @config[:site_source])
|
||||||
site_dest_filepath = File.expand_path(filepath, config[:site_dest])
|
site_dest_filepath = File.expand_path(filepath, @config[:site_dest])
|
||||||
|
|
||||||
if config['save_to_source']
|
if @config['save_to_source']
|
||||||
target_filepath = site_source_filepath
|
target_filepath = site_source_filepath
|
||||||
else
|
else
|
||||||
target_filepath = site_dest_filepath
|
target_filepath = site_dest_filepath
|
||||||
@ -36,16 +49,22 @@ module Jekyll
|
|||||||
|
|
||||||
Jekyll.logger.info "Generating #{target_filepath}"
|
Jekyll.logger.info "Generating #{target_filepath}"
|
||||||
|
|
||||||
if config['strip']
|
load_full_image unless @original_image_pixels_loaded
|
||||||
img.strip!
|
|
||||||
end
|
if @config['strip']
|
||||||
i = img.scale(ratio)
|
@original_image.strip!
|
||||||
i.write(target_filepath) do |f|
|
|
||||||
f.interlace = i.interlace
|
|
||||||
f.quality = size['quality'] || config['default_quality']
|
|
||||||
end
|
end
|
||||||
|
|
||||||
if config['save_to_source']
|
i = @original_image.scale(ratio)
|
||||||
|
|
||||||
|
quality = size['quality'] || @config['default_quality']
|
||||||
|
|
||||||
|
i.write(target_filepath) do |f|
|
||||||
|
f.interlace = i.interlace
|
||||||
|
f.quality = quality
|
||||||
|
end
|
||||||
|
|
||||||
|
if @config['save_to_source']
|
||||||
# Ensure the generated file is copied to the _site directory
|
# Ensure the generated file is copied to the _site directory
|
||||||
Jekyll.logger.info "Copying resized image to #{site_dest_filepath}"
|
Jekyll.logger.info "Copying resized image to #{site_dest_filepath}"
|
||||||
FileUtils.copy_file(site_source_filepath, site_dest_filepath)
|
FileUtils.copy_file(site_source_filepath, site_dest_filepath)
|
||||||
@ -54,7 +73,7 @@ module Jekyll
|
|||||||
i.destroy!
|
i.destroy!
|
||||||
end
|
end
|
||||||
|
|
||||||
img.destroy!
|
@original_image.destroy!
|
||||||
|
|
||||||
resized
|
resized
|
||||||
end
|
end
|
||||||
@ -65,8 +84,18 @@ module Jekyll
|
|||||||
Pathname.new(format % params).cleanpath.to_s
|
Pathname.new(format % params).cleanpath.to_s
|
||||||
end
|
end
|
||||||
|
|
||||||
def needs_resizing?(img, width)
|
def needs_resizing?(width)
|
||||||
img.columns > width
|
@original_image.columns > width
|
||||||
|
end
|
||||||
|
|
||||||
|
def load_full_image
|
||||||
|
@original_image = Magick::Image::read(@original_image_absolute_path).first
|
||||||
|
@original_image_pixels_loaded = true
|
||||||
|
end
|
||||||
|
|
||||||
|
def load_image_properties_only
|
||||||
|
@original_image = Magick::Image::ping(@original_image_absolute_path).first
|
||||||
|
@original_image_pixels_loaded = false
|
||||||
end
|
end
|
||||||
|
|
||||||
def ensure_output_dir_exists!(path)
|
def ensure_output_dir_exists!(path)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
module Jekyll
|
module Jekyll
|
||||||
module ResponsiveImage
|
module ResponsiveImage
|
||||||
VERSION = '1.5.3'.freeze
|
VERSION = '1.6.0'.freeze
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user