jekyll-thumbnail-img 0.1.1 → 0.1.3
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/lib/jekyll-thumbnail-img.rb +42 -37
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4610d0ffe9ada041ca0c4e403704035b7289e88aff074f197232899198587d3d
|
4
|
+
data.tar.gz: fef606de0dc20dac143b832ecf2c4cd2eb9507f46aa0378fe933763a6cd69ab2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 602cbf1217eb6c9cbd2934d34900ddd4294e65daf5af5b8abe1355af95f2f0142f7d9ea634b49e3e31dd87663b6874c4b158a6988c3be2041637310a330a6a84
|
7
|
+
data.tar.gz: 04ccd21c66b4e7f1ff3e65388cc609db794255cbe2e2c1fc12327f51f8823af11929215cb04fcc5fc7d646da8256cec75a7d69df12f4514c9e8b6cc10b803aa3
|
data/lib/jekyll-thumbnail-img.rb
CHANGED
@@ -3,56 +3,61 @@ require 'mini_magick'
|
|
3
3
|
|
4
4
|
module Jekyll
|
5
5
|
class JekyllThumbnailImg < Liquid::Tag
|
6
|
+
@@pending = [] # thumbnails to be generated
|
7
|
+
|
6
8
|
def initialize(tag_name, markup, tokens)
|
7
9
|
super
|
8
10
|
@markup = markup
|
9
11
|
end
|
10
12
|
|
11
13
|
def render(context)
|
12
|
-
source, width = @markup.split
|
14
|
+
source, width = @markup.split.map(&:strip)
|
15
|
+
# resolve liquid vars
|
16
|
+
source = context[source] if context[source]
|
17
|
+
width = context[width] if context[width]
|
13
18
|
|
14
|
-
|
15
|
-
if context[source]
|
16
|
-
source = context[source]
|
17
|
-
end
|
18
|
-
unless source && width
|
19
|
-
raise "Usage: {% thumbnail_img /path/to/local/image.png 500 %}"
|
20
|
-
end
|
19
|
+
raise "Usage: {% thumbnail_img /path/to/image.png 500 %}" unless source && width
|
21
20
|
|
22
|
-
|
21
|
+
site = context.registers[:site]
|
22
|
+
raise "File #{source} not found" unless File.exist?(File.join(site.source, source))
|
23
23
|
|
24
|
-
#
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
# Queue thumbnail for generation
|
25
|
+
thumb_filename = "#{File.basename(source, '.*')}_#{width}w#{File.extname(source)}"
|
26
|
+
cache_dir = File.join(site.config["cache_dir"], "jekyll-thumbnail-img/img-cache", File.dirname(source), "thumbnails")
|
27
|
+
dest = File.join(File.dirname(source), "thumbnails", thumb_filename)
|
28
|
+
|
29
|
+
# Add to pending list if not already there
|
30
|
+
@@pending << {
|
31
|
+
source: File.join(site.source, source),
|
32
|
+
cache: File.join(cache_dir, thumb_filename),
|
33
|
+
dest: File.join(site.dest, dest),
|
34
|
+
width: width
|
35
|
+
} unless @@pending.any? { |thumb| thumb[:source] == source && thumb[:width] == width }
|
28
36
|
|
29
|
-
# Calculate the relative path to the 'thumbnails' directory
|
30
|
-
relative_dest_dir = File.join(File.dirname(source), "thumbnails")
|
31
|
-
|
32
|
-
# Create the 'thumbnails' directory within the source directory
|
33
|
-
dest_dir = File.join(context.registers[:site].source, relative_dest_dir)
|
34
|
-
Dir.mkdir(dest_dir) unless Dir.exist?(dest_dir)
|
35
|
-
|
36
|
-
ext = File.extname(source)
|
37
|
-
dest = File.join(relative_dest_dir, File.basename(source, ext) + "_#{width}w#{ext}")
|
38
|
-
|
39
|
-
# Generate the thumbnail if it doesn't exist or if the source file was modified more recently than the thumbnail
|
40
|
-
full_dest_path = File.join(context.registers[:site].source, dest)
|
41
|
-
if !File.exist?(full_dest_path) || File.mtime(full_dest_path) <= File.mtime(source_path)
|
42
|
-
image = MiniMagick::Image.open(source_path)
|
43
|
-
image.resize("#{width}x")
|
44
|
-
image.write(full_dest_path)
|
45
|
-
Jekyll.logger.info "JekyllThumbnailImg", "Generating: " + File.join(context.registers[:site].source, dest)
|
46
|
-
context["site"]["static_files"] << StaticFile.new(
|
47
|
-
context.registers[:site],
|
48
|
-
context.registers[:site].source,
|
49
|
-
@dir,
|
50
|
-
dest
|
51
|
-
)
|
52
|
-
end
|
53
37
|
dest
|
54
38
|
end
|
39
|
+
|
40
|
+
def self.generate_thumbnails(site)
|
41
|
+
@@pending.each do |thumb|
|
42
|
+
FileUtils.mkdir_p(File.dirname(thumb[:cache]))
|
43
|
+
FileUtils.mkdir_p(File.dirname(thumb[:dest]))
|
44
|
+
|
45
|
+
# Generate thumbnail if not already generated or source is newer
|
46
|
+
if !File.exist?(thumb[:cache]) || File.mtime(thumb[:cache]) <= File.mtime(thumb[:source])
|
47
|
+
Jekyll.logger.info "JekyllThumbnailImg:", "Generating: #{thumb[:dest].sub(site.dest + '/', '')}"
|
48
|
+
MiniMagick::Image.open(thumb[:source]).resize("#{thumb[:width]}x").write(thumb[:cache])
|
49
|
+
end
|
50
|
+
|
51
|
+
# Copy from cache to destination
|
52
|
+
FileUtils.cp(thumb[:cache], thumb[:dest])
|
53
|
+
end
|
54
|
+
@@pending.clear
|
55
|
+
end
|
55
56
|
end
|
56
57
|
end
|
57
58
|
|
59
|
+
Jekyll::Hooks.register :site, :post_write do |site|
|
60
|
+
Jekyll::JekyllThumbnailImg.generate_thumbnails(site)
|
61
|
+
end
|
62
|
+
|
58
63
|
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.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Abhishek Paudel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2025-01-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jekyll
|