jekyll-auto-thumbnails 2.1.0 → 2.2.1

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
  SHA256:
3
- metadata.gz: 3f0fec7cfa3ac45d704dcce4f97edc9e71311df06174f99dd24c3231d1675589
4
- data.tar.gz: edc7920b2ff080b42728ead47fee4fcec02d9892979156127c85fba3661844d2
3
+ metadata.gz: 9e9c5ede1e1900fe3074040b44f1566cd3b697068f2e580d4a1087896692533c
4
+ data.tar.gz: 4514e267830f68e4b881fdf0bfffbcdd882e8211d199d6e702b4314750159d3f
5
5
  SHA512:
6
- metadata.gz: 06b2ed4d1aa33f8f4a978e7679688daf25ffdc31fd4d0f8ca3f09eb79aa121920ee76a017750b5e2eda9d5ed5d5970e3106fd7dc1620e8e818960c012227d2f9
7
- data.tar.gz: 837a87b1c64202fe40cd163081e9c03ee36c9289796b6c3bd90a9694e42d19c8408ce666401860c729cf5b942512ad5b2695f595b15f57c2530a2002c70906e2
6
+ metadata.gz: 1313ca36c9f012d43a1e26b17308607293ac4a536f22f4186f3f8690934b30cdb3c5549db96ce27549c55062ba090744d770828ff62f235bb2ef804b20192b25
7
+ data.tar.gz: ad178e312f32bc8511a2a42dba2b5fbd31bd96e24acc8ffe2e37fb6ad3dbabba189aaca0efaaddcdd3b91726dcc17cfe95c6284d28854e9377cb91c72ab1afac
data/CHANGELOG.md CHANGED
@@ -1,5 +1,22 @@
1
1
  # Changelog
2
2
 
3
+ ## [2.2.1](https://github.com/Texarkanine/jekyll-auto-thumbnails/compare/v2.2.0...v2.2.1) (2026-11-23)
4
+
5
+
6
+ ### Bug Fixes
7
+
8
+ * **ci:** let the lockfile job commit inside the Ruby image ([#73](https://github.com/Texarkanine/jekyll-auto-thumbnails/issues/73)) ([7464ef4](https://github.com/Texarkanine/jekyll-auto-thumbnails/commit/7464ef4937ce231de4f1ffa8fad162291ef10212))
9
+ * **deps:** bump nokogiri ([bc4c2a2](https://github.com/Texarkanine/jekyll-auto-thumbnails/commit/bc4c2a2bff998762f33c72b4909f65bbacb51238))
10
+ * **deps:** bump nokogiri from 1.19.4 to 1.19.7 in the production-deps group ([#68](https://github.com/Texarkanine/jekyll-auto-thumbnails/issues/68)) ([bc4c2a2](https://github.com/Texarkanine/jekyll-auto-thumbnails/commit/bc4c2a2bff998762f33c72b4909f65bbacb51238))
11
+ * write width and height on measured article images ([#71](https://github.com/Texarkanine/jekyll-auto-thumbnails/issues/71)) ([27d29c0](https://github.com/Texarkanine/jekyll-auto-thumbnails/commit/27d29c07d893f90cf30f3ce2d09f6c3f2d22368b))
12
+
13
+ ## [2.2.0](https://github.com/Texarkanine/jekyll-auto-thumbnails/compare/v2.1.0...v2.2.0) (2026-08-13)
14
+
15
+
16
+ ### Features
17
+
18
+ * **hooks:** append elapsed time to completion logs ([#54](https://github.com/Texarkanine/jekyll-auto-thumbnails/issues/54)) ([4bb9c6f](https://github.com/Texarkanine/jekyll-auto-thumbnails/commit/4bb9c6fcb9a83802e503d21d108017016c00c543))
19
+
3
20
  ## [2.1.0](https://github.com/Texarkanine/jekyll-auto-thumbnails/compare/v2.0.1...v2.1.0) (2026-07-19)
4
21
 
5
22
 
data/README.md CHANGED
@@ -69,7 +69,7 @@ Pages that contain no replaceable `<article><img>` are no longer re-serialized a
69
69
  - Images with `width` or `height` attributes → uses those dimensions
70
70
  - Unsized images exceeding max config → thumbnails to max dimensions
71
71
  3. **Generation**: Creates thumbnails with MD5-based caching in `.jekyll-cache/`
72
- 4. **URL Replacement**: Updates `<img src>` to point to thumbnails
72
+ 4. **Rewrite**: Points `<img src>` at a thumbnail when one was made, and writes `width` and `height` for the file the browser will load. A measured image whose `src` does not change still gets both numbers.
73
73
  5. **File Copying**: Copies thumbnails to `_site/` after build
74
74
 
75
75
  ## Usage Examples
@@ -5,6 +5,18 @@ require_relative "html_parser"
5
5
  module JekyllAutoThumbnails
6
6
  # Jekyll hook integration
7
7
  module Hooks
8
+ # Format a duration for Jekyll log suffixes.
9
+ #
10
+ # @param seconds [Numeric] elapsed seconds
11
+ # @return [String] `in Xs` under a minute, otherwise `in X m Y s`
12
+ def self.format_elapsed(seconds)
13
+ total = seconds.round
14
+ return "in #{total}s" if total < 60
15
+
16
+ minutes, remainder = total.divmod(60)
17
+ "in #{minutes} m #{remainder} s"
18
+ end
19
+
8
20
  # Initialize optimization system
9
21
  #
10
22
  # @param site [Jekyll::Site] Jekyll site
@@ -35,18 +47,23 @@ module JekyllAutoThumbnails
35
47
  return
36
48
  end
37
49
 
38
- # Scan all documents and pages
50
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
51
+
52
+ # Scan all documents and pages. Measurements are file pixels, not thumbnail requests.
53
+ measurements = {}
39
54
  (site.documents + site.pages).each do |doc|
40
55
  next unless doc.output
41
56
  next unless html_document?(doc)
42
57
 
43
- Scanner.scan_html(doc.output, registry, config, site.source)
58
+ measurements.merge!(Scanner.scan_html(doc.output, registry, config, site.source))
44
59
  end
45
60
 
46
61
  Jekyll.logger.info "AutoThumbnails:", "Found #{registry.entries.size} images to optimize"
47
62
 
48
- # Generate thumbnails
63
+ # Generate thumbnails. A served size is the thumbnail file when src changes,
64
+ # and the measured file when it does not.
49
65
  url_map = {}
66
+ served = {}
50
67
  registry.entries.each do |url, requirements|
51
68
  cached_path = generator.generate(url, requirements[:width], requirements[:height])
52
69
 
@@ -61,11 +78,19 @@ module JekyllAutoThumbnails
61
78
  "#{url_dir}/#{thumb_filename}"
62
79
  end
63
80
  url_map[url] = thumb_url
81
+ width, height = Scanner.image_dimensions(cached_path)
82
+ served[url] = { width: width, height: height } if width && height
64
83
  else
65
84
  Jekyll.logger.warn "AutoThumbnails:", "Failed to generate thumbnail for #{url}"
66
85
  end
67
86
  end
68
87
 
88
+ measurements.each do |url, dims|
89
+ next if url_map.key?(url)
90
+
91
+ served[url] = dims
92
+ end
93
+
69
94
  # Store url_map for post_write hook
70
95
  site.data["auto_thumbnails_url_map"] = url_map
71
96
 
@@ -74,10 +99,11 @@ module JekyllAutoThumbnails
74
99
  next unless doc.output
75
100
  next unless html_document?(doc)
76
101
 
77
- doc.output = replace_urls(doc.output, url_map, parser: config.parser)
102
+ doc.output = replace_urls(doc.output, url_map, parser: config.parser, sizes: served)
78
103
  end
79
104
 
80
- Jekyll.logger.info "AutoThumbnails:", "Generated #{url_map.size} thumbnails"
105
+ elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
106
+ Jekyll.logger.info "AutoThumbnails:", "Generated #{url_map.size} thumbnails #{format_elapsed(elapsed)}"
81
107
  end
82
108
 
83
109
  # Copy thumbnails from cache to _site
@@ -92,6 +118,8 @@ module JekyllAutoThumbnails
92
118
 
93
119
  Jekyll.logger.info "AutoThumbnails:", "Copying #{url_map.size} thumbnails to _site"
94
120
 
121
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
122
+
95
123
  url_map.each_value do |thumb_url|
96
124
  thumb_filename = File.basename(thumb_url)
97
125
  cached_path = File.join(config.cache_dir, thumb_filename)
@@ -104,13 +132,15 @@ module JekyllAutoThumbnails
104
132
  FileUtils.cp(cached_path, dest_path)
105
133
  end
106
134
 
107
- Jekyll.logger.info "AutoThumbnails:", "All thumbnails copied"
135
+ elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
136
+ Jekyll.logger.info "AutoThumbnails:", "All thumbnails copied #{format_elapsed(elapsed)}"
108
137
  end
109
138
 
110
139
  # Replace image URLs in HTML.
111
140
  #
112
- # Returns the input string unchanged (object identity) when no replacement
113
- # was actually made. This is both a perf win for pages with no matching
141
+ # Returns the input string unchanged (object identity) when no article image
142
+ # changes. A change is a new thumbnail `src`, or a `width` and `height`
143
+ # taken from `sizes`. This is both a perf win for pages with no matching
114
144
  # images and a correctness win: the HTML4 path otherwise round-trips every
115
145
  # page through libxml2's serializer, which injects a spurious
116
146
  # `<meta http-equiv="Content-Type">` on HTML5 sites.
@@ -118,9 +148,10 @@ module JekyllAutoThumbnails
118
148
  # @param html [String] HTML content
119
149
  # @param url_map [Hash] original URL => thumbnail URL
120
150
  # @param parser [Symbol] :html5 (default) or :html4
151
+ # @param sizes [Hash] original URL => {width:, height:} for the file the browser will load
121
152
  # @return [String] modified HTML (or the input itself, unchanged)
122
- def self.replace_urls(html, url_map, parser: :html5)
123
- return html if url_map.empty?
153
+ def self.replace_urls(html, url_map, parser: :html5, sizes: {})
154
+ return html if url_map.empty? && sizes.empty?
124
155
  return html unless html.match?(/<img/i)
125
156
 
126
157
  doc = HtmlParser.parse(html, parser)
@@ -128,11 +159,26 @@ module JekyllAutoThumbnails
128
159
  modified = false
129
160
  doc.css("article img").each do |img|
130
161
  src = img["src"]
131
- thumb_url = url_map[src]
132
- next unless thumb_url
162
+ next unless src
163
+
164
+ if (thumb_url = url_map[src]) && img["src"] != thumb_url
165
+ img["src"] = thumb_url
166
+ modified = true
167
+ end
133
168
 
134
- img["src"] = thumb_url
135
- modified = true
169
+ size = sizes[src]
170
+ next unless size && size[:width] && size[:height]
171
+
172
+ width = size[:width].to_s
173
+ height = size[:height].to_s
174
+ if img["width"] != width
175
+ img["width"] = width
176
+ modified = true
177
+ end
178
+ if img["height"] != height
179
+ img["height"] = height
180
+ modified = true
181
+ end
136
182
  end
137
183
 
138
184
  modified ? doc.to_html : html
@@ -14,7 +14,9 @@ module JekyllAutoThumbnails
14
14
  # @param registry [Registry] image registry
15
15
  # @param config [Configuration] configuration
16
16
  # @param site_source [String] Jekyll site source (optional, for unsized image checking)
17
+ # @return [Hash] url => {width:, height:} for each local article image whose file was read
17
18
  def self.scan_html(html, registry, config, site_source = nil)
19
+ measurements = {}
18
20
  doc = HtmlParser.parse(html, config.parser)
19
21
 
20
22
  # Only process images in <article> tags
@@ -29,17 +31,21 @@ module JekyllAutoThumbnails
29
31
 
30
32
  if width || height
31
33
  # Only identify for aspect-ratio fill when a dimension is missing
32
- width, height = calculate_dimensions(src, width, height, site_source) if site_source && (!width || !height)
34
+ if site_source && (!width || !height)
35
+ width, height = calculate_dimensions(src, width, height, site_source, measurements: measurements)
36
+ end
33
37
 
34
38
  # Skip if dimensions match original (no thumbnail needed)
35
- next if site_source && dimensions_match_original?(src, width, height, site_source)
39
+ next if site_source && dimensions_match_original?(src, width, height, site_source, measurements: measurements)
36
40
 
37
41
  registry.register(src, width, height)
38
42
  elsif site_source && (config.max_width || config.max_height)
39
43
  # Unsized but max config exists - check actual dimensions
40
- check_and_register_oversized(src, registry, config, site_source)
44
+ check_and_register_oversized(src, registry, config, site_source, measurements: measurements)
41
45
  end
42
46
  end
47
+
48
+ measurements
43
49
  end
44
50
 
45
51
  # Check if image exceeds max dimensions and register if so
@@ -48,13 +54,16 @@ module JekyllAutoThumbnails
48
54
  # @param registry [Registry] image registry
49
55
  # @param config [Configuration] configuration
50
56
  # @param site_source [String] site source directory
51
- def self.check_and_register_oversized(url, registry, config, site_source)
57
+ # @param measurements [Hash, nil] url => {width:, height:} of files read during this scan
58
+ def self.check_and_register_oversized(url, registry, config, site_source, measurements: nil)
52
59
  file_path = UrlResolver.to_filesystem_path(url, site_source)
53
60
  return unless file_path && File.exist?(file_path)
54
61
 
55
62
  actual_width, actual_height = image_dimensions(file_path)
56
63
  return unless actual_height
57
64
 
65
+ record_measurement(measurements, url, actual_width, actual_height)
66
+
58
67
  # Check if exceeds max dimensions
59
68
  exceeds_width = config.max_width && actual_width > config.max_width
60
69
  exceeds_height = config.max_height && actual_height > config.max_height
@@ -88,14 +97,17 @@ module JekyllAutoThumbnails
88
97
  # @param width [Integer, nil] specified width
89
98
  # @param height [Integer, nil] specified height
90
99
  # @param site_source [String] site source directory
100
+ # @param measurements [Hash, nil] url => {width:, height:} of files read during this scan
91
101
  # @return [Array<Integer, Integer>] [width, height] with calculated dimension
92
- def self.calculate_dimensions(url, width, height, site_source)
102
+ def self.calculate_dimensions(url, width, height, site_source, measurements: nil)
93
103
  file_path = UrlResolver.to_filesystem_path(url, site_source)
94
104
  return [width, height] unless file_path && File.exist?(file_path)
95
105
 
96
106
  actual_width, actual_height = image_dimensions(file_path)
97
107
  return [width, height] unless actual_height
98
108
 
109
+ record_measurement(measurements, url, actual_width, actual_height)
110
+
99
111
  # Calculate missing dimension preserving aspect ratio
100
112
  if width && !height
101
113
  # Width specified, calculate height
@@ -122,20 +134,40 @@ module JekyllAutoThumbnails
122
134
  Integer(numeric)
123
135
  end
124
136
 
125
- # Check if requested dimensions match original dimensions
137
+ # Check if requested dimensions match original dimensions.
138
+ # A pair already recorded for this URL is that file, so the match uses it.
126
139
  #
127
140
  # @param url [String] image URL
128
141
  # @param width [Integer] requested width
129
142
  # @param height [Integer] requested height
130
143
  # @param site_source [String] site source directory
144
+ # @param measurements [Hash, nil] url => {width:, height:} of files read during this scan
131
145
  # @return [Boolean] true if dimensions match original
132
- def self.dimensions_match_original?(url, width, height, site_source)
146
+ def self.dimensions_match_original?(url, width, height, site_source, measurements: nil)
147
+ if (known = measurements&.[](url))
148
+ return width == known[:width] && height == known[:height]
149
+ end
150
+
133
151
  file_path = UrlResolver.to_filesystem_path(url, site_source)
134
152
  return false unless file_path && File.exist?(file_path)
135
153
 
136
154
  actual_width, actual_height = image_dimensions(file_path)
155
+ record_measurement(measurements, url, actual_width, actual_height)
137
156
 
138
157
  width == actual_width && height == actual_height
139
158
  end
159
+
160
+ # Remember a file's pixels. Direct callers omit the hash, and a failed read has no pair.
161
+ #
162
+ # @param measurements [Hash, nil]
163
+ # @param url [String]
164
+ # @param width [Integer, nil]
165
+ # @param height [Integer, nil]
166
+ def self.record_measurement(measurements, url, width, height)
167
+ return unless measurements && width && height
168
+
169
+ measurements[url] = { width: width, height: height }
170
+ end
171
+ private_class_method :record_measurement
140
172
  end
141
173
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module JekyllAutoThumbnails
4
- VERSION = "2.1.0"
4
+ VERSION = "2.2.1"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-auto-thumbnails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Texarkanine