class Jekyll::Avatar

Constants

API_VERSION
DEFAULT_SIZE
SERVERS
VERSION

Public Class Methods

new(_tag_name, text, _tokens) click to toggle source
Calls superclass method
# File lib/jekyll-avatar.rb, line 13
def initialize(_tag_name, text, _tokens)
  super
  @text = text
end

Public Instance Methods

render(context) click to toggle source
# File lib/jekyll-avatar.rb, line 18
def render(context)
  @context = context
  @text    = Liquid::Template.parse(@text).render(@context)
  attrs    = attributes.map { |k, v| "#{k}=\"#{v}\"" }.join(" ")
  "<img #{attrs} />"
end

Private Instance Methods

attributes() click to toggle source
# File lib/jekyll-avatar.rb, line 27
def attributes
  result = {
    :class                => classes,
    :alt                  => username,
    :width                => size,
    :height               => size,
    "data-proofer-ignore" => true
  }

  if lazy_load?
    result[:src] = ""
    result["data-src"] = url
    result["data-srcset"] = srcset
  else
    result[:src] = url
    result[:srcset] = srcset
  end

  result
end
classes() click to toggle source

See primercss.io/avatars/#small-avatars

# File lib/jekyll-avatar.rb, line 99
def classes
  size < 48 ? "avatar avatar-small" : "avatar"
end
host() click to toggle source
# File lib/jekyll-avatar.rb, line 74
def host
  if ENV["PAGES_AVATARS_URL"].is_a?(String) && !ENV["PAGES_AVATARS_URL"].empty?
    ENV["PAGES_AVATARS_URL"]
  else
    "https://avatars#{server_number}.githubusercontent.com"
  end
end
lazy_load?() click to toggle source
# File lib/jekyll-avatar.rb, line 48
def lazy_load?
  @text.include?("lazy=true")
end
parsed_host() click to toggle source
# File lib/jekyll-avatar.rb, line 82
def parsed_host
  @parsed_host ||= {}
  @parsed_host[host] ||= Addressable::URI.parse(host)
end
path(scale = 1) click to toggle source
# File lib/jekyll-avatar.rb, line 66
def path(scale = 1)
  "#{username}?v=#{API_VERSION}&s=#{size * scale}"
end
server_number() click to toggle source
# File lib/jekyll-avatar.rb, line 70
def server_number
  Zlib.crc32(path) % SERVERS
end
size() click to toggle source
# File lib/jekyll-avatar.rb, line 61
def size
  matches = @text.match(%r!\bsize=(\d+)\b!i)
  matches ? matches[1].to_i : DEFAULT_SIZE
end
srcset() click to toggle source
# File lib/jekyll-avatar.rb, line 94
def srcset
  (1..4).map { |scale| "#{url(scale)} #{scale}x" }.join(", ")
end
url(scale = 1) click to toggle source
# File lib/jekyll-avatar.rb, line 87
def url(scale = 1)
  uri = parsed_host
  uri.path << "/" unless uri.path.end_with?("/")
  uri = uri.join path(scale)
  uri.to_s
end
username() click to toggle source
# File lib/jekyll-avatar.rb, line 52
def username
  matches = @text.match(%r!\buser=([\w\.]+)\b!)
  if matches
    lookup_variable(@context, matches[1])
  else
    @text.split(" ").first.sub("@", "")
  end
end