jekyll-image_optimizer 1.1.0 → 1.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.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3695bb55dbb989f9f054a6441e485e8261b5aacb
|
4
|
+
data.tar.gz: 17c68e09b4fa2361de2a76c84ba5e227e0c1e237
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e34e3c0a8ef34e18d9b9ac70197011ba150245fe60409e71d5901fa095d79c53c677b550e51cf7452ec935d3ed06ea9a067c8e765bbe8a47e838f0e5a1714eb2
|
7
|
+
data.tar.gz: a2cd559dee33fe01591e5a16ff2c7ef6be8218505e1067942a873da387dd5e93611a255f90a18e00ca055309e5529ce3e300ec7f6f894a7dffc102851ce8a217
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class ImageHash
|
2
|
+
def initialize(use_hash)
|
3
|
+
@use_hash=use_hash
|
4
|
+
end
|
5
|
+
|
6
|
+
def file_search_pattern(file)
|
7
|
+
pos=file.rindex('/')
|
8
|
+
self.dir_search_pattern(file[0..pos-1], file[pos+1..-1])
|
9
|
+
end
|
10
|
+
|
11
|
+
def dir_search_pattern(dir, file)
|
12
|
+
if @use_hash
|
13
|
+
pos=file.rindex('.')
|
14
|
+
dir+'/'+file[0..pos-1]+'-*'+file[pos..-1]
|
15
|
+
else
|
16
|
+
dir+'/'+file
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def name_with_hash(file)
|
21
|
+
pos=file.rindex('.')
|
22
|
+
file[0..pos-1]+'-'+Digest::MD5.file(file).hexdigest+file[pos..-1]
|
23
|
+
end
|
24
|
+
|
25
|
+
def with_hash(file)
|
26
|
+
return file unless @use_hash
|
27
|
+
new_file=self.name_with_hash(file)
|
28
|
+
File.rename(file, new_file)
|
29
|
+
new_file
|
30
|
+
end
|
31
|
+
|
32
|
+
def without_hash(file)
|
33
|
+
@use_hash ? file.sub(/-[0-9a-f]{32}\./, '.') : file
|
34
|
+
end
|
35
|
+
|
36
|
+
def hash?(file)
|
37
|
+
@use_hash ? file.match(/-[0-9a-f]{32}\./)!=nil : true
|
38
|
+
end
|
39
|
+
end
|
@@ -2,8 +2,8 @@ require_relative 'synchronizer.rb'
|
|
2
2
|
require 'RMagick'
|
3
3
|
|
4
4
|
class ImageOptimizer < Synchronizer
|
5
|
-
def initialize(source_path, target_path)
|
6
|
-
super(source_path, target_path)
|
5
|
+
def initialize(source_path, target_path, use_hash)
|
6
|
+
super(source_path, target_path, use_hash)
|
7
7
|
end
|
8
8
|
|
9
9
|
def optimize_images(geometry)
|
@@ -1,7 +1,8 @@
|
|
1
1
|
class Synchronizer
|
2
|
-
def initialize(source_path, target_path)
|
2
|
+
def initialize(source_path, target_path, use_hash)
|
3
3
|
@source_path=source_path
|
4
4
|
@target_path=target_path
|
5
|
+
@hash=ImageHash.new(use_hash)
|
5
6
|
end
|
6
7
|
|
7
8
|
def create_symlink(name, param)
|
@@ -31,11 +32,17 @@ class Synchronizer
|
|
31
32
|
|
32
33
|
def synchronize_file(source, target, param)
|
33
34
|
source_time = File.mtime(source)
|
34
|
-
|
35
|
+
target_found=Dir[@hash.file_search_pattern(target)]
|
36
|
+
target=target_found[0] unless target_found.empty?
|
37
|
+
if !File.file?(target) or File.mtime(target) != source_time or !@hash.hash?(target)
|
38
|
+
target=@hash.without_hash(target)
|
35
39
|
target_dir=File.dirname(target)
|
36
40
|
Dir.mkdir(target_dir) unless File.directory? target_dir
|
37
41
|
do_synchronize_file(source, target, param)
|
38
|
-
|
42
|
+
if File.file?(target)
|
43
|
+
target=@hash.with_hash(target)
|
44
|
+
File.utime(source_time, source_time, target)
|
45
|
+
end
|
39
46
|
end
|
40
47
|
end
|
41
48
|
|
@@ -60,10 +67,13 @@ class Synchronizer
|
|
60
67
|
files_and_dirs_by_length=Dir["#{target_path}/**/*"].sort_by { |dir| File.file?(dir) ? ('a'+dir) : ('b'+(1000-dir.size).to_s) }
|
61
68
|
for target in files_and_dirs_by_length
|
62
69
|
source=target.sub(target_path, @source_path)
|
70
|
+
source=@hash.without_hash(source)
|
63
71
|
if File.directory? target
|
64
72
|
delete_dir(target) unless Dir.exists? source
|
65
73
|
else
|
66
|
-
|
74
|
+
unless File.file? source and File.mtime(source)==File.mtime(target)
|
75
|
+
delete_file(target)
|
76
|
+
end
|
67
77
|
end
|
68
78
|
end
|
69
79
|
end
|
@@ -77,4 +87,5 @@ class Synchronizer
|
|
77
87
|
puts "deleting #{file}"
|
78
88
|
File.delete(file)
|
79
89
|
end
|
90
|
+
|
80
91
|
end
|
@@ -1,10 +1,16 @@
|
|
1
1
|
require_relative 'image_optimizer/image_optimizer'
|
2
|
+
require_relative 'image_optimizer/synchronizer'
|
3
|
+
require_relative 'image_optimizer/image_hash'
|
2
4
|
require 'RMagick'
|
3
5
|
|
4
6
|
def opt_dir(config)
|
5
7
|
config['opt_images'] || 'img/opt'
|
6
8
|
end
|
7
9
|
|
10
|
+
def use_hash(config)
|
11
|
+
(config['image_hash'] || 'true')=='true'
|
12
|
+
end
|
13
|
+
|
8
14
|
module Jekyll
|
9
15
|
|
10
16
|
class JekyllImageOptimizer < Generator
|
@@ -15,10 +21,11 @@ module Jekyll
|
|
15
21
|
@opt=opt_dir(config)
|
16
22
|
@symlink=config['images_link'] || 'images'
|
17
23
|
@geometry=config['image_geometry'] || '800x800>'
|
24
|
+
@hash=use_hash(config)
|
18
25
|
end
|
19
26
|
|
20
27
|
def generate(site)
|
21
|
-
io=ImageOptimizer.new(@raw, @opt)
|
28
|
+
io=ImageOptimizer.new(@raw, @opt, @hash)
|
22
29
|
if @geometry.is_a? Enumerable
|
23
30
|
@geometry.each { |geom| io.optimize_images(geom) }
|
24
31
|
else
|
@@ -32,7 +39,6 @@ module Jekyll
|
|
32
39
|
|
33
40
|
def initialize(tag_name, markup, tokens)
|
34
41
|
super
|
35
|
-
@markup
|
36
42
|
end
|
37
43
|
|
38
44
|
def render(context)
|
@@ -40,10 +46,10 @@ module Jekyll
|
|
40
46
|
base_url=context['site']['baseurl']
|
41
47
|
index=Liquid::Template.parse(@markup).render(context).to_i
|
42
48
|
src=context['page']['image'][index]['url']
|
43
|
-
|
44
|
-
|
45
|
-
if File.file? file
|
46
|
-
img=Magick::Image
|
49
|
+
hash=ImageHash.new(use_hash(context['site']))
|
50
|
+
for file in Dir[hash.dir_search_pattern(opt_dir(context['site'])+'*', src)]
|
51
|
+
if File.file? file and hash.hash? file
|
52
|
+
img=Magick::Image.ping(file).first
|
47
53
|
s+=base_url+'/'+file+' '+img.columns.to_s+'w,'
|
48
54
|
end
|
49
55
|
end
|
@@ -51,6 +57,29 @@ module Jekyll
|
|
51
57
|
end
|
52
58
|
end
|
53
59
|
|
60
|
+
class SrcTag < Liquid::Tag
|
61
|
+
|
62
|
+
def initialize(tag_name, markup, tokens)
|
63
|
+
super
|
64
|
+
if markup =~ /\s*(.*?)\s+(.*?)\s*/
|
65
|
+
@dir = $1
|
66
|
+
@index = $2
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
def render(context)
|
71
|
+
base_url=context['site']['baseurl']
|
72
|
+
index=Liquid::Template.parse(@index).render(context).to_i
|
73
|
+
src=context['page']['image'][index]['url']
|
74
|
+
hash=ImageHash.new(use_hash(context['site']))
|
75
|
+
for file in Dir[hash.dir_search_pattern(opt_dir(context['site'])+@dir, src)]
|
76
|
+
if File.file? file and hash.hash? file
|
77
|
+
return base_url+'/'+file
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
54
83
|
class ImageTag < Liquid::Tag
|
55
84
|
|
56
85
|
def initialize(tag_name, markup, tokens)
|
@@ -74,4 +103,5 @@ end
|
|
74
103
|
|
75
104
|
|
76
105
|
Liquid::Template.register_tag('srcset', Jekyll::SrcsetTag)
|
106
|
+
Liquid::Template.register_tag('src', Jekyll::SrcTag)
|
77
107
|
Liquid::Template.register_tag('image', Jekyll::ImageTag)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jekyll-image_optimizer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefan Niederhauser
|
@@ -32,6 +32,7 @@ extensions: []
|
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
34
|
- lib/jekyll/image_optimizer.rb
|
35
|
+
- lib/jekyll/image_optimizer/image_hash.rb
|
35
36
|
- lib/jekyll/image_optimizer/image_optimizer.rb
|
36
37
|
- lib/jekyll/image_optimizer/synchronizer.rb
|
37
38
|
homepage: https://github.com/nidi3/jekyll-image_optimizer
|