automatically detect if source is a folder

This commit is contained in:
2024-02-05 14:29:37 +01:00
parent 0a778ee8e9
commit 3dde053bca
2 changed files with 27 additions and 17 deletions

View File

@@ -13,20 +13,33 @@
{{/* Initialise index of this gallery */}}
<script>imageIndex[{{ $galno }}] = 1;</script>
{{/* Initialise variables holding image paths */}}
{{/* Initialise variables holding image paths and extension */}}
{{ $imgs_collect := slice }}
{{ $imgs := slice }}
{{ $img_exts := slice ".jpg" ".jpeg" ".png" ".gif" ".webp" ".tiff" }}
{{/* Get/sanitise image paths */}}
{{ if .Params.isdir }}
{{/* Get images from folder, put into map */}}
{{ $imgdir := print "/static/" .Params.src }}
{{- range readDir $imgdir -}}
{{ $imgs = $imgs | append (print $.Params.src "/" .Name ) }}
{{/* Get images from src Param, separated by comma */}}
{{ range (split .Params.src ",") }}
{{ $img := (trim . " ") }}
{{ $img_static := print "/static/" $img }}
{{/* Only proceed when path exists */}}
{{ if os.FileExists $img_static }}
{{/* If current item is a directory, range each of them, and add with full path to the slice */}}
{{ if (os.Stat $img_static).IsDir }}
{{- range readDir $img_static -}}
{{ $imgs_collect = $imgs_collect | append (path.Join $img .Name ) }}
{{ end }}
{{/* If a single file, just add it to the slice */}}
{{ else }}
{{ $imgs_collect = $imgs_collect | append $img }}
{{ end }}
{{ end }}
{{ else }}
{{/* Get images from src Param, separated by comma */}}
{{ range (split .Params.src ",") }}
{{ $imgs = $imgs | append (trim . " ") }}
{{ end }}
{{/* Filter collected files by whether they are an image. Throw others out */}}
{{ range $imgs_collect }}
{{ if in $img_exts (lower (path.Ext .)) }}
{{ $imgs = $imgs | append . }}
{{ end }}
{{ end }}