diff options
author | Derek Severin <severinderek@gmail.com> | 2019-02-10 09:41:12 +0300 |
---|---|---|
committer | Derek Severin <severinderek@gmail.com> | 2019-02-10 09:41:12 +0300 |
commit | 09da20da8cad89350be6179c284c17d8a1f84d6f (patch) | |
tree | 3d0b7d211d2152d1244b49c536ab69e62238f33a | |
parent | 3e61c8a43910d27b1cfa511158a73d634f824d9f (diff) |
Image gallery: changed 'gallery' shortcode to partial
-rw-r--r-- | layouts/_default/single.html | 11 | ||||
-rw-r--r-- | layouts/partials/gallery.html | 84 | ||||
-rw-r--r-- | layouts/partials/load-photoswipe.html (renamed from layouts/shortcodes/load-photoswipe.html) | 4 | ||||
-rw-r--r-- | layouts/shortcodes/figure.html | 29 | ||||
-rw-r--r-- | layouts/shortcodes/gallery.html | 53 |
5 files changed, 97 insertions, 84 deletions
diff --git a/layouts/_default/single.html b/layouts/_default/single.html index d7172ea..566fdf7 100644 --- a/layouts/_default/single.html +++ b/layouts/_default/single.html @@ -42,6 +42,17 @@ </ul> {{ end }} */}} +{{/* +- "dir" .Params.images_directory + => if not defined: "images" +- "static" .Params.images_static + => if not defined: N/A +- "thumb" .Params.images_thumb_suffix + => if not defined: N/A +... +*/}} +{{ partial "gallery.html" (dict "page" . "dir" "images") }} +{{ partial "load-photoswipe.html" (dict "page" .) }} {{ end }} {{ define "lists" }} diff --git a/layouts/partials/gallery.html b/layouts/partials/gallery.html new file mode 100644 index 0000000..72e11d2 --- /dev/null +++ b/layouts/partials/gallery.html @@ -0,0 +1,84 @@ +<!-- +Input Parameters: +- Mandatory: + - "page": Page + - $.Page.Scratch => $.page.Scratch + - .Site.BaseURL => $.page.Site.BaseURL + - $.Page.Dir => $.page.Dir +- Optional: + - "dir": String (directory) + - .Get "dir" => $.dir (if defined, else using ".Inner") + - "static": Boolean + - $.Get "static" => $.static (if defined, else "false" by default) + - "thumb": String (suffix) + - $.Get "thumb" => $.thumb (if defined, else "-thumb" by default) + +TODO: +- .Get "caption-position" + default "bottom" +- .Get "caption-effect" + default "slide" +- .Get "hover-effect" + default "zoom" +- .Get "hover-transition" + ++captions... +(& copyrights, etc.) +--> + +<!-- +Put this file in /layouts/shortcodes/gallery.html +Documentation and licence at https://github.com/liwenyip/hugo-easy-gallery/ +--> +<!-- count how many times we've called this shortcode; load the css if it's the first time --> +{{- if not (.page.Scratch.Get "figurecount") }}<link rel="stylesheet" href="/css/hugo-easy-gallery.css" />{{ end }} +{{- .page.Scratch.Add "figurecount" 1 }} +{{ $baseURL := .page.Site.BaseURL }} +<div class="gallery caption-position-{{ with .caption_position | default "bottom" }}{{.}}{{end}} caption-effect-{{ with .caption_effect | default "slide" }}{{.}}{{end}} hover-effect-{{ with .hover_effect | default "zoom" }}{{.}}{{end}} {{ if ne (.hover_transition) "none" }}hover-transition{{end}}" itemscope itemtype="http://schema.org/ImageGallery"> + {{- with .dir -}} + <!-- If a directory was specified, generate figures for all of the images in the directory --> + {{- if eq $.static "true" -}} + {{ $.page.Scratch.Set "dirURL" (print .) }} + {{ $.page.Scratch.Set "dirFiles" (print "/static/" .) }} + {{- else -}} + {{- $tmpDir := print (replace $.page.Dir "\\" "/") "/" . }} + {{ $.page.Scratch.Set "dirURL" $tmpDir }} + {{ $.page.Scratch.Set "dirFiles" (print "/content/" $tmpDir "/") }} + {{- end -}} + {{- $dir := $.page.Scratch.Get "dirURL" }} + {{ if (fileExists $dir) -}} + {{- $files := readDir ($.page.Scratch.Get "dirFiles" ) }} + + {{- range $files -}} + <!-- skip files that aren't images, or that inlcude the thumb suffix in their name --> + {{- $thumbext := $.thumb | default "-thumb" }} + {{- $isthumb := .Name | findRE ($thumbext | printf "%s\\.") }}<!-- is the current file a thumbnail image? --> + {{- $isimg := lower .Name | findRE "\\.(gif|jpg|jpeg|tiff|png|bmp)" }}<!-- is the current file an image? --> + {{- if and $isimg (not $isthumb) }} + {{- $caption := .Name | replaceRE "\\..*" "" | humanize }}<!-- humanized filename without extension --> + {{- $linkURL := print $baseURL $dir "/" .Name | absURL }}<!-- absolute URL to hi-res image --> + {{- $thumb := .Name | replaceRE "(\\.)" ($thumbext | printf "%s.") }}<!-- filename of thumbnail image --> + {{- $thumbexists := where $files "Name" $thumb }}<!-- does a thumbnail image exist? --> + {{- $thumbURL := print $baseURL $dir "/" $thumb | absURL }}<!-- absolute URL to thumbnail image --> + <div class="box"> + <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject"> + <div class="img" style="background-image: url('{{ if $thumbexists }}{{ $thumbURL }}{{ else }}{{ $linkURL }}{{ end }}');" > + <img itemprop="thumbnail" src="{{ if $thumbexists }}{{ $thumbURL }}{{ else }}{{ $linkURL }}{{ end }}" alt="{{ $caption }}" /><!-- <img> hidden if in .gallery --> + </div> + <figcaption> + <p>{{ $caption }}</p> + </figcaption> + <a href="{{ $linkURL }}" itemprop="contentUrl"></a><!-- put <a> last so it is stacked on top --> + </figure> + </div> + {{- end }} + {{- end }} + {{- end }} + {{ $.page.Scratch.Delete "dirURL" }} + {{ $.page.Scratch.Delete "dirFiles" }} + + {{- else -}} + <!-- If no directory was specified, include any figure shortcodes called within the gallery --> + {{ .Inner }} + {{- end }} +</div> diff --git a/layouts/shortcodes/load-photoswipe.html b/layouts/partials/load-photoswipe.html index b4dc61c..d9921b1 100644 --- a/layouts/shortcodes/load-photoswipe.html +++ b/layouts/partials/load-photoswipe.html @@ -4,8 +4,8 @@ Documentation and licence at https://github.com/liwenyip/hugo-easy-gallery/ --> <!-- prevent this shortcode from being loaded more than once per page --> -{{ if not ($.Page.Scratch.Get "photoswipeloaded") }} - {{ $.Page.Scratch.Set "photoswipeloaded" 1 }} +{{ if not (.page.Scratch.Get "photoswipeloaded") }} + {{ .page.Scratch.Set "photoswipeloaded" 1 }} <!-- *** jQuery must be loaded before load-photoswipe.js *** diff --git a/layouts/shortcodes/figure.html b/layouts/shortcodes/figure.html deleted file mode 100644 index 4c6ff1f..0000000 --- a/layouts/shortcodes/figure.html +++ /dev/null @@ -1,29 +0,0 @@ -<!-- -Put this file in /layouts/shortcodes/figure.html -NB this overrides Hugo's built-in "figure" shortcode but is backwards compatible -Documentation and licence at https://github.com/liwenyip/hugo-easy-gallery/ ---> -<!-- count how many times we've called this shortcode; load the css if it's the first time --> -{{- if not ($.Page.Scratch.Get "figurecount") }}<link rel="stylesheet" href="/css/hugo-easy-gallery.css" />{{ end }} -{{- $.Page.Scratch.Add "figurecount" 1 -}} -<!-- use either src or link-thumb for thumbnail image --> -{{- $thumb := .Get "src" | default (printf "%s." (.Get "thumb") | replace (.Get "link") ".") }} -<div class="box{{ with .Get "caption-position" }} fancy-figure caption-position-{{.}}{{end}}{{ with .Get "caption-effect" }} caption-effect-{{.}}{{end}}" {{ with .Get "width" }}style="max-width:{{.}}"{{end}}> - <figure {{ with .Get "class" }}class="{{.}}"{{ end }} itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject"> - <div class="img"{{ if .Parent }} style="background-image: url('{{ print .Site.BaseURL $thumb }}');"{{ end }}{{ with .Get "size" }} data-size="{{.}}"{{ end }}> - <img itemprop="thumbnail" src="{{ $thumb }}" {{ with .Get "alt" | default (.Get "caption") }}alt="{{.}}"{{ end }}/><!-- <img> hidden if in .gallery --> - </div> - {{ with .Get "link" | default (.Get "src") }}<a href="{{.}}" itemprop="contentUrl"></a>{{ end }} - {{- if or (or (.Get "title") (.Get "caption")) (.Get "attr")}} - <figcaption> - {{- with .Get "title" }}<h4>{{.}}</h4>{{ end }} - {{- if or (.Get "caption") (.Get "attr")}} - <p> - {{- .Get "caption" -}} - {{- with .Get "attrlink"}}<a href="{{.}}">{{ .Get "attr" }}</a>{{ else }}{{ .Get "attr"}}{{ end -}} - </p> - {{- end }} - </figcaption> - {{- end }} - </figure> -</div> diff --git a/layouts/shortcodes/gallery.html b/layouts/shortcodes/gallery.html deleted file mode 100644 index ab8671b..0000000 --- a/layouts/shortcodes/gallery.html +++ /dev/null @@ -1,53 +0,0 @@ -<!-- -Put this file in /layouts/shortcodes/gallery.html -Documentation and licence at https://github.com/liwenyip/hugo-easy-gallery/ ---> -<!-- count how many times we've called this shortcode; load the css if it's the first time --> -{{- if not ($.Page.Scratch.Get "figurecount") }}<link rel="stylesheet" href="/css/hugo-easy-gallery.css" />{{ end }} -{{- $.Page.Scratch.Add "figurecount" 1 }} -{{ $baseURL := .Site.BaseURL }} -<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"> - {{- with (.Get "dir") -}} - <!-- If a directory was specified, generate figures for all of the images in the directory --> - {{- if eq ($.Get "static") "true" -}} - {{ $.Page.Scratch.Set "dirURL" (print .) }} - {{ $.Page.Scratch.Set "dirFiles" (print "/static/" .) }} - {{- else -}} - {{- $dir := print (replace $.Page.Dir "\\" "/") "/" . }} - {{ $.Page.Scratch.Set "dirURL" $dir }} - {{ $.Page.Scratch.Set "dirFiles" (print "/content/" $dir "/") }} - {{- end -}} - {{- $dir := $.Page.Scratch.Get "dirURL" }} - {{- $files := readDir ($.Page.Scratch.Get "dirFiles" ) }} - {{ $.Page.Scratch.Delete "dirURL" }} - {{ $.Page.Scratch.Delete "dirFiles" }} - - {{- range $files -}} - <!-- skip files that aren't images, or that inlcude the thumb suffix in their name --> - {{- $thumbext := $.Get "thumb" | default "-thumb" }} - {{- $isthumb := .Name | findRE ($thumbext | printf "%s\\.") }}<!-- is the current file a thumbnail image? --> - {{- $isimg := lower .Name | findRE "\\.(gif|jpg|jpeg|tiff|png|bmp)" }}<!-- is the current file an image? --> - {{- if and $isimg (not $isthumb) }} - {{- $caption := .Name | replaceRE "\\..*" "" | humanize }}<!-- humanized filename without extension --> - {{- $linkURL := print $baseURL $dir "/" .Name | absURL }}<!-- absolute URL to hi-res image --> - {{- $thumb := .Name | replaceRE "(\\.)" ($thumbext | printf "%s.") }}<!-- filename of thumbnail image --> - {{- $thumbexists := where $files "Name" $thumb }}<!-- does a thumbnail image exist? --> - {{- $thumbURL := print $baseURL $dir "/" $thumb | absURL }}<!-- absolute URL to thumbnail image --> - <div class="box"> - <figure itemprop="associatedMedia" itemscope itemtype="http://schema.org/ImageObject"> - <div class="img" style="background-image: url('{{ if $thumbexists }}{{ $thumbURL }}{{ else }}{{ $linkURL }}{{ end }}');" > - <img itemprop="thumbnail" src="{{ if $thumbexists }}{{ $thumbURL }}{{ else }}{{ $linkURL }}{{ end }}" alt="{{ $caption }}" /><!-- <img> hidden if in .gallery --> - </div> - <figcaption> - <p>{{ $caption }}</p> - </figcaption> - <a href="{{ $linkURL }}" itemprop="contentUrl"></a><!-- put <a> last so it is stacked on top --> - </figure> - </div> - {{- end }} - {{- end }} - {{- else -}} - <!-- If no directory was specified, include any figure shortcodes called within the gallery --> - {{ .Inner }} - {{- end }} -</div> |