Gallery shortcode for Hugo. Fork of Li-Wen Yip's Easy Gallery
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

gallery.html 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <!--
  2. SPDX-FileCopyrightText: 2019 Li-Wen Yip <liwen.yip@gmail.com>
  3. SPDX-FileCopyrightText: 2019 Max Mehl <mail@mehl.mx>
  4. SPDX-License-Identifier: MIT
  5. Put this file in /layouts/shortcodes/gallery.html
  6. Documentation and licence at https://github.com/liwenyip/hugo-easy-gallery/
  7. -->
  8. <!-- count how many times we've called this shortcode; load the css if it's the first time -->
  9. <link rel="stylesheet" href="/css/snap-gallery.css" />
  10. {{- $.Page.Scratch.Add "figurecount" 1 }}
  11. {{ $baseURL := .Site.BaseURL }}
  12. <div class="gallery caption-position-{{ with .Get "caption-position" | default "bottom" }}{{.}}{{end}} caption-effect-{{ with .Get "caption-effect" | default "slide" }}{{.}}{{end}} hover-effect-{{ with .Get "hover-effect" | default "zoom" }}{{.}}{{end}} {{ if ne (.Get "hover-transition") "none" }}hover-transition{{end}}" itemscope itemtype="http://schema.org/ImageGallery">
  13. {{- with (.Get "dir") -}}
  14. <!-- If a directory was specified, generate figures for all of the images in the directory -->
  15. {{- $files := readDir (print "/static/" .) }}
  16. {{- range $files -}}
  17. <!-- skip files that aren't images, or that inlcude the thumb suffix in their name -->
  18. {{- $thumbext := $.Get "thumb" | default "-thumb" }}
  19. {{- $isthumb := .Name | findRE ($thumbext | printf "%s\\.") }}<!-- is the current file a thumbnail image? -->
  20. {{- $isimg := lower .Name | findRE "\\.(gif|jpg|jpeg|tiff|png|bmp)" }}<!-- is the current file an image? -->
  21. {{- if and $isimg (not $isthumb) }}
  22. {{- $caption := .Name | replaceRE "\\..*" "" | humanize }}<!-- humanized filename without extension -->
  23. {{- $linkURL := print $baseURL ($.Get "dir") "" .Name | absURL }}<!-- absolute URL to hi-res image -->
  24. {{- $thumb := .Name | replaceRE "(\\.)" ($thumbext | printf "%s.") }}<!-- filename of thumbnail image -->
  25. {{- $thumbexists := where $files "Name" $thumb }}<!-- does a thumbnail image exist? -->
  26. {{- $thumbURL := print $baseURL ($.Get "dir") "/" $thumb | absURL }}<!-- absolute URL to thumbnail image -->
  27. <div class="box">
  28. <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject">
  29. <div class="img" style="background-image: url('{{ if $thumbexists }}{{ $thumbURL }}{{ else }}{{ $linkURL }}{{ end }}');" >
  30. <img itemprop="thumbnail" src="{{ if $thumbexists }}{{ $thumbURL }}{{ else }}{{ $linkURL }}{{ end }}" alt="{{ $caption }}" /><!-- <img> hidden if in .gallery -->
  31. </div>
  32. <figcaption>
  33. <p>{{ $caption }}</p>
  34. </figcaption>
  35. <a href="{{ $linkURL }}" itemprop="contentUrl" class="hiddenlink" target="_blank"></a><!-- put <a> last so it is stacked on top -->
  36. </figure>
  37. </div>
  38. {{- end }}
  39. {{- end }}
  40. {{- else -}}
  41. <!-- If no directory was specified, include any figure shortcodes called within the gallery -->
  42. {{ .Inner }}
  43. {{- end }}
  44. </div>