jekyll-images 0.2.3 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 522e86cd58354de44d3f57ec228fccdb3a2b2872f409f0ba2c7c7602faebf885
4
- data.tar.gz: 4839dba88514687693ecc6ca174764f9c9b3dceecb6899e00e3d40383f9ffcb9
3
+ metadata.gz: 9343975c53eefd5a9e88ed8ef249455a5fc82ca6ae5ba5b36ce46f369ee0d24b
4
+ data.tar.gz: 2ab5924b0216ff9354bb404316f43b09448431d04fcfe9ae5233ca4da347f8a8
5
5
  SHA512:
6
- metadata.gz: 644c96a927fabe9d74aebcc67a936d26a45302413f1ce63b7e704124c5de6d30820fa951518ce6dc62de069393e62765df8be07b0f2c8d75b4ed06c9e885b4b2
7
- data.tar.gz: 324147f91123e3217079b6b9856e9e855852e285a9c4c3e83893488c0ad54f6a42d3dbbb826968e58dd5d1b578b99c79424f93a1917c400984cbe7bb96fe3f5b
6
+ metadata.gz: 52d5e60cc1edfb8e5a72dcbdbfaf062d51846fe0a281103ece714fc5251ee75d9c3f9c19ee3ac7e137c147f8a9a56b3c79fde6df28488f353923cc0b623f96a1
7
+ data.tar.gz: fccc5454c030535ace052d299c4f34b419d6d698aa3ff24fd3447adc52414118bb144204bdccf7ce04dd89f00b13be1200f8d3e823e20b9579ac9b1cef8d93ef
data/README.md CHANGED
@@ -53,10 +53,12 @@ In your templates, you can use the `thumbnail` filter:
53
53
 
54
54
  Options for this filter are in the following order:
55
55
 
56
- * `width` (required), the desired width for the image
56
+ * `width`, the desired width for the image, if `nil`, `height` is
57
+ required.
57
58
 
58
59
  * `height`, if provided, the thumbnail will crop to this size. If not,
59
- the image is scaled down proportionally to the width.
60
+ the image is scaled down proportionally to the width. It becomes
61
+ required if the width is `nil`.
60
62
 
61
63
  * `crop` the smart cropping algorithm. One of `none`, `centre`,
62
64
  `entropy` or `attention` (default). See
@@ -67,7 +69,7 @@ Options for this filter are in the following order:
67
69
  orientation metadata, this controls if it's automatically rotated.
68
70
 
69
71
  If you want to pass `crop` and `auto_rotate` but not `height`, just set
70
- `height` to `0` or `''`.
72
+ `height` to `nil`.
71
73
 
72
74
  ## TODO
73
75
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'jekyll/images/image'
3
4
  require_relative 'jekyll/images/thumbnail'
4
5
  require_relative 'jekyll/filters/thumbnail'
5
6
  require_relative 'jekyll/images/oxipng'
@@ -4,22 +4,34 @@ module Jekyll
4
4
  module Filters
5
5
  # Liquid filter for use in templates
6
6
  module Thumbnail
7
+ @@cached_images = {}
8
+
7
9
  # Generates a thumbnail and returns its alternate destination
8
- def thumbnail(input, width, height = nil, crop = :attention, auto_rotate = true)
10
+ def thumbnail(input, width = nil, height = nil, crop = nil, auto_rotate = nil)
9
11
  return unless input
10
12
 
11
- height = height if height.to_i > 1
12
- image = Jekyll::Images::Thumbnail.new(@context.registers[:site],
13
- input,
14
- width: width,
15
- height: height,
16
- crop: crop,
17
- auto_rotate: auto_rotate)
18
-
19
- # XXX: This won't run optimizations more than once but it won't
20
- # also optimize source images.
21
- image.write && image.optimize
22
- image.dest
13
+ image = cached_image input
14
+ thumb = image.thumbnail(width: width,
15
+ height: height,
16
+ crop: crop,
17
+ auto_rotate: auto_rotate)
18
+
19
+ thumb.write && thumb.optimize
20
+
21
+ thumb.dest
22
+ rescue Vips::Error => e
23
+ Jekyll.logger.warn "Failed to process #{input}: #{e.message}"
24
+ input
25
+ end
26
+
27
+ private
28
+
29
+ def cached_image?(input)
30
+ @@cached_images.key? input
31
+ end
32
+
33
+ def cached_image(input)
34
+ @@cached_images[input] ||= Jekyll::Images::Image.new(@context.registers[:site], input)
23
35
  end
24
36
  end
25
37
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'vips'
4
+
5
+ module Jekyll
6
+ module Images
7
+ class Image
8
+ attr_reader :site, :filename, :height, :width
9
+
10
+ def initialize(site, filename)
11
+ unless File.exist? filename
12
+ raise ArgumentError, "File not found: #{filename}"
13
+ end
14
+
15
+ @cached_thumbnails = {}
16
+ @site = site
17
+ @filename = filename
18
+
19
+ # We only need the image to get height and width
20
+ image = Vips::Image.new_from_file filename, access: :sequential
21
+
22
+ @height = image.height
23
+ @width = image.width
24
+ end
25
+
26
+ def thumbnail(**args)
27
+ @cached_thumbnails[args.hash.to_s] ||= Thumbnail.new site: site, filename: filename, image: self, **args
28
+ end
29
+ end
30
+ end
31
+ end
@@ -4,57 +4,56 @@ require 'vips'
4
4
 
5
5
  module Jekyll
6
6
  module Images
7
- # Use LibVIPS to generate images in different sizes
8
- #
9
- # We're assuming every image is going to be thumbnailed
10
7
  class Thumbnail
11
- attr_reader :site, :filename, :width, :height, :crop, :auto_rotate
8
+ attr_reader :site, :filename, :image, :width, :height, :crop, :auto_rotate
12
9
 
13
- def initialize(site, filename, **args)
14
- unless File.exist? filename
15
- raise ArgumentError, "File not found: #{filename}"
10
+ def initialize(site:, filename:, image:, **args)
11
+ if args.slice(:width, :height).values.none?
12
+ raise ArgumentError, "#{filename} thumbnail needs width or height"
16
13
  end
17
- raise ArgumentError, 'Missing width' unless args[:width]
18
14
 
19
15
  @site = site
20
16
  @filename = filename
21
- @width = args[:width]
22
- @height = args[:height]
23
- @crop = args[:crop].to_sym
24
- @auto_rotate = args[:auto_rotate]
25
- end
26
-
27
- def image
28
- @image ||= Vips::Image.new_from_file filename,
29
- access: :sequential
17
+ @image = image
18
+ @width = args[:width] || proportional_width(args[:height])
19
+ @height = args[:height] || proportional_height(args[:width])
20
+ @crop = args[:crop]&.to_sym || :attention
21
+ @auto_rotate = args[:auto_rotate].nil? ? true : args[:auto_rotate]
30
22
  end
31
23
 
24
+ # XXX: We don't memoize because we don't need the thumbnail in
25
+ # memory after it has been written.
32
26
  def thumbnail
33
- @thumbnail ||= image.thumbnail_image width,
34
- height: height,
35
- auto_rotate: auto_rotate,
36
- crop: crop
27
+ Vips::Image.thumbnail filename,
28
+ width,
29
+ height: height,
30
+ auto_rotate: auto_rotate,
31
+ crop: crop
37
32
  end
38
33
 
39
- def height
40
- @height || proportional_height
34
+ # Finds a height that's proportional to the width
35
+ def proportional_height(width)
36
+ @proportional_height ||= (image.height * (width / image.width.to_f)).round
41
37
  end
42
38
 
43
- # Finds a heigth that's proportional to the width
44
- def proportional_height
45
- @proportional_height ||= (image.height * (width / image.width.to_f)).round
39
+ # Find a width that's proportional to height
40
+ def proportional_width(height)
41
+ @proportional_width ||= (image.width * (height / image.height.to_f)).round
46
42
  end
47
43
 
48
44
  # Generates a destination from filename only if we're downsizing
49
45
  def dest
50
- @dest ||= if image.width > width
46
+ @dest ||= if thumbnail?
51
47
  filename.gsub(/#{extname}\z/, "_#{width}x#{height}#{extname}")
52
48
  else
53
- Jekyll.logger.info "Not thumbnailing #{filename}"
54
- filename
49
+ filename.gsub(/#{extname}\z/, "_optimized#{extname}")
55
50
  end
56
51
  end
57
52
 
53
+ def thumbnail?
54
+ image.width > width
55
+ end
56
+
58
57
  def extname
59
58
  @extname ||= File.extname filename
60
59
  end
@@ -69,8 +68,13 @@ module Jekyll
69
68
  def write
70
69
  return unless write?
71
70
 
72
- Jekyll.logger.info "Thumbnailing #{filename} => #{dest}"
73
- thumbnail.write_to_file(dest)
71
+ if thumbnail?
72
+ Jekyll.logger.info "Thumbnailing #{filename} => #{dest}"
73
+ thumbnail.write_to_file(dest)
74
+ else
75
+ Jekyll.logger.info "Copying #{filename} => #{dest}"
76
+ FileUtils.cp(filename, dest)
77
+ end
74
78
 
75
79
  # Add it to the static files so Jekyll copies them. Once they
76
80
  # are written they're copied when the site is loaded.
@@ -90,7 +94,7 @@ module Jekyll
90
94
 
91
95
  pct = ((after.to_f / before) * -100 + 100).round(2)
92
96
 
93
- Jekyll.logger.info "Reduced #{filename} from #{before} to #{after} bytes (%#{pct})"
97
+ Jekyll.logger.info "Reduced #{dest} from #{before} to #{after} bytes (%#{pct})"
94
98
  end
95
99
  end
96
100
  end
metadata CHANGED
@@ -1,139 +1,99 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-images
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - f
8
- autorequire:
9
- bindir: exe
8
+ autorequire:
9
+ bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-21 00:00:00.000000000 Z
11
+ date: 2020-07-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: ruby-vips
14
+ name: jekyll
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '2'
19
+ version: '4'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '2'
26
+ version: '4'
27
27
  - !ruby/object:Gem::Dependency
28
- name: ruby-filemagic
28
+ name: ruby-vips
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0.7'
33
+ version: '2'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0.7'
41
- - !ruby/object:Gem::Dependency
42
- name: bundler
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '2.0'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '2.0'
55
- - !ruby/object:Gem::Dependency
56
- name: minitest
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '5.0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '5.0'
69
- - !ruby/object:Gem::Dependency
70
- name: rake
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '10.0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '10.0'
40
+ version: '2'
83
41
  - !ruby/object:Gem::Dependency
84
- name: rubocop
42
+ name: ruby-filemagic
85
43
  requirement: !ruby/object:Gem::Requirement
86
44
  requirements:
87
45
  - - "~>"
88
46
  - !ruby/object:Gem::Version
89
- version: '0.74'
90
- type: :development
47
+ version: '0.7'
48
+ type: :runtime
91
49
  prerelease: false
92
50
  version_requirements: !ruby/object:Gem::Requirement
93
51
  requirements:
94
52
  - - "~>"
95
53
  - !ruby/object:Gem::Version
96
- version: '0.74'
54
+ version: '0.7'
97
55
  description: Optimizes images for Jekyll
98
56
  email:
99
57
  - f@sutty.nl
100
58
  executables: []
101
59
  extensions: []
102
- extra_rdoc_files: []
60
+ extra_rdoc_files:
61
+ - README.md
62
+ - LICENSE
103
63
  files:
104
- - ".gitignore"
105
- - ".travis.yml"
106
- - Gemfile
107
64
  - LICENSE
108
65
  - README.md
109
- - Rakefile
110
- - bin/console
111
- - bin/setup
112
- - jekyll-images.gemspec
113
66
  - lib/jekyll-images.rb
114
67
  - lib/jekyll/filters/thumbnail.rb
68
+ - lib/jekyll/images/image.rb
115
69
  - lib/jekyll/images/jpeg_optim.rb
116
70
  - lib/jekyll/images/oxipng.rb
117
71
  - lib/jekyll/images/runner.rb
118
72
  - lib/jekyll/images/thumbnail.rb
119
- - lib/jekyll/images/version.rb
120
73
  homepage: https://0xacab.org/sutty/jekyll/jekyll-images
121
74
  licenses:
122
75
  - GPL-3.0
123
76
  metadata:
124
- allowed_push_host: https://rubygems.org
77
+ bug_tracker_uri: https://0xacab.org/sutty/jekyll/jekyll-images/issues
125
78
  homepage_uri: https://0xacab.org/sutty/jekyll/jekyll-images
126
79
  source_code_uri: https://0xacab.org/sutty/jekyll/jekyll-images
127
- changelog_uri: https://0xacab.org/sutty/jekyll/jekyll-images/blob/master/CHANGELOG.md
128
- post_install_message:
129
- rdoc_options: []
80
+ documentation_uri: https://rubydoc.info/gems/jekyll-images
81
+ post_install_message:
82
+ rdoc_options:
83
+ - "--title"
84
+ - jekyll-images - Optimizes images for Jekyll
85
+ - "--main"
86
+ - README.md
87
+ - "--line-numbers"
88
+ - "--inline-source"
89
+ - "--quiet"
130
90
  require_paths:
131
91
  - lib
132
92
  required_ruby_version: !ruby/object:Gem::Requirement
133
93
  requirements:
134
- - - ">="
94
+ - - "~>"
135
95
  - !ruby/object:Gem::Version
136
- version: '0'
96
+ version: '2'
137
97
  required_rubygems_version: !ruby/object:Gem::Requirement
138
98
  requirements:
139
99
  - - ">="
@@ -141,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
101
  version: '0'
142
102
  requirements: []
143
103
  rubygems_version: 3.0.3
144
- signing_key:
104
+ signing_key:
145
105
  specification_version: 4
146
106
  summary: Optimizes images for Jekyll
147
107
  test_files: []
data/.gitignore DELETED
@@ -1,8 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
@@ -1,7 +0,0 @@
1
- ---
2
- sudo: false
3
- language: ruby
4
- cache: bundler
5
- rvm:
6
- - 2.6.5
7
- before_install: gem install bundler -v 2.0.2
data/Gemfile DELETED
@@ -1,5 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- source 'https://rubygems.org'
4
-
5
- gemspec
data/Rakefile DELETED
@@ -1,10 +0,0 @@
1
- require "bundler/gem_tasks"
2
- require "rake/testtask"
3
-
4
- Rake::TestTask.new(:test) do |t|
5
- t.libs << "test"
6
- t.libs << "lib"
7
- t.test_files = FileList["test/**/*_test.rb"]
8
- end
9
-
10
- task :default => :test
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "jekyll/image/optimization"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,40 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- lib = File.expand_path('lib', __dir__)
4
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
- require 'jekyll/images/version'
6
-
7
- Gem::Specification.new do |spec|
8
- spec.name = 'jekyll-images'
9
- spec.version = Jekyll::Images::VERSION
10
- spec.authors = ['f']
11
- spec.email = ['f@sutty.nl']
12
-
13
- spec.summary = 'Optimizes images for Jekyll'
14
- spec.description = 'Optimizes images for Jekyll'
15
- spec.homepage = 'https://0xacab.org/sutty/jekyll/jekyll-images'
16
- spec.license = 'GPL-3.0'
17
-
18
- spec.metadata['allowed_push_host'] = 'https://rubygems.org'
19
-
20
- spec.metadata['homepage_uri'] = spec.homepage
21
- spec.metadata['source_code_uri'] = spec.homepage
22
- spec.metadata['changelog_uri'] = "#{spec.homepage}/blob/master/CHANGELOG.md"
23
-
24
- # Specify which files should be added to the gem when it is released.
25
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
- spec.files = Dir.chdir(File.expand_path(__dir__)) do
27
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
- end
29
- spec.bindir = 'exe'
30
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
- spec.require_paths = ['lib']
32
-
33
- spec.add_dependency 'ruby-vips', '~> 2'
34
- spec.add_dependency 'ruby-filemagic', '~> 0.7'
35
-
36
- spec.add_development_dependency 'bundler', '~> 2.0'
37
- spec.add_development_dependency 'minitest', '~> 5.0'
38
- spec.add_development_dependency 'rake', '~> 10.0'
39
- spec.add_development_dependency 'rubocop', '~> 0.74'
40
- end
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Jekyll
4
- module Images
5
- VERSION = '0.2.3'
6
- end
7
- end