Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/chipzoller/hugo-clarity.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvan Boothe <git@rootwork.org>2022-04-25 02:07:11 +0300
committerGitHub <noreply@github.com>2022-04-25 02:07:11 +0300
commit2e321313f63118a0e90c6a6266b89bc9e11d961e (patch)
tree41ad3a5624a958e87f3969e29372eddc24860a54 /layouts
parent85845f5bfdaee8d97d25d4460e720b11a435dab0 (diff)
parent487c8615ce28fc30255e1e509a2b2f311fe16e63 (diff)
Merge pull request #278 from rootwork/remove-image-errors
Diffstat (limited to 'layouts')
-rw-r--r--layouts/_default/_markup/render-image.html140
-rw-r--r--layouts/partials/figure.html63
-rw-r--r--layouts/partials/image-error.html13
-rw-r--r--layouts/partials/image.html147
4 files changed, 140 insertions, 223 deletions
diff --git a/layouts/_default/_markup/render-image.html b/layouts/_default/_markup/render-image.html
index 418f071..1b5fc81 100644
--- a/layouts/_default/_markup/render-image.html
+++ b/layouts/_default/_markup/render-image.html
@@ -1,108 +1,44 @@
-{{- $file := .Destination | safeURL }}{{/* filename of image */}}
-{{- $fileWeb := "" -}}{{/* full path to image, relative to the web root */}}
-{{- $fileDisk := "" -}}{{/* full path to image, relative to the file root */}}
-{{- $alt := .Text -}}{{/* alt text */}}
-{{- $cap := .Title -}}{{/* caption */}}
+{{- $file := .Destination -}}
+{{- $alt := .Text -}}
+{{- $cap := .Title -}}
{{- $scratch := newScratch -}}
+{{- $scratch.Set "classes" "image_figure" -}}
-{{/* Determine if image is local */}}
+{{/* Determine if page bundles are set. */}}
+{{- $bundle := .Page.Site.Params.usePageBundles -}}
+{{- if eq .Page.Params.usePageBundles false -}}
+ {{- $bundle = false -}}
+{{- end -}}
+{{- if eq .Page.Params.usePageBundles true -}}
+ {{- $bundle = true -}}
+{{- end -}}
+
+{{/* Determine source of image and get attributes. */}}
+{{- $image := "" -}}
{{- if strings.HasPrefix $file "http" -}}
- {{- $scratch.Set "fileLoc" "external" -}}
+ {{- $scratch.Add "classes" " image_external" -}}
+ {{- $image = resources.GetRemote $file -}}
+ {{- $file = path.Join "images" $image -}}
+ {{- $image = $image.Content | resources.FromString $file -}}
{{- else -}}
- {{- $scratch.Set "fileLoc" "internal" -}}
+ {{- $scratch.Add "classes" " image_internal" -}}
+ {{ $file = (path.Clean $file) }}
+ {{- if eq $bundle true -}}
+ {{ $image = .Page.Resources.GetMatch $file }}
+ {{- if and (not $image) .Page.File -}}
+ {{ $file = path.Join .Page.File.Dir $file }}
+ {{ $image = resources.Get $file }}
+ {{- end -}}
+ {{- end -}}
{{- end -}}
-{{/* Provide list of config file location options */}}
-{{ $scratch.SetInMap "configFileLocations" "1" "config.toml" }}
-{{ $scratch.SetInMap "configFileLocations" "2" "config/_default/config.toml" }}
-{{ $scratch.SetInMap "configFileLocations" "3" "config.yaml" }}
-{{ $scratch.SetInMap "configFileLocations" "4" "config/_default/config.yaml" }}
-{{ $scratch.SetInMap "configFileLocations" "5" "config.json" }}
-{{ $scratch.SetInMap "configFileLocations" "6" "config/_default/config.json" }}
-
-{{/* For the first location that exists, set it as the config file location */}}
-{{ range $scratch.GetSortedMapValues "configFileLocations" }}
- {{ if fileExists . }}
- {{ $scratch.Set "configFile" . }}
- {{ end }}
-{{ end }}
-
-{{/* Retrieve the contentDir and staticDir settings from the config, and provide default values */}}
-{{ $scratch.Set "configFileMap" (os.ReadFile ($scratch.Get "configFile") | transform.Unmarshal) }}
-{{ if isset ($scratch.Get "configFileMap") "contentDir" }}
- {{ $scratch.Set "contentDir" (index ($scratch.Get "configFileMap") "contentDir") }}
-{{ else }}
- {{ $scratch.Set "contentDir" "content" }}
-{{ end }}
-{{ if isset ($scratch.Get "configFileMap") "staticDir" }}
- {{ $scratch.Set "staticDir" (index ($scratch.Get "configFileMap") "staticDir") }}
-{{ else }}
- {{ $scratch.Set "staticDir" "static" }}
-{{ end }}
-
-{{/* Check if page bundles are set. */}}
-{{- $bundle := .Page.Site.Params.usePageBundles }}
-{{ if eq .Page.Params.usePageBundles false }}
- {{- $bundle = false }}
-{{ end }}
-{{ if eq .Page.Params.usePageBundles true }}
- {{- $bundle = true }}
-{{ end }}
-
-{{- $fileWeb = absURL $file -}}
-{{ if eq $bundle true }}
- {{- $fileWeb = (path.Join $.Page.RelPermalink $file) -}}
- {{- $fileDisk = (path.Join ($scratch.Get "contentDir") (path.Join $.Page.File.Dir $file)) }}
-{{ else }}
- {{- $fileDisk = (path.Join ($scratch.Get "staticDir") $file) }}
-{{ end }}
-
-{{/* If image is external, or if image is internal and exists */}}
-{{- if or (
- (eq ($scratch.Get "fileLoc") "external"))
- (and
- (eq ($scratch.Get "fileLoc") "internal")
- (fileExists $fileDisk)
- )
--}}
- <figure>
- <picture>
-
- {{- if eq ($scratch.Get "fileLoc") "external" -}}
- <img class="image_figure image_external" loading="lazy" src="{{ $file }}" alt="{{ $alt }}" {{ with $cap}} title="{{ $cap }}" {{ end }} />
- {{- else -}}
-
- {{- $img := imageConfig $fileDisk -}}{{/* image element (passed through imageConfig) */}}
-
- {{ range $ext := (slice "avif" "webp" "jxl") }}
- {{ $dotext := print "." $ext }}
- {{ $diskPath := $fileDisk }}
- {{ $webPath := $fileWeb }}
- {{ $diskPath = replace $diskPath ".png" $dotext }}
- {{ $diskPath = replace $diskPath ".jpg" $dotext }}
- {{ $diskPath = replace $diskPath ".jpeg" $dotext }}
- {{ $diskPath = replace $diskPath ".gif" $dotext }}
- {{ $webPath = replace $webPath ".png" $dotext }}
- {{ $webPath = replace $webPath ".jpg" $dotext }}
- {{ $webPath = replace $webPath ".jpeg" $dotext }}
- {{ $webPath = replace $webPath ".gif" $dotext }}
- {{ if fileExists $diskPath }}
- <source srcset="{{ $webPath | safeURL }}" type="image/{{ $ext }}">
- {{ end }}
- {{ end }}
-
- <img class="image_figure image_internal" src="{{ $fileWeb }}" alt="{{ $alt }}" loading="lazy" decoding="async" width="{{ $img.Width }}" height="{{ $img.Height }}" {{ with $cap }} title="{{ $cap }}" {{ end }} />
-
- {{- end -}}
-
- {{/* Provide caption based on image title, if it is set. */}}
- {{ if ($cap) -}}
- <figcaption>{{ $cap | safeHTML }}</figcaption>
- {{- end }}
-
- </picture>
- </figure>
-{{/* Image is internal but does not exist, so render error. */}}
-{{- else }}
- {{- partial "image-error" (dict "fileWeb" $fileWeb "fileDisk" $fileDisk "bundle" $bundle) }}
-{{- end }}
+{{- partial "figure" (
+ dict
+ "file" $file
+ "image" $image
+ "dir" $.Page.File.Dir
+ "alt" $alt
+ "cap" $cap
+ "classes" ($scratch.Get "classes")
+ "bundle" $bundle
+) -}}
diff --git a/layouts/partials/figure.html b/layouts/partials/figure.html
new file mode 100644
index 0000000..163af57
--- /dev/null
+++ b/layouts/partials/figure.html
@@ -0,0 +1,63 @@
+{{/*
+ If Hugo has been able to access the image, it's loaded at .image, and we have
+ access to additional attributes.
+
+ If Hugo hasn't been able to access the image, it's loaded at .file, with only
+ its path available.
+
+ We set the Hugo variables to strings to allow for logical operations and
+ replacements.
+*/}}
+
+{{- $file := .file -}}
+{{- $image := .image -}}
+{{- $dir := .dir -}}
+{{- $alt := .alt -}}
+{{- $cap := .cap -}}
+{{- $classes := .classes -}}
+{{- $bundle := .bundle -}}
+
+<figure>
+ <picture>
+
+ {{/* Generate alternate image format tags. */}}
+ {{- with $file -}}
+ {{ $name := replace $file (path.Ext $file) "" }}
+ {{ $ext := slice "avif" "webp" "jxl" }}
+ {{- range $ext -}}
+ {{ $path := printf "%s" . | printf "%s%s" "." | printf "%s%s" $name | printf "%s" }}
+ {{- if eq $bundle true -}}
+ {{ $path = path.Join "/" $dir $path }}
+ {{- end -}}
+ {{- if fileExists $path -}}
+ <source srcset="{{ $path }}" type="image/{{ . }}">
+ {{- end -}}
+ {{- end -}}
+ {{- end -}}
+
+ {{/* Render image and attributes. */}}
+ <img
+ loading="lazy"
+ decoding="async"
+ alt="{{ htmlEscape $alt }}"
+ {{ with $image }}
+ class="{{ $classes }} image_processed"
+ width="{{ .Width }}"
+ height="{{ .Height }}"
+ src="{{ .RelPermalink }}"
+ {{ else }}
+ class="{{ $classes }} image_unprocessed"
+ src="{{ $file }}"
+ {{ end }}
+ {{ with $cap }}
+ title="{{ htmlEscape $cap }}"
+ {{ end }}
+ />
+
+ {{/* Provide caption based on image title, if it is set. */}}
+ {{- with $cap -}}
+ <figcaption>{{ $cap | safeHTML }}</figcaption>
+ {{- end -}}
+
+ </picture>
+</figure>
diff --git a/layouts/partials/image-error.html b/layouts/partials/image-error.html
deleted file mode 100644
index 65b0dcc..0000000
--- a/layouts/partials/image-error.html
+++ /dev/null
@@ -1,13 +0,0 @@
-<div class="notices warning image-warning">
- <div class="label">Image not found</div>
- <style>
- a.warning-link {
- color: inherit !important;
- font-weight: inherit !important;
- text-decoration: underline !important;
- border-bottom: none !important; }
- </style>
- <p>{{ T "img_web_path" }}: <a href='{{ .fileWeb }}' title='{{ T "img_web_path_desc" }}' target='_blank' class='warning-link'>{{ .fileWeb }}</a></p>
- <p>{{ T "img_disk_path" }}: {{ .fileDisk }}</p>
- <p>{{ T "img_pb_using" }} <a href='https://gohugo.io/content-management/page-bundles/' title='{{ T "img_pb_desc" }}' target='_blank' class='warning-link'>{{ T "img_pb" }}</a>: {{ if eq .bundle true }}true{{ else }}false{{ end }}</p>
-</div>
diff --git a/layouts/partials/image.html b/layouts/partials/image.html
index 069e49a..8aed4b6 100644
--- a/layouts/partials/image.html
+++ b/layouts/partials/image.html
@@ -1,119 +1,50 @@
-{{- $file := .file | safeURL }}{{/* filename of image */}}
-{{- $fileWeb := "" -}}{{/* full path to image, relative to the web root */}}
-{{- $fileDisk := "" -}}{{/* full path to image, relative to the file root */}}
-{{- $alt := .Text -}}{{/* alt text */}}
-{{- $cap := .Title -}}{{/* caption */}}
+{{- $file := .file -}}
+{{- $alt := .alt -}}
+{{- if not $alt -}}
+ {{- $alt = .Text -}}
+{{- end -}}
+{{- $cap := .Title -}}
{{- $scratch := newScratch -}}
-
-{{/* Set CSS classes */}}
{{- $scratch.Set "classes" "image_figure" -}}
-{{- if eq .type "thumbnail" -}}
- {{- $scratch.Add "classes" " image_thumbnail" -}}
+
+{{/* Add classes for image type */}}
+{{- $typeClass := "" -}}
+{{- with .type -}}
+ {{- $typeClass = printf "%s" . | printf "%s%s" " image_" | printf "%s" -}}
+{{- end -}}
+{{- $scratch.Add "classes" $typeClass -}}
+
+{{/* Determine if page bundles are set. */}}
+{{- $bundle := .Page.Site.Params.usePageBundles -}}
+{{- if eq .Page.Params.usePageBundles false -}}
+ {{- $bundle = false -}}
{{- end -}}
-{{- if eq .type "featured" -}}
- {{- $scratch.Add "classes" " image_featured" -}}
+{{- if eq .Page.Params.usePageBundles true -}}
+ {{- $bundle = true -}}
{{- end -}}
-{{/* Determine if image is local */}}
+{{/* Determine source of image and get attributes. */}}
+{{- $image := "" -}}
{{- if strings.HasPrefix $file "http" -}}
- {{- $scratch.Set "fileLoc" "external" -}}
{{- $scratch.Add "classes" " image_external" -}}
+ {{- $image = resources.GetRemote $file -}}
+ {{- $file = path.Join "images" $image -}}
+ {{- $image = $image.Content | resources.FromString $file -}}
{{- else -}}
- {{- $scratch.Set "fileLoc" "internal" -}}
{{- $scratch.Add "classes" " image_internal" -}}
+ {{ $file = (path.Join "/" $file) }}
+ {{- if eq $bundle true -}}
+ {{ $image = .Resources.GetMatch $file }}
+ {{- end -}}
{{- end -}}
-{{/* Provide list of config file location options */}}
-{{ $scratch.SetInMap "configFileLocations" "1" "config.toml" }}
-{{ $scratch.SetInMap "configFileLocations" "2" "config/_default/config.toml" }}
-{{ $scratch.SetInMap "configFileLocations" "3" "config.yaml" }}
-{{ $scratch.SetInMap "configFileLocations" "4" "config/_default/config.yaml" }}
-{{ $scratch.SetInMap "configFileLocations" "5" "config.json" }}
-{{ $scratch.SetInMap "configFileLocations" "6" "config/_default/config.json" }}
-
-{{/* For the first location that exists, set it as the config file location */}}
-{{ range $scratch.GetSortedMapValues "configFileLocations" }}
- {{ if fileExists . }}
- {{ $scratch.Set "configFile" . }}
- {{ end }}
-{{ end }}
-
-{{/* Retrieve the contentDir and staticDir settings from the config, and provide default values */}}
-{{ $scratch.Set "configFileMap" (os.ReadFile ($scratch.Get "configFile") | transform.Unmarshal) }}
-{{ if isset ($scratch.Get "configFileMap") "contentDir" }}
- {{ $scratch.Set "contentDir" (index ($scratch.Get "configFileMap") "contentDir") }}
-{{ else }}
- {{ $scratch.Set "contentDir" "content" }}
-{{ end }}
-{{ if isset ($scratch.Get "configFileMap") "staticDir" }}
- {{ $scratch.Set "staticDir" (index ($scratch.Get "configFileMap") "staticDir") }}
-{{ else }}
- {{ $scratch.Set "staticDir" "static" }}
-{{ end }}
-
-{{/* Check if page bundles are set. */}}
-{{- $bundle := .Page.Site.Params.usePageBundles }}
-{{ if eq .Page.Params.usePageBundles false }}
- {{- $bundle = false }}
-{{ end }}
-{{ if eq .Page.Params.usePageBundles true }}
- {{- $bundle = true }}
-{{ end }}
-
-{{ if eq $bundle true }}
- {{- $fileWeb = (path.Join $.Page.RelPermalink $file) -}}
- {{- $fileDisk = (path.Join ($scratch.Get "contentDir") (path.Join $.Page.File.Dir $file)) }}
-{{ else }}
- {{- $fileWeb = absURL $file -}}
- {{- $fileDisk = (path.Join ($scratch.Get "staticDir") $file) }}
-{{ end }}
-
-{{/* If image is external, or if image is internal and exists */}}
-{{- if or (
- (eq ($scratch.Get "fileLoc") "external"))
- (and
- (eq ($scratch.Get "fileLoc") "internal")
- (fileExists $fileDisk)
- )
--}}
- <figure>
- <picture>
-
- {{- if eq ($scratch.Get "fileLoc") "external" -}}
- <img class="{{ $scratch.Get "classes" }}" loading="lazy" src="{{ $file }}" alt="{{ $alt }}" {{ with $cap}} title="{{ $cap }}" {{ end }} />
- {{- else -}}
-
- {{- $img := imageConfig $fileDisk -}}{{/* image element (passed through imageConfig) */}}
-
- {{ range $ext := (slice "avif" "webp" "jxl") }}
- {{ $dotext := print "." $ext }}
- {{ $diskPath := $fileDisk }}
- {{ $webPath := $fileWeb }}
- {{ $diskPath = replace $diskPath ".png" $dotext }}
- {{ $diskPath = replace $diskPath ".jpg" $dotext }}
- {{ $diskPath = replace $diskPath ".jpeg" $dotext }}
- {{ $diskPath = replace $diskPath ".gif" $dotext }}
- {{ $webPath = replace $webPath ".png" $dotext }}
- {{ $webPath = replace $webPath ".jpg" $dotext }}
- {{ $webPath = replace $webPath ".jpeg" $dotext }}
- {{ $webPath = replace $webPath ".gif" $dotext }}
- {{ if fileExists $diskPath }}
- <source srcset="{{ $webPath | safeURL }}" type="image/{{ $ext }}">
- {{ end }}
- {{ end }}
-
- <img class="{{ $scratch.Get "classes" }}" src="{{ $fileWeb }}" alt="{{ $alt }}" loading="lazy" decoding="async" width="{{ $img.Width }}" height="{{ $img.Height }}" {{ with $cap }} title="{{ $cap }}" {{ end }} />
-
- {{- end -}}
-
- {{/* Provide caption based on image title, if it is set. */}}
- {{ if ($cap) -}}
- <figcaption>{{ $cap | safeHTML }}</figcaption>
- {{- end }}
-
- </picture>
- </figure>
-{{/* Image is internal but does not exist, so render error. */}}
-{{- else }}
- {{- partial "image-error" (dict "fileWeb" $fileWeb "fileDisk" $fileDisk "bundle" $bundle) }}
-{{- end }}
+{{- partial "figure" (
+ dict
+ "file" $file
+ "image" $image
+ "dir" $.Page.File.Dir
+ "alt" $alt
+ "cap" $cap
+ "classes" ($scratch.Get "classes")
+ "bundle" $bundle
+) -}}