jekyll-images 0.2.0 → 0.2.5

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: b16d950e3f192f849e1577a16fc572c9bb698604de4bf31a64b1c62285ac00ef
4
- data.tar.gz: 1072440b92d9e2351a799100d1aef666fe14441bc0d52c93ac40ed71eba14cfb
3
+ metadata.gz: ade8806597db4aad25430a39bb1c7a8db08e4e474015344e7afe2e8bbd717038
4
+ data.tar.gz: c97d3622656034a395f745aaeafe47fc994cb1c1a18ba9853251ef77f924ebcc
5
5
  SHA512:
6
- metadata.gz: 6c87cae74ddaacce079c5a192af9c1e1315a198176730a8b6f0b82dbf5e1868c0cc179ef191519ee502c6c839928ea56339a2a212099c87a21f53cd69c6c14ae
7
- data.tar.gz: a89803b7bc464ab3b1a27a135946406814016ca61374ad7c904c93144369d912249b06f6b1e32cbc3c988e1ecda7515cc0ecce0cea8f4524ee87a58a80479a66
6
+ metadata.gz: 3398cc500da4c3ee7ea3be463247422636e37d9a363ab5b2aa94a42acd755f90341995c35b51055fe6bb446b90526a383d3bffbb507cbde64750b98cb85ac94c
7
+ data.tar.gz: b76bdd36d899533d031990eb152bb93032e609877b32806b9a629c6c4590ddef5baae6e63e8f98a236574d3a21d0fb10538c64ac8d7fee7b64e92af55418c1a6
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
@@ -70,31 +69,13 @@ Options for this filter are in the following order:
70
69
  If you want to pass `crop` and `auto_rotate` but not `height`, just set
71
70
  `height` to `0` or `''`.
72
71
 
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
- ```
92
-
93
72
  ## TODO
94
73
 
95
74
  * Use `<picture>` and the `srcset` attribute automatically for posts
96
75
  <https://developer.mozilla.org/en-US/docs/Web/HTML/Element/picture>
97
76
 
77
+ * Download and thumbnail images in posts and other documents.
78
+
98
79
  ## Contributing
99
80
 
100
81
  Bug reports and pull requests are welcome on GitHub at
@@ -1,8 +1,6 @@
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
- require_relative 'jekyll/hooks/thumbnail'
7
5
  require_relative 'jekyll/images/oxipng'
8
6
  require_relative 'jekyll/images/jpeg_optim'
@@ -1,25 +1,40 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'pry'
3
4
  module Jekyll
4
5
  module Filters
5
6
  # Liquid filter for use in templates
6
7
  module Thumbnail
8
+ @@cached_images = {}
9
+
7
10
  # Generates a thumbnail and returns its alternate destination
8
11
  def thumbnail(input, width, height = nil, crop = :attention, auto_rotate = true)
9
12
  return unless input
13
+ return cached_image(input).dest if cached_image? input
10
14
 
11
15
  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.
16
+ image = cached_image input, width, height, crop, auto_rotate
17
+
21
18
  image.write && image.optimize
22
19
  image.dest
20
+ rescue Vips::Error => e
21
+ Jekyll.logger.warn "Failed to process #{input}: #{e.message}"
22
+ input
23
+ end
24
+
25
+ private
26
+
27
+ def cached_image?(input)
28
+ @@cached_images.key? input
29
+ end
30
+
31
+ def cached_image(input, width = nil, height = nil, crop = nil, auto_rotate = nil)
32
+ @@cached_images[input] ||= Jekyll::Images::Thumbnail.new(@context.registers[:site],
33
+ input,
34
+ width: width,
35
+ height: height,
36
+ crop: crop,
37
+ auto_rotate: auto_rotate)
23
38
  end
24
39
  end
25
40
  end
@@ -8,7 +8,7 @@ module Jekyll
8
8
  #
9
9
  # We're assuming every image is going to be thumbnailed
10
10
  class Thumbnail
11
- attr_reader :site, :image, :thumbnail, :filename, :width, :height
11
+ attr_reader :site, :filename, :width, :height, :crop, :auto_rotate
12
12
 
13
13
  def initialize(site, filename, **args)
14
14
  unless File.exist? filename
@@ -20,28 +20,44 @@ module Jekyll
20
20
  @filename = filename
21
21
  @width = args[:width]
22
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
23
+ @crop = args[:crop].to_sym
24
+ @auto_rotate = args[:auto_rotate]
28
25
  end
29
26
 
30
- # Finds a height that's proportional to the width
27
+ def image
28
+ @image ||= Vips::Image.new_from_file filename,
29
+ access: :sequential
30
+ end
31
+
32
+ def thumbnail
33
+ @thumbnail ||= image.thumbnail_image width,
34
+ height: height,
35
+ auto_rotate: auto_rotate,
36
+ crop: crop
37
+ end
38
+
39
+ def height
40
+ @height || proportional_height
41
+ end
42
+
43
+ # Finds a heigth that's proportional to the width
31
44
  def proportional_height
32
- @height = (image.height * (width / image.width.to_f)).round
45
+ @proportional_height ||= (image.height * (width / image.width.to_f)).round
33
46
  end
34
47
 
35
48
  # Generates a destination from filename only if we're downsizing
36
49
  def dest
37
- @dest ||= if image.width > width
50
+ @dest ||= if thumbnail?
38
51
  filename.gsub(/#{extname}\z/, "_#{width}x#{height}#{extname}")
39
52
  else
40
- Jekyll.logger.info "Not thumbnailing #{filename}"
41
- filename
53
+ filename.gsub(/#{extname}\z/, "_optimized#{extname}")
42
54
  end
43
55
  end
44
56
 
57
+ def thumbnail?
58
+ image.width > width
59
+ end
60
+
45
61
  def extname
46
62
  @extname ||= File.extname filename
47
63
  end
@@ -56,8 +72,12 @@ module Jekyll
56
72
  def write
57
73
  return unless write?
58
74
 
59
- Jekyll.logger.info "Thumbnailing #{filename} => #{dest}"
60
- thumbnail.write_to_file(dest)
75
+ if thumbnail?
76
+ Jekyll.logger.info "Thumbnailing #{filename} => #{dest}"
77
+ thumbnail.write_to_file(dest)
78
+ else
79
+ FileUtils.cp(filename, dest)
80
+ end
61
81
 
62
82
  # Add it to the static files so Jekyll copies them. Once they
63
83
  # are written they're copied when the site is loaded.
@@ -77,7 +97,7 @@ module Jekyll
77
97
 
78
98
  pct = ((after.to_f / before) * -100 + 100).round(2)
79
99
 
80
- Jekyll.logger.info "Reduced #{filename} from #{before} to #{after} bytes (%#{pct})"
100
+ Jekyll.logger.info "Reduced #{dest} from #{before} to #{after} bytes (%#{pct})"
81
101
  end
82
102
  end
83
103
  end
metadata CHANGED
@@ -1,139 +1,98 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-images
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.5
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
115
68
  - lib/jekyll/images/jpeg_optim.rb
116
69
  - lib/jekyll/images/oxipng.rb
117
70
  - lib/jekyll/images/runner.rb
118
71
  - lib/jekyll/images/thumbnail.rb
119
- - lib/jekyll/images/version.rb
120
72
  homepage: https://0xacab.org/sutty/jekyll/jekyll-images
121
73
  licenses:
122
74
  - GPL-3.0
123
75
  metadata:
124
- allowed_push_host: https://rubygems.org
76
+ bug_tracker_uri: https://0xacab.org/sutty/jekyll/jekyll-images/issues
125
77
  homepage_uri: https://0xacab.org/sutty/jekyll/jekyll-images
126
78
  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: []
79
+ documentation_uri: https://rubydoc.info/gems/jekyll-images
80
+ post_install_message:
81
+ rdoc_options:
82
+ - "--title"
83
+ - jekyll-images - Optimizes images for Jekyll
84
+ - "--main"
85
+ - README.md
86
+ - "--line-numbers"
87
+ - "--inline-source"
88
+ - "--quiet"
130
89
  require_paths:
131
90
  - lib
132
91
  required_ruby_version: !ruby/object:Gem::Requirement
133
92
  requirements:
134
- - - ">="
93
+ - - "~>"
135
94
  - !ruby/object:Gem::Version
136
- version: '0'
95
+ version: '2'
137
96
  required_rubygems_version: !ruby/object:Gem::Requirement
138
97
  requirements:
139
98
  - - ">="
@@ -141,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
141
100
  version: '0'
142
101
  requirements: []
143
102
  rubygems_version: 3.0.3
144
- signing_key:
103
+ signing_key:
145
104
  specification_version: 4
146
105
  summary: Optimizes images for Jekyll
147
106
  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.0'
6
- end
7
- end