From d36c70b98e56c9da4ca365259ba666422865dcd1 Mon Sep 17 00:00:00 2001 From: Mark Dumay Date: Thu, 15 Feb 2024 05:37:36 +0100 Subject: [PATCH] Refactor download button --- layouts/_default/single/panel-toc.html | 42 +++++++++++--------------- layouts/partials/assets/download.html | 38 +++++++++++++++++++++++ layouts/partials/assets/sharing.html | 2 +- 3 files changed, 57 insertions(+), 25 deletions(-) create mode 100644 layouts/partials/assets/download.html diff --git a/layouts/_default/single/panel-toc.html b/layouts/_default/single/panel-toc.html index 1e51bca66..c6a467a5a 100644 --- a/layouts/_default/single/panel-toc.html +++ b/layouts/_default/single/panel-toc.html @@ -1,17 +1,15 @@ {{- $download := .Params.download -}} -{{- if and $download (not (fileExists (path.Join "static" $download))) -}} - {{- errorf "Cannot find download file for page '%s': %s" .File.Path $download -}} -{{- end -}} -{{- $toc := .Site.Params.navigation.toc -}} -{{ if $toc }} +{{- $includeToc := false -}} +{{- if .Site.Params.navigation.toc -}} + {{ $includeToc = true }} {{ if isset .Params "includeToc" }} - {{ $toc = .Params.includeToc }} + {{ $includeToc = .Params.includeToc }} {{ else }} {{- with (index site.Params.pages .Type) -}} {{ if isset . "includetoc" }} - {{ $toc = index . "includetoc" }} - {{ if ne (printf "%T" $toc) "bool" }} + {{ $includeToc = index . "includetoc" }} + {{ if ne (printf "%T" $includeToc) "bool" }} {{ errorf "Expected bool value in site parameters: pages.%s.includeToc" $.Type }} {{ end }} {{ end }} @@ -19,24 +17,20 @@ {{ end }} {{ end }} -
- {{ with $download }} - {{ $title := (T "download" ) }} - {{ $lang := strings.TrimPrefix "." (path.Ext (path.BaseName .)) }} - {{ if and $lang (ne (string site.LanguageCode) $lang) }} - {{ range site.Languages }} - {{ if eq .LanguageCode $lang }} - {{ $title = printf "%s (%s)" (T "download") (T (printf "lang_%s" .LanguageCode)) }} - {{ end }} - {{ end }} - {{ end }} -
- {{ $attr := dict "download" (path.Base .) }} - {{ partial "assets/button.html" (dict "href" . "title" $title "color" "primary" "outline" "true" "icon" "fas download" "size" "sm" "attributes" $attr) }} +
+ {{ if $download }} +
+ {{ partial "assets/download.html" (dict + "download" $download + "outline" true + "size" "sm" + "order" "last" + ) }}
{{ end }} - {{ if $toc }} - {{ partial "assets/toc.html" (dict "page" .) }} + {{ if $includeToc }} + {{ $index := partial "assets/toc.html" (dict "page" .) }} + {{ $index | safeHTML }} {{ end -}}
diff --git a/layouts/partials/assets/download.html b/layouts/partials/assets/download.html new file mode 100644 index 000000000..8882800fc --- /dev/null +++ b/layouts/partials/assets/download.html @@ -0,0 +1,38 @@ +{{- $download := partial "utilities/GetTargetPath.html" (dict "path" .download "page" page) -}} +{{- $color := .color | default "primary" -}} +{{- $outline := .outline | default true -}} +{{- $icon := .icon | default "fas download" -}} +{{- $size := .size | default "sm" -}} +{{- $justify := .justify -}} +{{- $class := .class -}} +{{- $minimal := .minimal | default false -}} + +{{- if and $download (not (fileExists (path.Join "static" $download))) -}} + {{- errorf "Cannot find download file for page '%s': %s" page.File.Path $download -}} +{{- end -}} + +{{ with $download }} + {{ $title := "" }} + {{ if not $minimal }}{{ $title = (T "download" ) }}{{ end }} + {{ $lang := strings.TrimPrefix "." (path.Ext (path.BaseName .)) }} + {{ if and $lang (ne (string site.LanguageCode) $lang) }} + {{ range site.Languages }} + {{ if eq .LanguageCode $lang }} + {{ $title = printf "%s (%s)" (T "download") (T (printf "lang_%s" .LanguageCode)) }} + {{ end }} + {{ end }} + {{ end }} + {{ $attr := dict "download" (path.Base .) }} + {{ partial "assets/button.html" (dict + "href" . + "title" $title + "color" $color + "outline" $outline + "icon" $icon + "size" $size + "justify" $justify + "class" $class + "attributes" $attr + ) }} +{{ end }} + diff --git a/layouts/partials/assets/sharing.html b/layouts/partials/assets/sharing.html index 8cca0056f..118a8501a 100644 --- a/layouts/partials/assets/sharing.html +++ b/layouts/partials/assets/sharing.html @@ -35,7 +35,7 @@ {{- $sort := $page.Site.Params.sharing.sort | default "weight" -}} {{- $list = sort $list $sort $order -}} - {{- $download := $page.Params.download -}} + {{- $download := partial "utilities/GetTargetPath.html" (dict "path" $page.Params.download "page" $page) -}} {{- if and $download (not (fileExists (path.Join "static" $download))) -}} {{- errorf "Cannot find download file for page '%s': %s" $page.File.Path $download -}} {{- end -}}