Class: Jekyll::Cloudinary::CloudinaryTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll/cloudinary.rb

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens) ⇒ CloudinaryTag

Returns a new instance of CloudinaryTag.



9
10
11
12
# File 'lib/jekyll/cloudinary.rb', line 9

def initialize(tag_name, markup, tokens)
  @markup = markup
  super
end

Instance Method Details

#render(context) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/jekyll/cloudinary.rb', line 14

def render(context)
  # Default settings
  settings_defaults = {
    "cloud_name"         => "",
    "only_prod"          => false,
    "verbose"            => false,
  }
  preset_defaults = {
    "min_width"          => 320,
    "max_width"          => 1200,
    "fallback_max_width" => 1200,
    "steps"              => 5,
    "sizes"              => "100vw",
    "figure"             => "auto",
    "attributes"         => {},
    "width_height"       => true,
    # Cloudinary transformations
    "height"             => false,
    "crop"               => "limit",
    "aspect_ratio"       => false,
    "gravity"            => false,
    "zoom"               => false,
    "x"                  => false,
    "y"                  => false,
    "format"             => false,
    "fetch_format"       => "auto",
    "quality"            => "auto",
    "radius"             => false,
    "angle"              => false,
    "effect"             => false,
    "opacity"            => false,
    "border"             => false,
    "background"         => false,
    "overlay"            => false,
    "underlay"           => false,
    "default_image"      => false,
    "delay"              => false,
    "color"              => false,
    "color_space"        => false,
    "dpr"                => "auto",
    "page"               => false,
    "density"            => false,
    "flags"              => false,
    "transformation"     => false,
  }

  # TODO: Add validation for this parameters
  transformation_options = {
    "height"         => "h",
    "crop"           => "c", # can include add-on: imagga_scale
    "aspect_ratio"   => "ar",
    "gravity"        => "g",
    "zoom"           => "z",
    "x"              => "x",
    "y"              => "y",
    "fetch_format"   => "f",
    "quality"        => "q", # can include add-on: jpegmini
    "radius"         => "r",
    "angle"          => "a",
    "effect"         => "e", # can include add-on: viesus_correct
    "opacity"        => "o",
    "border"         => "bo",
    "background"     => "b",
    "overlay"        => "l",
    "underlay"       => "u",
    "default_image"  => "d",
    "delay"          => "dl",
    "color"          => "co",
    "color_space"    => "cs",
    "dpr"            => "dpr",
    "page"           => "pg",
    "density"        => "dn",
    "flags"          => "fl",
    "transformation" => "t",
  }

  # Settings
  site = context.registers[:site]
  site_url = site.config["url"] || ""
  site_baseurl = site.config["baseurl"] || ""
  if site.config["cloudinary"].nil?
    Jekyll.logger.abort_with("[Cloudinary]", "You must set your cloud_name in _config.yml")
  end
  settings = settings_defaults.merge(site.config["cloudinary"])
  if settings["cloud_name"] == ""
    Jekyll.logger.abort_with("[Cloudinary]", "You must set your cloud_name in _config.yml")
  end
  url = settings["origin_url"] || (site_url + site_baseurl)

  # Get Markdown converter
  markdown_converter = site.find_converter_instance(::Jekyll::Converters::Markdown)

  preset_user_defaults = {}
  if settings["presets"]
    if settings["presets"]["default"]
      preset_user_defaults = settings["presets"]["default"]
    end
  end

  preset = preset_defaults.merge(preset_user_defaults)

  # Render any liquid variables in tag arguments and unescape template code
  rendered_markup = Liquid::Template
    .parse(@markup)
    .render(context)
    .gsub(%r!\\\{\\\{|\\\{\\%!, '\{\{' => "{{", '\{\%' => "{%")

  # Extract tag segments
  markup =
    %r!^(?:(?<preset>[^\s.:\/]+)\s+)?(?<image_src>[^\s]+\.[a-zA-Z0-9]{3,4})\s*(?<html_attr>[\s\S]+)?$!
      .match(rendered_markup)

  unless markup
    Jekyll.logger.abort_with("[Cloudinary]", "Can't read this tag: #{@markup}")
  end

  # Dynamic image type
  type = "upload"
  # TODO: URL2PNG requires signed URLs... need to investigate more
  # if /^url2png\:/.match(image_src)
  #   type = "url2png"
  #   image_src.gsub! "url2png:", ""
  # end
  # More TODO: GK changed from fetch

  image_src = markup[:image_src]

  if markup[:preset]
    if settings["presets"][markup[:preset]]
      preset = preset.merge(settings["presets"][markup[:preset]])
    elsif settings["verbose"]
      Jekyll.logger.warn(
        "[Cloudinary]",
        "'#{markup[:preset]}' preset for the Cloudinary plugin doesn't exist, \
        using the default one"
      )
    end
  end

  attributes = preset["attributes"]

  # Deep copy preset for single instance manipulation
  instance = Marshal.load(Marshal.dump(preset))

  # Process attributes
  html_attr = if markup[:html_attr]
                Hash[ *markup[:html_attr].scan(%r!(?<attr>[^\s="]+)(?:="(?<value>[^"]+)")?\s?!).flatten ]
              else
                {}
              end

  if instance["attr"]
    html_attr = instance.delete("attr").merge(html_attr)
  end

  # Classes from the tag should complete, not replace, the ones from the preset
  if html_attr["class"] && attributes["class"]
    html_attr["class"] << " #{attributes["class"]}"
  end
  html_attr = attributes.merge(html_attr)

  # SPECIAL

  # Deal with "special transformation" 
  if html_attr["transform"]
    trnsfm = html_attr["transform"]
    Jekyll.logger.warn("There is a transformatio @" + image_src + " ->" + trnsfm)
    html_attr.delete("transform")
  end

  # Deal with the "caption" attribute as a true <figcaption>
  if html_attr["caption"]
    caption = markdown_converter.convert(html_attr["caption"])
    html_attr.delete("caption")
  end

  # alt and title attributes should go only to the <img> even when there is a caption
  img_attr = "".dup
  if html_attr["alt"]
    img_attr << " alt=\"#{html_attr["alt"]}\""
    html_attr.delete("alt")
  end
  if html_attr["title"]
    img_attr << " title=\"#{html_attr["title"]}\""
    html_attr.delete("title")
  end
  if html_attr["loading"]
    img_attr << " loading=\"#{html_attr["loading"]}\""
    html_attr.delete("loading")
  end

  attr_string = html_attr.map { |a, v| "#{a}=\"#{v}\"" }.join(" ")

  # Figure out the Cloudinary transformations
  transformations = []
  transformations_string = ""
  transformation_options.each do |key, shortcode|
    if preset[key]
      transformations << "#{shortcode}_#{preset[key]}"
    end
  end
  unless transformations.empty?
    transformations_string = transformations.compact.reject(&:empty?).join(",") + ","
  end

  # Build source image URL
  is_image_remote = true #%r!^https?!.match(image_src)
  if is_image_remote
    # It's remote
    image_dest_path = image_src
    #image_dest_url = image_src
    if trnsfm.nil?
    	image_dest_url = "https://res.cloudinary.com/#{settings["cloud_name"]}/image/#{type}/q_auto:best/#{image_src}"
    else
    	image_dest_url = "https://res.cloudinary.com/#{settings["cloud_name"]}/image/#{type}/#{trnsfm}/#{image_src}"
    end
    

    if ENV["JEKYLL_ENV"] != "production"
     natural_width, natural_height = nil
    else
     natural_width, natural_height = FastImage.size(image_dest_url)
    end
    if natural_width.nil?
      if ENV["JEKYLL_ENV"] == "production"
      	Jekyll.logger.warn("remote url doesn't exists " + image_dest_url)
      end
   return "<img src=\"#{image_dest_url}\" />"
    end
    width_height = "width=\"#{natural_width}\" height=\"#{natural_height}\""
    if trnsfm.nil?
    	fallback_url = "https://res.cloudinary.com/#{settings["cloud_name"]}/image/#{type}/#{transformations_string}w_#{preset["fallback_max_width"]}/#{image_src}"
    else
    	fallback_url = "https://res.cloudinary.com/#{settings["cloud_name"]}/image/#{type}/#{trnsfm}/#{image_src}"
    end
  end

  ## Don't generate responsive image HTML and Cloudinary URLs for local development
  #if settings["only_prod"] && ENV["JEKYLL_ENV"] != "production"
  #  return "<img src=\"#{image_dest_url}\" #{attr_string} #{img_attr} #{width_height} crossorigin=\"anonymous\" />"
  #end

  srcset = []
  steps = preset["steps"].to_i
  min_width = preset["min_width"].to_i
  max_width = preset["max_width"].to_i
  step_width = (max_width - min_width) / (steps - 1)
  sizes = preset["sizes"]

  if natural_width < min_width
    if settings["verbose"]
      Jekyll.logger.warn(
        "[Cloudinary]",
        "Width of source image '#{File.basename(image_src)}' (#{natural_width}px) \
        in #{context["page"]["path"]} not enough for ANY srcset version"
      )
    end
    if trnsfm.nil?
     srcset << "https://res.cloudinary.com/#{settings["cloud_name"]}/image/#{type}/#{transformations_string}w_#{natural_width}/#{image_src} #{natural_width}w"
    else
     srcset << "https://res.cloudinary.com/#{settings["cloud_name"]}/image/#{type}/#{trnsfm}/#{image_src} #{natural_width}w"
    end          
  else
    missed_sizes = []
    (1..steps).each do |factor|
      width = min_width + (factor - 1) * step_width
      if width <= natural_width
     if trnsfm.nil?
         srcset << "https://res.cloudinary.com/#{settings["cloud_name"]}/image/#{type}/#{transformations_string}w_#{width}/#{image_src} #{width}w"
     else
         srcset << "https://res.cloudinary.com/#{settings["cloud_name"]}/image/#{type}/#{trnsfm}/#{image_src} #{width}w"
     end          
      else
        missed_sizes.push(width)
      end
    end
    unless missed_sizes.empty?
      if trnsfm.nil?
       srcset << "https://res.cloudinary.com/#{settings["cloud_name"]}/image/#{type}/#{transformations_string}w_#{natural_width}/#{image_src} #{natural_width}w"
      else
       srcset << "https://res.cloudinary.com/#{settings["cloud_name"]}/image/#{type}/#{trnsfm}/#{image_src} #{natural_width}w"
      end          
      if settings["verbose"]
        Jekyll.logger.warn(
          "[Cloudinary]",
          "Width of source image '#{File.basename(image_src)}' (#{natural_width}px) \
          in #{context["page"]["path"]} not enough for #{missed_sizes.join("px, ")}px \
          version#{missed_sizes.length > 1 ? "s" : ""}"
        )
      end
    end
  end
  srcset_string = srcset.join(",\n")

  # preset['figure'] can be 'never', 'auto' or 'always'
  if (caption || preset["figure"] == "always") && preset["figure"] != "never"
    "\n<figure #{attr_string}>\n<img src=\"#{fallback_url}\" srcset=\"#{srcset_string}\" sizes=\"#{sizes}\" #{img_attr} #{width_height} />\n<figcaption>#{caption}</figcaption>\n</figure>\n"
  else
    "<img src=\"#{fallback_url}\" srcset=\"#{srcset_string}\" sizes=\"#{sizes}\" #{attr_string} #{img_attr} #{width_height} crossorigin=\"anonymous\" />"
  end
end