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'
}
LICENSES =
[
  {
    name: "All Rights Reserved", url: ""
  },
  {
    name: "CC Attribution-NonCommercial-ShareAlike License",
    url: "https://creativecommons.org/licenses/by-nc-sa/2.0/"
  },
  {
    name: "CC Attribution-NonCommercial License",
    url: "https://creativecommons.org/licenses/by-nc/2.0/"
  },
  {
    name: "CC Attribution-NonCommercial-NoDerivs License",
    url: "https://creativecommons.org/licenses/by-nc-nd/2.0/"
  },
  {
    name: "CC Attribution License",
    url: "https://creativecommons.org/licenses/by/2.0/"
  },
  {
    name: "CC Attribution-ShareAlike License",
    url: "https://creativecommons.org/licenses/by-sa/2.0/"
  },
  {
    name: "CC Attribution-NoDerivs License",
    url: "https://creativecommons.org/licenses/by-nd/2.0/",
  },
  {
    name: "No known copyright restrictions",
    url: "https://www.flickr.com/commons/usage/"
  },
  {
    name: "United States Government Work",
    url: "http://www.usa.gov/copyright.shtml"
  },
  {
    name: "Public Domain Dedication (CC0)",
    url: "https://creativecommons.org/publicdomain/zero/1.0/"
  },
  {
    name: "Public Domain Mark",
    url: "https://creativecommons.org/publicdomain/mark/1.0/"
  }
]

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, text, tokens) ⇒ FlickrTag

Returns a new instance of FlickrTag.



68
69
70
71
72
# File 'lib/jekyll-flickr/flickr_tag.rb', line 68

def initialize(tag_name, text, tokens)
  super

  @text = text.strip
end

Instance Method Details

#cacheObject



81
82
83
# File 'lib/jekyll-flickr/flickr_tag.rb', line 81

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

#configObject



85
86
87
# File 'lib/jekyll-flickr/flickr_tag.rb', line 85

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

#flickrObject



74
75
76
77
78
79
# File 'lib/jekyll-flickr/flickr_tag.rb', line 74

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

#render(context) ⇒ Object



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

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