jekyll-thumbnail-img 0.1.0 → 0.1.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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/jekyll-thumbnail-img.rb +37 -38
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c6f1558133356d35708570fe3adc2002f8527a3bbd0b6f0339f8df5f5861356e
4
- data.tar.gz: df2e246e6bfcdca889563df614e2b89b028ec3b9688c83ffd5b9e77b8bf49b51
3
+ metadata.gz: f2aac91c9210d6650d7870b5a70c2477ddac7d7f72b754adcdd02af0b1a7b988
4
+ data.tar.gz: f5234e4e9f262d3036d4ca62bb2d1cddaf747978cd4ebda013c18ff4ccfb138d
5
5
  SHA512:
6
- metadata.gz: 1edcdfceae5b2f2c75d00d57d9cef8a9eab64bcf737aef60d3e5dae93aca814b49c664e59c3286bf4bfdfef61c1213fed3d225ed5391149194a75fd60cf49d4e
7
- data.tar.gz: 816be152f8b6283a5db5ab1b83842313586bc073245f6a74554bd67e739afc8acbf9e2a89d40624c12772c08bc54761a7708bc776f3925ef6c56f6b2dacd2775
6
+ metadata.gz: 358d4723d03ee22488fef29ace01225445fb668152600219a28323369c242478d2f173204993bcd739e55d22510dcf104deaf98c022940138129dd63912843ba
7
+ data.tar.gz: f720d8d0bd21ec8cd054113ce8ff9aba228d2617523e8c3c5113961c8db81dc7650272c68c987389ad950bf59c187284a13e5eee1963c7cddb82baf8cf951930
@@ -1,50 +1,49 @@
1
- require "jekyll"
2
- require "mini_magick"
1
+ require 'jekyll'
2
+ require 'mini_magick'
3
3
 
4
- class JekyllThumbnailImg < Liquid::Tag
5
- def initialize(tag_name, markup, tokens)
6
- super
7
- @markup = markup
8
- end
9
-
10
- def render(context)
11
- source, width = @markup.split(" ").map(&:strip)
4
+ module Jekyll
5
+ class JekyllThumbnailImg < Liquid::Tag
6
+ @@pending = [] # thumbnails to be generated
12
7
 
13
- # Check if the source is a Liquid variable and attempt to resolve it
14
- if context[source]
15
- source = context[source]
16
- end
17
- unless source && width
18
- raise "Usage: {% thumbnail_img /path/to/local/image.png 500 %}"
8
+ def initialize(tag_name, markup, tokens)
9
+ super
10
+ @markup = markup
19
11
  end
20
12
 
21
- source_path = File.join(context.registers[:site].source, source)
22
-
23
- # Check if the source file exists
24
- unless File.exist?(source_path)
25
- raise "File #{source} could not be found"
26
- end
13
+ def render(context)
14
+ source, width = @markup.split.map(&:strip)
15
+ source = context[source] if context[source] # resolve liquid vars
27
16
 
28
- # Calculate the relative path to the 'thumbnails' directory
29
- relative_dest_dir = File.join(File.dirname(source), "thumbnails")
17
+ raise "Usage: {% thumbnail_img /path/to/image.png 500 %}" unless source && width
18
+ raise "File #{source} not found" unless File.exist?(File.join(context.registers[:site].source, source))
30
19
 
31
- # Create the 'thumbnails' directory within the source directory
32
- dest_dir = File.join(context.registers[:site].source, relative_dest_dir)
33
- Dir.mkdir(dest_dir) unless Dir.exist?(dest_dir)
20
+ # Queue thumbnail for generation
21
+ dest = File.join(File.dirname(source), "thumbnails", "#{File.basename(source, '.*')}_#{width}w#{File.extname(source)}")
22
+ @@pending << {
23
+ source: File.join(context.registers[:site].source, source),
24
+ dest: File.join(context.registers[:site].dest, dest),
25
+ width: width
26
+ }
34
27
 
35
- ext = File.extname(source)
36
- dest = File.join(relative_dest_dir, File.basename(source, ext) + "_#{width}w#{ext}")
37
-
38
- # Generate the thumbnail if it doesn't exist or if the source file was modified more recently than the thumbnail
39
- full_dest_path = File.join(context.registers[:site].source, dest)
40
- if !File.exist?(full_dest_path) || File.mtime(full_dest_path) <= File.mtime(source_path)
41
- image = MiniMagick::Image.open(source_path)
42
- image.resize("#{width}x")
43
- image.write(File.join(context.registers[:site].source, dest))
28
+ dest
44
29
  end
45
30
 
46
- dest
31
+ def self.generate_thumbnails(site)
32
+ @@pending.each do |thumb|
33
+ FileUtils.mkdir_p(File.dirname(thumb[:dest]))
34
+ # Check if thumbnail doesn't exist or if the source file was modified more recently than the thumbnail
35
+ if !File.exist?(thumb[:dest]) || File.mtime(thumb[:dest]) <= File.mtime(thumb[:source])
36
+ Jekyll.logger.info "JekyllThumbnailImg:", "Generating: #{thumb[:dest].sub(site.dest + '/', '')}"
37
+ MiniMagick::Image.open(thumb[:source]).resize("#{thumb[:width]}x").write(thumb[:dest])
38
+ end
39
+ end
40
+ @@pending.clear
41
+ end
47
42
  end
48
43
  end
49
44
 
50
- Liquid::Template.register_tag("thumbnail_img", JekyllThumbnailImg)
45
+ Jekyll::Hooks.register :site, :post_write do |site|
46
+ Jekyll::JekyllThumbnailImg.generate_thumbnails(site)
47
+ end
48
+
49
+ Liquid::Template.register_tag("thumbnail_img", Jekyll::JekyllThumbnailImg)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-thumbnail-img
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abhishek Paudel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-11-02 00:00:00.000000000 Z
11
+ date: 2024-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll