module Gtn::Images

Module to handle pre-calculating image dimensions We can then use those dimensions in the HTML to avoid reflow

Public Class Methods

_get_image_dimensions(path) click to toggle source
# File _plugins/gtn/images.rb, line 44
def self._get_image_dimensions(path)
  cache.getset(path) do
    [FastImage.size(path), path]
  rescue StandardError
    Jekyll.logger.info "[GTN/Images] Could not resolve size of #{path}"
  end
end
cache() click to toggle source
# File _plugins/gtn/images.rb, line 17
def self.cache
  @@cache ||= Jekyll::Cache.new('ImageDimensions')
end
get_image_dimensions(tuto_dir, url) click to toggle source
# File _plugins/gtn/images.rb, line 33
def self.get_image_dimensions(tuto_dir, url)
  if (match = url.match(%r{^{{\s*site.baseurl\s*}}/(.*)})) || (match = url.match(/{%\s*link\s*(.*)\s*%}/))
    _get_image_dimensions(match[1].strip)
  elsif !url.match(%r{https?://})
    img_path = File.absolute_path(File.join(tuto_dir, url))
    _get_image_dimensions(img_path) if File.exist?(img_path)
  else
    _get_image_dimensions(img_path)
  end
end
html_image_dimensions(tuto_dir, url) click to toggle source
# File _plugins/gtn/images.rb, line 21
def self.html_image_dimensions(tuto_dir, url)
  return '' if !FASTIMAGE_AVAILABLE or Jekyll.env == 'development'

  (width, height), path = get_image_dimensions(tuto_dir, url)
  return unless width && height

  [
    %(width="#{width}" height=#{height}),
    path
  ]
end