jekyll-img-srcset 0.1.3 → 0.2.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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jekyll-img-srcset.rb +64 -54
  3. metadata +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 92a18f3afa30c975e5595fb97644e09667ccb469c19fdb8ffb2641e6a6864b13
4
- data.tar.gz: 2c87894a3a7f6f37a2f9757ebe6370818c6f2a1c7aa0514258557d5c8a4f21db
3
+ metadata.gz: 8cba8bc40ca676dbd4b2889516261954dd9ccefcc6a8d020faa49df285e4004b
4
+ data.tar.gz: e234e70640cba4b30dc472d1a78954066be34b2630277d1627668f88b46a1bf8
5
5
  SHA512:
6
- metadata.gz: df94acc2e731136259369e950b9067ba4f2582b17a02e18709486346900c956206c24cb7b1c8cd7570367393358ae30aa78f77763e0bfedfbede2ff84345680c
7
- data.tar.gz: 23acba98d8129f4f00c475b142fe5a769f565ced4362846cf21868db848218e831a24dc2b47caebffb837f6a3b2d08c8d6a74e142ad3303e8693f95a7c01ca91
6
+ metadata.gz: 4520a7fbef0148dcebb03a6943fa53114aaae6217dc522aec7e41e43f397be3e230c5738e4ab8bb09ae61e6ed7b3537ce350f3d12ef838a6507cf790f1afd068
7
+ data.tar.gz: dea03820cebd35ed1b0147ecb2e90e94562dfb188c712c56f60082904303d0b9fe712858c489d1bc4c8b5a9f5d98807fe7de33212048dc8853807f05b1e361df
@@ -1,4 +1,5 @@
1
1
  require 'mini_magick'
2
+ require 'parallel'
2
3
 
3
4
  # Resizes an image to provide different versions for multiple
4
5
  # display resolutions
@@ -14,17 +15,28 @@ require 'mini_magick'
14
15
  # the generated widths and the original iamge width
15
16
  def resize_image(url, widths, dest, base_image_path, cache)
16
17
  src_path = File.join(base_image_path, url)
17
- new_widths, w = cache.getset(src_path) do
18
+ src_mtime = File.new(src_path).mtime
19
+ if cache.key? src_path
20
+ _, _, modified_time = cache[src_path]
21
+ if src_mtime > modified_time
22
+ cache.delete src_path
23
+ end
24
+ end
25
+ # resize images if they aren't already in the cache
26
+ new_widths, w, modified_time = cache.getset(src_path) do
27
+ modified_time = File.new(src_path).mtime
18
28
  image = MiniMagick::Image.open(src_path)
19
29
  w = image.dimensions[0]
20
30
  aspect = image.dimensions[1].to_f / w
21
31
  [w, aspect]
22
32
  new_widths = widths.select {|x| x <= w}
23
- new_widths.map do |width|
33
+ width_to_create = new_widths.map do |width|
24
34
  [width, File.join(dest, base_image_path, "#{width}", url)]
25
- end.select do |width, target|
26
- not File.exists? target
27
- end.each do |width, target|
35
+ end
36
+ .select do |width, target|
37
+ (not File.exists? target) or (File.new(target).mtime < src_mtime)
38
+ end
39
+ Parallel.each(width_to_create) do |width, target|
28
40
  image = MiniMagick::Image.open(src_path)
29
41
  image.resize "#{width}x#{width*aspect}"
30
42
  if not Dir.exists? File.dirname(target)
@@ -32,8 +44,9 @@ def resize_image(url, widths, dest, base_image_path, cache)
32
44
  end
33
45
  image.write target
34
46
  end
35
- [new_widths, w]
47
+ [new_widths, w, modified_time]
36
48
  end
49
+
37
50
  return new_widths, w
38
51
  end
39
52
 
@@ -120,50 +133,50 @@ def format_svg_image(url, ident, title, baseurl, base_image_path)
120
133
  content += "/>"
121
134
  end
122
135
 
123
- def process_page(document, payload)
124
- # see https://gist.github.com/mmistakes/77c68fbb07731a456805a7b473f47841
125
- doc_ext = document.extname.tr('.', '')
126
- cache = Jekyll::Cache.new("jekyll-img-srcset")
127
- if payload['site']['markdown_ext'].include? doc_ext
128
- content = document.content
129
- base_image_path = document.site.config["base_image_path"] || "assets/images"
130
- image_regex = /!\[(?<caption>.*?)?\]\((?<url>\S+?)(?<title> .+?)?\)(?<identifier>\{.+?\})?/
131
- content = content.gsub(image_regex) do | img |
132
- img = image_regex.match img
133
- if (/\.(jpe?g|png)$/i =~ img["url"]).nil?
134
- "#{base_image_path}/#{img[0]}"
135
- elsif img["url"].start_with? "http"
136
- img[0]
137
- elsif img["url"].end_with? ".svg"
138
- ident = parse_identifier img[:identifier]
139
- format_svg_image(img["url"], ident, img["title"],
140
- document.site.baseurl)
141
- else
142
- if File.exists?(File.join base_image_path, img['url'])
143
- widths, original_width = resize_image(img['url'],
144
- document.site.config["widths"],
145
- document.site.config["destination"],
146
- base_image_path, cache)
147
- document.site.keep_files.concat(widths.map do |w|
148
- File.join(base_image_path, "#{w}", img['url'])
149
- end)
150
- ident = parse_identifier img[:identifier]
151
- format_image(ident, widths, original_width,
152
- img["url"], img["caption"], img["title"],
153
- document.site.baseurl, base_image_path)
154
- else
155
- img[0]
156
- end
157
- end
158
- end
159
- # puts content
160
- document.content = content
161
- end
162
- end
136
+ # def process_page(document, payload)
137
+ # # see https://gist.github.com/mmistakes/77c68fbb07731a456805a7b473f47841
138
+ # doc_ext = document.extname.tr('.', '')
139
+ # cache = Jekyll::Cache.new("jekyll-img-srcset")
140
+ # if payload['site']['markdown_ext'].include? doc_ext
141
+ # content = document.content
142
+ # base_image_path = document.site.config["base_image_path"] || "assets/images"
143
+ # image_regex = /!\[(?<caption>.*?)?\]\((?<url>\S+?)(?<title> .+?)?\)(?<identifier>\{.+?\})?/
144
+ # content = content.gsub(image_regex) do | img |
145
+ # img = image_regex.match img
146
+ # if (/\.(jpe?g|png)$/i =~ img["url"]).nil?
147
+ # "#{base_image_path}/#{img[0]}"
148
+ # elsif img["url"].start_with? "http"
149
+ # img[0]
150
+ # elsif img["url"].end_with? ".svg"
151
+ # ident = parse_identifier img[:identifier]
152
+ # format_svg_image(img["url"], ident, img["title"],
153
+ # document.site.baseurl)
154
+ # else
155
+ # if File.exists?(File.join base_image_path, img['url'])
156
+ # widths, original_width = resize_image(img['url'],
157
+ # document.site.config["widths"],
158
+ # document.site.config["destination"],
159
+ # base_image_path, cache)
160
+ # document.site.keep_files.concat(widths.map do |w|
161
+ # File.join(base_image_path, "#{w}", img['url'])
162
+ # end)
163
+ # ident = parse_identifier img[:identifier]
164
+ # format_image(ident, widths, original_width,
165
+ # img["url"], img["caption"], img["title"],
166
+ # document.site.baseurl, base_image_path)
167
+ # else
168
+ # img[0]
169
+ # end
170
+ # end
171
+ # end
172
+ # # puts content
173
+ # document.content = content
174
+ # end
175
+ # end
163
176
 
164
- Jekyll::Hooks.register [:pages, :posts, :documents], :pre_render do |document, payload|
165
- process_page(document, payload)
166
- end
177
+ # Jekyll::Hooks.register [:pages, :posts, :documents], :pre_render do |document, payload|
178
+ # process_page(document, payload)
179
+ # end
167
180
 
168
181
  def check_in_context(context, path)
169
182
  parts = path.split(".")
@@ -182,7 +195,7 @@ module Jekyll
182
195
  class ImgSrcsetTag < Liquid::Tag
183
196
 
184
197
  def cache
185
- @@cache ||= Jekyll::Cache.new("jekyll-img-srcset")
198
+ @@cache ||= Jekyll::Cache.new("Jekyll::ImgSrcset")
186
199
  end
187
200
 
188
201
  def initialize(tag_name, text, tokens)
@@ -235,7 +248,4 @@ module Jekyll
235
248
  end
236
249
  end
237
250
 
238
- Liquid::Template.register_tag('imgsrcset', Jekyll::ImgSrcsetTag)
239
-
240
-
241
- # /![(?<caption>.*?)?]\((?<url>[^\s]+?)(?<title> .+?)?\)(?<identifier>{.+?})?/
251
+ Liquid::Template.register_tag('imgsrcset', Jekyll::ImgSrcsetTag)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-img-srcset
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan-Philipp Stauffert
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-19 00:00:00.000000000 Z
11
+ date: 2020-01-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '4.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: parallel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.19'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.19'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: bundler
43
57
  requirement: !ruby/object:Gem::Requirement