jekyll-webp-resize 1.0.4 → 1.0.5

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: c06771078704e438beb5cdabcd4d26bb95a7d6c0
4
- data.tar.gz: 72b8429afd77c194d4005c58ed8ce811335a44e0
3
+ metadata.gz: 2e9dff223f4761869e7f5402374348a5e827837d
4
+ data.tar.gz: 35c2334fb968df5c8ef1149057e07673c8025525
5
5
  SHA512:
6
- metadata.gz: e0c6c2eec3646d44d725ab40a3f97ecf9ebedadeebbb01e9b7c33d6ec90c35fe0e49dad53ec0f43a692257ceb52092b8d63cec68f373ee85521e94cf47218c8e
7
- data.tar.gz: 134a901737fb7dfbbab44dc5610da9056dce6bc1430e7c98986c9bed959b04116f7c3da4eac31e1f91aa2a547fbbd3d1d56436e150d04debcd99f9663cdd4dca
6
+ metadata.gz: b65563476de4a21ecedea13cf14ad690b2284ea8328344e49571fb4bb302ebb900b733ca74346473e88ef9117b7e763c37762625d152e865c18cde39239615ad
7
+ data.tar.gz: ac5fe30d7ad575b259f7cf3f10bbd7d3d9d93f19d2303bd27e46986e2496e6baaca44c9b56d97795fcbb650d906e7c97fcb2533a5cb56fd6a925a14cbfc8700c
Binary file
Binary file
@@ -23,4 +23,5 @@ Gem::Specification.new do |spec|
23
23
  spec.add_development_dependency "bundler", "~> 1.5"
24
24
  spec.add_development_dependency "rake", "~> 1.5"
25
25
  spec.add_development_dependency "minitest", '~> 5.4', '>= 5.4.3'
26
+ spec.add_development_dependency "fastimage", '~> 2.1'
26
27
  end
@@ -19,6 +19,7 @@ module Jekyll
19
19
  # List of directories containing images to optimize, Nested directories will not be checked
20
20
  'img_dir' => ["/img"],
21
21
 
22
+ 'resize' => [1024, 512, 256],
22
23
  # add ".gif" to the format list to generate webp for animated gifs as well
23
24
  'formats' => [".jpeg", ".jpg", ".png", ".tiff"],
24
25
 
@@ -1,6 +1,6 @@
1
1
  module Jekyll
2
2
  module Webp
3
- VERSION = "1.0.4"
3
+ VERSION = "1.0.5"
4
4
  # When modifying remember to issue a new tag command in git before committing, then push the new tag
5
5
  # git tag -a v0.1.0 -m "Gem v0.1.0"
6
6
  # git push origin --tags
@@ -1,11 +1,14 @@
1
1
  require 'open3'
2
+ require 'fastimage'
2
3
 
3
4
  module Jekyll
4
5
  module Webp
5
6
  class WebpExec
6
7
  # Runs the WebP executable for the given input parameters
7
8
  # the function detects the OS platform and architecture automatically
8
- def self.run(flags, input_file, output_file)
9
+ def self.run(input_file, output_file, flags, size)
10
+ width, height = FastImage.size(input_file)
11
+
9
12
  # What is the path to the execs inside the gem? perhaps just bin/?
10
13
  bin_path = "bin/"
11
14
 
@@ -21,9 +24,17 @@ module Jekyll
21
24
  # Construct the full path to the executable
22
25
  full_path = File.join(gem_root, bin_path, exe_name)
23
26
 
24
- Jekyll.logger.info("Webp:", "Converting #{input_file}")
27
+ Jekyll.logger.info("Webp:", "Generating #{output_file}")
25
28
  # Construct the full program call
26
- cmd = "\"#{full_path}\" -quiet -mt #{flags} \"#{input_file}\" -o \"#{output_file}\""
29
+ cmd = "\"#{full_path}\" -quiet -mt #{flags} \"#{input_file}\" "
30
+ if size != 0
31
+ if width > height
32
+ cmd += "-resize #{size.to_s} 0 "
33
+ else
34
+ cmd += "-resize 0 #{size.to_s} "
35
+ end
36
+ end
37
+ cmd += "-o \"#{output_file}\""
27
38
 
28
39
  # Execute the command
29
40
  exit_code = 0
@@ -18,6 +18,24 @@ module Jekyll
18
18
  # This generator should be passive with regard to its execution
19
19
  priority :lowest
20
20
 
21
+ # Return a map {size -> filename}
22
+ def compute_file_resizes(output_file, resize=@config['resize'])
23
+ output = {0 => output_file}
24
+ if resize.length == 0
25
+ return output
26
+ end
27
+
28
+ for resize_entry in resize
29
+ dirname = File.dirname(output_file)
30
+ extension = File.extname(output_file)
31
+ basename = File.basename(output_file, extension)
32
+ new_output_file = dirname + "/" + basename + "-" + resize_entry.to_s + extension
33
+ output[resize_entry] = new_output_file
34
+ end
35
+
36
+ return output
37
+ end
38
+
21
39
  def generate(site)
22
40
  # Retrieve and merge the configuration from the site yml file
23
41
  @config = DEFAULT.merge(site.config['webp'] || {})
@@ -54,25 +72,27 @@ module Jekyll
54
72
  FileUtils::mkdir_p(destination_directory + prefix)
55
73
  output_full_path = File.join(destination_directory + prefix, filename)
56
74
 
57
- # Keep the webp file from being cleaned by Jekyll
58
- site.static_files << WebpFile.new(site,
59
- site.dest,
60
- File.join(imgdir, prefix),
61
- filename)
62
-
63
- # Check if the file already has a webp alternative?
64
- # If we're force rebuilding all webp files then ignore the check
65
- # also check the modified time on the files to ensure that the webp file
66
- # is newer than the source file, if not then regenerate
67
- next if File.file?(output_full_path) && File.mtime(output_full_path) > File.mtime(file)
68
-
69
- if File.file?(output_full_path) && File.mtime(output_full_path) <= File.mtime(file)
70
- Jekyll.logger.info("WebP:", "Change to source image file #{file} detected, regenerating WebP")
75
+ compute_file_resizes(output_full_path).each do |size, output|
76
+ # Check if the file already has a webp alternative?
77
+ # If we're force rebuilding all webp files then ignore the check
78
+ # also check the modified time on the files to ensure that the webp file
79
+ # is newer than the source file, if not then regenerate
80
+ if !File.file?(output) ||
81
+ (File.mtime(output) <= File.mtime(file))
82
+ # Jekyll.logger.info("WebP:", "Change to image file #{file} detected, regenerating WebP")
83
+
84
+ # Generate the file
85
+ WebpExec.run(file, output, @config['flags'], size)
86
+ files_generated += 1
87
+ end
88
+ if File.file?(output)
89
+ # Keep the webp file from being cleaned by Jekyll
90
+ site.static_files << WebpFile.new(site,
91
+ site.dest,
92
+ File.join(imgdir, prefix),
93
+ File.basename(output))
94
+ end
71
95
  end
72
-
73
- # Generate the file
74
- WebpExec.run(@config['flags'], file, output_full_path)
75
- files_generated += 1
76
96
  end # dir.foreach
77
97
  end # img_dir
78
98
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-webp-resize
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sverrir Sigmundarson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-11-24 00:00:00.000000000 Z
12
+ date: 2017-12-01 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jekyll
@@ -73,6 +73,20 @@ dependencies:
73
73
  - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: 5.4.3
76
+ - !ruby/object:Gem::Dependency
77
+ name: fastimage
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.1'
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '2.1'
76
90
  description: WebP Image Generator for Jekyll 3 Sites that automatically generate WebP
77
91
  images for all images on your static site and serves them when possible.
78
92
  email: