middleman-webp 1.0.1 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a6cebf6f4bd680eae44d288470a613aeb522957193a60a4f11289d6df0ac281b
4
- data.tar.gz: 6f549838d63f14529001e24493aca41620baa83041841d02c912686757c69de1
3
+ metadata.gz: dac0600e3003f02ae0bc163f05c058dc28ece25bb3aca636b2e0e5f276d36a19
4
+ data.tar.gz: ee43f1f79fc1be6bf0d551586215842c21765746e39cec5aca46792ebfaf2185
5
5
  SHA512:
6
- metadata.gz: 725fb936092df4350d64e63a7940f8404d2fa50adf520ae2a39f84170d642470a3319dedab34a5faa1d472728890c6da76296f23082699fee855a7fee49d3185
7
- data.tar.gz: 7751fe51a977459985f51c30df472753a9b77de96d8a38b67d017b00f9f8dbb1ae39f1de30fb3299d6c1d18086d653716e278542a50a79c98d5ebff666d02e4c
6
+ metadata.gz: 0d9563457ee228b74f0de64de914c07f6617299f484bc1cdb8f956c70010a4640005fd970b792cd3555df2f1daf486417e1a9267b9975c91dfd21b1a02c75ebc
7
+ data.tar.gz: b7381592fe36293329b65cd8e1c0f1109a1d3efc3ed17ffaeb2d15c1d415a225713d05182be1ca1886b4f5bbcb752f658904eb718dd47412ebc9dce5268470bf
@@ -18,6 +18,3 @@ jobs:
18
18
  sudo apt-get install -y webp
19
19
  bundle install
20
20
  bundle exec rake test
21
- - uses: joshmfrankel/simplecov-check-action@main
22
- with:
23
- github_token: ${{ secrets.GITHUB_TOKEN }}
data/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## 1.0.3
2
+
3
+ - Fix converting large number of files. ([PR
4
+ #24](https://github.com/iiska/middleman-webp/pull/24))
5
+
6
+ ## 1.0.2
7
+
8
+ - Fix logic for selecting command line args for the most precise filename
9
+ pattern, if multiple patterns would match
10
+
11
+ ## 1.0.1
12
+
13
+ - Add `required_ruby_version` to gemspec.
14
+
1
15
  ## 1.0.0
2
16
 
3
17
  - Add support for Ruby 2.7, 3,0, 3.1, 3.2
data/README.md CHANGED
@@ -52,8 +52,8 @@ You are not able to use latest gem version if you are using **Middleman 3**. In
52
52
  that case install gem with following Gemfile statement:
53
53
 
54
54
  gem 'middleman-webp', '~> 0.3.2'
55
-
56
-
55
+
56
+
57
57
  ### Using with Rubies <= 2.4
58
58
 
59
59
  Support for Rubies <= 2.4 was dropped in 1.0.0 version of this gem. If you are
@@ -167,3 +167,4 @@ Look for [this example how to do it in .htaccess][htaccess].
167
167
  - [Ryan Townsend](https://github.com/ryantownsend)
168
168
  - [François VANTOMME](https://github.com/akarzim)
169
169
  - [Novtopro He](https://github.com/Novtopro)
170
+ - [Joshua Wood](https://github.com/joshuap)
@@ -34,9 +34,12 @@ module Middleman
34
34
  begin
35
35
  dst = destination_path(p)
36
36
  exec_convert_tool(p, dst)
37
- yield File.new(p), File.new(dst.to_s)
37
+ yield (src_file = File.new(p)), (dest_file = File.new(dst.to_s))
38
38
  rescue StandardError => e
39
39
  @builder.trigger :error, "Converting #{p} failed", e.backtrace
40
+ ensure
41
+ src_file&.close
42
+ dest_file&.close
40
43
  end
41
44
  end
42
45
  end
@@ -35,7 +35,7 @@ module Middleman
35
35
 
36
36
  return '' if matching.empty?
37
37
 
38
- matching.sort { |(ga, _oa), (gb, _ob)| gb.size <=> ga.size }[0][1]
38
+ matching.max_by { |(pathname_matcher, _oa)| pathname_matcher }[1]
39
39
  end
40
40
 
41
41
  private
@@ -1,6 +1,9 @@
1
1
  module Middleman
2
2
  module WebP
3
3
  class PathnameMatcher
4
+ include Comparable
5
+ attr_reader :pattern
6
+
4
7
  # Initializes matcher with given pattern.
5
8
  #
6
9
  # pattern - Pattern to match pathnames against to. May be
@@ -18,6 +21,21 @@ module Middleman
18
21
  send match_method, Pathname.new(path)
19
22
  end
20
23
 
24
+ # Compares matchers based on their preciness.
25
+ #
26
+ # - One with longest pattern is considered to be more precise
27
+ # - Glob or Regexp patterns are considered more precise than procs.
28
+ def <=>(other)
29
+ is_proc_involed = other.pattern.respond_to?(:call) || @pattern.respond_to?(:call)
30
+ return compare_to_proc(other) if is_proc_involed
31
+
32
+ @pattern.to_s.length <=> other.pattern.to_s.length
33
+ end
34
+
35
+ def hash
36
+ @pattern.hash
37
+ end
38
+
21
39
  private
22
40
 
23
41
  def match_method
@@ -42,6 +60,19 @@ module Middleman
42
60
  def matches_proc?(path)
43
61
  @pattern.call(path.to_s)
44
62
  end
63
+
64
+ def compare_to_proc(other)
65
+ i_am_proc = @pattern.respond_to?(:call)
66
+ other_is_proc = other.pattern.respond_to?(:call)
67
+
68
+ if i_am_proc && !other_is_proc
69
+ return -1
70
+ elsif !i_am_proc && other_is_proc
71
+ return 1
72
+ end
73
+
74
+ 0
75
+ end
45
76
  end
46
77
  end
47
78
  end
@@ -1,5 +1,5 @@
1
1
  module Middleman
2
2
  module Webp
3
- VERSION = '1.0.1'.freeze
3
+ VERSION = '1.0.3'.freeze
4
4
  end
5
5
  end
@@ -36,7 +36,7 @@ describe Middleman::WebPExtension do
36
36
  @extension = Middleman::WebPExtension.new(app_mock)
37
37
  @extension.before_build(@builder)
38
38
 
39
- Dir.glob('spec/fixtures/ok-source/**/*.webp').size.must_equal 0
39
+ value(Dir.glob('spec/fixtures/ok-source/**/*.webp').size).must_equal 0
40
40
  end
41
41
 
42
42
  it 'generates WebP versions using external tools when option is set' do
@@ -65,7 +65,7 @@ describe Middleman::WebPExtension do
65
65
  end
66
66
  @extension.before_build(@builder)
67
67
 
68
- Dir.glob('spec/fixtures/ok-source/**/*.webp').size.must_equal 2
68
+ value(Dir.glob('spec/fixtures/ok-source/**/*.webp').size).must_equal 2
69
69
  end
70
70
  end
71
71
 
@@ -95,7 +95,7 @@ describe Middleman::WebPExtension do
95
95
  @extension = Middleman::WebPExtension.new(app_mock)
96
96
  @extension.after_build(@builder)
97
97
 
98
- Dir.glob('spec/fixtures/ok-build/**/*.webp').size.must_equal 2
98
+ value(Dir.glob('spec/fixtures/ok-build/**/*.webp').size).must_equal 2
99
99
  end
100
100
 
101
101
  it 'shows errors if files couldn\'t be converted' do
@@ -146,7 +146,7 @@ describe Middleman::WebPExtension do
146
146
  @extension = Middleman::WebPExtension.new(app_mock)
147
147
  @extension.after_build(@builder)
148
148
 
149
- Dir.glob('spec/fixtures/dummy-build/**/*.webp').size.must_equal 0
149
+ value(Dir.glob('spec/fixtures/dummy-build/**/*.webp').size).must_equal 0
150
150
  end
151
151
  end
152
152
  end
@@ -12,7 +12,7 @@ describe Middleman::WebP::Converter do
12
12
  describe '#destination_path' do
13
13
  it 'returns file name with same basename and webp suffix' do
14
14
  d = @converter.destination_path(Pathname.new('build/images/sample.jpg'))
15
- d.to_s.must_equal 'build/images/sample.webp'
15
+ value(d.to_s).must_equal 'build/images/sample.webp'
16
16
  end
17
17
  end
18
18
 
@@ -24,52 +24,52 @@ describe Middleman::WebP::Converter do
24
24
 
25
25
  it 'returns file name with same basename and webp suffix' do
26
26
  d = @converter.destination_path(Pathname.new('build/images/sample.jpg'))
27
- d.to_s.must_equal 'build/images/sample.jpg.webp'
27
+ value(d.to_s).must_equal 'build/images/sample.jpg.webp'
28
28
  end
29
29
  end
30
30
 
31
31
  describe '#change_percentage' do
32
32
  it 'returns how many percents smaller destination file is' do
33
- @converter.change_percentage(10_000, 8746).must_equal '12.54 %'
33
+ value(@converter.change_percentage(10_000, 8746)).must_equal '12.54 %'
34
34
  end
35
35
 
36
36
  it 'omits zeroes in the end of decimal part' do
37
- @converter.change_percentage(100, 76).must_equal '24 %'
37
+ value(@converter.change_percentage(100, 76)).must_equal '24 %'
38
38
  end
39
39
 
40
40
  it 'returns 0% when original and new size are both 0' do
41
- @converter.change_percentage(0, 0).must_equal '0 %'
41
+ value(@converter.change_percentage(0, 0)).must_equal '0 %'
42
42
  end
43
43
  end
44
44
 
45
45
  describe '#number_to_human_size' do
46
46
  it 'uses human readable unit' do
47
- @converter.number_to_human_size(100).must_equal '100 B'
48
- @converter.number_to_human_size(1234).must_equal '1.21 KiB'
49
- @converter.number_to_human_size(2_634_234).must_equal '2.51 MiB'
47
+ value(@converter.number_to_human_size(100)).must_equal '100 B'
48
+ value(@converter.number_to_human_size(1234)).must_equal '1.21 KiB'
49
+ value(@converter.number_to_human_size(2_634_234)).must_equal '2.51 MiB'
50
50
  end
51
51
 
52
52
  it 'handles zero size properly' do
53
- @converter.number_to_human_size(0).must_equal '0 B'
53
+ value(@converter.number_to_human_size(0)).must_equal '0 B'
54
54
  end
55
55
  end
56
56
 
57
57
  describe '#tool_for' do
58
58
  it 'uses gif2webp for gif files' do
59
59
  path = Pathname.new('/some/path/image.gif')
60
- @converter.tool_for(path).must_equal 'gif2webp'
60
+ value(@converter.tool_for(path)).must_equal 'gif2webp'
61
61
  end
62
62
 
63
63
  it 'uses cwebp for jpeg, png and tiff files' do
64
- @converter.tool_for(Pathname('/some/path/image.jpg')).must_equal 'cwebp'
65
- @converter.tool_for(Pathname('/some/path/image.png')).must_equal 'cwebp'
66
- @converter.tool_for(Pathname('/some/path/image.tiff')).must_equal 'cwebp'
64
+ value(@converter.tool_for(Pathname('/some/path/image.jpg'))).must_equal 'cwebp'
65
+ value(@converter.tool_for(Pathname('/some/path/image.png'))).must_equal 'cwebp'
66
+ value(@converter.tool_for(Pathname('/some/path/image.tiff'))).must_equal 'cwebp'
67
67
  end
68
68
  end
69
69
 
70
70
  describe '#image_files' do
71
71
  it 'includes all image files in Middleman build dir' do
72
- @converter.image_files.size.must_equal 3
72
+ value(@converter.image_files.size).must_equal 3
73
73
  end
74
74
 
75
75
  it 'won\'t include ignored files' do
@@ -77,7 +77,7 @@ describe Middleman::WebP::Converter do
77
77
  ignore: [/jpg$/, '**/*.gif']
78
78
 
79
79
  files_to_include = [Pathname('spec/fixtures/dummy-build/empty.png')]
80
- @converter.image_files.must_equal files_to_include
80
+ value(@converter.image_files).must_equal files_to_include
81
81
  end
82
82
 
83
83
  it 'won\'t include files rejected by given proc' do
@@ -86,7 +86,7 @@ describe Middleman::WebP::Converter do
86
86
  }
87
87
  @converter = Middleman::WebP::Converter.new @app_mock, nil, options
88
88
 
89
- @converter.image_files.size.must_equal 2
89
+ value(@converter.image_files.size).must_equal 2
90
90
  end
91
91
  end
92
92
  end
@@ -17,7 +17,7 @@ describe Middleman::WebPExtension do
17
17
  Shell.any_instance.expects(:find_system_command).with('cwebp').returns('/usr/bin/cwebp')
18
18
  Shell.any_instance.expects(:find_system_command).with('gif2webp').returns('/usr/bin/gif2webp')
19
19
 
20
- @extension.dependencies_installed?(@builder_mock).must_equal true
20
+ value(@extension.dependencies_installed?(@builder_mock)).must_equal true
21
21
  end
22
22
 
23
23
  it 'returns false and displays error if cwebp is missing' do
@@ -27,7 +27,7 @@ describe Middleman::WebPExtension do
27
27
  @builder_mock.expects(:trigger).once.with do |event|
28
28
  event == :error
29
29
  end
30
- @extension.dependencies_installed?(@builder_mock).must_equal false
30
+ value(@extension.dependencies_installed?(@builder_mock)).must_equal false
31
31
  end
32
32
 
33
33
  it 'displays error if only gif2webp is missing and returns still true' do
@@ -37,7 +37,7 @@ describe Middleman::WebPExtension do
37
37
  @builder_mock.expects(:trigger).once.with do |event, _target, msg|
38
38
  event == :webp && msg =~ /gif2webp/
39
39
  end
40
- @extension.dependencies_installed?(@builder_mock).must_equal true
40
+ value(@extension.dependencies_installed?(@builder_mock)).must_equal true
41
41
  end
42
42
  end
43
43
  end
@@ -6,12 +6,12 @@ describe Middleman::WebP::Options do
6
6
  describe '#allow_skip' do
7
7
  it 'should default to true' do
8
8
  options = Middleman::WebP::Options.new
9
- options.allow_skip.must_equal(true)
9
+ value(options.allow_skip).must_equal(true)
10
10
  end
11
11
 
12
12
  it 'should allow setting to true' do
13
13
  options = Middleman::WebP::Options.new(allow_skip: false)
14
- options.allow_skip.must_equal(false)
14
+ value(options.allow_skip).must_equal(false)
15
15
  end
16
16
  end
17
17
 
@@ -29,7 +29,7 @@ describe Middleman::WebP::Options do
29
29
  options = Middleman::WebP::Options.new options_hash
30
30
 
31
31
  args = options.for(path)
32
- args.must_match(/^(-q 85|-lossless) (-q 85|-lossless)$/)
32
+ value(args).must_match(/^(-q 85|-lossless) (-q 85|-lossless)$/)
33
33
  end
34
34
 
35
35
  it 'returns empty string when no options are defined' do
@@ -37,7 +37,7 @@ describe Middleman::WebP::Options do
37
37
  options = Middleman::WebP::Options.new
38
38
 
39
39
  args = options.for(path)
40
- args.must_be_empty
40
+ value(args).must_be_empty
41
41
  end
42
42
 
43
43
  it 'returns cwebp args when given file matches option pattern regexp' do
@@ -52,7 +52,25 @@ describe Middleman::WebP::Options do
52
52
  options = Middleman::WebP::Options.new options_hash
53
53
 
54
54
  args = options.for(path)
55
- args.must_match(/^-q 85$/)
55
+ value(args).must_match(/^-q 85$/)
56
+ end
57
+
58
+ it 'selects most precise file pattern to get file specific option overrides correctly' do
59
+ path = Pathname.new('lizard.jpg')
60
+ options_hash = {
61
+ conversion_options: {
62
+ '*.jpg' => {
63
+ q: 85
64
+ },
65
+ 'lizard.jpg' => {
66
+ q: 100
67
+ }
68
+ }
69
+ }
70
+ options = Middleman::WebP::Options.new options_hash
71
+
72
+ args = options.for(path)
73
+ value(args).must_match(/^-q 100$/)
56
74
  end
57
75
  end
58
76
  end
@@ -14,7 +14,7 @@ describe Middleman::WebP::PathnameMatcher do
14
14
  patterns.each do |p|
15
15
  matcher = Middleman::WebP::PathnameMatcher.new(p)
16
16
  files.each do |f|
17
- matcher.matches?(f).must_equal true, "Pattern: #{p.inspect}, "\
17
+ value(matcher.matches?(f)).must_equal true, "Pattern: #{p.inspect}, "\
18
18
  "file: #{f.inspect}"
19
19
  end
20
20
  end
@@ -31,7 +31,7 @@ describe Middleman::WebP::PathnameMatcher do
31
31
  patterns.each do |p|
32
32
  matcher = Middleman::WebP::PathnameMatcher.new(p)
33
33
  files.each do |f|
34
- matcher.matches?(f).must_equal false, "Pattern: #{p.inspect}, "\
34
+ value(matcher.matches?(f)).must_equal false, "Pattern: #{p.inspect}, "\
35
35
  "file: #{f.inspect}"
36
36
  end
37
37
  end
@@ -46,7 +46,7 @@ describe Middleman::WebP::PathnameMatcher do
46
46
 
47
47
  matcher = Middleman::WebP::PathnameMatcher.new
48
48
  paths.each do |p|
49
- matcher.matches?(p).must_equal true
49
+ value(matcher.matches?(p)).must_equal true
50
50
  end
51
51
  end
52
52
 
@@ -54,4 +54,32 @@ describe Middleman::WebP::PathnameMatcher do
54
54
  Middleman::WebP::PathnameMatcher.new('*.jpg').matches? nil
55
55
  end
56
56
  end
57
+
58
+ describe '#<=>' do
59
+ it 'sorts by pattern length' do
60
+ patterns = ['*.jpg', 'really_long_example_name.jpg', %r{^images/.*\.jpg$}]
61
+ matchers = patterns.map { |p| Middleman::WebP::PathnameMatcher.new(p) }
62
+
63
+ sorted_matchers = matchers.sort
64
+
65
+ value(sorted_matchers.map(&:pattern)).must_equal(patterns.sort_by { |p| p.to_s.length })
66
+ end
67
+
68
+ it 'sorts proc pattern smaller than string' do
69
+ patterns = ['*.jpg', Proc.new { |p| p == 'jpg' }, %r{^images/.*\.jpg$}]
70
+ matchers = patterns.map { |p| Middleman::WebP::PathnameMatcher.new(p) }
71
+ min_value = matchers.min
72
+
73
+ value(min_value.pattern).must_equal(patterns[1])
74
+ end
75
+
76
+ it 'considers any procs equal' do
77
+ proc1 = Proc.new { |p| p == 'jpg' }
78
+ proc2 = Proc.new { |p| p == 'png' }
79
+ matcher1 = Middleman::WebP::PathnameMatcher.new(proc1)
80
+ matcher2 = Middleman::WebP::PathnameMatcher.new(proc2)
81
+
82
+ value(matcher1 <=> matcher2).must_equal 0
83
+ end
84
+ end
57
85
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: middleman-webp
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Juhamatti Niemelä
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-30 00:00:00.000000000 Z
11
+ date: 2023-10-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: middleman-core
@@ -150,7 +150,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
150
  - !ruby/object:Gem::Version
151
151
  version: '0'
152
152
  requirements: []
153
- rubygems_version: 3.3.7
153
+ rubygems_version: 3.4.12
154
154
  signing_key:
155
155
  specification_version: 4
156
156
  summary: WebP image conversion for Middleman