Skip to content

Instantly share code, notes, and snippets.

@richardp2
Created April 7, 2014 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save richardp2/10020777 to your computer and use it in GitHub Desktop.
Save richardp2/10020777 to your computer and use it in GitHub Desktop.
Flickr Image Tag Plugin for Jekyll
# Modified Flickr Image Tag Plugin
#
# The original plugin was created by Daniel Reszka and is available here:
# https://gist.github.com/danielres/3156265/
#
# The modifications include:
# - a link to the original, full size image as well as the Flickr Page for the image;
# - and the ability to specify an image class in the tag
# i.e. {% flickr_image 7614906062 alignright %}.
require 'flickraw'
module Jekyll
class FlickrImageTag < Liquid::Tag
def initialize(tag_name, markup, tokens)
super
params = Shellwords.shellwords markup
@id = params[0]
@classes = params[1] || "alignleft"
@size = params[2] || "q"
end
def render(context)
# hack to convert a variable into an actual flickr set id
if @id =~ /([\w]+\.[\w]+)/i
@id = Liquid::Template.parse('{{ '+@id+' }}').render context
end
flickrConfig = context.registers[:site].config["flickr"]
FlickRaw.api_key = flickrConfig['api_key']
FlickRaw.shared_secret = flickrConfig['shared_secret']
flickr.access_token = flickrConfig['access_token']
flickr.access_secret = flickrConfig['access_secret']
info = flickr.photos.getInfo(:photo_id => @id)
server = info['server']
farm = info['farm']
id = info['id']
secret = info['secret']
title = info['title']
description = info['description']
size = "_#{@size}" if @size
classes = "#{@classes}" if @classes
src = "http://farm#{farm}.static.flickr.com/#{server}/#{id}_#{secret}#{size}.jpg"
full = "http://farm#{farm}.static.flickr.com/#{server}/#{id}_#{secret}_b.jpg"
page_url = info['urls'][0]["_content"]
img_tag = "<img src='#{src}' alt=\"#{title}\" />"
link_tag = "<ul class='flickr image #{classes}'>"
link_tag += "<li>"
link_tag += "<a title=\"#{title}\" href='#{full}' class=\"image\">#{img_tag}</a>"
link_tag += "<a title='View on Flickr' href='#{page_url}' class='flickrlink'> </a>"
link_tag += "</li>"
link_tag += "</ul>"
end
end
end
Liquid::Template.register_tag('flickr_image', Jekyll::FlickrImageTag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment