jekyll-imgwh 1.3.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7efeac12ff14b51441a976e3ea7b7d9c286d9429d59aeebfae72ed8666f7313c
4
- data.tar.gz: 69f883d0d544d950cff948d71aeb5ff5333f11ad2810458b30a171192b9c50f4
3
+ metadata.gz: c07b034a8e148f443f9a0da9950592f0425786509651fb7f67ab5bf6bae1c076
4
+ data.tar.gz: d87846a205f7259100a63a3153a285105958e0903345e3c926fe7a4a0c7aaa9a
5
5
  SHA512:
6
- metadata.gz: 1a0e4b3815bd80f84029a9f3ee4ca5237a9d70f4aa14f930914ddd134298eb1668da03b49e8a643aae8311c4cb4d5d970eea9af4d6ffbceaabf93360c07d2a66
7
- data.tar.gz: 152ed5a03712c50cd8e412cd5c9892cbeac5317b7605489f0124df1a59d800908d4046b7704f8c035384f668a30988d89ebfe9ed6ceaa18f211d7393a79e9122
6
+ metadata.gz: ea444a44d2d81ae1b695ae405a7017c8f17800131229f84658f6fa7eab9efcb65fb5ee9cd4063c32a4a1ab0e4446de5ddde625515cb82766989e6ac4cc103339
7
+ data.tar.gz: 28ae016af67d07656650d1dbe293f652c025eef1ebb7e8e3e02f825a4447132aeeb6a54b41eeb39eafaf1def3c7d443d7f471e5f30ad0ae54c410a69c5a2a961
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- [![Gem Version](https://badge.fury.io/rb/jekyll-imgwh.svg)](https://badge.fury.io/rb/jekyll-imgwh)
1
+ # jekyll-imgwh [![Gem Version](https://badge.fury.io/rb/jekyll-imgwh.svg)](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
- # Installation
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
- # Usage
19
+ ## Usage
20
20
 
21
- ## Liquid Tag
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
- ### Quotes and Whitespace
49
+ #### Quotes and Whitespace
50
50
 
51
- `<src>` can be specified with single quotes, double quotes, or without quotes. This also defines quotation for the generated `src`, `width`, and `height` attributes: they always use the same quotes as `<src>`:
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" %} -> <img src="/foo.png" width="123" height="456">
55
- {% imgwh '/foo.png' %} -> <img src='/foo.png' width='123' height='456'>
56
- {% imgwh /foo.png %} -> <img src=/foo.png width=123 height=456>
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"" }}" %} -> 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')
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" }} %} -> 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)
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
- ## Liquid Filter
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
- ## Path Resolution
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
 
@@ -115,18 +125,18 @@ When the image is not found, and a theme is used, and the path is absolute, imag
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.
117
127
 
118
- ## Error Handling
128
+ ### Error Handling
119
129
 
120
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.
121
131
 
122
- # Configuration
132
+ ## Configuration
123
133
 
124
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):
125
135
 
126
136
  ```yml
127
- jekyll-imgwh:
137
+ imgwh:
128
138
  allowed_schemes: []
129
- extra_rest:
139
+ extra_rest: loading={{imgwh_quote}}lazy{{imgwh_quote}}
130
140
  ```
131
141
 
132
142
  These are default options i.e. you do not need to specify any of them unless you want to use different value.
@@ -140,7 +150,7 @@ Option `allowed_schemes` adds exception for the schemes specified in it. For URI
140
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:
141
151
 
142
152
  ```yml
143
- jekyll-imgwh:
153
+ imgwh:
144
154
  allowed_schemes: ["data", "https"]
145
155
  ```
146
156
 
@@ -152,23 +162,25 @@ Remember `imgwh` tag syntax? This option injects additional text into all genera
152
162
  {% imgwh <src> <extra_rest> [<rest>] %}
153
163
  ```
154
164
 
155
- For example, since all generated HTML `<img>` elements get the size attributes, it might be a good idea to set lazy loading for the images:
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:
156
168
 
157
169
  ```yml
158
- jekyll-imgwh:
159
- extra_rest: loading="lazy"
170
+ imgwh:
171
+ extra_rest: ""
160
172
  ```
161
173
 
162
174
  Like `<src>` and `<rest>`, `<extra_rest>` can also include Liquid markup, which is rendered in context of images where `<extra_rest>` is injected.
163
175
 
164
- # Troubleshooting
176
+ ## Troubleshooting
165
177
 
166
178
  When error is related to the image image file, plugin mentions the file path in the error message:
167
179
 
168
- ```
180
+ ```shell
169
181
  $ bundle exec jekyll serve
170
182
  <...>
171
- Liquid Exception: jekyll-imgwh: 'Y:/ssg/assets/images/foo.png' could not be found in index.html
183
+ Liquid Exception: imgwh: 'Y:/ssg/assets/images/foo.png' could not be found in index.html
172
184
  <...>
173
185
  ```
174
186
 
@@ -176,27 +188,27 @@ Plugin also logs a lot of info which can help to resolve errors raised by it. Us
176
188
 
177
189
  For example, for template
178
190
 
179
- ```
180
- {% imgwh "/assets/images/{{ product.key }}.png" alt="{{ project.title }} Logo" class="www-logo" %}
191
+ ```shell
192
+ {% imgwh '/assets/images/{{ product.key }}.png' alt='{{ project.title }} Logo' class='www-logo' %}
181
193
  ```
182
194
 
183
195
  it would print something like this in case of successful generation:
184
196
 
185
- ```
197
+ ```shell
186
198
  $ bundle exec jekyll serve --verbose
187
199
  <...>
188
- jekyll-imgwh: ---
189
- jekyll-imgwh: content: '"/assets/images/{{ product.key }}.png" alt="{{ project.title }} Logo" class="www-logo"'
190
- jekyll-imgwh: src: '/assets/images/{{ product.key }}.png'
191
- jekyll-imgwh: rest: 'alt="{{ project.title }} Logo" class="www-logo"'
192
- jekyll-imgwh: src rendered: '/assets/images/foo.png'
193
- jekyll-imgwh: image path: 'Y:/ssg/assets/images/foo.png'
194
- jekyll-imgwh: image size: [128, 64]
195
- jekyll-imgwh: rest rendered: 'alt="My Product Logo" class="www-logo"'
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'"
196
208
  <...>
197
209
  ```
198
210
 
199
- # Development
211
+ ## Development
200
212
 
201
213
  To get started with the development:
202
214
 
@@ -206,3 +218,27 @@ cd jekyll-imgwh
206
218
  bundle install
207
219
  bundle exec rspec
208
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
+ ```
@@ -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 debug(message)
13
- Jekyll.logger.debug "#{NAME}:", message
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,13 +22,13 @@ 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(NAME, "allowed_schemes") || []
25
+ allowed_schemes = context.registers[:site].config.dig(name, "allowed_schemes") || []
23
26
  unless allowed_schemes.include?(uri.scheme)
24
- raise ArgumentError, "#{NAME}: URIs with '#{uri.scheme}' scheme are not allowed"
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, "#{NAME}: could not get size of image '#{src}'"
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)
@@ -35,12 +38,12 @@ module Jekyll
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, "#{NAME}: '#{local_path}' could not be found" unless themed_path
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, "#{NAME}: none of '#{local_path}', '#{themed_path}' could be found"
46
+ raise LoadError, "#{name}: none of '#{local_path}', '#{themed_path}' could be found"
44
47
  end
45
48
  end
46
49
  end
@@ -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
- @content = content.strip
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 = @content.match(%r/^(?!["'])((?:(?!\{\{)\S|\{\{.+?\}\})+)(?:\s+(.+))?$/))
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, "#{NAME}: invalid #{tag_name} tag: '#{@content}'"
23
+ raise SyntaxError, "#{@tag_name}: invalid tag markup: #{@markup.inspect}"
25
24
  end
26
25
  end
27
26
 
28
27
  def render(context)
29
- ["---", "content: '#{@content}'", "src: '#{@src}'", "rest: '#{@rest}'"].map { |x| debug x }
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
- src = Liquid::Template.parse(@src).render(context)
32
- debug "src rendered: '#{src}'"
33
- img = "<img src=#{quoted src}"
36
+ size = image_size(src, context)
37
+ debug "image size: #{size}"
34
38
 
35
- size = image_size(src, context)
36
- debug "image size: #{size}"
37
- img << " width=#{quoted size[0]} height=#{quoted size[1]}" << render_rest(context) << ">"
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(NAME, "extra_rest")
50
- unless extra_rest.nil?
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: '#{extra_rest}'"
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: '#{tag_rest}'"
63
+ debug "rest rendered: #{tag_rest.inspect}"
58
64
  rest << " #{tag_rest}" unless tag_rest.empty?
59
65
 
60
66
  rest
@@ -2,7 +2,6 @@
2
2
 
3
3
  module Jekyll
4
4
  module Imgwh
5
- NAME = "jekyll-imgwh"
6
- VERSION = "1.3.1"
5
+ VERSION = "1.4.0"
7
6
  end
8
7
  end
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.3.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-04-25 00:00:00.000000000 Z
10
+ date: 2025-05-23 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: fastimage