Class: Jekyll::Flickr::FlickrTag

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

Constant Summary collapse

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



83
84
85
# File 'lib/jekyll-flickr/flickr_tag.rb', line 83

def cache
  @@cache ||= Jekyll::Cache::FileStore.new 'flickr'
end

#configObject



87
88
89
# File 'lib/jekyll-flickr/flickr_tag.rb', line 87

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

#flickrObject



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

def flickr
  @@flickr ||= begin
    FlickRaw.api_key = ENV['FLICKR_API_KEY'] || config['api_key']
    FlickRaw.shared_secret = ENV['FLICKR_API_SECRET'] || config['api_secret']

    @@flickr = FlickRaw::Flickr.new
  end
end

#render(context) ⇒ Object



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

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.fetch photo_id, expires_in: -1 do # disable expiry in production and development environment
    {
      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