jekyll-images 0.2.1 → 0.2.6

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: b61dff2c6710eddeb885f339f68eb3c5c8df598c9bb83cc1065d130c7d2f0bb5
4
- data.tar.gz: 6626adea05ae4f94829c815d0f4a71427b949478e22732a8e387d9b654d099a5
3
+ metadata.gz: 85114fbf8d07e0b91732c4c2ced348e28d7462a01cf1bafcd049d8a940d0c6bc
4
+ data.tar.gz: c49b4ed4846e8ba47817c320da37eb95f6bbebfe3496658e1d0c6618c71863ef
5
5
  SHA512:
6
- metadata.gz: d33086b2bfc1bad9a0d051ffdb2f5383aa616f2bbb989fc2b16520cb7cd9dfd159568ad531af67dcd94557efb3dffa3c0bcb4394ff2a0fbc2a5e677b0c9bb320
7
- data.tar.gz: be91ff3b175356cf2f66f26ff0bfe8b9536001c89924a9106f8b191a47a289c8e2401ed12f37059a57dd827c4935c3345ea0a06e5457e33a0d4e2441758f09ea
6
+ metadata.gz: 3584ac278b582ffe5355cacd68d8a34b18fa928ba6275a90d457c77c0cbd0de87e824450b9987aa61328e146b9cbd8a210a77d3aba9ba285c438ba12d856454c
7
+ data.tar.gz: aa1b7a026a5dbafe44b4ba9b34c2f466b6c804ad62b00bcabbf9d3d1cdb666e0885e30271f9c38f54c73b5b7c1384fe89851ce055c671ef9a7b6a299a67985da
data/README.md CHANGED
@@ -1,8 +1,7 @@
1
1
  # Image optimization for Jekyll
2
2
 
3
3
  This Jekyll plugin helps you optimize images by thumbnailing them to
4
- the sizes you specify in templates and in the configuration for images
5
- in posts and collections.
4
+ the sizes you specify in templates.
6
5
 
7
6
  It uses [Libvips](https://libvips.github.io/libvips/) for performance
8
7
  and low resource usage. It'll also cache the thumbnails so it only runs
@@ -54,10 +53,12 @@ In your templates, you can use the `thumbnail` filter:
54
53
 
55
54
  Options for this filter are in the following order:
56
55
 
57
- * `width` (required), the desired width for the image
56
+ * `width`, the desired width for the image, if `nil`, `height` is
57
+ required.
58
58
 
59
59
  * `height`, if provided, the thumbnail will crop to this size. If not,
60
- 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`.
61
62
 
62
63
  * `crop` the smart cropping algorithm. One of `none`, `centre`,
63
64
  `entropy` or `attention` (default). See
@@ -68,33 +69,15 @@ Options for this filter are in the following order:
68
69
  orientation metadata, this controls if it's automatically rotated.
69
70
 
70
71
  If you want to pass `crop` and `auto_rotate` but not `height`, just set
71
- `height` to `0` or `''`.
72
-
73
- ### Configuration and posts
74
-
75
- Images in a post content need to be configured globally:
76
-
77
- ```yaml
78
- # _config.yml
79
- images:
80
- # These are bootstrap4 breakpoints in pixels
81
- sizes:
82
- - 576
83
- - 768
84
- - 992
85
- - 1200
86
- thumbnail:
87
- width: 600
88
- height: 0 # Leave this out for proportional thumbnailing
89
- crop: attention # See Vips::Interesting
90
- auto_rotate: true
91
- ```
72
+ `height` to `nil`.
92
73
 
93
74
  ## TODO
94
75
 
95
76
  * Use `<picture>` and the `srcset` attribute automatically for posts
96
77
  <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture>
97
78
 
79
+ * Download and thumbnail images in posts and other documents.
80
+
98
81
  ## Contributing
99
82
 
100
83
  Bug reports and pull requests are welcome on GitHub at
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'pry'
4
3
  require_relative 'jekyll/images/thumbnail'
5
4
  require_relative 'jekyll/filters/thumbnail'
6
5
  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,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'vips'
4
+
5
+ module Jekyll
6
+ module Images
7
+ class Image
8
+ attr_reader :site, :filename
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
+ end
19
+
20
+ def image
21
+ @image ||= Vips::Image.new_from_file filename, access: :sequential
22
+ end
23
+
24
+ def thumbnail(**args)
25
+ @cached_thumbnails[args.hash.to_s] ||= Thumbnail.new site: site, filename: filename, image: image, **args
26
+ end
27
+ end
28
+ end
29
+ end
@@ -4,44 +4,54 @@ 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, :image, :thumbnail, :filename, :width, :height
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
- @image = Vips::Image.new_from_file filename, access: :sequential
24
- @thumbnail = image.thumbnail_image width,
25
- height: height || proportional_height,
26
- auto_rotate: args[:auto_rotate],
27
- crop: args[:crop].to_sym
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]
22
+ end
23
+
24
+ def thumbnail
25
+ @thumbnail ||= Vips::Image.thumbnail filename,
26
+ width,
27
+ height: height,
28
+ auto_rotate: auto_rotate,
29
+ crop: crop
28
30
  end
29
31
 
30
32
  # Finds a height that's proportional to the width
31
- def proportional_height
32
- @height = (image.height * (width / image.width.to_f)).round
33
+ def proportional_height(width)
34
+ @proportional_height ||= (image.height * (width / image.width.to_f)).round
35
+ end
36
+
37
+ # Find a width that's proportional to height
38
+ def proportional_width(height)
39
+ @proportional_width ||= (image.width * (height / image.height.to_f)).round
33
40
  end
34
41
 
35
42
  # Generates a destination from filename only if we're downsizing
36
43
  def dest
37
- @dest ||= if image.width > width
44
+ @dest ||= if thumbnail?
38
45
  filename.gsub(/#{extname}\z/, "_#{width}x#{height}#{extname}")
39
46
  else
40
- Jekyll.logger.info "Not thumbnailing #{filename}"
41
- filename
47
+ filename.gsub(/#{extname}\z/, "_optimized#{extname}")
42
48
  end
43
49
  end
44
50
 
51
+ def thumbnail?
52
+ image.width > width
53
+ end
54
+
45
55
  def extname
46
56
  @extname ||= File.extname filename
47
57
  end
@@ -56,8 +66,13 @@ module Jekyll
56
66
  def write
57
67
  return unless write?
58
68
 
59
- Jekyll.logger.info "Thumbnailing #{filename} => #{dest}"
60
- thumbnail.write_to_file(dest)
69
+ if thumbnail?
70
+ Jekyll.logger.info "Thumbnailing #{filename} => #{dest}"
71
+ thumbnail.write_to_file(dest)
72
+ else
73
+ Jekyll.logger.info "Copying #{filename} => #{dest}"
74
+ FileUtils.cp(filename, dest)
75
+ end
61
76
 
62
77
  # Add it to the static files so Jekyll copies them. Once they
63
78
  # are written they're copied when the site is loaded.
@@ -77,7 +92,7 @@ module Jekyll
77
92
 
78
93
  pct = ((after.to_f / before) * -100 + 100).round(2)
79
94
 
80
- Jekyll.logger.info "Reduced #{filename} from #{before} to #{after} bytes (%#{pct})"
95
+ Jekyll.logger.info "Reduced #{dest} from #{before} to #{after} bytes (%#{pct})"
81
96
  end
82
97
  end
83
98
  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.1
4
+ version: 0.2.6
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-01-29 00:00:00.000000000 Z
11
+ date: 2020-07-20 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.1'
6
- end
7
- end