Skip to content

Instantly share code, notes, and snippets.

@codemartial
Last active April 13, 2023 09:59
Show Gist options
  • Save codemartial/42dc8d0169230e2f56dab02494346a67 to your computer and use it in GitHub Desktop.
Save codemartial/42dc8d0169230e2f56dab02494346a67 to your computer and use it in GitHub Desktop.
Awesomest Hugo shortcode for Flickr photos. Responsive images, EXIF data, custom titles πŸ‘
{{/*
Flickr shortcode for Hugo
Usage: {{< flickr photo_url="https://www.flickr.com/photos/username/1234567890" title="Photo title" >}}
Parameters:
photo_url: URL of the photo on Flickr
title: Title of the photo
caption: Caption of the photo (disables EXIF info)
href: URL of the link (overrides photo_url)
Output:
<figure>
<a href="https://www.flickr.com/photos/username/1234567890" target="_blank" rel="noopener">
<p>Photo title</p>
<img src="..." srcset="..." alt="Photo title" style="aspect-ratio: 1.78;" loading="lazy">
<figcaption>Canon EOS 5D Mark II on EF 24-105mm f/4L IS USM @ 105mm, Ζ’/4, ISO100, 1/250s</figcaption>
</a>
</figure>
*/}}
{{/* Configs */}}
{{ $flickr_api_key := os.Getenv "HUGO_FLICKR_API_KEY" }}
{{/* Defining shortcode parameters */}}
{{ $photo_url := .Get "url" }}
{{ $title := .Get "title" }}
{{ $caption := .Get "caption" }}
{{ $href := .Get "href" }}
{{ $photo_id := "" }}
{{/* Regular expression pattern for Flickr photo URLs */}}
{{ $photo_url_pattern := "^https://www.flickr.com/photos/[0-9a-zA-Z@_]+/[0-9]+/?$" }}
{{/* Check if the URL matches the Flickr photo URL pattern */}}
{{ if gt (len (findRE $photo_url_pattern $photo_url)) 0 }}
{{/* Extract the photo ID from the URL */}}
{{ $photo_id = index (split $photo_url "/") 5 }}
{{ end }}
{{ if $photo_id }}
{{ if not $title }}
{{/* Flickr API URL for photo metadata */}}
{{ $api_url := printf "https://api.flickr.com/services/rest/?method=flickr.photos.getInfo&api_key=%s&photo_id=%s&format=json&nojsoncallback=1" $flickr_api_key $photo_id }}
{{ with (getJSON $api_url) }}
{{/* Extracting title from photo metadata */}}
{{ $title = .photo.title._content }}
{{ end }}
{{ end }}
{{ if not $caption }}
{{/* Flickr API URL for photo EXIF data */}}
{{ $exif_url := printf "https://api.flickr.com/services/rest/?method=flickr.photos.getExif&api_key=%s&photo_id=%s&format=json&nojsoncallback=1" $flickr_api_key $photo_id }}
{{ with (getJSON $exif_url) }}
{{ $camera := replace .photo.camera "_2" "ii" }}
{{ $camera = partial "sanitizeCam" $camera }}
{{ $lens := (index (where .photo.exif "tag" "==" "LensModel") 0).raw._content }}
{{ if not $lens }}
{{ $lens = (index (where .photo.exif "tag" "==" "Lens") 0).raw._content }}
{{ end }}
{{ if not $lens }}
{{ $lens = (index (where .photo.exif "tag" "==" "LensInfo") 0).raw._content }}
{{ end }}
{{ if not $lens }}
{{ $lens = (index (where .photo.exif "tag" "==" "LensID") 0).raw._content }}
{{ end }}
{{ $lens = partial "sanitizeCam" $lens }}
{{ if $camera }}
{{ $gear := $camera }}
{{ if and ($lens) (ne (lower $lens) (lower $camera)) }}
{{ $gear = printf "%s on %s" $lens $camera }}
{{ end }}
{{ $caption = printf "%s @" $gear }}
{{ end }}
{{ with (index (where .photo.exif "tag" "==" "FocalLength") 0).raw._content }}
{{ $caption = printf "%s %s," $caption . }}
{{ end }}
{{ with (index (where .photo.exif "tag" "==" "FNumber") 0).raw._content }}
{{ $caption = printf "%s Ζ’/%s," $caption . }}
{{ end }}
{{ with (index (where .photo.exif "tag" "==" "ISO") 0).raw._content }}
{{ $caption = printf "%s ISO%s," $caption . }}
{{ end }}
{{ with (index (where .photo.exif "tag" "==" "ExposureTime") 0).raw._content }}
{{ $caption = printf "%s %ss" $caption . }}
{{ end }}
{{ end }}
{{ end }}
{{ $srcset := "" }}
{{ $sizes := "" }}
{{ $aspect := 1.0 }}
{{ $src := "" }}
{{/* Fetching photo sizes using Flickr API */}}
{{ $api_url := printf "https://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=%s&photo_id=%s&format=json&nojsoncallback=1" $flickr_api_key $photo_id }}
{{ with (getJSON $api_url) }}
{{/* Creating srcset and sizes attributes */}}
{{ $min_width := 0.0 }}
{{ range .sizes.size }}
{{ $srcset = printf "%s%s %.0fw," $srcset .source .width }}
{{ $sizes = printf "%s ((min-width: %.0fpx) and (max-width: %.0fpx) %.0fpx," $sizes $min_width .width .width }}
{{ $aspect = div .width .height }}
{{ $min_width = .width }}
{{ $src = .source }}
{{ end }}
{{ end }}
{{/* Creating HTML structure */}}
<figure>
<a href="{{ $photo_url }}" target="_blank" rel="noopener">
<p>{{ $title }}</p>
<img src="{{ $src }}" srcset="{{ $srcset }}" sizes="{{ $sizes }}" alt="{{ $title }}" style="aspect-ratio: {{ $aspect }};" loading="lazy">
<figcaption>{{ $caption }}</figcaption>
</a>
</figure>
{{ end }}
{{/*
Recognises long allcaps sequences and converts them to lowercase.
Usage:
{{$txt := partial "sanitizeCam" "text" >}}
*/}}
{{ $output := "" }}
{{ $input := . }}
{{ $words := split $input " " }}
{{ $uppercase_pattern := "([A-Z]){4,}" }}
{{ range $words }}
{{ if eq . "CORPORATION" }}
{{ continue }}
{{ end }}
{{ $match := findRE $uppercase_pattern . }}
{{ if and (ge (len .) 4) (ge (len $match) 1) }}
{{ $output = printf "%s%s " $output (title (lower .)) }}
{{ else }}
{{ $output = printf "%s%s " $output . }}
{{ end }}
{{ end }}
{{ return trim $output " " }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment