bridgetown-cloudinary 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e2a0a39fcc006a2b1293e4a18feddac2cbb2aa37d305c45830c3493a3b031543
4
+ data.tar.gz: 6a3b8794b0151383dc4f331df4d9282a229681b315fa490c26791ca96fa04c92
5
+ SHA512:
6
+ metadata.gz: b1680c1d594c65fe4d1ffda7818c1bc46ffc2951a5d13569678e7054feab59442ae3f0fda80037d22bac46e7c39e04aa4c7a141d59070fc2b5cca7c25b914a42
7
+ data.tar.gz: 62cc8410cd8248e69d61793b94b9c9ff91744c2cc4650d720f795789244c3fb067b6c60b0d558281c4a3a94e7986a423a5961b852a1b3ceeba9867baea5edbf7
@@ -0,0 +1,38 @@
1
+ /vendor
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
11
+ *.bundle
12
+ *.so
13
+ *.o
14
+ *.a
15
+ mkmf.log
16
+ *.gem
17
+ Gemfile.lock
18
+ .bundle
19
+ .ruby-version
20
+
21
+ # Node
22
+ node_modules
23
+ .npm
24
+ .node_repl_history
25
+
26
+ # Yarn
27
+ yarn-error.log
28
+ yarn-debug.log*
29
+ .pnp/
30
+ .pnp.js
31
+
32
+ # Yarn Integrity file
33
+ .yarn-integrity
34
+
35
+ spec/dest
36
+ .bridgetown-metadata
37
+ .bridgetown-cache
38
+ .bridgetown-webpack
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -0,0 +1,26 @@
1
+ require: rubocop-bridgetown
2
+
3
+ inherit_gem:
4
+ rubocop-bridgetown: .rubocop.yml
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 2.5
8
+ Include:
9
+ - lib/**/*.rb
10
+
11
+ Exclude:
12
+ - .gitignore
13
+ - .rspec
14
+ - .rubocop.yml
15
+
16
+ - Gemfile.lock
17
+ - CHANGELOG.md
18
+ - LICENSE.txt
19
+ - README.md
20
+
21
+ - script/**/*
22
+ - vendor/**/*
23
+
24
+ Metrics/AbcSize:
25
+ Exclude:
26
+ - lib/bridgetown-cloudinary/utils.rb
@@ -0,0 +1,6 @@
1
+ # master
2
+
3
+ # 1.0.0 / 2020-05-18
4
+
5
+ * First version of the Cloudinary plugin for Bridgetown, including defaults for
6
+ various image sizes.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020-present
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.
@@ -0,0 +1,137 @@
1
+ # Bridgetown Cloudinary Plugin
2
+
3
+ [Bridgetown](https://www.bridgetownrb.com) is a Ruby-powered static site generator.
4
+ [Cloudinary](https://www.cloudinary.com) is digital asset management service which
5
+ makes uploading and serving resized, transformed, and compressed images and videos
6
+ easy and fast.
7
+
8
+ This plugin wires the two up so you can use Cloudinary images in your blog posts,
9
+ articles, product pages, site templates, and anywhere else you might need to
10
+ reference media optimized for mobile and responsive design.
11
+
12
+ ## Installation
13
+
14
+ Run this command to add this plugin to your site's Gemfile:
15
+
16
+ ```shell
17
+ $ bundle add bridgetown-cloudinary -g bridgetown_plugins
18
+ ```
19
+
20
+ Or simply add this line to your Gemfile and run `bundle install`:
21
+
22
+ ```ruby
23
+ gem 'bridgetown-cloudinary', group: "bridgetown_plugins"
24
+ ```
25
+
26
+ Then modify your `bridgetown.config.yml` configuration to point to your Cloudinary
27
+ cloud name:
28
+
29
+ ```yaml
30
+ cloudinary:
31
+ cloud_name: my-cloud-name
32
+ ```
33
+
34
+ ## Usage
35
+
36
+ The simplest usage of the Bridgetown Cloudinary plugin is to add a `cloudinary_id` to
37
+ the front matter of a page or document. For example:
38
+
39
+ ```yaml
40
+ ---
41
+ title: I'm a Blog Post
42
+ cloudinary_id: some-folder-with/image-in-cloudinary
43
+ category: neat
44
+ ---
45
+
46
+ I'm a blog post with an image!
47
+ ```
48
+
49
+ The plugin will automatically add `image.path` front matter with a generated URL to
50
+ the image in Cloudinary using the default configured transformation `open_graph`.
51
+ This sizes and compresses an image suitable for use on Twitter, Facebook, etc.
52
+ (See below for information on how to change the default transformation or add your
53
+ own.)
54
+
55
+ You can use `image.path` in a template:
56
+
57
+ `{{ post.image.path }}`
58
+
59
+ Since `image.path` is also referenced by the Bridgetown [Feed](https://github.com/bridgetownrb/bridgetown-feed) and [SEO](https://github.com/bridgetownrb/bridgetown-seo-tag) plugins,
60
+ your Cloudinary images will be picked up in those contexts automatically.
61
+
62
+ To reference other available sizes, you can use either a Liquid tag or filter,
63
+ depending on your needs. Using a tag:
64
+
65
+ `{% cloudinary_img "Alt text goes here", post.cloudinary_id, "large" %}`
66
+
67
+ Or a filter:
68
+
69
+ `<img alt="Alt text" src="{{ post.cloudinary_id | cloudinary_url: "medium" }}" />`
70
+
71
+
72
+ ### Default Sizes Included
73
+
74
+ Here's a list of all the Cloudinary transformation strings that ship with the plugin:
75
+
76
+ ```yaml
77
+ open_graph: "c_fill,g_face:center,w_1600,h_900,q_50" # default
78
+ tiny: "w_300,c_limit,q_90"
79
+ small: "w_600,c_limit,q_85"
80
+ medium: "w_1200,c_limit,q_80"
81
+ large: "w_1800,c_limit,q_80"
82
+ xlarge: "w_2048,c_limit,q_75"
83
+ ```
84
+
85
+ The image format used in all cases is JPG.
86
+
87
+ ### Optional Configuration Options
88
+
89
+ If you'd like to reference any of the configured image sizes directly through front
90
+ matter as an alternative to using tags or filters, you can switch this on in your
91
+ `bridgetown.config.yml`:
92
+
93
+ ```yaml
94
+ cloudinary:
95
+ add_transformed_urls_to_image_front_matter: true
96
+ ```
97
+
98
+ Then you'll be able to reference image sizes like so:
99
+
100
+ `<img alt="Alt text" src="{{ post.image.tiny }}" />`
101
+
102
+ Be aware that if an `image` front matter variable has already defined for a document,
103
+ it will remain intact and the Cloudinary image transformations won't be apply for
104
+ that document.
105
+
106
+ You can also change or add your own transformations! Simply define them in your
107
+ config:
108
+
109
+ ```yaml
110
+ cloudinary:
111
+ transformations:
112
+ tiny: w_300,c_limit,q_90 # this overrides the builtin tiny transformation
113
+ max_bw: e_grayscale,w_4096,c_limit,q_65 # this is a new custom transformation
114
+ ```
115
+
116
+ If you configure transformations to get added to front matter, all custom
117
+ transformations will show up there as well:
118
+
119
+ `B&W image URL: {{ post.image.max_bw }}`
120
+
121
+ ## Testing
122
+
123
+ * Run `bundle exec rspec` to run the test suite
124
+ * Or run `script/cibuild` to validate with Rubocop and test with rspec together
125
+
126
+ ## Contributing
127
+
128
+ 1. Fork it (https://github.com/bridgetownrb/bridgetown-cloudinary/fork)
129
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
130
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
131
+ 4. Push to the branch (`git push origin my-new-feature`)
132
+ 5. Create a new Pull Request
133
+
134
+ ## Releasing
135
+
136
+ To release a new version of the plugin, simply bump up the version number in
137
+ `version.rb` and then run `script/release`.
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/bridgetown-cloudinary/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "bridgetown-cloudinary"
7
+ spec.version = Bridgetown::Cloudinary::VERSION
8
+ spec.author = "Bridgetown Team"
9
+ spec.email = "maintainers@bridgetownrb.com"
10
+ spec.summary = "Embed or access images with transformations using Cloudinary"
11
+ spec.homepage = "https://github.com/bridgetownrb/bridgetown-cloudinary"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r!^(test|script|spec|features|frontend)/!) }
15
+ spec.test_files = spec.files.grep(%r!^spec/!)
16
+ spec.require_paths = ["lib"]
17
+
18
+ spec.required_ruby_version = ">= 2.5.0"
19
+
20
+ spec.add_dependency "bridgetown", ">= 0.14", "< 2.0"
21
+ spec.add_dependency "cloudinary", ">= 0.13"
22
+
23
+ spec.add_development_dependency "bundler"
24
+ spec.add_development_dependency "nokogiri", "~> 1.6"
25
+ spec.add_development_dependency "rake", "~> 12.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "rubocop-bridgetown", "~> 0.2"
28
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bridgetown"
4
+ require "cloudinary"
5
+ require "bridgetown-cloudinary/utils"
6
+ require "bridgetown-cloudinary/builder"
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Cloudinary
5
+ class Builder < Bridgetown::Builder
6
+ CONFIG_DEFAULTS = {
7
+ cloudinary: {
8
+ cloud_name: nil,
9
+ default_transformation: "open_graph",
10
+ default_image_format: "jpg",
11
+ transformations: {
12
+ open_graph: "c_fill,g_face:center,w_1600,h_900,q_50",
13
+ tiny: "w_300,c_limit,q_90",
14
+ small: "w_600,c_limit,q_85",
15
+ medium: "w_1200,c_limit,q_80",
16
+ large: "w_1800,c_limit,q_80",
17
+ xlarge: "w_2048,c_limit,q_75",
18
+ },
19
+ add_transformed_urls_to_image_front_matter: false,
20
+ },
21
+ }.freeze
22
+
23
+ # Set up the Cloudinary configuration
24
+ def build
25
+ ::Cloudinary.config({
26
+ "cloud_name" => config[:cloudinary][:cloud_name],
27
+ "secure" => true,
28
+ })
29
+
30
+ generator :add_image_urls_to_documents
31
+ liquid_tag "cloudinary_img", :img_tag
32
+ liquid_filter "cloudinary_url", :url_filter
33
+ end
34
+
35
+ # Populate front matter
36
+ def add_image_urls_to_documents
37
+ Bridgetown::Cloudinary::Utils.add_image_urls_to_documents(
38
+ site, config[:cloudinary]
39
+ )
40
+ end
41
+
42
+ # Define the "cloudinary_img" Liquid tag
43
+ def img_tag(attributes, tag)
44
+ alt, id, transformation = attributes.split(",").map(&:strip)
45
+ alt.delete! '"'
46
+ if id.include? '"'
47
+ id.delete! '"'
48
+ elsif id.include? "."
49
+ obj, var = id.split(".")
50
+ id = tag.context[obj][var]
51
+ else
52
+ id = tag.context[id]
53
+ end
54
+ transformation&.delete! '"'
55
+
56
+ "<img alt=\"#{alt}\" src=\"#{url_filter(id, transformation)}\" />"
57
+ end
58
+
59
+ # Define the "cloudinary_url" Liquid filter
60
+ def url_filter(id, transformation = nil)
61
+ Bridgetown::Cloudinary::Utils.url(
62
+ config: config[:cloudinary], id: id, transformation: transformation
63
+ )
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ Bridgetown::Cloudinary::Builder.register
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Cloudinary
5
+ module Utils
6
+ def self.url(config:, id:, transformation:)
7
+ transformation = if transformation
8
+ "/#{config[:transformations][transformation]}"
9
+ else
10
+ "/#{config[:transformations][config[:default_transformation]]}"
11
+ end
12
+ image_format = config[:default_image_format]
13
+
14
+ cloudinary_url = ::Cloudinary::Utils.cloudinary_url(
15
+ id, { format: image_format }
16
+ )
17
+
18
+ cloudinary_url&.sub("/image/upload", "/image/upload#{transformation}")
19
+ end
20
+
21
+ def self.add_image_urls_to_documents(site, config)
22
+ (site.documents + site.pages).each do |page|
23
+ next unless page.data[:cloudinary_id] && !page.data[:image]
24
+
25
+ page.data[:image] = {
26
+ path: url(
27
+ config: config,
28
+ id: page.data[:cloudinary_id],
29
+ transformation: nil
30
+ ),
31
+ }
32
+ next unless config[:add_transformed_urls_to_image_front_matter]
33
+
34
+ config[:transformations].each_key do |transformation|
35
+ page.data[:image][transformation] = url(
36
+ config: config,
37
+ id: page.data[:cloudinary_id],
38
+ transformation: transformation
39
+ )
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Bridgetown
4
+ module Cloudinary
5
+ VERSION = "1.0.0"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bridgetown-cloudinary
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Bridgetown Team
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bridgetown
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0.14'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '2.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '0.14'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '2.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: cloudinary
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0.13'
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0.13'
47
+ - !ruby/object:Gem::Dependency
48
+ name: bundler
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: nokogiri
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '1.6'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '1.6'
75
+ - !ruby/object:Gem::Dependency
76
+ name: rake
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '12.0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - "~>"
87
+ - !ruby/object:Gem::Version
88
+ version: '12.0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rspec
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - "~>"
94
+ - !ruby/object:Gem::Version
95
+ version: '3.0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - "~>"
101
+ - !ruby/object:Gem::Version
102
+ version: '3.0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: rubocop-bridgetown
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: '0.2'
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: '0.2'
117
+ description:
118
+ email: maintainers@bridgetownrb.com
119
+ executables: []
120
+ extensions: []
121
+ extra_rdoc_files: []
122
+ files:
123
+ - ".gitignore"
124
+ - ".rspec"
125
+ - ".rubocop.yml"
126
+ - CHANGELOG.md
127
+ - Gemfile
128
+ - LICENSE.txt
129
+ - README.md
130
+ - Rakefile
131
+ - bridgetown-cloudinary.gemspec
132
+ - lib/bridgetown-cloudinary.rb
133
+ - lib/bridgetown-cloudinary/builder.rb
134
+ - lib/bridgetown-cloudinary/utils.rb
135
+ - lib/bridgetown-cloudinary/version.rb
136
+ homepage: https://github.com/bridgetownrb/bridgetown-cloudinary
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: 2.5.0
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubygems_version: 3.0.8
156
+ signing_key:
157
+ specification_version: 4
158
+ summary: Embed or access images with transformations using Cloudinary
159
+ test_files: []