gallery.html 2.7 KB

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