middleman-webp 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -2
- data/lib/middleman-webp/converter.rb +13 -11
- data/lib/middleman-webp/extension.rb +5 -2
- data/lib/middleman-webp/options.rb +40 -0
- data/lib/middleman-webp/version.rb +1 -1
- data/middleman-webp.gemspec +1 -0
- data/spec/spec_helper.rb +4 -1
- data/spec/unit/converter_spec.rb +32 -1
- data/spec/unit/options_spec.rb +23 -0
- metadata +32 -14
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ee8dc469ed517f77130ab6e9e9bcce14553fe30b
|
4
|
+
data.tar.gz: 5e4b7f341b270b426ec5ff840a978b2de819919e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 38398c88e7565d7c8c53ff9cd6f230d7565087ced45bf910cfd04841cdffc3cc0a07446eb867402f7d3a7ec712992bb1b9bf96671bac5c03cb54fe7fb86ebaa5
|
7
|
+
data.tar.gz: a19c29547c2c7b4dd8f0e86cc728f22f6f7ffd451b41ad9be017452144f15103efc3ef9e43005a6cbf41803f223b5ce45f8c06055f83027312426a97410ac536
|
data/README.md
CHANGED
@@ -11,7 +11,12 @@ file requests so you can refer to jpegs or pngs in your HTML and use
|
|
11
11
|
for example .htaccess configuration to provide smaller alternatives
|
12
12
|
for browser's supporting WebP.
|
13
13
|
|
14
|
+
See my blog post
|
15
|
+
["Introducing WebP extension for Middleman"][blog-post] for more
|
16
|
+
details why I think using WebP matters.
|
17
|
+
|
14
18
|
[middleman]: http://middlemanapp.com
|
19
|
+
[blog-post]: http://byteplumbing.net/2014/03/introducing-webp-extension-for-middleman/
|
15
20
|
|
16
21
|
## Installation
|
17
22
|
|
@@ -36,12 +41,28 @@ And activate :webp extension in your config.rb:
|
|
36
41
|
activate :webp
|
37
42
|
```
|
38
43
|
|
44
|
+
Options for conversion are defined using
|
45
|
+
[Ruby's glob syntax](http://www.ruby-doc.org/core-2.1.1/Dir.html#method-c-glob)
|
46
|
+
for matching image files and hashes containing args supported by
|
47
|
+
cwebp:
|
48
|
+
|
49
|
+
``` ruby
|
50
|
+
activate :webp do |webp|
|
51
|
+
webp.conversion_options = {
|
52
|
+
"icons/*.png" => {lossless: true},
|
53
|
+
"photos/**/*.jpg" => {q: 100}
|
54
|
+
}
|
55
|
+
end
|
56
|
+
```
|
57
|
+
|
39
58
|
## Configuring your site to provide WebP alternatives
|
40
59
|
|
41
|
-
|
60
|
+
Configure web server to serve WebP images if they are available and
|
61
|
+
browser has set the HTTP Accept header.
|
42
62
|
|
43
|
-
|
63
|
+
Look for [this example how to do it in .htaccess][htaccess].
|
44
64
|
|
65
|
+
[htaccess]: https://github.com/vincentorback/WebP-images-with-htaccess
|
45
66
|
|
46
67
|
## Contributing
|
47
68
|
|
@@ -1,10 +1,13 @@
|
|
1
|
+
require "middleman-webp/options"
|
2
|
+
|
1
3
|
module Middleman
|
2
4
|
module WebP
|
3
5
|
|
4
6
|
class Converter
|
5
7
|
|
6
|
-
def initialize(app, builder)
|
8
|
+
def initialize(app, options={}, builder)
|
7
9
|
@app = app
|
10
|
+
@options = Middleman::WebP::Options.new(options)
|
8
11
|
@builder = builder
|
9
12
|
end
|
10
13
|
|
@@ -22,21 +25,20 @@ module Middleman
|
|
22
25
|
def convert_images(paths, &after_conversion)
|
23
26
|
paths.each do |p|
|
24
27
|
dst = destination_path(p)
|
25
|
-
|
26
|
-
run_gif2webp(p, dst)
|
27
|
-
else
|
28
|
-
run_cwebp(p, dst)
|
29
|
-
end
|
28
|
+
exec_convert_tool(p, dst)
|
30
29
|
yield File.new(p), File.new(dst)
|
31
30
|
end
|
32
31
|
end
|
33
32
|
|
34
|
-
def
|
35
|
-
system("
|
33
|
+
def exec_convert_tool(src, dst)
|
34
|
+
system("#{tool_for(src)} #{@options.for(src)} -quiet #{src} -o #{dst}")
|
36
35
|
end
|
37
36
|
|
38
|
-
|
39
|
-
|
37
|
+
# Internal: Return proper tool command based on file type
|
38
|
+
#
|
39
|
+
# file - Filename
|
40
|
+
def tool_for(file)
|
41
|
+
file.to_s =~ /gif$/i ? "gif2webp" : "cwebp"
|
40
42
|
end
|
41
43
|
|
42
44
|
def reject_file(file)
|
@@ -49,7 +51,7 @@ module Middleman
|
|
49
51
|
# src - File instance of the source
|
50
52
|
# dst - File instance of the destination
|
51
53
|
def change_percentage(src, dst)
|
52
|
-
"%.2f
|
54
|
+
"%g %%" % ("%.2f" % [100 - 100.0 * dst.size / src.size])
|
53
55
|
end
|
54
56
|
|
55
57
|
def destination_path(src_path)
|
@@ -3,11 +3,14 @@ require "middleman-webp/converter"
|
|
3
3
|
|
4
4
|
module Middleman
|
5
5
|
class WebPExtension < Extension
|
6
|
-
|
6
|
+
option :conversion_options, {}, "Custom conversion options for cwebp/gif2webp"
|
7
|
+
|
8
|
+
def initialize(app, options_hash={}, &block)
|
7
9
|
super
|
8
10
|
|
11
|
+
args = options.conversion_options
|
9
12
|
app.after_build do |builder|
|
10
|
-
Middleman::WebP::Converter.new(app, builder).convert
|
13
|
+
Middleman::WebP::Converter.new(app, args, builder).convert
|
11
14
|
end
|
12
15
|
end
|
13
16
|
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Middleman
|
2
|
+
module WebP
|
3
|
+
|
4
|
+
class Options
|
5
|
+
|
6
|
+
def initialize(options={})
|
7
|
+
@options = options.inject(Hash.new("")) do |h,(k,v)|
|
8
|
+
h[k] = to_args(v); h
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
# Internal: Generate command line args for cwebp or gif2webp command
|
13
|
+
#
|
14
|
+
# Find options defined for given file. Selects all options whose
|
15
|
+
# glob pattern matches file path and uses the one with longest
|
16
|
+
# glob, because it's assumed to be the most precise one.
|
17
|
+
def for(file)
|
18
|
+
matching = @options.select {|g,o| file.fnmatch?(g)}
|
19
|
+
|
20
|
+
return "" if matching.empty?
|
21
|
+
|
22
|
+
matching.sort {|(ga,oa),(gb,ob)| gb.size <=> ga.size}[0][1]
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def to_args(options)
|
28
|
+
options.map do |k,v|
|
29
|
+
if v == true
|
30
|
+
"-#{k}"
|
31
|
+
elsif v != false
|
32
|
+
"-#{k} #{v}"
|
33
|
+
end
|
34
|
+
end.join(" ").gsub(/ +/, " ")
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
data/middleman-webp.gemspec
CHANGED
data/spec/spec_helper.rb
CHANGED
data/spec/unit/converter_spec.rb
CHANGED
@@ -1,9 +1,24 @@
|
|
1
1
|
require "spec_helper"
|
2
|
+
require "pathname"
|
2
3
|
require_relative "../../lib/middleman-webp/converter"
|
3
4
|
|
4
5
|
describe Middleman::WebP::Converter do
|
5
6
|
before do
|
6
|
-
@converter = Middleman::WebP::Converter.new(nil, nil)
|
7
|
+
@converter = Middleman::WebP::Converter.new(nil, {}, nil)
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#change_percentage" do
|
11
|
+
it "returns how many percents smaller destination file is" do
|
12
|
+
src = stub(:size => 10000)
|
13
|
+
dst = stub(:size => 8746)
|
14
|
+
@converter.change_percentage(src, dst).must_equal "12.54 %"
|
15
|
+
end
|
16
|
+
|
17
|
+
it "omits zeroes in the end of decimal part" do
|
18
|
+
src = stub(:size => 100)
|
19
|
+
dst = stub(:size => 76)
|
20
|
+
@converter.change_percentage(src, dst).must_equal "24 %"
|
21
|
+
end
|
7
22
|
end
|
8
23
|
|
9
24
|
describe "#number_to_human_size" do
|
@@ -13,4 +28,20 @@ describe Middleman::WebP::Converter do
|
|
13
28
|
@converter.number_to_human_size(2_634_234).must_equal "2.51 MiB"
|
14
29
|
end
|
15
30
|
end
|
31
|
+
|
32
|
+
describe "#tool_for" do
|
33
|
+
it "uses gif2webp for gif files" do
|
34
|
+
path = Pathname.new("/some/path/image.gif")
|
35
|
+
@converter.tool_for(path).must_equal "gif2webp"
|
36
|
+
end
|
37
|
+
|
38
|
+
it "uses cwebp for jpeg, png and tiff files" do
|
39
|
+
path = Pathname.new("/some/path/image.jpg")
|
40
|
+
@converter.tool_for(path).must_equal "cwebp"
|
41
|
+
path = Pathname.new("/some/path/image.png")
|
42
|
+
@converter.tool_for(path).must_equal "cwebp"
|
43
|
+
path = Pathname.new("/some/path/image.tiff")
|
44
|
+
@converter.tool_for(path).must_equal "cwebp"
|
45
|
+
end
|
46
|
+
end
|
16
47
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "pathname"
|
3
|
+
require_relative "../../lib/middleman-webp/options"
|
4
|
+
|
5
|
+
describe Middleman::WebP::Options do
|
6
|
+
describe "#for" do
|
7
|
+
it "returns cwebp args when given file matches option file pattern" do
|
8
|
+
path = Pathname.new("test_image.jpg")
|
9
|
+
options = Middleman::WebP::Options.new("*.jpg" => {lossless: true, q: 85})
|
10
|
+
|
11
|
+
args = options.for(path)
|
12
|
+
args.must_match(/^(-q 85|-lossless) (-q 85|-lossless)$/)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns empty string when no options are defined" do
|
16
|
+
path = Pathname.new("test_image.jpg")
|
17
|
+
options = Middleman::WebP::Options.new
|
18
|
+
|
19
|
+
args = options.for(path)
|
20
|
+
args.must_be_empty
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
metadata
CHANGED
@@ -1,69 +1,83 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: middleman-webp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juhamatti Niemelä
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-04-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: middleman-core
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - ~>
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: 3.2.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - ~>
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: 3.2.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - ~>
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '1.5'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - ~>
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '1.5'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- -
|
45
|
+
- - '>='
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- -
|
52
|
+
- - '>='
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: simplecov
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- -
|
59
|
+
- - '>='
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- -
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mocha
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
67
81
|
- !ruby/object:Gem::Version
|
68
82
|
version: '0'
|
69
83
|
description: Generate WebP versions of each image used in Middleman site during build.
|
@@ -73,7 +87,7 @@ executables: []
|
|
73
87
|
extensions: []
|
74
88
|
extra_rdoc_files: []
|
75
89
|
files:
|
76
|
-
-
|
90
|
+
- .gitignore
|
77
91
|
- Gemfile
|
78
92
|
- LICENSE.txt
|
79
93
|
- README.md
|
@@ -81,11 +95,13 @@ files:
|
|
81
95
|
- lib/middleman-webp.rb
|
82
96
|
- lib/middleman-webp/converter.rb
|
83
97
|
- lib/middleman-webp/extension.rb
|
98
|
+
- lib/middleman-webp/options.rb
|
84
99
|
- lib/middleman-webp/version.rb
|
85
100
|
- lib/middleman_extension.rb
|
86
101
|
- middleman-webp.gemspec
|
87
102
|
- spec/spec_helper.rb
|
88
103
|
- spec/unit/converter_spec.rb
|
104
|
+
- spec/unit/options_spec.rb
|
89
105
|
homepage: http://github.com/iiska/middleman-webp
|
90
106
|
licenses:
|
91
107
|
- MIT
|
@@ -96,20 +112,22 @@ require_paths:
|
|
96
112
|
- lib
|
97
113
|
required_ruby_version: !ruby/object:Gem::Requirement
|
98
114
|
requirements:
|
99
|
-
- -
|
115
|
+
- - '>='
|
100
116
|
- !ruby/object:Gem::Version
|
101
117
|
version: '0'
|
102
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
119
|
requirements:
|
104
|
-
- -
|
120
|
+
- - '>='
|
105
121
|
- !ruby/object:Gem::Version
|
106
122
|
version: '0'
|
107
123
|
requirements: []
|
108
124
|
rubyforge_project:
|
109
|
-
rubygems_version: 2.
|
125
|
+
rubygems_version: 2.0.14
|
110
126
|
signing_key:
|
111
127
|
specification_version: 4
|
112
128
|
summary: WebP image conversion for Middleman
|
113
129
|
test_files:
|
114
130
|
- spec/spec_helper.rb
|
115
131
|
- spec/unit/converter_spec.rb
|
132
|
+
- spec/unit/options_spec.rb
|
133
|
+
has_rdoc:
|