jekyll-picture-tag-ng 0.3.1 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0db7bf60332a01764f52029da09c289aae64dca5211d85d12e17c6fd3518b616
4
- data.tar.gz: f86895b50c8ac8cf8062d55f9084df2650458dde3c7f6dae6ac411d26c4d4fbc
3
+ metadata.gz: 9b13b667958ab46916ceda313a2dec27a6d824e9e567fc68faa369734cd56272
4
+ data.tar.gz: 390e2ab18a416bf1e3e3523d18d6fe5b94a1a2a5bbf7b05a2a6cec8fcbddb8b9
5
5
  SHA512:
6
- metadata.gz: 036f7231a4ca24104af88f7a79d048e4924de12920c2ed54353d07f58f4ff6375e8cdd0b19f6c4775de8d5835f59ccb195b29770f380c5f8d76e0b061fdb051b
7
- data.tar.gz: 7990c47da142e389e07a2d74a878a9f4d05ad91fe12668ffc7ffa9abd9c87f52989ab29438b3f6861d588aa41b52cb4acb2aebd769f610b15904d236f6f92363
6
+ metadata.gz: 39af2751eab33d119cb9f494e99bf387a542dd397bbc098ab346075100608263040412036f85618a8d5eca60a98fa6c584a3cd2acfb271d9b9e1e4a27f277790
7
+ data.tar.gz: ac269bf8ed4c504e7139b2b16eb6078ba3f3105c67a95a6d55371cda449f8a51f9c5bf7a9d061d69895bf66087bd6b81fbc5a7bdab7ee81008e658ab7ebe1eed
data/README.md CHANGED
@@ -72,10 +72,112 @@ picture_tag_ng:
72
72
  The example above is equivalent to the defaults.
73
73
 
74
74
  - `background_color` is the color used to replace transparency when converting from `webp` to `jpeg`
75
- - `picture_versions` maps version names to target widths in pixels. The default configuration above produces output files 700px wide in `img/m/` and 400px wide in `img/s/`.
75
+ - `picture_versions` in the simplest form, maps version names to target widths in pixels. The default configuration above produces output files 700px wide in `img/m/` and 400px wide in `img/s/`. See below for more complex forms.
76
76
  - `parallel` is a boolean indicating if you want to generate the output files in parallel threads. With a website that has a lot of large pictures, I get ~30% speed improvements when generating the site locally.
77
77
  - `threads` is the number of concurrent threads for generating the website (only used if `parallel` is `true`)
78
78
 
79
+ #### `picture_versions` option
80
+
81
+ The `picture_versions` option must be a map. The keys are the version identifiers, and the values control the output for each version. The values can be defined in one of the following formats :
82
+
83
+ ```yaml
84
+ picture_tag_ng:
85
+ picture_versions:
86
+ s: 400
87
+ ```
88
+
89
+ When the version consists only of an integer, the value is used for both the output width and the corresponding `max-width` media attribute.
90
+
91
+ ```yaml
92
+ picture_tag_ng:
93
+ picture_versions:
94
+ s:
95
+ out_size: 400
96
+ ```
97
+
98
+ Each version can be defined as a map, with the `out_size` key being required (must be an integer). This value controls the output width for the version. If `out_size` is the only defined key, it is also used for the corresponding `max-with` media attribute.
99
+
100
+ ```yaml
101
+ picture_tag_ng:
102
+ picture_versions:
103
+ m:
104
+ out_size: 700
105
+ media: 1200
106
+ ```
107
+
108
+ Each version that is a map can define the `media` key. If the value is an integer, produces `(max-width: #{media_integer_from_conf}px)` for the associated media attribute.
109
+
110
+ ```yaml
111
+ picture_tag_ng:
112
+ picture_versions:
113
+ m:
114
+ out_size: 700
115
+ media: "screen and (max-width: 1200px)"
116
+ ```
117
+
118
+ If `media` is a string, its value is used as-is for the corresponding media attribute.
119
+
120
+ Additionally, you can add the `default: true` property to any version to remove the corresponding media attribute from HTML `source` elements, and use this version as the `src` for the default HTML `img` element. If no version is explicitly set as the default, the largest one will me used.
121
+
122
+ The following configuration shows one version for each allowed format :
123
+
124
+ ```yaml
125
+ picture_tag_ng:
126
+ picture_versions:
127
+ s:
128
+ out_size: 400
129
+ m:
130
+ out_size: 700
131
+ media: 1200
132
+ l:
133
+ out_size: 1200
134
+ media: "screen and (max-width: 1200px)"
135
+ xl:
136
+ out_size: 2000
137
+ default: true
138
+ ```
139
+
140
+ When using the above configuration, the plugin will convert
141
+
142
+ ```md
143
+ ![Alt text](/path/to/img/orig.jpg)
144
+ ```
145
+
146
+ to the following HTML :
147
+
148
+ ```html
149
+ <picture>
150
+ <source media="(max-width: 400px)" srcset="/img/s/path/to/img/orig.webp" type="image/webp">
151
+ <source media="(max-width: 400px)" srcset="/img/s/path/to/img/orig.jpg" type="image/jpeg">
152
+ <source media="(max-width: 1200px)" srcset="/img/m/path/to/img/orig.webp" type="image/webp">
153
+ <source media="(max-width: 1200px)" srcset="/img/m/path/to/img/orig.jpg" type="image/jpeg">
154
+ <source media="screen and (max-width: 1200px)" srcset="/img/l/path/to/img/orig.webp" type="image/webp">
155
+ <source media="screen and (max-width: 1200px)" srcset="/img/l/path/to/img/orig.jpg" type="image/jpeg">
156
+ <source srcset="/img/xl/path/to/img/orig.webp" type="image/webp">
157
+ <source srcset="/img/xl/path/to/img/orig.jpg" type="image/jpeg">
158
+ <img src="/img/xl/path/to/img/orig.jpg" alt="Alt text" loading="lazy">
159
+ </picture>
160
+ ```
161
+
162
+ #### Extra `convert` arguments
163
+
164
+ The `extra_convert_args` and `pre_extra_convert_args` options allow you to add any [`convert` argument](https://imagemagick.org/script/convert.php) to the image conversion processes. The `pre_extra_convert_args` will be added before the resize operation, and the `extra_convert_args` after the resize operation. These options must be either arrays of strings or a string that will be split on spaces. This can be used to add a watermark to all your pictures (see example below).
165
+
166
+ Additionally, you can set different values based on the version for both these options. In the example below, the `m` version of output pictures will be blurred :
167
+
168
+ ```yaml
169
+ picture_tag_ng:
170
+ pre_extra_convert_args: ["-font", "DejaVu-Sans-Book", "-pointsize", "72",
171
+ "-fill", "black", "-annotate", "+25+75", "Watermark"] # Add watermark to all output pics
172
+ picture_versions:
173
+ m:
174
+ out_size: 700
175
+ extra_convert_args: ["-scale", "20%", "-blur", "0x2.5", "-resize", "500%"] # Blur `m` pics
176
+ s: 400
177
+ ```
178
+
179
+ You can disable the default resize operation by setting `replace_convert_args: true` either at the plugin level or at the version level. If the, the version-specific value will override the plugin-global value.
180
+
79
181
  ## Development
80
182
 
81
183
  After cloning the repo, you can run the following commands in a local Jekyll website's folder to start hacking on the code of `jekyll-picture-tag-ng` (you'll need to replace the path in the second command) :
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Jekyll
4
4
  module PictureTag
5
- VERSION = "0.3.1"
5
+ VERSION = "0.5.0"
6
6
  end
7
7
  end
@@ -16,7 +16,9 @@ module Jekyll
16
16
  "s" => "400",
17
17
  "m" => "700"
18
18
  },
19
- "background_color" => "FFFFFF"
19
+ "background_color" => "FFFFFF",
20
+ "extra_convert_args" => [],
21
+ "replace_convert_args" => false
20
22
  }.freeze
21
23
 
22
24
  class Error < StandardError; end
@@ -26,7 +28,11 @@ module Jekyll
26
28
  def initialize(site, orig_static_file, version, pictype)
27
29
  super(site, site.source, orig_static_file.dir, orig_static_file.name)
28
30
  @version = version
29
- @picture_dim = picture_versions[@version]
31
+ @picture_dim = if picture_versions[@version].is_a?(Hash)
32
+ picture_versions[@version]["out_size"]
33
+ else
34
+ picture_versions[@version]
35
+ end
30
36
  @pictype = pictype
31
37
  @collection = nil
32
38
  end
@@ -35,33 +41,75 @@ module Jekyll
35
41
  @config ||= CONFIG.merge(@site.config["picture_tag_ng"] || {})
36
42
  end
37
43
 
44
+ def replace_args
45
+ result = config["replace_convert_args"]
46
+ if picture_versions[@version].is_a?(Hash) &&
47
+ !picture_versions[@version]["replace_convert_args"].nil?
48
+ result = picture_versions[@version]["replace_convert_args"]
49
+ end
50
+ result
51
+ end
52
+
38
53
  def picture_versions
39
54
  config["picture_versions"]
40
55
  end
41
56
 
57
+ def as_args(input)
58
+ if input.is_a?(Array)
59
+ input.clone
60
+ elsif input.is_a?(String)
61
+ input.split(" ")
62
+ else
63
+ raise(
64
+ TypeError,
65
+ "[jekyll-picture-tag-ng] `extra_convert_args` must be an array or a string"
66
+ )
67
+ end
68
+ end
69
+
70
+ def convert_args
71
+ @convert_args ||= as_args(config["extra_convert_args"]).concat(
72
+ picture_versions[@version].is_a?(Hash) &&
73
+ picture_versions[@version]["extra_convert_args"] || []
74
+ )
75
+ end
76
+
77
+ def pre_convert_args
78
+ @pre_convert_args ||= as_args(config["pre_extra_convert_args"]).concat(
79
+ picture_versions[@version].is_a?(Hash) &&
80
+ picture_versions[@version]["pre_extra_convert_args"] || []
81
+ )
82
+ end
83
+
42
84
  def picture?
43
85
  extname =~ /(\.jpg|\.jpeg|\.webp)$/i
44
86
  end
45
87
 
46
88
  def destination(dest)
47
89
  output_dir = File.join("img", @version, @dir)
48
- output_basename = @site.in_dest_dir(@site.dest, File.join(output_dir, "#{basename}.#{@pictype}"))
90
+ output_basename = @site.in_dest_dir(
91
+ @site.dest,
92
+ File.join(output_dir, "#{basename}.#{@pictype}")
93
+ )
49
94
  FileUtils.mkdir_p(File.dirname(output_dir))
50
95
  @destination ||= {}
51
96
  @destination[dest] ||= output_basename
52
97
  end
53
98
 
54
99
  def popen_args(dest_path)
55
- args = ["convert", @path, "-resize", "#{@picture_dim}x>"]
100
+ args = ["convert", @path]
101
+ args.concat pre_convert_args
102
+ args.concat ["-resize", "#{@picture_dim}x>"] unless replace_args
56
103
  if @pictype == "jpg"
57
104
  args.concat ["-background", "##{@config["background_color"]}",
58
105
  "-flatten", "-alpha", "off"]
59
106
  end
107
+ args.concat convert_args
60
108
  args.push dest_path
61
109
  end
62
110
 
63
111
  def copy_file(dest_path)
64
- Jekyll.logger.debug "copy_file : #{path} -> #{dest_path}"
112
+ Jekyll.logger.debug "copy_file : #{path} -> #{dest_path} (#{popen_args(dest_path)})"
65
113
  p = IO.popen(popen_args(dest_path))
66
114
  p.close
67
115
  File.utime(self.class.mtimes[path], self.class.mtimes[path], dest_path)
@@ -197,13 +245,51 @@ module Kramdown
197
245
  site_config["picture_versions"]
198
246
  end
199
247
 
248
+ def _get_default_pic_version
249
+ largest_version = ""
250
+ largest_size = 0
251
+ picture_versions.each do |version, geometry|
252
+ size = if geometry.is_a?(Integer)
253
+ geometry
254
+ elsif geometry["default"]
255
+ 999_999_999
256
+ else
257
+ geometry["out_size"]
258
+ end
259
+ if size > largest_size
260
+ largest_version = version
261
+ largest_size = size
262
+ end
263
+ end
264
+ largest_version
265
+ end
266
+
267
+ def default_pic_version
268
+ @default_pic_version ||= _get_default_pic_version
269
+ end
270
+
271
+ def media_attribute(version)
272
+ geometry = picture_versions[version]
273
+ if geometry.is_a?(Hash)
274
+ if geometry["media"].is_a?(String)
275
+ "media=\"#{geometry["media"]}\""
276
+ elsif geometry["media"].is_a?(Integer)
277
+ "media=\"(max-width: #{geometry["media"]}px)\""
278
+ else
279
+ "media=\"(max-width: #{geometry["out_size"]}px)\""
280
+ end
281
+ else
282
+ "media=\"(max-width: #{geometry}px)\""
283
+ end
284
+ end
285
+
200
286
  def convert_img(el, _indent)
201
287
  require "cgi"
202
288
  res = "<picture>"
203
289
  new_src = el.attr["src"]
204
290
  if File.extname(el.attr["src"]) =~ /(\.jpg|\.jpeg|\.webp)$/i &&
205
291
  el.attr["src"] !~ %r{^https?://}
206
- picture_versions.each_with_index do |(version, geometry), index|
292
+ picture_versions.each do |version, _geometry|
207
293
  src_base = File.join(
208
294
  "/img",
209
295
  version,
@@ -212,12 +298,8 @@ module Kramdown
212
298
  end.join("/"),
213
299
  File.basename(el.attr["src"], File.extname(el.attr["src"])).gsub(" ", "%20")
214
300
  )
215
- if index == picture_versions.size - 1
216
- media = ""
217
- new_src = "#{src_base}.jpg"
218
- else
219
- media = "media=\"(max-width: #{geometry}px)\""
220
- end
301
+ media = media_attribute(version)
302
+ new_src = "#{src_base}.jpg" if version == default_pic_version
221
303
  res += "<source #{media} srcset=\"#{src_base}.webp\" type=\"image/webp\">"
222
304
  res += "<source #{media} srcset=\"#{src_base}.jpg\" type=\"image/jpeg\">"
223
305
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-picture-tag-ng
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pcouy
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-27 00:00:00.000000000 Z
11
+ date: 2023-04-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -76,6 +76,7 @@ licenses:
76
76
  metadata:
77
77
  allowed_push_host: https://rubygems.org
78
78
  homepage_uri: https://pierre-couy.dev/projects/jekyll-picture-tag-ng.html
79
+ documentation_uri: https://pierre-couy.dev/projects/jekyll-picture-tag-ng.html
79
80
  source_code_uri: https://github.com/pcouy/jekyll-picture-tag-ng
80
81
  post_install_message:
81
82
  rdoc_options: []