Class: Jekyll::Cloudinary::CloudinaryTag
- Inherits:
-
Liquid::Tag
- Object
- Liquid::Tag
- Jekyll::Cloudinary::CloudinaryTag
- Defined in:
- lib/jekyll/cloudinary.rb
Instance Method Summary collapse
-
#initialize(tag_name, markup, tokens) ⇒ CloudinaryTag
constructor
A new instance of CloudinaryTag.
- #render(context) ⇒ Object
Constructor Details
#initialize(tag_name, markup, tokens) ⇒ CloudinaryTag
Returns a new instance of CloudinaryTag.
7 8 9 10 |
# File 'lib/jekyll/cloudinary.rb', line 7 def initialize(tag_name, markup, tokens) @markup = markup super end |
Instance Method Details
#render(context) ⇒ Object
12 13 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 |
# File 'lib/jekyll/cloudinary.rb', line 12 def render(context) # Default settings preset_defaults = { "min_width" => 320, "max_width" => 1200, "fallback_max_width" => 1200, "steps" => 5, "sizes" => "100vw", "figure" => "auto", "attributes" => {}, "verbose" => false, "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" => false, "page" => false, "density" => false, "flags" => false, "transformation" => false } # TODO: Add validation for this parameters = { "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] baseurl = site.config["baseurl"] || "" settings = site.config["cloudinary"] url = settings["url"] || site.config["url"] # 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 image_src = markup[:image_src] # Dynamic image type type = "fetch" # TODO: URL2PNG requires signed URLs… need to investigate more # if /^url2png\:/.match(image_src) # type = "url2png" # image_src.gsub! "url2png:", "" # end 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) # Deal with the "caption" attribute as a true <figcaption> if html_attr["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 = "" 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 attr_string = html_attr.map { |a, v| "#{a}=\"#{v}\"" }.join(" ") # Figure out the Cloudinary transformations transformations = [] transformations_string = "" .each do | key, shortcode | if preset[key] transformations << "#{shortcode}_#{preset[key]}" end end if transformations.length > 0 transformations_string = transformations.compact.reject(&:empty?).join(',') + "," end # Build source image URL is_image_remote = /^https?/.match(image_src) if is_image_remote # It’s remote image_dest_path = image_src image_dest_url = image_src natural_width, natural_height = FastImage.size(image_dest_url) # width_height = "width=\"#{natural_width}\" height=\"#{natural_height}\"" fallback_url = "https://res.cloudinary.com/#{settings["cloud_name"]}/image/#{type}/#{transformations_string}w_#{preset["fallback_max_width"]}/#{image_dest_url}" else # It’s a local image is_image_src_absolute = %r!^/.*$!.match(image_src) if is_image_src_absolute image_src_path = File.join( site.config["source"], image_src ) image_dest_path = File.join( site.config["destination"], image_src ) image_dest_url = File.join( url, baseurl, image_src ) else image_src_path = File.join( site.config["source"], File.dirname(context["page"]["path"]), image_src ) image_dest_path = File.join( site.config["destination"], File.dirname(context["page"]["url"]), image_src ) image_dest_url = File.join( url, baseurl, File.dirname(context["page"]["url"]), image_src ) end if File.exist?(image_src_path) natural_width, natural_height = FastImage.size(image_src_path) # width_height = "width=\"#{natural_width}\" height=\"#{natural_height}\"" fallback_url = "https://res.cloudinary.com/#{settings["cloud_name"]}/image/#{type}/#{transformations_string}w_#{preset["fallback_max_width"]}/#{image_dest_url}" else natural_width = 100_000 width_height = "" Jekyll.logger.warn( "[Cloudinary]", "Couldn't find this image to check its width: #{image_src_path}." ) fallback_url = image_dest_url end 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 srcset << "https://res.cloudinary.com/#{settings["cloud_name"]}/image/#{type}/#{transformations_string}w_#{natural_width}/#{image_dest_url} #{natural_width}w" else missed_sizes = [] (1..steps).each do |factor| width = min_width + (factor - 1) * step_width if width <= natural_width srcset << "https://res.cloudinary.com/#{settings["cloud_name"]}/image/#{type}/#{transformations_string}w_#{width}/#{image_dest_url} #{width}w" else missed_sizes.push(width) end end unless missed_sizes.empty? srcset << "https://res.cloudinary.com/#{settings["cloud_name"]}/image/#{type}/#{transformations_string}w_#{natural_width}/#{image_dest_url} #{natural_width}w" 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 ( || 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>#{}</figcaption>\n</figure>\n" else "<img src=\"#{fallback_url}\" srcset=\"#{srcset_string}\" sizes=\"#{sizes}\" #{attr_string} #{img_attr} #{width_height} />" end end |