middleman-webp 1.0.0 → 1.0.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bc74ef4bee41ffcd27959df4d38b3763047e6c68ed8b04c81d75ec72eb699233
4
- data.tar.gz: 6c748300919396d18389c6fda75f92e83aaafe5f00f9472c521b84782596aff4
3
+ metadata.gz: e2d9332e316e367d0a46c912bc49b1fd5e7c3d74281ab525f1ccc510db79f843
4
+ data.tar.gz: 98293f56a56207ad7547d29ecd64be15a01d11403d7af40b15a9d9274840fc2b
5
5
  SHA512:
6
- metadata.gz: c60fd8dfbd9fe0ca5432297bac7690fe46878c64027747b4f18425a42a256836bd522b90e5160b3e86487a8194bbc00c84eda986e2d88bec25459cfe5a529007
7
- data.tar.gz: 4c98ed56f1a550d62a8ef19391514edd9ca59aa5d9a5f9e56a11dbf7944336fdc2e9f99980ce4d6cf6ab4e1643d4989e49d4750023487593449010ca070aad1f
6
+ metadata.gz: 735b2569bfbbb132e7cd533fd4fa4258b4bbcb88a5edd68cda2ec23b05d5c71743324a2357f32ed13580e20b4606ff5ced154f002470328ded97e70405b85008
7
+ data.tar.gz: da7aa0c43e16c6805ba4e95d738480b607b4499684e1feae0dc62864d22f97eeb6b9622309d9d783971571b62b05676ac4c242eeb5bed036873dff2af1e6ceab
data/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## 1.0.2
2
+
3
+ - Fix logic for selecting command line args for the most precise filename
4
+ pattern, if multiple patterns would match
5
+
6
+ ## 1.0.1
7
+
8
+ - Add `required_ruby_version` to gemspec.
9
+
1
10
  ## 1.0.0
2
11
 
3
12
  - Add support for Ruby 2.7, 3,0, 3.1, 3.2
@@ -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.0'.freeze
3
+ VERSION = '1.0.2'.freeze
4
4
  end
5
5
  end
@@ -10,7 +10,7 @@ Gem::Specification.new do |spec|
10
10
  spec.email = ["iiska@iki.fi"]
11
11
  spec.summary = %q{WebP image conversion for Middleman}
12
12
  spec.description = %q{Generate WebP versions of each image used in Middleman site during build.}
13
- spec.homepage = "http://github.com/iiska/middleman-webp"
13
+ spec.homepage = "https://github.com/iiska/middleman-webp"
14
14
  spec.license = "MIT"
15
15
 
16
16
  spec.files = `git ls-files`.split($/)
@@ -18,6 +18,8 @@ Gem::Specification.new do |spec|
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ["lib"]
20
20
 
21
+ spec.required_ruby_version = ">= 2.5.0"
22
+
21
23
  spec.add_dependency "middleman-core", "~> 4.0"
22
24
  spec.add_dependency "shell", "~> 0.8.1"
23
25
 
@@ -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.0
4
+ version: 1.0.2
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-29 00:00:00.000000000 Z
11
+ date: 2023-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: middleman-core
@@ -131,7 +131,7 @@ files:
131
131
  - spec/unit/extension_spec.rb
132
132
  - spec/unit/options_spec.rb
133
133
  - spec/unit/pathname_matcher_spec.rb
134
- homepage: http://github.com/iiska/middleman-webp
134
+ homepage: https://github.com/iiska/middleman-webp
135
135
  licenses:
136
136
  - MIT
137
137
  metadata: {}
@@ -143,7 +143,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
143
143
  requirements:
144
144
  - - ">="
145
145
  - !ruby/object:Gem::Version
146
- version: '0'
146
+ version: 2.5.0
147
147
  required_rubygems_version: !ruby/object:Gem::Requirement
148
148
  requirements:
149
149
  - - ">="