jekyll-thumb 0.1.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
+ SHA256:
3
+ metadata.gz: 5782f1e77d7731e352385aa2af92366c76aaf564bbe7c3391d02318548064128
4
+ data.tar.gz: 06e511b964f7a608461ed16f90ac0c89bcba8ae23703691b513ead2dc5ac19f7
5
+ SHA512:
6
+ metadata.gz: 1ec16956aa7d0f8768667a675cecd2faccfac76a833ce85a91564fc2b1ae5a53215d1025c37d88c237509e043dd0bd6e1e4f9b9ef625a9b09bf2521ad60505fc
7
+ data.tar.gz: eb902bc4b38b74a15c26b6c4d2732dbac7b246f8045236f8a25ce36192e4fab79df1ab91c97daa8c9fcadd0522d1a50ac859b9e1ea372dbafe2e6bb3a4537e47
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jekyll-thumb.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2018 Jonathan Davies
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ # jekyll-thumb
2
+
3
+ This Jekyll plugin automatically generates image thumnails using the [vips](https://jcupitt.github.io/libvips) library and the accompanying ruby wrapper, [ruby-vips.](https://github.com/jcupitt/ruby-vips)
4
+
5
+ libvips is like imagemagick but less bloated.
6
+
7
+ The plugin adds a `thumb` Liquid tag that can be used like so:
8
+
9
+ ```html
10
+ {% thumb src="/image.png" width="100" %}
11
+ ```
12
+
13
+ This will generate the thumbnail and output something like:
14
+
15
+ ```html
16
+ <a href="/image.png" target="_blank" class="thumb">
17
+ <img src="/image-100w.png" width="100" height="50">
18
+ </a>
19
+ ```
20
+
21
+ ## Development Status
22
+
23
+ This project was made because I needed thumbnails for [my blog](https://cleverna.me) and I didn't want to use imagemagick. I made it in like an hour and it definitely has bugs. OTOH it's 119 lines of code so if you don't like something you can just change it.
24
+
25
+ ## Installation
26
+
27
+ Add the gem to your Gemfile:
28
+
29
+ ```ruby
30
+ gem 'jekyll-thumb'
31
+ ```
32
+
33
+ Then bundle:
34
+
35
+ $ bundle
36
+
37
+ Or install it yourself as:
38
+
39
+ $ gem install jekyll-thumb
40
+
41
+ Finally install vips using your operating system's package manager, eg.
42
+
43
+ $ brew install vips
44
+
45
+ ## Usage
46
+
47
+ Use it like this in any Liquid template:
48
+
49
+ ```html
50
+ {% thumb src="/image.png" width="100" %}
51
+ ```
52
+
53
+ You must specify either a `width` or a `height`, but not both. Whichever dimension you specify, the other will be scaled to a matching ratio.
54
+
55
+ To use variables for the image or the dimensions, simply leave out the quotes:
56
+
57
+ ```html
58
+ {% thumb src=page.cover_image height=page.cover_image_height %}
59
+ ```
60
+
61
+ ## Optipng
62
+
63
+ If you have `optipng` installed and in your PATH, you can tell the plugin to run it on all generated png images.
64
+
65
+ Just add:
66
+
67
+ ```
68
+ thumb:
69
+ optipng: true
70
+ ```
71
+
72
+ To your \_config.yml
73
+
74
+ Currently the plugin doesn't optimize other image formats, except for stripping color palettes.
75
+
76
+ ## Caching images
77
+
78
+ Optimizing and resizing can take a while for some images. You can specify a cache folder in your Jekyll config to let jekyll-thumb cache images between runs.
79
+
80
+ ```
81
+ thumb:
82
+ cache: "/tmp/images"
83
+ ```
84
+
85
+ ## Contributing
86
+
87
+ Make an issue, send a pull request, you know the drill.
88
+
89
+ ## Acknowledgements
90
+
91
+ Project shamelessly ripped off of [netlify/jekyll-srcset](https://github.com/netlify/jekyll-srcset), it's basically a find-and-replace job.
92
+
93
+ ## Copyright
94
+
95
+ Copyright (c) 2018 Jonathan Davies. See [LICENSE](LICENSE) for details.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jekyll/thumb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jekyll-thumb"
8
+ spec.version = Jekyll::Thumb::VERSION
9
+ spec.authors = ["Jonathan Davies"]
10
+ spec.email = ["jonnie@cleverna.me"]
11
+ spec.summary = %q{This Jekyll plugin adds a thumb tag which will autogenerate image thumbnails}
12
+ spec.description = %q{
13
+ This Jekyll plugin autogenerates image thumbnails using the vips library
14
+
15
+ \{% thumb src="/image.png" width="100" %\}
16
+ }
17
+ spec.homepage = "https://github.com/JonnieCache/jekyll-thumb"
18
+ spec.license = "MIT"
19
+
20
+ spec.files = `git ls-files -z`.split("\x0")
21
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency "ruby-vips"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.7"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_runtime_dependency 'jekyll', '> 2'
30
+ end
@@ -0,0 +1,118 @@
1
+ require "ruby-vips"
2
+ require "digest/sha1"
3
+ require "pry"
4
+ require "pry-byebug"
5
+
6
+ module Jekyll
7
+ class ThumbTag < Liquid::Tag
8
+ attr_accessor :markup
9
+
10
+ def self.optipng?
11
+ @optinpng ||= system("which optipng")
12
+ end
13
+
14
+ def initialize(tag_name, markup, _)
15
+ @markup = markup
16
+ super
17
+ end
18
+
19
+ def render(context)
20
+ options = parse_options(markup, context)
21
+
22
+ return "Bad options to thumb_tag, syntax is: {% thumb_tag src=\"image.png\" width=\"100\"}" unless options["src"]
23
+ return "Error resizing - can't set both width and height" if options["width"] && options["height"]
24
+
25
+ site = context.registers[:site]
26
+ img_attrs = generate_image(site, options["src"], options)
27
+
28
+ %Q{<a href="#{options['src']}" target="_blank" class="thumb"><img #{options.merge(img_attrs).map {|k,v| "#{k}=\"#{v}\""}.join(" ")}></a>}
29
+ end
30
+
31
+ def parse_options(markup, context)
32
+ options = {}
33
+ markup.scan(/(\w+)=((?:"[^"]+")|(?:'[^']+')|[\w\.\_-]+)/) do |key,value|
34
+ if (value[0..0] == "'" && value[-1..-1]) == "'" || (value[0..0] == '"' && value[-1..-1] == '"')
35
+ options[key] = value[1..-2]
36
+ else
37
+ options[key] = context[value]
38
+ end
39
+ end
40
+ options
41
+ end
42
+
43
+ def config(site)
44
+ site.config['thumb'] || {}
45
+ end
46
+
47
+ def optimize?(site)
48
+ config(site)['optipng']
49
+ end
50
+
51
+ def cache_dir(site)
52
+ config(site)['cache']
53
+ end
54
+
55
+ def generate_image(site, src, attrs)
56
+ cache = cache_dir(site)
57
+
58
+ sha = cache && Digest::SHA1.hexdigest(attrs.sort.inspect + File.read(File.join(site.source, src)) + (optimize?(site) ? "optimize" : ""))
59
+ if sha
60
+ if File.exists?(File.join(cache, sha))
61
+ img_attrs = JSON.parse(File.read(File.join(cache,sha,"json")))
62
+ filename = img_attrs["src"].sub(/^\//, '')
63
+ dest = File.join(site.dest, filename)
64
+ FileUtils.mkdir_p(File.dirname(dest))
65
+ FileUtils.cp(File.join(cache,sha,"img"), dest)
66
+
67
+ site.config['keep_files'] << filename unless site.config['keep_files'].include?(filename)
68
+
69
+ return img_attrs
70
+ end
71
+ end
72
+ original_img_path = File.join(site.source, src)
73
+ original_img = Vips::Image.new_from_file original_img_path
74
+
75
+ img_attrs = {}
76
+
77
+ if attrs['width']
78
+ scale = attrs['width'].to_f / original_img.width
79
+ attrs['height'] = original_img.height * scale
80
+ elsif attrs['height']
81
+ scale = attrs['height'].to_f / original_img.height
82
+ attrs['width'] = original_img.width * scale
83
+ else
84
+ raise 'must specify either width or height'
85
+ end
86
+
87
+ img_attrs["height"] = attrs["height"].to_i if attrs["height"]
88
+ img_attrs["width"] = attrs["width"].to_i if attrs["width"]
89
+ img_attrs["src"] = src.sub(/(\.\w+)$/, "-#{img_attrs["width"]}w" + '\1')
90
+
91
+ filename = img_attrs["src"].sub(/^\//, '')
92
+ dest = File.join(site.dest, filename)
93
+
94
+ FileUtils.mkdir_p(File.dirname(dest))
95
+
96
+ unless File.exist?(dest)
97
+ thumb = Vips::Image.thumbnail original_img_path, img_attrs["width"]
98
+ thumb.write_to_file(dest, strip: true)
99
+ if dest.match(/\.png$/) && optimize?(site) && self.class.optipng?
100
+ `optipng #{dest}`
101
+ end
102
+ end
103
+ site.config['keep_files'] << filename unless site.config['keep_files'].include?(filename)
104
+ # Keep files around for incremental builds in Jekyll 3
105
+ site.regenerator.add(filename) if site.respond_to?(:regenerator)
106
+
107
+ if sha
108
+ FileUtils.mkdir_p(File.join(cache, sha))
109
+ FileUtils.cp(dest, File.join(cache, sha, "img"))
110
+ File.open(File.join(cache, sha, "json"), "w") do |f|
111
+ f.write(JSON.generate(img_attrs))
112
+ end
113
+ end
114
+
115
+ img_attrs
116
+ end
117
+ end
118
+ end
@@ -0,0 +1,5 @@
1
+ module Jekyll
2
+ module Thumb
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,8 @@
1
+ require "jekyll/thumb/version"
2
+ require "jekyll/thumb/tag"
3
+ require "jekyll"
4
+
5
+ module Jekyll
6
+ module Thumb
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require "jekyll/thumb"
2
+
3
+ Liquid::Template.register_tag('thumb', Jekyll::ThumbTag)
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jekyll-thumb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jonathan Davies
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-10-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ruby-vips
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jekyll
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">"
60
+ - !ruby/object:Gem::Version
61
+ version: '2'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">"
67
+ - !ruby/object:Gem::Version
68
+ version: '2'
69
+ description: |2
70
+
71
+ This Jekyll plugin autogenerates image thumbnails using the vips library
72
+
73
+ {% thumb src="/image.png" width="100" %}
74
+ email:
75
+ - jonnie@cleverna.me
76
+ executables: []
77
+ extensions: []
78
+ extra_rdoc_files: []
79
+ files:
80
+ - ".gitignore"
81
+ - Gemfile
82
+ - LICENSE.txt
83
+ - README.md
84
+ - Rakefile
85
+ - jekyll-thumb.gemspec
86
+ - lib/jekyll-thumb.rb
87
+ - lib/jekyll/thumb.rb
88
+ - lib/jekyll/thumb/tag.rb
89
+ - lib/jekyll/thumb/version.rb
90
+ homepage: https://github.com/JonnieCache/jekyll-thumb
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.7.6
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: This Jekyll plugin adds a thumb tag which will autogenerate image thumbnails
114
+ test_files: []