jekyll-compress-images 1.2 → 1.3

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.
Files changed (5) hide show
  1. checksums.yaml +4 -4
  2. data/LICENSE +21 -0
  3. data/README.md +79 -0
  4. data/lib/jekyll-compress-images.rb +65 -24
  5. metadata +82 -18
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c2543e41304877b76a939d0c94961cb5163bd3bd615442071f39d1f029acd9ce
4
- data.tar.gz: a39560c4ce594ae71345b213b14b70b067cc5a6f28eba16b786b74010270a0b4
3
+ metadata.gz: 0acb0ae2e739816700bc6fb491d44d272ffb7dad3bd0f725b8b90e61f9589709
4
+ data.tar.gz: e2d6ff51708865f6ba43a21f9c6a2f93ff5f1867a9b0aac31253999e31cdd97b
5
5
  SHA512:
6
- metadata.gz: 19237d4099f43dc770017d8a78079b9fb33de32e5cb775d8d8ad346ccbb90ad7f77bebc123206aea9484bffdec060855f78e9499469a85572276128f5bfc6565
7
- data.tar.gz: 63b1e61949cb6102a90566b75c552075a44cbd8d8107aca671ca6609fb5be8cb6081853df96c9e860436ddd7371e3b081fd1a1ddf53befb146c2cf4db65bc8b6
6
+ metadata.gz: d1cbccb9e8ba180bada845b8b8fcbaf6dc5e5be4ce4d092ff7d7d024d06822f7c44135be321b9bc76d063d864aafded65ebae9289b16ee770fe22b68731ac53a
7
+ data.tar.gz: 4a5536237ae2205c4ca9372a1bea7e907d831943d47dd0c3cf8bcc59f53e11a6ccb492ae8908d2438bd60c62f2f8ef41f90352c256f4198cbb63b41641b8fc12
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 valerijaspasojevic
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ [![Gem Version](https://badge.fury.io/rb/jekyll-compress-images.svg)](https://badge.fury.io/rb/jekyll-compress-images)
2
+ [![Test](https://github.com/valerijaspasojevic/jekyll-compress-images/actions/workflows/test.yml/badge.svg)](https://github.com/valerijaspasojevic/jekyll-compress-images/actions/workflows/test.yml)
3
+
4
+ # jekyll-compress-images
5
+
6
+ Plugin for compressing/optimizing images (jpg, png, gif, svg).
7
+
8
+ # Installation
9
+
10
+ Add to your `Gemfile`:
11
+
12
+ ```ruby
13
+ gem 'jekyll-compress-images'
14
+ ```
15
+
16
+ and in `_config.yml`:
17
+
18
+ ```yaml
19
+ plugins:
20
+ - jekyll-compress-images
21
+ ```
22
+
23
+ Run `bundle install` in your project folder.
24
+
25
+ # Configuration
26
+
27
+ If you want to set up a different path for images, open `_config.yml` and add:
28
+
29
+ ```yaml
30
+ compress_images:
31
+ images_path: "yourpath/img/**/*.{gif,png,jpg,jpeg,svg}"
32
+ ```
33
+
34
+ If you don't configure it, the default path will be `assets/img/**/*.{gif,png,jpg,jpeg,svg}`.
35
+
36
+ All options with their defaults:
37
+
38
+ ```yaml
39
+ compress_images:
40
+ images_path: "assets/img/**/*.{gif,png,jpg,jpeg,svg}" # relative to your site source
41
+ cache_file: "_compress_images_cache.yml" # remembers which images are already optimized
42
+ threads: 8 # defaults to the number of CPU cores
43
+ ```
44
+
45
+ ## image_optim options
46
+
47
+ You can pass [image_optim](https://github.com/toy/image_optim) options by using:
48
+
49
+ ```yaml
50
+ imageoptim:
51
+ pngout: false
52
+ svgo: true
53
+ verbose: false
54
+ ```
55
+
56
+ SVG optimization uses [svgo](https://github.com/svg/svgo), which isn't bundled. Install it with `npm install -g svgo`, or set `svgo: false` to hide the warning.
57
+
58
+ # Usage
59
+
60
+ On `jekyll serve` or `jekyll build`, compression will run. If your images are already compressed, you don't need to worry, because it won't run again, which saves a bunch of time! :)
61
+
62
+ Good to know:
63
+
64
+ - Images are optimized **in place**, in your source folder. Commit them afterwards.
65
+ - Commit `_compress_images_cache.yml` too, so other machines (and CI) know which images are already done.
66
+ - If you replace an image with a new file, it will be optimized again automatically.
67
+
68
+ # Development
69
+
70
+ ```sh
71
+ bundle install
72
+ bundle exec rake test
73
+ ```
74
+
75
+ If this project helps you, you can give me a robot instead of coffee. :)
76
+
77
+ [![paypal](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.me/ValerijaSpasojevic)
78
+
79
+ **Enjoy** 🎉
@@ -1,48 +1,89 @@
1
+ require "digest"
2
+ require "etc"
3
+ require "pathname"
4
+ require "yaml"
1
5
  require "image_optim"
2
6
  require "image_optim_pack"
3
- require 'yaml'
7
+ require "in_threads"
4
8
 
5
9
  module Jekyll
6
10
  class CompressImages < Generator
7
11
  safe true
8
12
 
13
+ LOG_TOPIC = "CompressImages:".freeze
14
+
15
+ DEFAULT_OPTIONS = {
16
+ "cache_file" => "_compress_images_cache.yml",
17
+ "images_path" => "assets/img/**/*.{gif,png,jpg,jpeg,svg}",
18
+ "threads" => Etc.nprocessors
19
+ }.freeze
20
+
21
+ DEFAULT_IMAGEOPTIM_OPTIONS = {
22
+ "pngout" => false,
23
+ "svgo" => true,
24
+ "verbose" => false
25
+ }.freeze
26
+
9
27
  def generate(site)
28
+ @site = site
29
+ @config = DEFAULT_OPTIONS.merge(site.config["compress_images"] || {})
30
+ @cache_path = File.expand_path(@config["cache_file"], site.source)
10
31
 
11
- config = YAML::load_file "_config.yml"
32
+ images = Dir.glob(File.expand_path(@config["images_path"], site.source)).select { |path| File.file?(path) }.sort
33
+ @original_cache = load_cache
34
+ # Only keep entries for images that still exist, so deleted images drop out of the cache
35
+ @cache = @original_cache.select { |key, _| images.any? { |path| relative(path) == key } }
36
+ @mutex = Mutex.new
12
37
 
13
- config = config["compress_images"] || {}
38
+ pending = images.reject { |path| @cache[relative(path)] == digest(path) }
39
+ pending.in_threads([@config["threads"].to_i, 1].max).each { |path| optimize(path) } if pending.any?
40
+ ensure
41
+ save_cache if @cache
42
+ end
14
43
 
15
- @config = default_options.merge! config
44
+ private
16
45
 
17
- @image_optim = ImageOptim.new pngout: false, svgo: true, verbose: false
46
+ def image_optim
47
+ @image_optim ||= ImageOptim.new(DEFAULT_IMAGEOPTIM_OPTIONS.merge(@site.config["imageoptim"] || {}))
48
+ end
18
49
 
19
- @last_update = YAML::load_file @config["cache_file"] if File.file? @config["cache_file"]
20
- @last_update ||= {}
50
+ def optimize(path)
51
+ size_before = File.size(path)
52
+ image_optim.optimize_image!(path)
53
+ size_after = File.size(path)
54
+ saved = size_before.zero? ? 0 : ((size_before - size_after) * 100.0 / size_before).round(1)
55
+ Jekyll.logger.info LOG_TOPIC, "Optimized #{relative(path)} (-#{saved}%)"
21
56
 
22
- Dir.glob(@config["images_path"]) { |image| analyze image }
57
+ # Store the digest of the optimized file, so we only run again when the image is replaced
58
+ new_digest = digest(path)
59
+ @mutex.synchronize { @cache[relative(path)] = new_digest }
60
+ rescue StandardError => e
61
+ Jekyll.logger.warn LOG_TOPIC, "Could not optimize #{relative(path)}: #{e.message}"
62
+ end
23
63
 
24
- File.open(@config["cache_file"], "w") { |file| file.write @last_update.to_yaml }
64
+ def digest(path)
65
+ Digest::SHA256.file(path).hexdigest
25
66
  end
26
67
 
27
- def default_options
28
- {
29
- "cache_file" => "_compress_images_cache.yml",
30
- "images_path" => "assets/img/**/*.{gif,png,jpg,jpeg,svg}",
31
- }
68
+ def relative(path)
69
+ Pathname.new(path).relative_path_from(Pathname.new(@site.source)).to_s
32
70
  end
33
71
 
34
- def analyze(image)
35
- if @last_update.has_key? image
36
- optimize image if @last_update[image] != File.basename(image)
37
- else
38
- optimize image
39
- end
72
+ def load_cache
73
+ return {} unless File.file?(@cache_path)
74
+
75
+ cache = YAML.safe_load(File.read(@cache_path))
76
+ cache.is_a?(Hash) ? cache : {}
77
+ rescue Psych::Exception
78
+ {}
40
79
  end
41
80
 
42
- def optimize(image)
43
- puts "Optimizing #{image}".green
44
- @image_optim.optimize_image! image
45
- @last_update[image] = File.basename image
81
+ # Only write when something changed, so `jekyll serve` doesn't pick up
82
+ # a modified file and rebuild again
83
+ def save_cache
84
+ return if @cache == @original_cache
85
+
86
+ File.write(@cache_path, @cache.sort.to_h.to_yaml)
46
87
  end
47
88
  end
48
89
  end
metadata CHANGED
@@ -1,57 +1,122 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-compress-images
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.2'
4
+ version: '1.3'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Valerija Spasojevic
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-09 00:00:00.000000000 Z
11
+ date: 2026-09-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: image_optim
14
+ name: jekyll
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
19
+ version: '3.8'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '5'
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - ">="
25
28
  - !ruby/object:Gem::Version
26
- version: '0'
29
+ version: '3.8'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '5'
33
+ - !ruby/object:Gem::Dependency
34
+ name: image_optim
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '0.31'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: '0.31'
27
47
  - !ruby/object:Gem::Dependency
28
48
  name: image_optim_pack
29
49
  requirement: !ruby/object:Gem::Requirement
30
50
  requirements:
31
- - - ">="
51
+ - - "~>"
32
52
  - !ruby/object:Gem::Version
33
- version: '0'
53
+ version: '0.10'
34
54
  type: :runtime
35
55
  prerelease: false
36
56
  version_requirements: !ruby/object:Gem::Requirement
37
57
  requirements:
38
- - - ">="
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '0.10'
61
+ - !ruby/object:Gem::Dependency
62
+ name: in_threads
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
39
66
  - !ruby/object:Gem::Version
40
- version: '0'
41
- description: 'Plugin for compress/optimize images (jpg, png, gif, svg). If you are
42
- struggle how to install more information you can find here: https://github.com/valerijaspasojevic/jekyll-compress-images'
67
+ version: '1.5'
68
+ type: :runtime
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.5'
75
+ - !ruby/object:Gem::Dependency
76
+ name: minitest
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '5.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '5.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rake
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '13.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '13.0'
103
+ description: 'Plugin for compressing/optimizing images (jpg, png, gif, svg). If you''re
104
+ struggling with installation, you can find more information here: https://github.com/valerijaspasojevic/jekyll-compress-images'
43
105
  email: spasojevic.valerija@gmail.com
44
106
  executables: []
45
107
  extensions: []
46
108
  extra_rdoc_files: []
47
109
  files:
110
+ - LICENSE
111
+ - README.md
48
112
  - lib/jekyll-compress-images.rb
49
- homepage: https://rubygems.org/gems/example
113
+ homepage: https://github.com/valerijaspasojevic/jekyll-compress-images
50
114
  licenses:
51
115
  - MIT
52
116
  metadata:
53
117
  source_code_uri: https://github.com/valerijaspasojevic/jekyll-compress-images
54
- post_install_message:
118
+ changelog_uri: https://github.com/valerijaspasojevic/jekyll-compress-images/blob/master/CHANGELOG.md
119
+ post_install_message:
55
120
  rdoc_options: []
56
121
  require_paths:
57
122
  - lib
@@ -59,16 +124,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
124
  requirements:
60
125
  - - ">="
61
126
  - !ruby/object:Gem::Version
62
- version: '0'
127
+ version: '2.7'
63
128
  required_rubygems_version: !ruby/object:Gem::Requirement
64
129
  requirements:
65
130
  - - ">="
66
131
  - !ruby/object:Gem::Version
67
132
  version: '0'
68
133
  requirements: []
69
- rubyforge_project:
70
- rubygems_version: 2.7.2
71
- signing_key:
134
+ rubygems_version: 3.4.15
135
+ signing_key:
72
136
  specification_version: 4
73
137
  summary: Jekyll plugin for compress/optimize images (jpg, png, gif, svg)
74
138
  test_files: []