jekyll-responsive-image 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/jekyll/responsive_image.rb +19 -0
- data/lib/jekyll/responsive_image/block.rb +12 -0
- data/lib/jekyll/responsive_image/config.rb +22 -0
- data/lib/jekyll/responsive_image/extra_image_generator.rb +22 -0
- data/lib/jekyll/responsive_image/image_processor.rb +25 -0
- data/lib/jekyll/responsive_image/render_cache.rb +21 -0
- data/lib/jekyll/responsive_image/renderer.rb +37 -0
- data/lib/jekyll/responsive_image/resize_handler.rb +68 -0
- data/lib/jekyll/responsive_image/tag.rb +20 -0
- data/lib/jekyll/responsive_image/utils.rb +40 -0
- data/lib/jekyll/responsive_image/version.rb +5 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9b3cf9ae3bb950e0229a1dbd0f6a892e3645c93c
|
4
|
+
data.tar.gz: 56df60468696d856b5b25aac144030c41eab7532
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 32cdee3321bb41690494989f0dfcf02d50b4f72eb33577a9982b868bfd77d5171e92520b9f696f179ad555172e47bc2989617d83958096d94c0fce909ea72179
|
7
|
+
data.tar.gz: 60e7b705ab7f9db9a4bbdc457b77778c6148bfd5acbbd9024f83638275206bf8b84c3f6cd9cffab83fe7575a6cfb1b612f6954e4fa12f577bed83ade55d6e237
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'fileutils'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
require 'jekyll'
|
5
|
+
require 'rmagick'
|
6
|
+
|
7
|
+
require 'jekyll/responsive_image/version'
|
8
|
+
require 'jekyll/responsive_image/config'
|
9
|
+
require 'jekyll/responsive_image/utils'
|
10
|
+
require 'jekyll/responsive_image/render_cache'
|
11
|
+
require 'jekyll/responsive_image/image_processor'
|
12
|
+
require 'jekyll/responsive_image/resize_handler'
|
13
|
+
require 'jekyll/responsive_image/renderer'
|
14
|
+
require 'jekyll/responsive_image/tag'
|
15
|
+
require 'jekyll/responsive_image/block'
|
16
|
+
require 'jekyll/responsive_image/extra_image_generator'
|
17
|
+
|
18
|
+
Liquid::Template.register_tag('responsive_image', Jekyll::ResponsiveImage::Tag)
|
19
|
+
Liquid::Template.register_tag('responsive_image_block', Jekyll::ResponsiveImage::Block)
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Jekyll
|
2
|
+
module ResponsiveImage
|
3
|
+
class Block < Liquid::Block
|
4
|
+
include Jekyll::ResponsiveImage::Utils
|
5
|
+
|
6
|
+
def render(context)
|
7
|
+
attributes = YAML.load(super)
|
8
|
+
Renderer.new(context.registers[:site], attributes).render_responsive_image
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Jekyll
|
2
|
+
module ResponsiveImage
|
3
|
+
class Config
|
4
|
+
DEFAULTS = {
|
5
|
+
'default_quality' => 85,
|
6
|
+
'base_path' => 'assets',
|
7
|
+
'output_path_format' => 'assets/resized/%{filename}-%{width}x%{height}.%{extension}',
|
8
|
+
'sizes' => [],
|
9
|
+
'extra_images' => []
|
10
|
+
}
|
11
|
+
|
12
|
+
def initialize(site)
|
13
|
+
@site = site
|
14
|
+
end
|
15
|
+
|
16
|
+
def to_h
|
17
|
+
DEFAULTS.merge(@site.config['responsive_image'])
|
18
|
+
.merge(site_source: @site.source, site_dest: @site.dest)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module Jekyll
|
2
|
+
module ResponsiveImage
|
3
|
+
class ExtraImageGenerator < Jekyll::Generator
|
4
|
+
include Jekyll::ResponsiveImage::Utils
|
5
|
+
|
6
|
+
def generate(site)
|
7
|
+
config = Config.new(site).to_h
|
8
|
+
site_source = Pathname.new(site.source)
|
9
|
+
|
10
|
+
config['extra_images'].each do |pathspec|
|
11
|
+
Dir.glob(site.in_source_dir(pathspec)) do |image_path|
|
12
|
+
path = Pathname.new(image_path)
|
13
|
+
relative_image_path = path.relative_path_from(site_source)
|
14
|
+
|
15
|
+
result = ImageProcessor.process(relative_image_path, config)
|
16
|
+
result[:resized].each { |image| keep_resized_image!(site, image) }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Jekyll
|
2
|
+
module ResponsiveImage
|
3
|
+
class ImageProcessor
|
4
|
+
include ResponsiveImage::Utils
|
5
|
+
|
6
|
+
def process(image_path, config)
|
7
|
+
absolute_image_path = File.expand_path(image_path.to_s, config[:site_source])
|
8
|
+
|
9
|
+
raise SyntaxError.new("Invalid image path specified: #{image_path}") unless File.file?(absolute_image_path)
|
10
|
+
|
11
|
+
resize_handler = ResizeHandler.new
|
12
|
+
img = Magick::Image::read(absolute_image_path).first
|
13
|
+
|
14
|
+
{
|
15
|
+
original: image_hash(config, image_path, img.columns, img.rows),
|
16
|
+
resized: resize_handler.resize_image(img, config),
|
17
|
+
}
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.process(image_path, config)
|
21
|
+
self.new.process(image_path, config)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Jekyll
|
2
|
+
module ResponsiveImage
|
3
|
+
class RenderCache
|
4
|
+
attr_accessor :store
|
5
|
+
|
6
|
+
class << self
|
7
|
+
def store
|
8
|
+
@store ||= {}
|
9
|
+
end
|
10
|
+
|
11
|
+
def get(key)
|
12
|
+
store[key]
|
13
|
+
end
|
14
|
+
|
15
|
+
def set(key, val)
|
16
|
+
store[key] = val
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module Jekyll
|
2
|
+
module ResponsiveImage
|
3
|
+
class Renderer
|
4
|
+
include Jekyll::ResponsiveImage::Utils
|
5
|
+
|
6
|
+
def initialize(site, attributes)
|
7
|
+
@site = site
|
8
|
+
@attributes = attributes
|
9
|
+
end
|
10
|
+
|
11
|
+
def render_responsive_image
|
12
|
+
cache_key = @attributes.to_s
|
13
|
+
result = @attributes['cache'] ? RenderCache.get(cache_key) : nil
|
14
|
+
|
15
|
+
if result.nil?
|
16
|
+
config = Config.new(@site).to_h
|
17
|
+
|
18
|
+
image = ImageProcessor.process(@attributes['path'], config)
|
19
|
+
@attributes['original'] = image[:original]
|
20
|
+
@attributes['resized'] = image[:resized]
|
21
|
+
|
22
|
+
@attributes['resized'].each { |resized| keep_resized_image!(@site, resized) }
|
23
|
+
|
24
|
+
image_template = @site.in_source_dir(@attributes['template'] || config['template'])
|
25
|
+
partial = File.read(image_template)
|
26
|
+
template = Liquid::Template.parse(partial)
|
27
|
+
|
28
|
+
result = template.render!(@attributes.merge(@site.site_payload))
|
29
|
+
|
30
|
+
RenderCache.set(cache_key, result)
|
31
|
+
end
|
32
|
+
|
33
|
+
result
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Jekyll
|
2
|
+
module ResponsiveImage
|
3
|
+
class ResizeHandler
|
4
|
+
include ResponsiveImage::Utils
|
5
|
+
|
6
|
+
def resize_image(img, config)
|
7
|
+
resized = []
|
8
|
+
|
9
|
+
config['sizes'].each do |size|
|
10
|
+
width = size['width']
|
11
|
+
ratio = width.to_f / img.columns.to_f
|
12
|
+
height = (img.rows.to_f * ratio).round
|
13
|
+
|
14
|
+
next unless needs_resizing?(img, width)
|
15
|
+
|
16
|
+
image_path = img.filename.force_encoding(Encoding::UTF_8)
|
17
|
+
filepath = format_output_path(config['output_path_format'], config, image_path, width, height)
|
18
|
+
resized.push(image_hash(config, filepath, width, height))
|
19
|
+
|
20
|
+
site_source_filepath = File.expand_path(filepath, config[:site_source])
|
21
|
+
site_dest_filepath = File.expand_path(filepath, config[:site_dest])
|
22
|
+
|
23
|
+
# Don't resize images more than once
|
24
|
+
next if File.exist?(site_source_filepath)
|
25
|
+
|
26
|
+
ensure_output_dir_exists!(site_source_filepath)
|
27
|
+
ensure_output_dir_exists!(site_dest_filepath)
|
28
|
+
|
29
|
+
Jekyll.logger.info "Generating #{site_source_filepath}"
|
30
|
+
|
31
|
+
i = img.scale(ratio)
|
32
|
+
i.write(site_source_filepath) do |f|
|
33
|
+
f.quality = size['quality'] || config['default_quality']
|
34
|
+
end
|
35
|
+
|
36
|
+
# Ensure the generated file is copied to the _site directory
|
37
|
+
Jekyll.logger.info "Copying resized image to #{site_dest_filepath}"
|
38
|
+
FileUtils.copy_file(site_source_filepath, site_dest_filepath)
|
39
|
+
|
40
|
+
i.destroy!
|
41
|
+
end
|
42
|
+
|
43
|
+
img.destroy!
|
44
|
+
|
45
|
+
resized
|
46
|
+
end
|
47
|
+
|
48
|
+
def format_output_path(format, config, image_path, width, height)
|
49
|
+
params = symbolize_keys(image_hash(config, image_path, width, height))
|
50
|
+
|
51
|
+
Pathname.new(format % params).cleanpath.to_s
|
52
|
+
end
|
53
|
+
|
54
|
+
def needs_resizing?(img, width)
|
55
|
+
img.columns > width
|
56
|
+
end
|
57
|
+
|
58
|
+
def ensure_output_dir_exists!(path)
|
59
|
+
dir = File.dirname(path)
|
60
|
+
|
61
|
+
unless Dir.exist?(dir)
|
62
|
+
Jekyll.logger.info "Creating output directory #{dir}"
|
63
|
+
FileUtils.mkdir_p(dir)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Jekyll
|
2
|
+
module ResponsiveImage
|
3
|
+
class Tag < Liquid::Tag
|
4
|
+
def initialize(tag_name, markup, tokens)
|
5
|
+
super
|
6
|
+
|
7
|
+
@attributes = {}
|
8
|
+
|
9
|
+
markup.scan(::Liquid::TagAttributes) do |key, value|
|
10
|
+
# Strip quotes from around attribute values
|
11
|
+
@attributes[key] = value.gsub(/^['"]|['"]$/, '')
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def render(context)
|
16
|
+
Renderer.new(context.registers[:site], @attributes).render_responsive_image
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
module Jekyll
|
4
|
+
module ResponsiveImage
|
5
|
+
module Utils
|
6
|
+
def keep_resized_image!(site, image)
|
7
|
+
keep_dir = File.dirname(image['path'])
|
8
|
+
site.config['keep_files'] << keep_dir unless site.config['keep_files'].include?(keep_dir)
|
9
|
+
end
|
10
|
+
|
11
|
+
def symbolize_keys(hash)
|
12
|
+
result = {}
|
13
|
+
hash.each_key do |key|
|
14
|
+
result[key.to_sym] = hash[key]
|
15
|
+
end
|
16
|
+
result
|
17
|
+
end
|
18
|
+
|
19
|
+
# Build a hash containing image information
|
20
|
+
def image_hash(config, image_path, width, height)
|
21
|
+
{
|
22
|
+
'path' => image_path,
|
23
|
+
'dirname' => relative_dirname(config, image_path),
|
24
|
+
'basename' => File.basename(image_path),
|
25
|
+
'filename' => File.basename(image_path, '.*'),
|
26
|
+
'extension' => File.extname(image_path).delete('.'),
|
27
|
+
'width' => width,
|
28
|
+
'height' => height,
|
29
|
+
}
|
30
|
+
end
|
31
|
+
|
32
|
+
def relative_dirname(config, image_path)
|
33
|
+
path = Pathname.new(File.expand_path(image_path, config[:site_source]))
|
34
|
+
base = Pathname.new(File.expand_path(config['base_path'], config[:site_source]))
|
35
|
+
|
36
|
+
path.relative_path_from(base).dirname.to_s.delete('.')
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jekyll-responsive-image
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Joseph Wynn
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-12-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jekyll
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '4.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '2.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '4.0'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rmagick
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '2.0'
|
40
|
+
- - "<"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '3.0'
|
43
|
+
type: :runtime
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '2.0'
|
50
|
+
- - "<"
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '3.0'
|
53
|
+
description: "\n Highly configurable Jekyll plugin for managing responsive images.
|
54
|
+
Automatically\n resizes images and provides a Liquid template tag for loading
|
55
|
+
the images with\n picture, img srcset, Imager.js, etc.\n "
|
56
|
+
email:
|
57
|
+
- joseph@wildlyinaccurate.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- lib/jekyll/responsive_image.rb
|
63
|
+
- lib/jekyll/responsive_image/block.rb
|
64
|
+
- lib/jekyll/responsive_image/config.rb
|
65
|
+
- lib/jekyll/responsive_image/extra_image_generator.rb
|
66
|
+
- lib/jekyll/responsive_image/image_processor.rb
|
67
|
+
- lib/jekyll/responsive_image/render_cache.rb
|
68
|
+
- lib/jekyll/responsive_image/renderer.rb
|
69
|
+
- lib/jekyll/responsive_image/resize_handler.rb
|
70
|
+
- lib/jekyll/responsive_image/tag.rb
|
71
|
+
- lib/jekyll/responsive_image/utils.rb
|
72
|
+
- lib/jekyll/responsive_image/version.rb
|
73
|
+
homepage: https://github.com/wildlyinaccurate/jekyll-responsive-image
|
74
|
+
licenses:
|
75
|
+
- MIT
|
76
|
+
metadata: {}
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: '0'
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubyforge_project:
|
93
|
+
rubygems_version: 2.6.8
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Responsive images management for Jekyll
|
97
|
+
test_files: []
|