class Jekyll::Flickr::FlickrTag

Constants

DEFAULT_CONFIG

selection of sizes from those offered by Flickr API

LICENSES

Flickr licenses from (added CC myself) www.flickr.com/services/api/flickr.photos.licenses.getInfo.html

Public Class Methods

new(tag_name, text, tokens) click to toggle source
Calls superclass method
# File lib/jekyll-flickr/flickr_tag.rb, line 67
def initialize(tag_name, text, tokens)
  super

  @text = text.strip
end

Public Instance Methods

cache() click to toggle source
# File lib/jekyll-flickr/flickr_tag.rb, line 80
def cache
  @@cache ||= Jekyll::Cache.new("flickr")
end
config() click to toggle source
# File lib/jekyll-flickr/flickr_tag.rb, line 84
def config
  @@config ||= DEFAULT_CONFIG.merge(Jekyll.configuration({})['flickr'])
end
flickr() click to toggle source
# File lib/jekyll-flickr/flickr_tag.rb, line 73
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) click to toggle source
# File lib/jekyll-flickr/flickr_tag.rb, line 88
      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'}

        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 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 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