jekyll-cloudinary 1.2.15 → 1.2.16
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 +4 -4
- data/lib/jekyll/cloudinary/version.rb +1 -1
- data/lib/jekyll/cloudinary.rb +90 -65
- data/lib/jekyll-cloudinary.rb +1 -1
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cbd2c52248fa02e936e4dbf7816a23aa5cfaa866
|
4
|
+
data.tar.gz: a417e4a1b1369e3b8f7ce6cb5a696853661fc1d5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 24e32b20536c43c13db0cf44d4f02a88854bda4fa02dfa6c8a8eb13bf99b993587c1fadc38560772f86a68d410713068da5f11d31879c59a442eb30b8c099b75
|
7
|
+
data.tar.gz: 25cf64ff3dda1cd46a99c34a789903f4c22a63efe48279f5ae43201156f02f063d8e3b4db38fc9ce0c28e234941dcaacc1f281595c6aa559f1804d5cae0eeee3
|
data/lib/jekyll/cloudinary.rb
CHANGED
@@ -2,7 +2,6 @@ module Jekyll
|
|
2
2
|
module Cloudinary
|
3
3
|
|
4
4
|
class CloudinaryTag < Liquid::Tag
|
5
|
-
|
6
5
|
require "RMagick"
|
7
6
|
|
8
7
|
def initialize(tag_name, markup, tokens)
|
@@ -11,152 +10,179 @@ module Jekyll
|
|
11
10
|
end
|
12
11
|
|
13
12
|
def render(context)
|
14
|
-
|
15
13
|
# Default settings
|
16
14
|
preset_defaults = {
|
17
|
-
"min_width"
|
18
|
-
"max_width"
|
19
|
-
"steps"
|
20
|
-
"sizes"
|
21
|
-
"figure"
|
22
|
-
"attributes" => {
|
23
|
-
"verbose"
|
15
|
+
"min_width" => 320,
|
16
|
+
"max_width" => 1200,
|
17
|
+
"steps" => 5,
|
18
|
+
"sizes" => "100vw",
|
19
|
+
"figure" => "auto",
|
20
|
+
"attributes" => {},
|
21
|
+
"verbose" => false
|
24
22
|
}
|
25
23
|
|
26
24
|
# Settings
|
27
25
|
site = context.registers[:site]
|
28
|
-
url = site.config[
|
29
|
-
settings = site.config[
|
26
|
+
url = site.config["url"]
|
27
|
+
settings = site.config["cloudinary"]
|
30
28
|
|
31
29
|
# Get Markdown converter
|
32
30
|
markdown_converter = site.find_converter_instance(::Jekyll::Converters::Markdown)
|
33
31
|
|
34
32
|
preset_user_defaults = {}
|
35
|
-
if settings[
|
36
|
-
if settings[
|
37
|
-
preset_user_defaults = settings[
|
33
|
+
if settings["presets"]
|
34
|
+
if settings["presets"]["default"]
|
35
|
+
preset_user_defaults = settings["presets"]["default"]
|
38
36
|
end
|
39
37
|
end
|
40
38
|
|
41
39
|
preset = preset_defaults.merge(preset_user_defaults)
|
42
40
|
|
43
41
|
# Render any liquid variables in tag arguments and unescape template code
|
44
|
-
rendered_markup = Liquid::Template
|
42
|
+
rendered_markup = Liquid::Template
|
43
|
+
.parse(@markup)
|
44
|
+
.render(context)
|
45
|
+
.gsub(%r!\\\{\\\{|\\\{\\%!, '\{\{' => '{{', '\{\%' => '{%')
|
45
46
|
|
46
47
|
# Extract tag segments
|
47
|
-
markup =
|
48
|
-
|
49
|
-
|
48
|
+
markup =
|
49
|
+
%r!^(?:(?<preset>[^\s.:\/]+)\s+)?(?<image_src>[^\s]+\.[a-zA-Z0-9]{3,4})\s*(?<html_attr>[\s\S]+)?$!
|
50
|
+
.match(rendered_markup)
|
51
|
+
|
52
|
+
unless markup
|
53
|
+
Jekyll.logger.abort_with("[Cloudinary]", "Can't read this tag: #{@markup}")
|
50
54
|
end
|
51
55
|
|
52
56
|
image_src = markup[:image_src]
|
53
57
|
|
54
58
|
# Build source image URL
|
55
|
-
is_image_path_absolute =
|
59
|
+
is_image_path_absolute = %r!^/.*$!.match(image_src)
|
56
60
|
if is_image_path_absolute
|
57
|
-
image_path = File.join(site.config[
|
61
|
+
image_path = File.join(site.config["destination"], image_src)
|
58
62
|
image_url = File.join(url, image_src)
|
59
63
|
else
|
60
|
-
image_path = File.join(
|
61
|
-
|
64
|
+
image_path = File.join(
|
65
|
+
site.config["destination"],
|
66
|
+
File.dirname(context["page"].url),
|
67
|
+
image_src
|
68
|
+
)
|
69
|
+
image_url = File.join(
|
70
|
+
url,
|
71
|
+
File.dirname(context["page"].url),
|
72
|
+
image_src
|
73
|
+
)
|
62
74
|
end
|
63
75
|
|
64
76
|
# Get source image natural width
|
65
77
|
if File.exist?(image_path)
|
66
78
|
image = Magick::Image::read(image_path).first
|
67
79
|
natural_width = image.columns
|
68
|
-
fallback_url = "https://res.cloudinary.com/#{settings[
|
80
|
+
fallback_url = "https://res.cloudinary.com/#{settings["cloud_name"]}/image/fetch/c_limit,w_#{natural_width},q_auto,f_auto/#{image_url}"
|
69
81
|
else
|
70
|
-
natural_width =
|
71
|
-
Jekyll.logger.warn(
|
72
|
-
|
82
|
+
natural_width = 100_000
|
83
|
+
Jekyll.logger.warn(
|
84
|
+
"[Cloudinary]",
|
85
|
+
"Couldn't find this image to check its width: #{image_path}. \
|
86
|
+
Try to run Jekyll build a second time."
|
87
|
+
)
|
88
|
+
fallback_url = image_url
|
73
89
|
end
|
74
90
|
|
75
91
|
if markup[:preset]
|
76
|
-
if settings[
|
77
|
-
preset = preset.merge(settings[
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
92
|
+
if settings["presets"][markup[:preset]]
|
93
|
+
preset = preset.merge(settings["presets"][markup[:preset]])
|
94
|
+
elsif settings["verbose"]
|
95
|
+
Jekyll.logger.warn(
|
96
|
+
"[Cloudinary]",
|
97
|
+
"'#{markup[:preset]}' preset for the Cloudinary plugin doesn't exist, \
|
98
|
+
using the default one"
|
99
|
+
)
|
82
100
|
end
|
83
101
|
end
|
84
102
|
|
85
|
-
attributes = preset[
|
103
|
+
attributes = preset["attributes"]
|
86
104
|
|
87
105
|
# Deep copy preset for single instance manipulation
|
88
106
|
instance = Marshal.load(Marshal.dump(preset))
|
89
107
|
|
90
108
|
# Process attributes
|
91
109
|
html_attr = if markup[:html_attr]
|
92
|
-
Hash[ *markup[:html_attr].scan(
|
110
|
+
Hash[ *markup[:html_attr].scan(%r!(?<attr>[^\s="]+)(?:="(?<value>[^"]+)")?\s?!).flatten ]
|
93
111
|
else
|
94
112
|
{}
|
95
113
|
end
|
96
114
|
|
97
|
-
if instance[
|
98
|
-
html_attr = instance.delete(
|
115
|
+
if instance["attr"]
|
116
|
+
html_attr = instance.delete("attr").merge(html_attr)
|
99
117
|
end
|
100
118
|
|
101
119
|
# Classes from the tag should complete, not replace, the ones from the preset
|
102
|
-
if html_attr[
|
103
|
-
html_attr[
|
120
|
+
if html_attr["class"] && attributes["class"]
|
121
|
+
html_attr["class"] << " #{attributes["class"]}"
|
104
122
|
end
|
105
123
|
html_attr = attributes.merge(html_attr)
|
106
124
|
|
107
125
|
# Deal with the "caption" attribute as a true <figcaption>
|
108
|
-
if html_attr[
|
109
|
-
caption = markdown_converter.convert(html_attr[
|
110
|
-
html_attr.delete(
|
126
|
+
if html_attr["caption"]
|
127
|
+
caption = markdown_converter.convert(html_attr["caption"])
|
128
|
+
html_attr.delete("caption")
|
111
129
|
end
|
112
130
|
|
113
131
|
# alt and title attributes should go only to the <img> even when there is a caption
|
114
132
|
img_attr = ""
|
115
|
-
if html_attr[
|
116
|
-
img_attr << " alt=\"#{html_attr[
|
117
|
-
html_attr.delete(
|
133
|
+
if html_attr["alt"]
|
134
|
+
img_attr << " alt=\"#{html_attr["alt"]}\""
|
135
|
+
html_attr.delete("alt")
|
118
136
|
end
|
119
|
-
if html_attr[
|
120
|
-
img_attr << " title=\"#{html_attr[
|
121
|
-
html_attr.delete(
|
137
|
+
if html_attr["title"]
|
138
|
+
img_attr << " title=\"#{html_attr["title"]}\""
|
139
|
+
html_attr.delete("title")
|
122
140
|
end
|
123
141
|
|
124
|
-
attr_string = html_attr.map { |a,v| "#{a}=\"#{v}\"" }.join(
|
142
|
+
attr_string = html_attr.map { |a, v| "#{a}=\"#{v}\"" }.join(" ")
|
125
143
|
|
126
144
|
srcset = []
|
127
|
-
steps = preset[
|
128
|
-
min_width = preset[
|
129
|
-
max_width = preset[
|
145
|
+
steps = preset["steps"].to_i
|
146
|
+
min_width = preset["min_width"].to_i
|
147
|
+
max_width = preset["max_width"].to_i
|
130
148
|
step_width = (max_width - min_width) / (steps - 1)
|
131
|
-
sizes = preset[
|
149
|
+
sizes = preset["sizes"]
|
132
150
|
|
133
151
|
if natural_width < min_width
|
134
|
-
if settings[
|
135
|
-
Jekyll.logger.warn(
|
152
|
+
if settings["verbose"]
|
153
|
+
Jekyll.logger.warn(
|
154
|
+
"[Cloudinary]",
|
155
|
+
"Width of source image '#{File.basename(image_src)}' (#{natural_width}px) \
|
156
|
+
in #{context["page"].path} not enough for ANY srcset version"
|
157
|
+
)
|
136
158
|
end
|
137
|
-
srcset << "https://res.cloudinary.com/#{settings[
|
159
|
+
srcset << "https://res.cloudinary.com/#{settings["cloud_name"]}/image/fetch/c_limit,w_#{natural_width},q_auto,f_auto/#{image_url} #{natural_width}w"
|
138
160
|
else
|
139
161
|
missed_sizes = []
|
140
162
|
(1..steps).each do |factor|
|
141
163
|
width = min_width + (factor - 1) * step_width
|
142
164
|
if width <= natural_width
|
143
|
-
srcset << "https://res.cloudinary.com/#{settings[
|
165
|
+
srcset << "https://res.cloudinary.com/#{settings["cloud_name"]}/image/fetch/c_limit,w_#{width},q_auto,f_auto/#{image_url} #{width}w"
|
144
166
|
else
|
145
167
|
missed_sizes.push(width)
|
146
168
|
end
|
147
169
|
end
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
170
|
+
if missed_sizes.length.empty?
|
171
|
+
srcset << "https://res.cloudinary.com/#{settings["cloud_name"]}/image/fetch/c_limit,w_#{natural_width},q_auto,f_auto/#{image_url} #{natural_width}w"
|
172
|
+
if settings["verbose"]
|
173
|
+
Jekyll.logger.warn(
|
174
|
+
"[Cloudinary]",
|
175
|
+
"Width of source image '#{File.basename(image_src)}' (#{natural_width}px) \
|
176
|
+
in #{context["page"].path} not enough for #{missed_sizes.join("px, ")}px \
|
177
|
+
version#{missed_sizes.length > 1 ? "s" : ""}"
|
178
|
+
)
|
153
179
|
end
|
154
|
-
|
180
|
+
end
|
155
181
|
end
|
156
182
|
srcset_string = srcset.join(",\n")
|
157
183
|
|
158
184
|
# preset['figure'] can be 'never', 'auto' or 'always'
|
159
|
-
if (caption || preset[
|
185
|
+
if (caption || preset["figure"] == "always") && preset["figure"] != "never"
|
160
186
|
"\n<figure #{attr_string}>\n<img src=\"#{fallback_url}\" srcset=\"#{srcset_string}\" sizes=\"#{sizes}\" #{img_attr} />\n<figcaption>#{caption}</figcaption>\n</figure>\n"
|
161
187
|
else
|
162
188
|
"<img src=\"#{fallback_url}\" srcset=\"#{srcset_string}\" sizes=\"#{sizes}\" #{attr_string} #{img_attr} />"
|
@@ -167,5 +193,4 @@ module Jekyll
|
|
167
193
|
end
|
168
194
|
end
|
169
195
|
|
170
|
-
Liquid::Template.register_tag(
|
171
|
-
|
196
|
+
Liquid::Template.register_tag("cloudinary", Jekyll::Cloudinary::CloudinaryTag)
|
data/lib/jekyll-cloudinary.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require "jekyll/cloudinary"
|
1
|
+
require "jekyll/cloudinary"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jekyll-cloudinary
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.16
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nicolas Hoizey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-08-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: jekyll
|
@@ -78,6 +78,20 @@ dependencies:
|
|
78
78
|
- - "~>"
|
79
79
|
- !ruby/object:Gem::Version
|
80
80
|
version: '10.0'
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: rubocop
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - "~>"
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0.42'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - "~>"
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0.42'
|
81
95
|
description: " Liquid tag to use Cloudinary for optimized responsive posts images.\n"
|
82
96
|
email:
|
83
97
|
- nicolas@hoizey.com
|