Class: Jekyll::Flickr::FlickrTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/jekyll-flickr/flickr_tag.rb

Constant Summary collapse

CACHE_NAME =
"Jekyll::Flickr::FlickrTag"
DEFAULT_CONFIG =

selection of sizes from those offered by Flickr API

{
  'widths' => [320, 640, 800, 1024, 1600],
  'width_legacy' => 1024,
  'figcaption' => true,
  'license' => true,
  'caption' => true,
  'width_viewport' => '100vw'
}

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, text, tokens) ⇒ FlickrTag

Returns a new instance of FlickrTag.



20
21
22
23
24
# File 'lib/jekyll-flickr/flickr_tag.rb', line 20

def initialize(tag_name, text, tokens)
  super

  @text = text.strip
end

Instance Method Details

#cacheObject



33
34
35
# File 'lib/jekyll-flickr/flickr_tag.rb', line 33

def cache
  @@cache ||= Jekyll::Cache.new(CACHE_NAME)
end

#configObject



44
45
46
# File 'lib/jekyll-flickr/flickr_tag.rb', line 44

def config
  @@config ||= DEFAULT_CONFIG.merge(Jekyll.configuration({})['flickr'])
end

#flickrObject



26
27
28
29
30
31
# File 'lib/jekyll-flickr/flickr_tag.rb', line 26

def flickr
  @@flickr ||= begin
    # by default, Flickr uses ENV FLICKR_API_KEY and FLICKR_SHARED_SECRET, support here legacy name FLICKR_API_SECRET, too
    @@flickr = ::Flickr.new(ENV['FLICKR_API_KEY'] || config['api_key'], ENV['FLICKR_SHARED_SECRET'] || ENV['FLICKR_API_SECRET'] || config['api_secret'])
  end
end

#licensesObject



37
38
39
40
41
42
# File 'lib/jekyll-flickr/flickr_tag.rb', line 37

def licenses
  @@licenses ||= begin
    # https://www.flickr.com/services/api/flickr.photos.licenses.getInfo.html
    @@licenses = flickr.photos.licenses.getInfo.sort_by{ |license| license.id.to_i }
  end
end

#render(context) ⇒ Object



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
# File 'lib/jekyll-flickr/flickr_tag.rb', line 48

def render(context)

  match = /(?<photo_id>\d+)(\s(\"(?<caption>[^"]*)\"))?(?<attr>.*)/.match @text

  photo_id = match.named_captures['photo_id']
  photo_caption = match.named_captures['caption']
  photo_attr = match.named_captures['attr']

  photo_data = cache.getset photo_id do
    {
      info:  flickr.photos.getInfo(photo_id: photo_id),
      sizes: flickr.photos.getSizes(photo_id: photo_id)
    }
  end

  widths = config['widths'] + [config['width_legacy']]
  photo_sizes = photo_data[:sizes].select{ |photo| widths.include?(photo['width'].to_i) }
  photo_legacy = photo_data[:sizes].find{ |photo| photo['width'].to_i == config['width_legacy']} || photo_data[:sizes].find{ |photo| photo['label'] == 'Original'}

  # if these sizes are not found, pick the one closes to width_legacy
  unless photo_legacy
    candidate = nil
    smallest_delta = nil
    photo_data[:sizes].each do |photo|
      current_delta = (photo['width'].to_i - config['width_legacy']).abs
      if !smallest_delta || (current_delta < smallest_delta) 
        candidate = photo
        smallest_delta = current_delta
      end
    end
    photo_legacy = candidate
  end

  # if no photo is found return
  unless photo_legacy
    return ""
  end

  srcset = photo_sizes.map{|photo| "#{photo['source']} #{photo['width']}w"}.join(", ")

  sizes = unless photo_attr.include?('sizes=') then
    context.registers[:site].config['flickr']['width_viewport']
  else
    ""
  end

  if photo_caption and not photo_attr.include?('alt=') then
    photo_attr += " alt=\"#{photo_caption}\""
  end

  img_tag = "<img class=\"flickr\" src=\"#{photo_legacy.source}\" srcset=\"#{srcset}\" sizes=\"#{sizes}\" #{photo_attr}>"

  return compute_html_tag(img_tag) if not config['figcaption']

  photo_license = if config['license'] then
    license = licenses[photo_data[:info].license.to_i]

    owner_link = "&copy; Flickr/<a href=\"#{photo_data[:info]['urls'].first['_content']}\">#{photo_data[:info]['owner']['username']}</a>"

    license_link = if license[:url]
      "<a href=\"#{license[:url]}\">#{license[:name]}</a>"
    else
      license[:name]
    end
    "<div class=\"license\">#{owner_link} #{license_link}</div>"
  else
    ""
  end

  photo_caption = if config['caption'] and photo_caption then
    "<div class=\"caption\">#{photo_caption}</div>"
  else
    ""
  end

  return compute_html_tag(img_tag) if photo_license.empty? and photo_caption.empty?

  return <<-HTML
<figure class="flickr">
  #{img_tag}
  <figcaption>#{photo_caption}#{photo_license}</figcaption>
</figure>
  HTML
end