jekyll-responsive_image 0.9.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b473c6730697db2d34bf8a02b5c715abe4a35149
4
+ data.tar.gz: aa1bfa551cd86999fa2cb4b253b038c1d7ce7185
5
+ SHA512:
6
+ metadata.gz: 5e22114a262175eea4826c6d73419c9709bc936507b7b5a2c13c26c9510a5bb44611308a44b64c379936a760b1e3188efd145c3fc52e3914a6c4bbcd78c60a23
7
+ data.tar.gz: 37da18d95c3579ba4cea87190dca29c2e76936e9181ca9af678993b3e69dce6b222d905c73e0be43c2dcc0aca7aaf87434360c045864e23c1b432689fbce5c5c
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ Gemfile.lock
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ bundler_args: --without debug
3
+ script: bundle exec rake features_with_coveralls
4
+ rvm:
5
+ - 2.1.0
6
+ - 2.0.0
7
+ - 1.9.3
8
+ - rbx-2
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'https://rubygems.org/'
2
+ gemspec
3
+
4
+ group :development do
5
+ gem 'rake'
6
+ gem 'cucumber', '~> 1.3'
7
+
8
+ gem 'coveralls', :require => false
9
+ end
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Joseph Wynn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # Jekyll Responsive Images
2
+
3
+ Jekyll Responsive Images is a [Jekyll](http://jekyllrb.com/) plugin and utility for automatically resizing images. Its intended use is for sites which want to display responsive images using something like [`srcset`](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Img#Specifications) or [Imager.js](https://github.com/BBC-News/Imager.js/).
4
+
5
+ [![Build Status](https://travis-ci.org/wildlyinaccurate/jekyll-responsive-images.svg?branch=master)](https://travis-ci.org/wildlyinaccurate/jekyll-responsive-images)
6
+ [![Coverage Status](https://img.shields.io/coveralls/wildlyinaccurate/jekyll-responsive-images.svg)](https://coveralls.io/r/wildlyinaccurate/jekyll-responsive-images)
7
+ [![Dependency Status](https://gemnasium.com/wildlyinaccurate/jekyll-responsive-images.svg)](https://gemnasium.com/wildlyinaccurate/jekyll-responsive-images)
8
+
9
+ ## Installation
10
+
11
+ Install the gem yourself
12
+
13
+ ```
14
+ $ gem install jekyll-responsive_image
15
+ ```
16
+
17
+ Or simply add it to your Jekyll `_config.yml`:
18
+
19
+ ```yaml
20
+ gems: [jekyll-responsive_image]
21
+ ```
22
+
23
+ ## Configuration
24
+
25
+ An example configuration is below.
26
+
27
+ ```yaml
28
+ responsive_image:
29
+ template: '_includes/responsive-image.html' # Path to the template to render. Required.
30
+
31
+ # An array of resize configurations. When this array is empty (or not specified),
32
+ # no resizing will take place.
33
+ sizes:
34
+ - width: 480 # How wide the resized image will be. Required
35
+ - width: 800
36
+ quality: 90 # JPEG quality. Optional.
37
+ - width: 1400
38
+ ```
39
+
40
+ ## Usage
41
+
42
+ Replace your images with the `responsive_image` tag, specifying a path to the image.
43
+
44
+ ```
45
+ {% responsive_image path: assets/my-file.jpg %}
46
+ ```
47
+
48
+ Any extra attributes will be passed to the template.
49
+
50
+ ```
51
+ {% responsive_image path: assets/image.jpg alt: "Lorem ipsum..." title: "Lorem ipsum..." %}
52
+ ```
53
+
54
+ Create a template to suit your needs. A basic template example is below.
55
+
56
+ ```html
57
+ <img src="/{{ path }}"
58
+ alt="{{ alt }}"
59
+ title="{{ title }}
60
+
61
+ {% if resized %}
62
+ srcset="{% for i in resized %}
63
+ /{{ i.path }} {{ i.width }}w{% if forloop.last == false %},{% endif %}
64
+ {% endfor %}"
65
+ {% endif %}
66
+ >
67
+ ```
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ require 'bundler'
2
+
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+
11
+ require 'rake'
12
+ require 'jekyll/responsive_image/version'
13
+ require 'cucumber/rake/task'
14
+ require 'coveralls/rake/task'
15
+
16
+ Cucumber::Rake::Task.new(:features)
17
+
18
+ Coveralls::RakeTask.new
19
+ task :features_with_coveralls => [:features, 'coveralls:push']
20
+
21
+ task :default => [:features]
22
+
23
+ task :release do |t|
24
+ system "gem build jekyll-responsive_image.gemspec"
25
+ system "git tag v#{Jekyll::ResponsiveImage::VERSION}"
26
+ system "git push --tags"
27
+ system "gem push jekyll-responsive_image-#{Jekyll::ResponsiveImage::VERSION}.gem"
28
+ end
@@ -0,0 +1,5 @@
1
+ <img alt="{{ alt }}" src="/{{ path }}"{% if resized %} srcset="
2
+ {% for i in resized %}
3
+ /{{ i.path }} {{ i.width }}w{% if forloop.last == false %},{% endif %}
4
+ {% endfor %}
5
+ "{% endif %}>
Binary file
@@ -0,0 +1,47 @@
1
+ Feature: Jekyll responsive-image tag
2
+ As a Jekyll template developer
3
+ I want to include responsive images in my page
4
+ In order to best cater for devices of all sizes
5
+
6
+ Scenario: Simple image tag
7
+ Given I have a responsive_image configuration with:
8
+ """
9
+ template: _includes/responsive-image.html
10
+ """
11
+ And I have a file "index.html" with:
12
+ """
13
+ {% responsive_image path: assets/test.png alt: Foobar %}
14
+ """
15
+ When I run Jekyll
16
+ Then I should see "<img alt=\"Foobar\" src=\"/assets/test.png\">" in "_site/index.html"
17
+
18
+ Scenario: UTF-8 alt attribute
19
+ Given I have a responsive_image configuration with:
20
+ """
21
+ template: _includes/responsive-image.html
22
+ """
23
+ And I have a file "index.html" with:
24
+ """
25
+ {% responsive_image path: assets/test.png alt: "かっこいい! ジェケルが好きです!" %}
26
+ """
27
+ When I run Jekyll
28
+ Then I should see "<img alt=\"かっこいい! ジェケルが好きです!\" src=\"/assets/test.png\">" in "_site/index.html"
29
+
30
+ Scenario: Image with multiple sizes
31
+ Given I have a responsive_image configuration with:
32
+ """
33
+ template: _includes/responsive-image.html
34
+ sizes:
35
+ - width: 100
36
+ - width: 200
37
+ """
38
+ And I have a file "index.html" with:
39
+ """
40
+ {% responsive_image path: assets/test.png %}
41
+ """
42
+ When I run Jekyll
43
+ Then I should see "<img alt=\"\" src=\"/assets/test.png\"" in "_site/index.html"
44
+ And I should see "/assets/resized/test-100x50.png 100w" in "_site/index.html"
45
+ And I should see "/assets/resized/test-200x100.png 200w" in "_site/index.html"
46
+ And the file "assets/resized/test-100x50.png" should exist
47
+ And the file "assets/resized/test-200x100.png" should exist
@@ -0,0 +1,26 @@
1
+ When /^I run Jekyll$/ do
2
+ run_jekyll
3
+ end
4
+
5
+ Given /^I have a responsive_image configuration with:$/ do |config|
6
+ write_file('_config.yml', "responsive_image:\n#{config}")
7
+ end
8
+
9
+ Given /^I have a file "(.+)" with:$/ do |path, contents|
10
+ write_file(path, "---\n---\n#{contents}")
11
+ end
12
+
13
+ Then /^I should see "(.+)" in "(.*)"$/ do |text, file|
14
+ assert_match(Regexp.new(text), File.open(file).readlines.join)
15
+ end
16
+
17
+ Then /^the file "(.+)" should exist$/ do |path|
18
+ assert File.exists?(path)
19
+ end
20
+
21
+ def write_file(path, contents)
22
+ File.open(path, 'w') do |f|
23
+ f.write(contents)
24
+ f.close
25
+ end
26
+ end
@@ -0,0 +1,20 @@
1
+ begin
2
+ require 'coveralls' if ENV['CI']
3
+ rescue LoadError
4
+ # ignore
5
+ end
6
+
7
+ require 'test/unit'
8
+ require 'jekyll/responsive_image'
9
+
10
+ TEST_DIR = File.join('/', 'tmp', 'jekyll')
11
+
12
+ def run_jekyll(options = {})
13
+ options['source'] ||= TEST_DIR
14
+ options['destination'] ||= File.join(TEST_DIR, '_site')
15
+
16
+ options = Jekyll.configuration(options)
17
+
18
+ site = Jekyll::Site.new(options)
19
+ site.process
20
+ end
@@ -0,0 +1,13 @@
1
+ Before do
2
+ FileUtils.rm_rf(TEST_DIR) if File.exist?(TEST_DIR)
3
+ FileUtils.mkdir_p(TEST_DIR)
4
+
5
+ fixtures = File.expand_path('../../fixtures', __FILE__)
6
+ FileUtils.cp_r(Dir.glob("#{fixtures}/*"), TEST_DIR)
7
+
8
+ Dir.chdir(TEST_DIR)
9
+ end
10
+
11
+ at_exit do
12
+ FileUtils.rm_rf(TEST_DIR) if File.exist?(TEST_DIR)
13
+ end
data/index.html ADDED
@@ -0,0 +1 @@
1
+ {% responsive_image path: assets/test.jpg %}
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib/', __FILE__)
3
+ $:.unshift lib unless $:.include?(lib)
4
+
5
+ require 'jekyll/responsive_image/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'jekyll-responsive_image'
9
+ spec.version = Jekyll::ResponsiveImage::VERSION
10
+ spec.authors = ['Joseph Wynn']
11
+ spec.email = ['joseph@wildlyinaccurate.com']
12
+ spec.summary = 'Responsive images for Jekyll via srcset'
13
+ spec.homepage = 'https://github.com/wildlyinaccurate/jekyll-responsive-image'
14
+ spec.licenses = ['MIT']
15
+ spec.description = %q{
16
+ Jekyll Responsive Images is a Jekyll plugin and utility for automatically resizing images.
17
+ Its intended use is for sites which want to display responsive images using something like srcset or Imager.js.
18
+ }
19
+
20
+ spec.files = `git ls-files`.split($/)
21
+ spec.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ spec.executables = []
23
+ spec.require_path = 'lib'
24
+
25
+ spec.add_runtime_dependency 'jekyll', '~> 2.0'
26
+ spec.add_runtime_dependency 'rmagick'
27
+ end
@@ -0,0 +1,100 @@
1
+ require 'jekyll'
2
+ require 'rmagick'
3
+
4
+ module Jekyll
5
+ class ResponsiveImage
6
+ class Tag < Liquid::Tag
7
+ DEFAULT_QUALITY = 85
8
+
9
+ def initialize(tag_name, markup, tokens)
10
+ super
11
+
12
+ @attributes = {}
13
+ @markup = markup
14
+
15
+ @markup.scan(::Liquid::TagAttributes) do |key, value|
16
+ # Strip quotes from around attribute values
17
+ @attributes[key] = value.gsub(/^['"]|['"]$/, '')
18
+ end
19
+ end
20
+
21
+ def resize_image(path, config)
22
+ sizes = config['sizes']
23
+
24
+ return if sizes.empty?
25
+
26
+ output_dir = config['output_dir']
27
+ ensure_output_dir_exists!(output_dir)
28
+
29
+ resized = []
30
+ img = Magick::Image::read(path).first
31
+
32
+ sizes.each do |size|
33
+ width = size['width']
34
+ ratio = width.to_f / img.columns.to_f
35
+ height = (img.rows.to_f * ratio).round
36
+
37
+ filename = resized_filename(path, width, height)
38
+ newpath = "#{output_dir}/#{filename}"
39
+
40
+ next unless needs_resizing?(img, width)
41
+
42
+ resized.push({
43
+ 'width' => width,
44
+ 'height' => height,
45
+ 'path' => newpath,
46
+ })
47
+
48
+ # Don't resize images more than once
49
+ next if File.exists?(newpath)
50
+
51
+ Jekyll.logger.info "Generating #{newpath}"
52
+
53
+ i = img.scale(ratio)
54
+ i.write(newpath) do |f|
55
+ f.quality = size['quality'] || DEFAULT_QUALITY
56
+ end
57
+
58
+ i.destroy!
59
+ end
60
+
61
+ resized
62
+ end
63
+
64
+ # Insert resize information into a file path
65
+ #
66
+ # resized_filename(/foo/bar/file.name.jpg, 500, 300)
67
+ # => /foo/bar/file.name-500x300.jpg
68
+ #
69
+ def resized_filename(path, width, height)
70
+ File.basename(path).sub(/\.([^.]+)$/, "-#{width}x#{height}.\\1")
71
+ end
72
+
73
+ def needs_resizing?(img, width)
74
+ img.columns > width
75
+ end
76
+
77
+ def ensure_output_dir_exists!(dir)
78
+ unless Dir.exists?(dir)
79
+ Jekyll.logger.info "Creating output directory #{dir}"
80
+ Dir.mkdir(dir)
81
+ end
82
+ end
83
+
84
+ def render(context)
85
+ config = context.registers[:site].config['responsive_image']
86
+ config['output_dir'] ||= 'assets/resized'
87
+ config['sizes'] ||= []
88
+
89
+ @attributes['resized'] = resize_image(@attributes['path'], config)
90
+
91
+ partial = File.read(config['template'])
92
+ template = Liquid::Template.parse(partial)
93
+
94
+ template.render!(@attributes)
95
+ end
96
+ end
97
+ end
98
+ end
99
+
100
+ Liquid::Template.register_tag('responsive_image', Jekyll::ResponsiveImage::Tag)
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ class ResponsiveImage
3
+ VERSION = '0.9.0'.freeze
4
+ end
5
+ end
data/test-index.html ADDED
@@ -0,0 +1 @@
1
+ {% responsive_image path: assets/test.jpg %}
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-responsive_image
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Joseph Wynn
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-12-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jekyll
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rmagick
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: "\n Jekyll Responsive Images is a Jekyll plugin and utility for automatically
42
+ resizing images.\n Its intended use is for sites which want to display responsive
43
+ images using something like srcset or Imager.js.\n "
44
+ email:
45
+ - joseph@wildlyinaccurate.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".coveralls.yml"
51
+ - ".gitignore"
52
+ - ".travis.yml"
53
+ - Gemfile
54
+ - LICENSE
55
+ - README.md
56
+ - Rakefile
57
+ - features/fixtures/_includes/responsive-image.html
58
+ - features/fixtures/assets/test.png
59
+ - features/responsive-image.feature
60
+ - features/step_definitions/jekyll_steps.rb
61
+ - features/support/env.rb
62
+ - features/support/hooks.rb
63
+ - index.html
64
+ - jekyll-responsive_image.gemspec
65
+ - lib/jekyll/responsive_image.rb
66
+ - lib/jekyll/responsive_image/version.rb
67
+ - test-index.html
68
+ homepage: https://github.com/wildlyinaccurate/jekyll-responsive-image
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.2.2
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Responsive images for Jekyll via srcset
92
+ test_files: []