jekyll-auto-thumbnails 2.2.0 → 2.2.2
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 +4 -4
- data/CHANGELOG.md +17 -0
- data/README.md +4 -1
- data/lib/jekyll-auto-thumbnails/hooks.rb +67 -13
- data/lib/jekyll-auto-thumbnails/scanner.rb +39 -7
- data/lib/jekyll-auto-thumbnails/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 546393752a2bbd53df36203bf7868325fb22b4f4bf67b9dfd607ceeba1e065ba
|
|
4
|
+
data.tar.gz: 256c67cec473e8142db69613658dd6b5ae527d1b8093fe5677b80cea9ac021a2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fbe3f35ac23fce623905aa5edf3d47c301677da85c38915d4dd8e2e46f0d35a750e15c47ae71cdfe405023c044bfca4c6848d6a1acdd96228cecbb5422d57d31
|
|
7
|
+
data.tar.gz: b8d9c4d046f6a75bc6ccb5484ad53971730cd2b681b223fb94dff31334090f9c52df5da7cbefab9ae48fb749f87f079e7b25152b43b74ea6db7b7273140513f6
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [2.2.2](https://github.com/Texarkanine/jekyll-auto-thumbnails/compare/v2.2.1...v2.2.2) (2026-11-28)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Bug Fixes
|
|
7
|
+
|
|
8
|
+
* keep article image ratio when the column shrinks ([#74](https://github.com/Texarkanine/jekyll-auto-thumbnails/issues/74)) ([e30d5bc](https://github.com/Texarkanine/jekyll-auto-thumbnails/commit/e30d5bc24b65bd5d8ddc06c9b939cbbdda6eeca4))
|
|
9
|
+
|
|
10
|
+
## [2.2.1](https://github.com/Texarkanine/jekyll-auto-thumbnails/compare/v2.2.0...v2.2.1) (2026-11-23)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
### Bug Fixes
|
|
14
|
+
|
|
15
|
+
* **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))
|
|
16
|
+
* **deps:** bump nokogiri ([bc4c2a2](https://github.com/Texarkanine/jekyll-auto-thumbnails/commit/bc4c2a2bff998762f33c72b4909f65bbacb51238))
|
|
17
|
+
* **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))
|
|
18
|
+
* 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))
|
|
19
|
+
|
|
3
20
|
## [2.2.0](https://github.com/Texarkanine/jekyll-auto-thumbnails/compare/v2.1.0...v2.2.0) (2026-08-13)
|
|
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. **
|
|
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. A changed page also gets `<style>@layer{:where(article img){height:auto}}</style>` in `<head>`, because the `width` and `height` attributes also set a CSS height, and `height: auto` keeps a theme with `max-width: 100%` from stretching the picture.
|
|
73
73
|
5. **File Copying**: Copies thumbnails to `_site/` after build
|
|
74
74
|
|
|
75
75
|
## Usage Examples
|
|
@@ -106,6 +106,9 @@ Works automatically with any Markdown processor, because it checks the rendered
|
|
|
106
106
|
 <!-- Auto-detects size -->
|
|
107
107
|
```
|
|
108
108
|
|
|
109
|
+
## Narrow columns
|
|
110
|
+
|
|
111
|
+
The `width` and `height` attributes keep the image's shape, and they also set a CSS height. The style sets `height: auto`, so a theme with `max-width: 100%` does not stretch the picture. Your own CSS `height` still wins, including a height declared in `@layer`. The style is absent when the page is not rewritten, when the page has no `head`, or when the browser ignores `:where()`.
|
|
109
112
|
|
|
110
113
|
## Cache Behavior
|
|
111
114
|
|
|
@@ -5,6 +5,10 @@ require_relative "html_parser"
|
|
|
5
5
|
module JekyllAutoThumbnails
|
|
6
6
|
# Jekyll hook integration
|
|
7
7
|
module Hooks
|
|
8
|
+
# Anonymous layer, declared first. Beats the height attribute.
|
|
9
|
+
# Loses to a later author layer and to unlayered author height.
|
|
10
|
+
ARTICLE_IMAGE_HEIGHT_RULE = "@layer{:where(article img){height:auto}}"
|
|
11
|
+
|
|
8
12
|
# Format a duration for Jekyll log suffixes.
|
|
9
13
|
#
|
|
10
14
|
# @param seconds [Numeric] elapsed seconds
|
|
@@ -49,18 +53,21 @@ module JekyllAutoThumbnails
|
|
|
49
53
|
|
|
50
54
|
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
51
55
|
|
|
52
|
-
# Scan all documents and pages
|
|
56
|
+
# Scan all documents and pages. Measurements are file pixels, not thumbnail requests.
|
|
57
|
+
measurements = {}
|
|
53
58
|
(site.documents + site.pages).each do |doc|
|
|
54
59
|
next unless doc.output
|
|
55
60
|
next unless html_document?(doc)
|
|
56
61
|
|
|
57
|
-
Scanner.scan_html(doc.output, registry, config, site.source)
|
|
62
|
+
measurements.merge!(Scanner.scan_html(doc.output, registry, config, site.source))
|
|
58
63
|
end
|
|
59
64
|
|
|
60
65
|
Jekyll.logger.info "AutoThumbnails:", "Found #{registry.entries.size} images to optimize"
|
|
61
66
|
|
|
62
|
-
# Generate thumbnails
|
|
67
|
+
# Generate thumbnails. A served size is the thumbnail file when src changes,
|
|
68
|
+
# and the measured file when it does not.
|
|
63
69
|
url_map = {}
|
|
70
|
+
served = {}
|
|
64
71
|
registry.entries.each do |url, requirements|
|
|
65
72
|
cached_path = generator.generate(url, requirements[:width], requirements[:height])
|
|
66
73
|
|
|
@@ -75,11 +82,19 @@ module JekyllAutoThumbnails
|
|
|
75
82
|
"#{url_dir}/#{thumb_filename}"
|
|
76
83
|
end
|
|
77
84
|
url_map[url] = thumb_url
|
|
85
|
+
width, height = Scanner.image_dimensions(cached_path)
|
|
86
|
+
served[url] = { width: width, height: height } if width && height
|
|
78
87
|
else
|
|
79
88
|
Jekyll.logger.warn "AutoThumbnails:", "Failed to generate thumbnail for #{url}"
|
|
80
89
|
end
|
|
81
90
|
end
|
|
82
91
|
|
|
92
|
+
measurements.each do |url, dims|
|
|
93
|
+
next if url_map.key?(url)
|
|
94
|
+
|
|
95
|
+
served[url] = dims
|
|
96
|
+
end
|
|
97
|
+
|
|
83
98
|
# Store url_map for post_write hook
|
|
84
99
|
site.data["auto_thumbnails_url_map"] = url_map
|
|
85
100
|
|
|
@@ -88,7 +103,7 @@ module JekyllAutoThumbnails
|
|
|
88
103
|
next unless doc.output
|
|
89
104
|
next unless html_document?(doc)
|
|
90
105
|
|
|
91
|
-
doc.output = replace_urls(doc.output, url_map, parser: config.parser)
|
|
106
|
+
doc.output = replace_urls(doc.output, url_map, parser: config.parser, sizes: served)
|
|
92
107
|
end
|
|
93
108
|
|
|
94
109
|
elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started
|
|
@@ -127,8 +142,9 @@ module JekyllAutoThumbnails
|
|
|
127
142
|
|
|
128
143
|
# Replace image URLs in HTML.
|
|
129
144
|
#
|
|
130
|
-
# Returns the input string unchanged (object identity) when no
|
|
131
|
-
#
|
|
145
|
+
# Returns the input string unchanged (object identity) when no article image
|
|
146
|
+
# changes. A change is a new thumbnail `src`, or a `width` and `height`
|
|
147
|
+
# taken from `sizes`. This is both a perf win for pages with no matching
|
|
132
148
|
# images and a correctness win: the HTML4 path otherwise round-trips every
|
|
133
149
|
# page through libxml2's serializer, which injects a spurious
|
|
134
150
|
# `<meta http-equiv="Content-Type">` on HTML5 sites.
|
|
@@ -136,9 +152,10 @@ module JekyllAutoThumbnails
|
|
|
136
152
|
# @param html [String] HTML content
|
|
137
153
|
# @param url_map [Hash] original URL => thumbnail URL
|
|
138
154
|
# @param parser [Symbol] :html5 (default) or :html4
|
|
155
|
+
# @param sizes [Hash] original URL => {width:, height:} for the file the browser will load
|
|
139
156
|
# @return [String] modified HTML (or the input itself, unchanged)
|
|
140
|
-
def self.replace_urls(html, url_map, parser: :html5)
|
|
141
|
-
return html if url_map.empty?
|
|
157
|
+
def self.replace_urls(html, url_map, parser: :html5, sizes: {})
|
|
158
|
+
return html if url_map.empty? && sizes.empty?
|
|
142
159
|
return html unless html.match?(/<img/i)
|
|
143
160
|
|
|
144
161
|
doc = HtmlParser.parse(html, parser)
|
|
@@ -146,15 +163,52 @@ module JekyllAutoThumbnails
|
|
|
146
163
|
modified = false
|
|
147
164
|
doc.css("article img").each do |img|
|
|
148
165
|
src = img["src"]
|
|
149
|
-
|
|
150
|
-
next unless thumb_url
|
|
166
|
+
next unless src
|
|
151
167
|
|
|
152
|
-
img["src"]
|
|
153
|
-
|
|
168
|
+
if (thumb_url = url_map[src]) && img["src"] != thumb_url
|
|
169
|
+
img["src"] = thumb_url
|
|
170
|
+
modified = true
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
size = sizes[src]
|
|
174
|
+
next unless size && size[:width] && size[:height]
|
|
175
|
+
|
|
176
|
+
width = size[:width].to_s
|
|
177
|
+
height = size[:height].to_s
|
|
178
|
+
if img["width"] != width
|
|
179
|
+
img["width"] = width
|
|
180
|
+
modified = true
|
|
181
|
+
end
|
|
182
|
+
if img["height"] != height
|
|
183
|
+
img["height"] = height
|
|
184
|
+
modified = true
|
|
185
|
+
end
|
|
154
186
|
end
|
|
155
187
|
|
|
156
|
-
|
|
188
|
+
return html unless modified
|
|
189
|
+
|
|
190
|
+
release_article_image_height(doc)
|
|
191
|
+
doc.to_html
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
def self.release_article_image_height(doc)
|
|
195
|
+
return if article_image_height_rule?(doc)
|
|
196
|
+
|
|
197
|
+
head = doc.at_css("head")
|
|
198
|
+
return unless head
|
|
199
|
+
|
|
200
|
+
style = doc.create_element("style")
|
|
201
|
+
style.content = ARTICLE_IMAGE_HEIGHT_RULE
|
|
202
|
+
head.prepend_child(style)
|
|
203
|
+
end
|
|
204
|
+
|
|
205
|
+
def self.article_image_height_rule?(doc)
|
|
206
|
+
needle = ARTICLE_IMAGE_HEIGHT_RULE.gsub(/\s+/, "")
|
|
207
|
+
doc.css("style").any? do |node|
|
|
208
|
+
node.content.gsub(/\s+/, "").include?(needle)
|
|
209
|
+
end
|
|
157
210
|
end
|
|
211
|
+
private_class_method :release_article_image_height, :article_image_height_rule?
|
|
158
212
|
|
|
159
213
|
# Check if a document outputs HTML (not CSS, JS, etc.)
|
|
160
214
|
#
|
|
@@ -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
|
-
|
|
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
|
-
|
|
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
|