Sha256: 8850c23fdb14cbbbe31ea59a0efd7bc3660c522a4321624627ba90ba750778ef

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Jekyll
  class Avatar < Liquid::Tag
    SERVERS = 4
    DEFAULT_SIZE = 40
    VERSION = 3

    def initialize(_tag_name, text, _tokens)
      super
      @text = text
    end

    def render(context)
      @context = context
      @text    = Liquid::Template.parse(@text).render(@context)
      tag = '<img '

      # See http://primercss.io/avatars/#small-avatars
      tag << if size < 48
               'class="avatar avatar-small" '
             else
               'class="avatar" '
             end

      tag << "src=\"#{url}\" alt=\"#{username}\" "
      tag << "width=\"#{size}\" height=\"#{size}\" />"
    end

    private

    def username
      matches = @text.match(/\s+user=(\w+)\s+/)
      if matches
        @context[matches[1]]
      else
        @text.split(' ').first.sub('@', '')
      end
    end

    def size
      matches = @text.match(/\bsize=(\d+)\b/i)
      matches ? matches[1].to_i : DEFAULT_SIZE
    end

    def path
      "#{username}?v=#{VERSION}&s=#{size}"
    end

    def server_number
      Zlib.crc32(path) % SERVERS
    end

    def host
      "avatars#{server_number}.githubusercontent.com"
    end

    def url
      "https://#{host}/#{path}"
    end
  end
end

Liquid::Template.register_tag('avatar', Jekyll::Avatar)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jekyll-avatar-0.2.2 lib/jekyll-avatar.rb