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'}
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
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 = "© 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
|