jekyll-responsive_image 1.0.0.pre3 → 1.0.0.pre4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a46011bdb0ba186b7c73ad6955106be3916497d6
4
- data.tar.gz: d27ed962eddedf75476e9c34b5913ef4bbdada59
3
+ metadata.gz: 52e6d96f085c965ad79ac57cf56ab3a40e32f21b
4
+ data.tar.gz: 26b46156c6e64631f955ea31f5f045ce5a95c03f
5
5
  SHA512:
6
- metadata.gz: f0b4c556a2577f428db1230a1cf003bb768e5df38acef94de1a1449293f8675d1e793a46eecd11550f60874c0227fecd46e75e1402b1baa0d320d7ca877b745a
7
- data.tar.gz: 5e494a47149dd30af3408861f28a044fc1cceddb12a3d0b40d4883bf5298b2c28203fde9c06353a9ac7df01bde780916a0d6bc67ef78257a1797ee4a73f94fb6
6
+ metadata.gz: 9e2617adb6db1e71a830c7e37a4721ab98f4c15815d192ee097737ebf78255ac1bb0fbb725550fbcd00dc78a7f7e67b668eb45053c0e92ba32b78131565349c7
7
+ data.tar.gz: ff8ea68c0724566e0adbe4dac3dc82c8dd7cdcd9e980e89f6c5bc32cf518b4d5899ef135db70ec861f122b79d6f8d9537e1f001063f084cc3fdd4afad0be792e
@@ -5,12 +5,12 @@ require 'jekyll'
5
5
  require 'rmagick'
6
6
 
7
7
  require 'jekyll/responsive_image/version'
8
- require 'jekyll/responsive_image/defaults'
8
+ require 'jekyll/responsive_image/config'
9
9
  require 'jekyll/responsive_image/utils'
10
10
  require 'jekyll/responsive_image/render_cache'
11
11
  require 'jekyll/responsive_image/image_processor'
12
12
  require 'jekyll/responsive_image/resize_handler'
13
- require 'jekyll/responsive_image/common'
13
+ require 'jekyll/responsive_image/renderer'
14
14
  require 'jekyll/responsive_image/tag'
15
15
  require 'jekyll/responsive_image/block'
16
16
  require 'jekyll/responsive_image/extra_image_generator'
@@ -1,20 +1,11 @@
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
1
  module Jekyll
11
- class ResponsiveImage
2
+ module ResponsiveImage
12
3
  class Block < Liquid::Block
13
- include Jekyll::ResponsiveImage::Common
4
+ include Jekyll::ResponsiveImage::Utils
14
5
 
15
6
  def render(context)
16
7
  attributes = YAML.load(super)
17
- render_responsive_image(context, attributes)
8
+ Renderer.new(context.registers[:site], attributes).render_responsive_image
18
9
  end
19
10
  end
20
11
  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
@@ -1,16 +1,18 @@
1
1
  module Jekyll
2
- class ResponsiveImage
2
+ module ResponsiveImage
3
3
  class ExtraImageGenerator < Jekyll::Generator
4
- include Jekyll::ResponsiveImage::Common
4
+ include Jekyll::ResponsiveImage::Utils
5
5
 
6
6
  def generate(site)
7
- config = make_config(site)
7
+ config = Config.new(site).to_h
8
+ site_source = Pathname.new(site.source)
8
9
 
9
10
  config['extra_images'].each do |pathspec|
10
11
  Dir.glob(site.in_source_dir(pathspec)) do |image_path|
11
- relative_image_path = image_path.sub(/^#{Regexp.escape(image_path)}/, '')
12
+ path = Pathname.new(image_path)
13
+ relative_image_path = path.relative_path_from(site_source)
12
14
 
13
- result = ImageProcessor.process(image_path, relative_image_path, config)
15
+ result = ImageProcessor.process(relative_image_path, config)
14
16
  result[:resized].each { |image| keep_resized_image!(site, image) }
15
17
  end
16
18
  end
@@ -1,22 +1,24 @@
1
1
  module Jekyll
2
- class ResponsiveImage
2
+ module ResponsiveImage
3
3
  class ImageProcessor
4
4
  include ResponsiveImage::Utils
5
5
 
6
- def process(absolute_image_path, relative_image_path, config)
7
- raise SyntaxError.new("Invalid image path specified: #{absolute_image_path}") unless File.file?(absolute_image_path)
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)
8
10
 
9
11
  resize_handler = ResizeHandler.new
10
12
  img = Magick::Image::read(absolute_image_path).first
11
13
 
12
14
  {
13
- original: image_hash(config['base_path'], relative_image_path, img.columns, img.rows),
15
+ original: image_hash(config, image_path, img.columns, img.rows),
14
16
  resized: resize_handler.resize_image(img, config),
15
17
  }
16
18
  end
17
19
 
18
- def self.process(absolute_image_path, relative_image_path, config)
19
- self.new.process(absolute_image_path, relative_image_path, config)
20
+ def self.process(image_path, config)
21
+ self.new.process(image_path, config)
20
22
  end
21
23
  end
22
24
  end
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
- class ResponsiveImage
2
+ module ResponsiveImage
3
3
  class RenderCache
4
4
  attr_accessor :store
5
5
 
@@ -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
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
- class ResponsiveImage
2
+ module ResponsiveImage
3
3
  class ResizeHandler
4
4
  include ResponsiveImage::Utils
5
5
 
@@ -14,8 +14,8 @@ module Jekyll
14
14
  next unless needs_resizing?(img, width)
15
15
 
16
16
  image_path = img.filename.force_encoding(Encoding::UTF_8)
17
- filepath = format_output_path(config['output_path_format'], config['base_path'], image_path, width, height)
18
- resized.push(image_hash(config['base_path'], filepath, width, height))
17
+ filepath = format_output_path(config['output_path_format'], config, image_path, width, height)
18
+ resized.push(image_hash(config, filepath, width, height))
19
19
 
20
20
  site_source_filepath = File.expand_path(filepath, config[:site_source])
21
21
  site_dest_filepath = File.expand_path(filepath, config[:site_dest])
@@ -23,10 +23,10 @@ module Jekyll
23
23
  # Don't resize images more than once
24
24
  next if File.exist?(site_source_filepath)
25
25
 
26
- ensure_output_dir_exists!(File.dirname(site_source_filepath))
27
- ensure_output_dir_exists!(File.dirname(site_dest_filepath))
26
+ ensure_output_dir_exists!(site_source_filepath)
27
+ ensure_output_dir_exists!(site_dest_filepath)
28
28
 
29
- Jekyll.logger.info "Generating #{filepath}"
29
+ Jekyll.logger.info "Generating #{site_source_filepath}"
30
30
 
31
31
  i = img.scale(ratio)
32
32
  i.write(site_source_filepath) do |f|
@@ -34,6 +34,7 @@ module Jekyll
34
34
  end
35
35
 
36
36
  # Ensure the generated file is copied to the _site directory
37
+ Jekyll.logger.info "Copying resized image to #{site_dest_filepath}"
37
38
  FileUtils.copy_file(site_source_filepath, site_dest_filepath)
38
39
 
39
40
  i.destroy!
@@ -44,11 +45,19 @@ module Jekyll
44
45
  resized
45
46
  end
46
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
+
47
54
  def needs_resizing?(img, width)
48
55
  img.columns > width
49
56
  end
50
57
 
51
- def ensure_output_dir_exists!(dir)
58
+ def ensure_output_dir_exists!(path)
59
+ dir = File.dirname(path)
60
+
52
61
  unless Dir.exist?(dir)
53
62
  Jekyll.logger.info "Creating output directory #{dir}"
54
63
  FileUtils.mkdir_p(dir)
@@ -1,8 +1,6 @@
1
1
  module Jekyll
2
- class ResponsiveImage
2
+ module ResponsiveImage
3
3
  class Tag < Liquid::Tag
4
- include Jekyll::ResponsiveImage::Common
5
-
6
4
  def initialize(tag_name, markup, tokens)
7
5
  super
8
6
 
@@ -15,7 +13,7 @@ module Jekyll
15
13
  end
16
14
 
17
15
  def render(context)
18
- render_responsive_image(context, @attributes)
16
+ Renderer.new(context.registers[:site], @attributes).render_responsive_image
19
17
  end
20
18
  end
21
19
  end
@@ -1,8 +1,13 @@
1
1
  require 'pathname'
2
2
 
3
3
  module Jekyll
4
- class ResponsiveImage
4
+ module ResponsiveImage
5
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
+
6
11
  def symbolize_keys(hash)
7
12
  result = {}
8
13
  hash.each_key do |key|
@@ -11,17 +16,11 @@ module Jekyll
11
16
  result
12
17
  end
13
18
 
14
- def format_output_path(format, base_path, image_path, width, height)
15
- params = symbolize_keys(image_hash(base_path, image_path, width, height))
16
-
17
- Pathname.new(format % params).cleanpath.to_s
18
- end
19
-
20
19
  # Build a hash containing image information
21
- def image_hash(base_path, image_path, width, height)
20
+ def image_hash(config, image_path, width, height)
22
21
  {
23
22
  'path' => image_path,
24
- 'dirname' => relative_dirname(base_path, image_path),
23
+ 'dirname' => relative_dirname(config, image_path),
25
24
  'basename' => File.basename(image_path),
26
25
  'filename' => File.basename(image_path, '.*'),
27
26
  'extension' => File.extname(image_path).delete('.'),
@@ -30,9 +29,9 @@ module Jekyll
30
29
  }
31
30
  end
32
31
 
33
- def relative_dirname(base_path, image_path)
34
- path = Pathname.new(image_path).expand_path
35
- base = Pathname.new(base_path).expand_path
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]))
36
35
 
37
36
  path.relative_path_from(base).dirname.to_s.delete('.')
38
37
  end
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
- class ResponsiveImage
3
- VERSION = '1.0.0.pre3'.freeze
2
+ module ResponsiveImage
3
+ VERSION = '1.0.0.pre4'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-responsive_image
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.pre3
4
+ version: 1.0.0.pre4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Wynn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-25 00:00:00.000000000 Z
11
+ date: 2016-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -61,11 +61,11 @@ extra_rdoc_files: []
61
61
  files:
62
62
  - lib/jekyll/responsive_image.rb
63
63
  - lib/jekyll/responsive_image/block.rb
64
- - lib/jekyll/responsive_image/common.rb
65
- - lib/jekyll/responsive_image/defaults.rb
64
+ - lib/jekyll/responsive_image/config.rb
66
65
  - lib/jekyll/responsive_image/extra_image_generator.rb
67
66
  - lib/jekyll/responsive_image/image_processor.rb
68
67
  - lib/jekyll/responsive_image/render_cache.rb
68
+ - lib/jekyll/responsive_image/renderer.rb
69
69
  - lib/jekyll/responsive_image/resize_handler.rb
70
70
  - lib/jekyll/responsive_image/tag.rb
71
71
  - lib/jekyll/responsive_image/utils.rb
@@ -90,7 +90,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
90
  version: 1.3.1
91
91
  requirements: []
92
92
  rubyforge_project:
93
- rubygems_version: 2.5.1
93
+ rubygems_version: 2.6.6
94
94
  signing_key:
95
95
  specification_version: 4
96
96
  summary: Responsive images for Jekyll via srcset
@@ -1,46 +0,0 @@
1
- module Jekyll
2
- class ResponsiveImage
3
- module Common
4
- include Jekyll::ResponsiveImage::Utils
5
-
6
- def make_config(site)
7
- ResponsiveImage.defaults.dup
8
- .merge(site.config['responsive_image'])
9
- .merge(:site_source => site.source, :site_dest => site.dest)
10
- end
11
-
12
- def keep_resized_image!(site, image)
13
- keep_dir = File.dirname(image['path'])
14
- site.config['keep_files'] << keep_dir unless site.config['keep_files'].include?(keep_dir)
15
- end
16
-
17
- def render_responsive_image(context, attributes)
18
- cache_key = attributes.to_s
19
- result = attributes['cache'] ? RenderCache.get(cache_key) : nil
20
-
21
- if result.nil?
22
- site = context.registers[:site]
23
- config = make_config(site)
24
-
25
- absolute_image_path = site.in_source_dir(attributes['path'].to_s)
26
- image = ImageProcessor.process(absolute_image_path, attributes['path'], config)
27
- attributes['original'] = image[:original]
28
- attributes['resized'] = image[:resized]
29
-
30
- attributes['resized'].each { |resized| keep_resized_image!(site, resized) }
31
-
32
- image_template = site.in_source_dir(attributes['template'] || config['template'])
33
- partial = File.read(image_template)
34
- template = Liquid::Template.parse(partial)
35
-
36
- result = template.render!(attributes.merge(site.site_payload))
37
-
38
-
39
- RenderCache.set(cache_key, result)
40
- end
41
-
42
- result
43
- end
44
- end
45
- end
46
- end
@@ -1,15 +0,0 @@
1
- module Jekyll
2
- class ResponsiveImage
3
- @defaults = {
4
- 'default_quality' => 85,
5
- 'base_path' => 'assets',
6
- 'output_path_format' => 'assets/resized/%{filename}-%{width}x%{height}.%{extension}',
7
- 'sizes' => [],
8
- 'extra_images' => []
9
- }.freeze
10
-
11
- class << self
12
- attr_reader :defaults
13
- end
14
- end
15
- end