jekyll-responsive_image 0.10.0 → 0.11.0

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
  SHA1:
3
- metadata.gz: ed00bfad560bc67b74bd57aeae4cf8245a3584ec
4
- data.tar.gz: 4f9315a2ac66b01e024b1c650783e864ff712b65
3
+ metadata.gz: 1e7197c871c2395b35ba1521d0e096e81216b0d9
4
+ data.tar.gz: 8677468d6e5251e720703e48bc7dc481034eedec
5
5
  SHA512:
6
- metadata.gz: 1951e99caf0c73b6716810dfa26043085f9b3aa522f3a393c5963ab85cd613bb5657ebb23c3af0a056a951200536e6af4a5c0b9c3a561d83f26d27a488da663f
7
- data.tar.gz: 39505e832aad11402d47a1f4d617a6dca827e19104652a359f846f838b55f2c1b0891776ac663054e449383ddaa7977f966e5ddfd89f5824c178a3354ce98185
6
+ metadata.gz: 6ac5b13caf07f0da51e9ff6f178f2c0aec17f86524c1df04ec586c9812eaff8203af1b153cd2c40ccb49559442b95ea86cbff51fad4affe54a828ef41ed9ccfb
7
+ data.tar.gz: bc3e2951c10dd05d2a224932de713e90f34be9e2f2050c33f69fd0845f612ec60f6008b7dba139cdecdc93a90de99f798b5a97c45bf47fe225624536a7238f3e
data/README.md CHANGED
@@ -78,6 +78,25 @@ Any extra attributes will be passed straight to the template as variables.
78
78
  {% responsive_image path: assets/image.jpg alt: "Lorem ipsum..." title: "Lorem ipsum..." %}
79
79
  ```
80
80
 
81
+ ### Liquid variables as attributes
82
+
83
+ You can use Liquid variables as attributes with the `responsive_image_block` tag. This tag works in exactly the same way as the `responsive_image` tag, but is implemented as a block tag to allow for more complex logic.
84
+
85
+ > **Important!** The attributes in the `responsive_image_block` tag are parsed as YAML, so whitespace and indentation are important!
86
+
87
+ ```
88
+ {% assign path = 'assets/test.png' %}
89
+ {% assign alt = 'Lorem ipsum...' %}
90
+
91
+ {% responsive_image_block %}
92
+ path: {{ path }}
93
+ alt: {{ alt }}
94
+ {% if title %}
95
+ title: {{ title }}
96
+ {% endif %}
97
+ {% endresponsive_image_block %}
98
+ ```
99
+
81
100
  ### Template
82
101
 
83
102
  You will need to create a template in order to use the `responsive_image` tag. Below are some sample templates to get you started.
@@ -0,0 +1,40 @@
1
+ Feature: Jekyll responsive_image_block tag
2
+ As a Jekyll template developer
3
+ I want to include Liquid variables when rendering my responsive images
4
+ In order to dynamically generate my responsive images
5
+
6
+ Scenario: Simple image tag
7
+ Given I have a responsive_image configuration with "template" set to "_includes/responsive-image.html"
8
+ And I have a file "index.html" with:
9
+ """
10
+ {% assign path = 'assets/test.png' %}
11
+ {% assign alt = 'Lorem ipsum' %}
12
+
13
+ {% responsive_image_block %}
14
+ path: {{ path }}
15
+ title: Magic rainbow adventure!
16
+ alt: {{ alt }}
17
+ {% endresponsive_image_block %}
18
+ """
19
+ When I run Jekyll
20
+ Then I should see "<img alt=\"Lorem ipsum\" src=\"/assets/test.png\" title=\"Magic rainbow adventure!\"" in "_site/index.html"
21
+
22
+ Scenario: More complex logic in the block tag
23
+ Given I have a responsive_image configuration with "template" set to "_includes/responsive-image.html"
24
+ And I have a file "index.html" with:
25
+ """
26
+ {% assign path = 'assets/test.png' %}
27
+ {% assign alt = 'Lorem ipsum' %}
28
+
29
+ {% responsive_image_block %}
30
+ path: {{ path }}
31
+
32
+ {% if another_alt %}
33
+ alt: {{ another_alt }}
34
+ {% else %}
35
+ alt: {{ alt }}
36
+ {% endif %}
37
+ {% endresponsive_image_block %}
38
+ """
39
+ When I run Jekyll
40
+ Then I should see "<img alt=\"Lorem ipsum\" src=\"/assets/test.png\"" in "_site/index.html"
@@ -1,41 +1,26 @@
1
- Feature: Jekyll responsive-image tag
1
+ Feature: Jekyll responsive_image tag
2
2
  As a Jekyll template developer
3
3
  I want to include responsive images in my page
4
4
  In order to best cater for devices of all sizes
5
5
 
6
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
- """
7
+ Given I have a responsive_image configuration with "template" set to "_includes/responsive-image.html"
8
+ And I have a file "index.html" with "{% responsive_image path: assets/test.png alt: Foobar %}"
15
9
  When I run Jekyll
16
10
  Then I should see "<img alt=\"Foobar\" src=\"/assets/test.png\"" in "_site/index.html"
17
11
 
18
12
  Scenario: Adding custom attributes
19
- Given I have a responsive_image configuration with:
20
- """
21
- template: _includes/responsive-image.html
22
- """
13
+ Given I have a responsive_image configuration with "template" set to "_includes/responsive-image.html"
23
14
  And I have a file "index.html" with:
24
15
  """
25
- {% responsive_image path: assets/test.png alt: Foobar title: "Lorem Ipsum" %}
16
+ {% responsive_image path: assets/test.png alt: 'Foobar bazbar' title: "Lorem Ipsum" %}
26
17
  """
27
18
  When I run Jekyll
28
- Then I should see "<img alt=\"Foobar\" src=\"/assets/test.png\" title=\"Lorem Ipsum\"" in "_site/index.html"
19
+ Then I should see "<img alt=\"Foobar bazbar\" src=\"/assets/test.png\" title=\"Lorem Ipsum\"" in "_site/index.html"
29
20
 
30
21
  Scenario: UTF-8 attributes
31
- Given I have a responsive_image configuration with:
32
- """
33
- template: _includes/responsive-image.html
34
- """
35
- And I have a file "index.html" with:
36
- """
37
- {% responsive_image path: assets/test.png alt: "かっこいい! ジェケルが好きです!" %}
38
- """
22
+ Given I have a responsive_image configuration with "template" set to "_includes/responsive-image.html"
23
+ And I have a file "index.html" with "{% responsive_image path: assets/test.png alt: 'かっこいい! ジェケルが好きです!' %}"
39
24
  When I run Jekyll
40
25
  Then I should see "<img alt=\"かっこいい! ジェケルが好きです!\" src=\"/assets/test.png\"" in "_site/index.html"
41
26
 
@@ -47,10 +32,7 @@ Feature: Jekyll responsive-image tag
47
32
  - width: 100
48
33
  - width: 200
49
34
  """
50
- And I have a file "index.html" with:
51
- """
52
- {% responsive_image path: assets/test.png %}
53
- """
35
+ And I have a file "index.html" with "{% responsive_image path: assets/test.png %}"
54
36
  When I run Jekyll
55
37
  Then I should see "<img alt=\"\" src=\"/assets/test.png\"" in "_site/index.html"
56
38
  And I should see "/assets/resized/test-100x50.png 100w" in "_site/index.html"
@@ -68,10 +50,7 @@ Feature: Jekyll responsive-image tag
68
50
  - width: 200
69
51
  - width: 300
70
52
  """
71
- And I have a file "index.html" with:
72
- """
73
- {% responsive_image path: assets/test.png template: _includes/custom-template.html %}
74
- """
53
+ And I have a file "index.html" with "{% responsive_image path: assets/test.png template: _includes/custom-template.html %}"
75
54
  When I run Jekyll
76
55
  Then I should see "[100, 200, 300]" in "_site/index.html"
77
56
 
@@ -83,10 +62,7 @@ Feature: Jekyll responsive-image tag
83
62
  sizes:
84
63
  - width: 100
85
64
  """
86
- And I have a file "index.html" with:
87
- """
88
- {% responsive_image path: assets/test.png %}
89
- """
65
+ And I have a file "index.html" with "{% responsive_image path: assets/test.png %}"
90
66
  When I run Jekyll
91
67
  Then I should see "/assets/test.png-resized/100/test-50.png 100w" in "_site/index.html"
92
68
  And the file "assets/test.png-resized/100/test-50.png" should exist
@@ -6,10 +6,18 @@ Given /^I have a responsive_image configuration with:$/ do |config|
6
6
  write_file('_config.yml', "responsive_image:\n#{config}")
7
7
  end
8
8
 
9
+ Given /^I have a responsive_image configuration with "(.+)" set to "(.+)"$/ do |config, value|
10
+ write_file('_config.yml', "responsive_image:\n #{config}: #{value}")
11
+ end
12
+
9
13
  Given /^I have a file "(.+)" with:$/ do |path, contents|
10
14
  write_file(path, "---\n---\n#{contents}")
11
15
  end
12
16
 
17
+ Given /^I have a file "(.+)" with "(.+)"$/ do |path, contents|
18
+ write_file(path, "---\n---\n#{contents}")
19
+ end
20
+
13
21
  Then /^I should see "(.+)" in "(.*)"$/ do |text, file|
14
22
  assert_match(Regexp.new(text), File.open(file).readlines.join)
15
23
  end
@@ -1,4 +1,5 @@
1
1
  require 'fileutils'
2
+ require 'yaml'
2
3
 
3
4
  require 'jekyll'
4
5
  require 'rmagick'
@@ -6,6 +7,9 @@ require 'rmagick'
6
7
  require 'jekyll/responsive_image/version'
7
8
  require 'jekyll/responsive_image/defaults'
8
9
  require 'jekyll/responsive_image/utils'
10
+ require 'jekyll/responsive_image/resize_handler'
9
11
  require 'jekyll/responsive_image/tag'
12
+ require 'jekyll/responsive_image/block'
10
13
 
11
14
  Liquid::Template.register_tag('responsive_image', Jekyll::ResponsiveImage::Tag)
15
+ Liquid::Template.register_tag('responsive_image_block', Jekyll::ResponsiveImage::Block)
@@ -0,0 +1,23 @@
1
+ module Jekyll
2
+ class ResponsiveImage
3
+ class Block < Liquid::Block
4
+ def render(context)
5
+ config = ResponsiveImage.defaults.dup
6
+ config.merge!(context.registers[:site].config['responsive_image'])
7
+
8
+ attributes = YAML.load(super)
9
+ image_template = attributes['template'] || config['template']
10
+
11
+ resize_handler = ResizeHandler.new
12
+ img = Magick::Image::read(attributes['path']).first
13
+ attributes['original'] = Utils.image_hash(attributes['path'], img.columns, img.rows)
14
+ attributes['resized'] = resize_handler.resize_image(img, config)
15
+
16
+ partial = File.read(image_template)
17
+ template = Liquid::Template.parse(partial)
18
+
19
+ template.render!(attributes)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,52 @@
1
+ module Jekyll
2
+ class ResponsiveImage
3
+ class ResizeHandler
4
+ def resize_image(img, config)
5
+ resized = []
6
+
7
+ config['sizes'].each do |size|
8
+ width = size['width']
9
+ ratio = width.to_f / img.columns.to_f
10
+ height = (img.rows.to_f * ratio).round
11
+
12
+ next unless needs_resizing?(img, width)
13
+
14
+ filepath = format_output_path(config['output_path_format'], img.filename, width, height)
15
+ resized.push(Utils.image_hash(filepath, width, height))
16
+
17
+ # Don't resize images more than once
18
+ next if File.exists?(filepath)
19
+
20
+ ensure_output_dir_exists!(File.dirname(filepath))
21
+
22
+ Jekyll.logger.info "Generating #{filepath}"
23
+
24
+ i = img.scale(ratio)
25
+ i.write(filepath) do |f|
26
+ f.quality = size['quality'] || config['default_quality']
27
+ end
28
+
29
+ i.destroy!
30
+ end
31
+
32
+ resized
33
+ end
34
+
35
+ def format_output_path(format, path, width, height)
36
+ params = Utils.symbolize_keys(Utils.image_hash(path, width, height))
37
+ format % params
38
+ end
39
+
40
+ def needs_resizing?(img, width)
41
+ img.columns > width
42
+ end
43
+
44
+ def ensure_output_dir_exists!(dir)
45
+ unless Dir.exists?(dir)
46
+ Jekyll.logger.info "Creating output directory #{dir}"
47
+ FileUtils.mkdir_p(dir)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
@@ -12,72 +12,14 @@ module Jekyll
12
12
  end
13
13
  end
14
14
 
15
- def resize_image(img, config)
16
- resized = []
17
-
18
- config['sizes'].each do |size|
19
- width = size['width']
20
- ratio = width.to_f / img.columns.to_f
21
- height = (img.rows.to_f * ratio).round
22
-
23
- next unless needs_resizing?(img, width)
24
-
25
- filepath = format_output_path(config['output_path_format'], img.filename, width, height)
26
- resized.push(image_hash(filepath, width, height))
27
-
28
- # Don't resize images more than once
29
- next if File.exists?(filepath)
30
-
31
- ensure_output_dir_exists!(File.dirname(filepath))
32
-
33
- Jekyll.logger.info "Generating #{filepath}"
34
-
35
- i = img.scale(ratio)
36
- i.write(filepath) do |f|
37
- f.quality = size['quality'] || config['default_quality']
38
- end
39
-
40
- i.destroy!
41
- end
42
-
43
- resized
44
- end
45
-
46
- def format_output_path(format, path, width, height)
47
- params = Utils.symbolize_keys(image_hash(path, width, height))
48
- format % params
49
- end
50
-
51
- def needs_resizing?(img, width)
52
- img.columns > width
53
- end
54
-
55
- def ensure_output_dir_exists!(dir)
56
- unless Dir.exists?(dir)
57
- Jekyll.logger.info "Creating output directory #{dir}"
58
- FileUtils.mkdir_p(dir)
59
- end
60
- end
61
-
62
- # Build a hash containing image information
63
- def image_hash(path, width, height)
64
- {
65
- 'path' => path,
66
- 'basename' => File.basename(path),
67
- 'filename' => File.basename(path, '.*'),
68
- 'extension' => File.extname(path).delete('.'),
69
- 'width' => width,
70
- 'height' => height,
71
- }
72
- end
73
-
74
15
  def render(context)
75
16
  config = ResponsiveImage.defaults.dup
76
17
  config.merge!(context.registers[:site].config['responsive_image'])
77
18
 
19
+ resize_handler = ResizeHandler.new
78
20
  img = Magick::Image::read(@attributes['path']).first
79
- @attributes['original'] = image_hash(@attributes['path'], img.columns, img.rows)
80
- @attributes['resized'] = resize_image(img, config)
21
+ @attributes['original'] = Utils.image_hash(@attributes['path'], img.columns, img.rows)
22
+ @attributes['resized'] = resize_handler.resize_image(img, config)
81
23
 
82
24
  image_template = @attributes['template'] || config['template']
83
25
 
@@ -8,6 +8,18 @@ module Jekyll
8
8
  end
9
9
  result
10
10
  end
11
+
12
+ # Build a hash containing image information
13
+ def self.image_hash(path, width, height)
14
+ {
15
+ 'path' => path,
16
+ 'basename' => File.basename(path),
17
+ 'filename' => File.basename(path, '.*'),
18
+ 'extension' => File.extname(path).delete('.'),
19
+ 'width' => width,
20
+ 'height' => height,
21
+ }
22
+ end
11
23
  end
12
24
  end
13
25
  end
@@ -1,5 +1,5 @@
1
1
  module Jekyll
2
2
  class ResponsiveImage
3
- VERSION = '0.10.0'.freeze
3
+ VERSION = '0.11.0'.freeze
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-responsive_image
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.0
4
+ version: 0.11.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Wynn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-06 00:00:00.000000000 Z
11
+ date: 2014-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jekyll
@@ -57,6 +57,7 @@ files:
57
57
  - features/fixtures/_includes/custom-template.html
58
58
  - features/fixtures/_includes/responsive-image.html
59
59
  - features/fixtures/assets/test.png
60
+ - features/responsive-image-block.feature
60
61
  - features/responsive-image.feature
61
62
  - features/step_definitions/jekyll_steps.rb
62
63
  - features/support/env.rb
@@ -64,7 +65,9 @@ files:
64
65
  - index.html
65
66
  - jekyll-responsive_image.gemspec
66
67
  - lib/jekyll/responsive_image.rb
68
+ - lib/jekyll/responsive_image/block.rb
67
69
  - lib/jekyll/responsive_image/defaults.rb
70
+ - lib/jekyll/responsive_image/resize_handler.rb
68
71
  - lib/jekyll/responsive_image/tag.rb
69
72
  - lib/jekyll/responsive_image/utils.rb
70
73
  - lib/jekyll/responsive_image/version.rb