jekyll-imgwh 1.3.0 → 1.4.0
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/README.md +85 -45
- data/lib/jekyll/imgwh/helpers.rb +12 -9
- data/lib/jekyll/imgwh/tag.rb +22 -16
- data/lib/jekyll/imgwh/version.rb +1 -2
- data/lib/jekyll-imgwh.rb +3 -2
- metadata +7 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c07b034a8e148f443f9a0da9950592f0425786509651fb7f67ab5bf6bae1c076
|
4
|
+
data.tar.gz: d87846a205f7259100a63a3153a285105958e0903345e3c926fe7a4a0c7aaa9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ea444a44d2d81ae1b695ae405a7017c8f17800131229f84658f6fa7eab9efcb65fb5ee9cd4063c32a4a1ab0e4446de5ddde625515cb82766989e6ac4cc103339
|
7
|
+
data.tar.gz: 28ae016af67d07656650d1dbe293f652c025eef1ebb7e8e3e02f825a4447132aeeb6a54b41eeb39eafaf1def3c7d443d7f471e5f30ad0ae54c410a69c5a2a961
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
[](https://badge.fury.io/rb/jekyll-imgwh)
|
1
|
+
# jekyll-imgwh [](https://badge.fury.io/rb/jekyll-imgwh)
|
2
2
|
|
3
3
|
A [Jekyll](https://jekyllrb.com/) plugin to simplify maintenance of the images on the site.
|
4
4
|
|
@@ -6,7 +6,7 @@ It provides a [Liquid tag `imgwh`](#liquid-tag) for `<img>` elements, which ensu
|
|
6
6
|
|
7
7
|
It also provides a [Liquid filter `imgwh`](#liquid-filter), which returns image size as an array.
|
8
8
|
|
9
|
-
|
9
|
+
## Installation
|
10
10
|
|
11
11
|
Add preferred variant from the following ones to your site's `Gemfile` and run `bundle install`:
|
12
12
|
|
@@ -16,9 +16,9 @@ gem "jekyll-imgwh", group: :jekyll_plugins, git: "https://github.com/ojuuji/jeky
|
|
16
16
|
gem "jekyll-imgwh", group: :jekyll_plugins, path: "/local/path/to/jekyll-imgwh"
|
17
17
|
```
|
18
18
|
|
19
|
-
|
19
|
+
## Usage
|
20
20
|
|
21
|
-
|
21
|
+
### Liquid Tag
|
22
22
|
|
23
23
|
This plugin exposes Liquid tag `imgwh` with the following syntax:
|
24
24
|
|
@@ -43,39 +43,49 @@ Example:
|
|
43
43
|
with `site.title="My Site"` and image size 200x67 it would generate the following HTML `<img>` element:
|
44
44
|
|
45
45
|
```html
|
46
|
-
<img src="/assets/my-site.png" width="200" height="67" alt="My Site">
|
46
|
+
<img src="/assets/my-site.png" width="200" height="67" loading="lazy" alt="My Site">
|
47
47
|
```
|
48
48
|
|
49
|
-
|
49
|
+
#### Quotes and Whitespace
|
50
50
|
|
51
|
-
`<src>` can be specified with single quotes, double quotes, or without quotes. This also defines quotation for
|
51
|
+
`<src>` can be specified with single quotes, double quotes, or without quotes. This also defines quotation for auto generated attributes: they always use the same quotes as `<src>`:
|
52
52
|
|
53
|
-
```
|
54
|
-
{% imgwh "/foo.png" %}
|
55
|
-
{% imgwh '/foo.png' %}
|
56
|
-
{% imgwh /foo.png %}
|
53
|
+
```liquid
|
54
|
+
{% imgwh "/foo.png" %} ⟶ <img src="/foo.png" width="123" height="456" loading="lazy">
|
55
|
+
{% imgwh '/foo.png' %} ⟶ <img src='/foo.png' width='123' height='456' loading='lazy'>
|
56
|
+
{% imgwh /foo.png %} ⟶ <img src=/foo.png width=123 height=456 loading=lazy>
|
57
57
|
```
|
58
58
|
|
59
59
|
Whitespace can be freely used in single- and double-quoted `<src>`. To use the same quote character in the `<src>` value specify it twice:
|
60
60
|
|
61
|
-
```
|
62
|
-
{% imgwh "/f{{ 'oo' | append: "".png"" }}" %}
|
63
|
-
{% imgwh "/f{{ 'oo' | append: ".png" }}" %}
|
64
|
-
{% imgwh '/f{{ 'oo' | append: ".png" }}' %}
|
65
|
-
{% imgwh '/f{{ ''oo'' | append: ".png" }}' %}
|
61
|
+
```liquid
|
62
|
+
{% imgwh "/f{{ 'oo' | append: "".png"" }}" %} ⟶ OK (src="/foo.png")
|
63
|
+
{% imgwh "/f{{ 'oo' | append: ".png" }}" %} ⟶ ERROR
|
64
|
+
{% imgwh '/f{{ 'oo' | append: ".png" }}' %} ⟶ ERROR
|
65
|
+
{% imgwh '/f{{ ''oo'' | append: ".png" }}' %} ⟶ OK (src='/foo.png')
|
66
66
|
```
|
67
67
|
|
68
68
|
For unquoted `<src>` whitespace is allowed only within Liquid filters (i.e. between `{{` and `}}`):
|
69
69
|
|
70
|
-
```
|
71
|
-
{% imgwh /f{{ 'oo' | append: ".png" }} %}
|
72
|
-
{% imgwh
|
73
|
-
{% imgwh
|
70
|
+
```liquid
|
71
|
+
{% imgwh /f{{ 'oo' | append: ".png" }} %} ⟶ OK (src=/foo.png)
|
72
|
+
{% imgwh /My Site.png %} ⟶ ERROR (tries to open "/My" image)
|
73
|
+
{% imgwh /{{ site.title }}.png %} ⟶ OK (src=/My Site.png)
|
74
74
|
```
|
75
75
|
|
76
76
|
Note, in the last example, although plugin did not fire an error, generated `src` attribute is not valid (`<img>` element would use `src=/My`). After rendering Liquid markup in the `<src>` value, plugin does not perform any further normalization for the resulting URI. It is up to the caller to provide correct URI. Plugin only extracts and URL-decodes the path from it.
|
77
77
|
|
78
|
-
|
78
|
+
When rendering markup for `imgwh` tag, plugin adds `imgwh_quote` variable to the rendering context. This variable contains the quote character used in the `imgwh` tag currently being evaluated. Note, it is available only in the markup used inside `imgwh` tag.
|
79
|
+
|
80
|
+
Examples:
|
81
|
+
|
82
|
+
```liquid
|
83
|
+
{% imgwh "/foo.png" bar={{imgwh_quote}}baz{{imgwh_quote}} %} ⟶ <img src="/foo.png" ... bar="baz">
|
84
|
+
{% imgwh '/foo.png' bar={{imgwh_quote}}baz{{imgwh_quote}} %} ⟶ <img src='/foo.png' ... bar='baz'>
|
85
|
+
{% imgwh /foo.png bar={{imgwh_quote}}baz{{imgwh_quote}} %} ⟶ <img src=/foo.png ... bar=baz>
|
86
|
+
```
|
87
|
+
|
88
|
+
### Liquid Filter
|
79
89
|
|
80
90
|
This plugin exposes a Liquid filter `imgwh`, which returns image size as an array.
|
81
91
|
|
@@ -101,7 +111,7 @@ would render to
|
|
101
111
|
</pre>
|
102
112
|
```
|
103
113
|
|
104
|
-
|
114
|
+
### Path Resolution
|
105
115
|
|
106
116
|
When the given URI contains scheme, plugin raises an error unless this scheme is listed in [`allowed_schemes`](#allowed_schemes) option (which is empty by default). In case of allowed scheme plugin tries to retrieve image size using the given URI as-is.
|
107
117
|
|
@@ -113,18 +123,20 @@ When the image path is relative, image is searched relative to the directory of
|
|
113
123
|
|
114
124
|
When the image is not found, and a theme is used, and the path is absolute, image is also searched relative to the theme root directory.
|
115
125
|
|
116
|
-
|
126
|
+
For the local files, like Jekyll itself, plugin does not allow using files outside the source or theme root directories (in fact, plugin uses the path sanitize helpers from Jekyll). It will not be an immediate error in this case (for example, `"/../foo.png"` i.e. one level above the site root), but the image will be searched then in the source directory and the theme root directory no matter how many levels above the site root it is.
|
127
|
+
|
128
|
+
### Error Handling
|
117
129
|
|
118
130
|
In case plugin cannot determine the image size (due to a syntax error, Liquid template error, image being nonexistent, not an image, etc.) it unconditionally raises an error which stops the site generation.
|
119
131
|
|
120
|
-
|
132
|
+
## Configuration
|
121
133
|
|
122
134
|
This plugin uses the following configuration options by default. The configuration file is the same as Jekyll's (which is `_config.yml` unless overridden):
|
123
135
|
|
124
136
|
```yml
|
125
|
-
|
137
|
+
imgwh:
|
126
138
|
allowed_schemes: []
|
127
|
-
extra_rest:
|
139
|
+
extra_rest: loading={{imgwh_quote}}lazy{{imgwh_quote}}
|
128
140
|
```
|
129
141
|
|
130
142
|
These are default options i.e. you do not need to specify any of them unless you want to use different value.
|
@@ -138,7 +150,7 @@ Option `allowed_schemes` adds exception for the schemes specified in it. For URI
|
|
138
150
|
For example, to allow HTTPS image URLs and [data URLs](https://developer.mozilla.org/en-US/docs/Web/URI/Reference/Schemes/data) use the following:
|
139
151
|
|
140
152
|
```yml
|
141
|
-
|
153
|
+
imgwh:
|
142
154
|
allowed_schemes: ["data", "https"]
|
143
155
|
```
|
144
156
|
|
@@ -150,21 +162,25 @@ Remember `imgwh` tag syntax? This option injects additional text into all genera
|
|
150
162
|
{% imgwh <src> <extra_rest> [<rest>] %}
|
151
163
|
```
|
152
164
|
|
153
|
-
|
165
|
+
Since all generated HTML `<img>` elements get the size attributes, plugin also sets lazy loading for the images via this option.
|
166
|
+
|
167
|
+
If you want to omit any extra attributes, set `extra_rest` to an empty value:
|
154
168
|
|
155
169
|
```yml
|
156
|
-
|
157
|
-
extra_rest:
|
170
|
+
imgwh:
|
171
|
+
extra_rest: ""
|
158
172
|
```
|
159
173
|
|
160
|
-
|
174
|
+
Like `<src>` and `<rest>`, `<extra_rest>` can also include Liquid markup, which is rendered in context of images where `<extra_rest>` is injected.
|
175
|
+
|
176
|
+
## Troubleshooting
|
161
177
|
|
162
178
|
When error is related to the image image file, plugin mentions the file path in the error message:
|
163
179
|
|
164
|
-
```
|
180
|
+
```shell
|
165
181
|
$ bundle exec jekyll serve
|
166
182
|
<...>
|
167
|
-
Liquid Exception:
|
183
|
+
Liquid Exception: imgwh: 'Y:/ssg/assets/images/foo.png' could not be found in index.html
|
168
184
|
<...>
|
169
185
|
```
|
170
186
|
|
@@ -172,27 +188,27 @@ Plugin also logs a lot of info which can help to resolve errors raised by it. Us
|
|
172
188
|
|
173
189
|
For example, for template
|
174
190
|
|
175
|
-
```
|
176
|
-
{% imgwh
|
191
|
+
```shell
|
192
|
+
{% imgwh '/assets/images/{{ product.key }}.png' alt='{{ project.title }} Logo' class='www-logo' %}
|
177
193
|
```
|
178
194
|
|
179
195
|
it would print something like this in case of successful generation:
|
180
196
|
|
181
|
-
```
|
197
|
+
```shell
|
182
198
|
$ bundle exec jekyll serve --verbose
|
183
199
|
<...>
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
200
|
+
imgwh: ---
|
201
|
+
imgwh: markup: "'/assets/images/{{ site.key }}.png' alt='{{ site.title }} Logo' class='www-logo'"
|
202
|
+
imgwh: src: "/assets/images/{{ site.key }}.png"
|
203
|
+
imgwh: rest: "alt='{{ site.title }} Logo' class='www-logo'"
|
204
|
+
imgwh: src rendered: "/assets/images/foo.png"
|
205
|
+
imgwh: image path: 'Y:/ssg/assets/images/foo.png'
|
206
|
+
imgwh: image size: [128, 64]
|
207
|
+
imgwh: rest rendered: "alt='My Product Logo' class='www-logo'"
|
192
208
|
<...>
|
193
209
|
```
|
194
210
|
|
195
|
-
|
211
|
+
## Development
|
196
212
|
|
197
213
|
To get started with the development:
|
198
214
|
|
@@ -202,3 +218,27 @@ cd jekyll-imgwh
|
|
202
218
|
bundle install
|
203
219
|
bundle exec rspec
|
204
220
|
```
|
221
|
+
|
222
|
+
### Using in own code
|
223
|
+
|
224
|
+
Example how to use the path resolving logic from the plugin in your code:
|
225
|
+
|
226
|
+
```ruby
|
227
|
+
require "jekyll/imgwh/helpers"
|
228
|
+
|
229
|
+
module Utils
|
230
|
+
def file_size(src)
|
231
|
+
File.size(resolve_path(src, @context))
|
232
|
+
end
|
233
|
+
|
234
|
+
Liquid::Template.register_filter(self)
|
235
|
+
|
236
|
+
include Jekyll::Imgwh::Helpers
|
237
|
+
end
|
238
|
+
```
|
239
|
+
|
240
|
+
Save it as `_plugins/file_size.rb` in your site (can use other file name but must place in `_plugins` directory unless it is overridden in the site config). Now you can use it like this:
|
241
|
+
|
242
|
+
```liquid
|
243
|
+
{{ "/attachments/some/archive.tar.gz" | file_size }}
|
244
|
+
```
|
data/lib/jekyll/imgwh/helpers.rb
CHANGED
@@ -3,14 +3,17 @@
|
|
3
3
|
require "cgi"
|
4
4
|
require "fastimage"
|
5
5
|
require "jekyll"
|
6
|
-
require "jekyll/imgwh/version"
|
7
6
|
require "uri"
|
8
7
|
|
9
8
|
module Jekyll
|
10
9
|
module Imgwh
|
11
10
|
module Helpers
|
12
|
-
def
|
13
|
-
|
11
|
+
def name
|
12
|
+
"imgwh"
|
13
|
+
end
|
14
|
+
|
15
|
+
def debug(*messages)
|
16
|
+
messages.each { |message| Jekyll.logger.debug "#{name}:", message }
|
14
17
|
end
|
15
18
|
|
16
19
|
def image_size(src, context)
|
@@ -19,28 +22,28 @@ module Jekyll
|
|
19
22
|
if uri.scheme.nil?
|
20
23
|
src = resolve_path(CGI.unescape(uri.path), context)
|
21
24
|
else
|
22
|
-
allowed_schemes = context.registers[:site].config.dig(
|
25
|
+
allowed_schemes = context.registers[:site].config.dig(name, "allowed_schemes") || []
|
23
26
|
unless allowed_schemes.include?(uri.scheme)
|
24
|
-
raise ArgumentError, "#{
|
27
|
+
raise ArgumentError, "#{name}: URIs with '#{uri.scheme}' scheme are not allowed"
|
25
28
|
end
|
26
29
|
end
|
27
30
|
|
28
|
-
FastImage.size(src) or raise LoadError, "#{
|
31
|
+
FastImage.size(src) or raise LoadError, "#{name}: could not get size of image '#{src}'"
|
29
32
|
end
|
30
33
|
|
31
34
|
def resolve_path(path, context)
|
32
35
|
local_path = path.start_with?("/") ? path : File.join(context.registers[:page]["dir"], path)
|
33
|
-
local_path =
|
36
|
+
local_path = context.registers[:site].in_source_dir(local_path)
|
34
37
|
debug "image path: '#{local_path}'"
|
35
38
|
return local_path if File.file?(local_path)
|
36
39
|
|
37
40
|
themed_path = context.registers[:site].in_theme_dir(path) if path.start_with?("/")
|
38
|
-
raise LoadError, "#{
|
41
|
+
raise LoadError, "#{name}: '#{local_path}' could not be found" unless themed_path
|
39
42
|
|
40
43
|
debug "themed image path: '#{themed_path}'"
|
41
44
|
return themed_path if File.file?(themed_path)
|
42
45
|
|
43
|
-
raise LoadError, "#{
|
46
|
+
raise LoadError, "#{name}: none of '#{local_path}', '#{themed_path}' could be found"
|
44
47
|
end
|
45
48
|
end
|
46
49
|
end
|
data/lib/jekyll/imgwh/tag.rb
CHANGED
@@ -8,33 +8,38 @@ module Jekyll
|
|
8
8
|
include Helpers
|
9
9
|
|
10
10
|
def initialize(tag_name, content, tokens)
|
11
|
+
content.strip!
|
11
12
|
super
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
if (m = @content.match(%r/^(["'])((?:\1\1|(?!\1).)+)\1(?:\s+(.+))?$/))
|
14
|
+
if (m = @markup.match(%r/^(["'])((?:\1\1|(?!\1).)+)\1(?:\s+(.+))?$/))
|
16
15
|
@quote, @src, @rest = m.captures
|
17
16
|
@src = @src.gsub("#{@quote}#{@quote}", @quote)
|
18
17
|
|
19
|
-
elsif (m = @
|
18
|
+
elsif (m = @markup.match(%r/^(?!["'])((?:(?!\{\{)\S|\{\{.+?\}\})+)(?:\s+(.+))?$/))
|
20
19
|
@quote = ""
|
21
20
|
@src, @rest = m.captures
|
22
21
|
|
23
22
|
else
|
24
|
-
raise SyntaxError, "#{
|
23
|
+
raise SyntaxError, "#{@tag_name}: invalid tag markup: #{@markup.inspect}"
|
25
24
|
end
|
26
25
|
end
|
27
26
|
|
28
27
|
def render(context)
|
29
|
-
|
28
|
+
debug "---", "markup: #{@markup.inspect}", "src: #{@src.inspect}", "rest: #{@rest.inspect}"
|
29
|
+
|
30
|
+
context.stack do
|
31
|
+
context["#{@tag_name}_quote"] = @quote
|
32
|
+
|
33
|
+
src = Liquid::Template.parse(@src).render(context)
|
34
|
+
debug "src rendered: #{src.inspect}"
|
30
35
|
|
31
|
-
|
32
|
-
|
33
|
-
img = "<img src=#{quoted src}"
|
36
|
+
size = image_size(src, context)
|
37
|
+
debug "image size: #{size}"
|
34
38
|
|
35
|
-
|
36
|
-
|
37
|
-
|
39
|
+
rest = render_rest(context)
|
40
|
+
|
41
|
+
"<img src=#{quoted src} width=#{quoted size[0]} height=#{quoted size[1]}#{rest}>"
|
42
|
+
end
|
38
43
|
end
|
39
44
|
|
40
45
|
private
|
@@ -46,15 +51,16 @@ module Jekyll
|
|
46
51
|
def render_rest(context)
|
47
52
|
rest = +""
|
48
53
|
|
49
|
-
extra_rest = context.registers[:site].config.dig(
|
50
|
-
|
54
|
+
extra_rest = context.registers[:site].config.dig(@tag_name, "extra_rest")
|
55
|
+
extra_rest ||= "loading={{#{@tag_name}_quote}}lazy{{#{@tag_name}_quote}}"
|
56
|
+
unless extra_rest.empty?
|
51
57
|
extra_rest = Liquid::Template.parse(extra_rest).render(context)
|
52
|
-
debug "extra_rest rendered:
|
58
|
+
debug "extra_rest rendered: #{extra_rest.inspect}"
|
53
59
|
rest << " #{extra_rest}" unless extra_rest.empty?
|
54
60
|
end
|
55
61
|
|
56
62
|
tag_rest = Liquid::Template.parse(@rest).render(context)
|
57
|
-
debug "rest rendered:
|
63
|
+
debug "rest rendered: #{tag_rest.inspect}"
|
58
64
|
rest << " #{tag_rest}" unless tag_rest.empty?
|
59
65
|
|
60
66
|
rest
|
data/lib/jekyll/imgwh/version.rb
CHANGED
data/lib/jekyll-imgwh.rb
CHANGED
@@ -4,8 +4,6 @@ require "jekyll/imgwh/tag"
|
|
4
4
|
|
5
5
|
module Jekyll
|
6
6
|
module Imgwh
|
7
|
-
Liquid::Template.register_tag "imgwh", Tag
|
8
|
-
|
9
7
|
def imgwh(src)
|
10
8
|
image_size(src, @context)
|
11
9
|
end
|
@@ -13,6 +11,9 @@ module Jekyll
|
|
13
11
|
Liquid::Template.register_filter(self)
|
14
12
|
|
15
13
|
# Include after register_filter so they do not leak to Liquid yet are accessible within imgwh()
|
14
|
+
extend self
|
16
15
|
include Helpers
|
16
|
+
|
17
|
+
Liquid::Template.register_tag(name, Tag)
|
17
18
|
end
|
18
19
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jekyll-imgwh
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mikalai Ananenka
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-
|
10
|
+
date: 2025-05-23 00:00:00.000000000 Z
|
11
11
|
dependencies:
|
12
12
|
- !ruby/object:Gem::Dependency
|
13
13
|
name: fastimage
|
@@ -122,7 +122,9 @@ files:
|
|
122
122
|
homepage: https://github.com/ojuuji/jekyll-imgwh
|
123
123
|
licenses:
|
124
124
|
- MIT
|
125
|
-
metadata:
|
125
|
+
metadata:
|
126
|
+
homepage_uri: https://github.com/ojuuji/jekyll-imgwh
|
127
|
+
bug_tracker_uri: https://github.com/ojuuji/jekyll-imgwh/issues
|
126
128
|
rdoc_options: []
|
127
129
|
require_paths:
|
128
130
|
- lib
|
@@ -139,6 +141,6 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
139
141
|
requirements: []
|
140
142
|
rubygems_version: 3.6.2
|
141
143
|
specification_version: 4
|
142
|
-
summary: A tag for <img>
|
143
|
-
size
|
144
|
+
summary: A tag for <img> element with some automation, and a filter returning image
|
145
|
+
size.
|
144
146
|
test_files: []
|