diff --git a/layouts/404.html b/layouts/404.html index 787e486c25..8e0cf10804 100644 --- a/layouts/404.html +++ b/layouts/404.html @@ -1,4 +1,3 @@ -{{- .Scratch.Set "relearnOutputFormat" "HTML" }} diff --git a/layouts/_default/index.html b/layouts/_default/index.html index 3ac47b1d8c..e865a08dd1 100644 --- a/layouts/_default/index.html +++ b/layouts/_default/index.html @@ -1,2 +1 @@ -{{- .Scratch.Set "relearnOutputFormat" "HTML" }} {{- partial "_main.html" . }} \ No newline at end of file diff --git a/layouts/_default/index.print.html b/layouts/_default/index.print.html deleted file mode 100644 index 611dc5b42f..0000000000 --- a/layouts/_default/index.print.html +++ /dev/null @@ -1,2 +0,0 @@ -{{- .Scratch.Set "relearnOutputFormat" "PRINT" }} -{{- partial "_main.html" . }} \ No newline at end of file diff --git a/layouts/_default/list.html b/layouts/_default/list.html index 3ac47b1d8c..e865a08dd1 100644 --- a/layouts/_default/list.html +++ b/layouts/_default/list.html @@ -1,2 +1 @@ -{{- .Scratch.Set "relearnOutputFormat" "HTML" }} {{- partial "_main.html" . }} \ No newline at end of file diff --git a/layouts/_default/list.print.html b/layouts/_default/list.print.html deleted file mode 100644 index 611dc5b42f..0000000000 --- a/layouts/_default/list.print.html +++ /dev/null @@ -1,2 +0,0 @@ -{{- .Scratch.Set "relearnOutputFormat" "PRINT" }} -{{- partial "_main.html" . }} \ No newline at end of file diff --git a/layouts/_default/single.html b/layouts/_default/single.html index 3ac47b1d8c..e865a08dd1 100644 --- a/layouts/_default/single.html +++ b/layouts/_default/single.html @@ -1,2 +1 @@ -{{- .Scratch.Set "relearnOutputFormat" "HTML" }} {{- partial "_main.html" . }} \ No newline at end of file diff --git a/layouts/_default/single.print.html b/layouts/_default/single.print.html deleted file mode 100644 index 611dc5b42f..0000000000 --- a/layouts/_default/single.print.html +++ /dev/null @@ -1,2 +0,0 @@ -{{- .Scratch.Set "relearnOutputFormat" "PRINT" }} -{{- partial "_main.html" . }} \ No newline at end of file diff --git a/layouts/_default/taxonomy.html b/layouts/_default/taxonomy.html index fb0e02a177..4c70c18a99 100644 --- a/layouts/_default/taxonomy.html +++ b/layouts/_default/taxonomy.html @@ -1,2 +1 @@ -{{- .Scratch.Set "relearnOutputFormat" "HTML" }} {{- partial "_taxonomy.html" . }} \ No newline at end of file diff --git a/layouts/_default/taxonomy.print.html b/layouts/_default/taxonomy.print.html deleted file mode 100644 index 9c47723966..0000000000 --- a/layouts/_default/taxonomy.print.html +++ /dev/null @@ -1,2 +0,0 @@ -{{- .Scratch.Set "relearnOutputFormat" "PRINT" }} -{{- partial "_taxonomy.html" . }} \ No newline at end of file diff --git a/layouts/partials/footer.html b/layouts/partials/footer.html index f690608a5a..859ff5cf34 100644 --- a/layouts/partials/footer.html +++ b/layouts/partials/footer.html @@ -7,7 +7,7 @@ -{{- $wantsMathJax := .Scratch.Get (printf "%sHasMathJax" ((.Scratch.Get "relearnOutputFormat") | lower)) }} +{{- $wantsMathJax := .Scratch.Get (printf "%sHasMathJax" ((partial "output-format.html" .) | lower)) }} {{- if $wantsMathJax }} {{- if isset .Params "mathjaxinitialize" }} {{- $.Scratch.Set "mathJaxInitialize" .Params.mathJaxInitialize }} @@ -48,7 +48,7 @@ {{- end }} {{- end }} -{{- $wantsMermaid := .Scratch.Get (printf "%sHasMermaid" ((.Scratch.Get "relearnOutputFormat") | lower)) }} +{{- $wantsMermaid := .Scratch.Get (printf "%sHasMermaid" ((partial "output-format.html" .) | lower)) }} {{- if $wantsMermaid }} {{- if isset .Params "custommermaidurl" }} @@ -78,7 +78,7 @@ useMermaid( JSON.parse({{ $.Scratch.Get "mermaidInitialize" }}) ); {{- end }} -{{- $wantsSwagger := .Scratch.Get (printf "%sHasSwagger" ((.Scratch.Get "relearnOutputFormat") | lower)) }} +{{- $wantsSwagger := .Scratch.Get (printf "%sHasSwagger" ((partial "output-format.html" .) | lower)) }} {{- if $wantsSwagger }} {{- if isset .Params "customswaggerurl" }} diff --git a/layouts/partials/header.html b/layouts/partials/header.html index 0715bffa16..5d85a8ebcd 100644 --- a/layouts/partials/header.html +++ b/layouts/partials/header.html @@ -25,7 +25,7 @@ {{- partial "stylesheet.html" . }} {{- partial "custom-header.html" . }} - +
diff --git a/layouts/partials/nested-article.html b/layouts/partials/nested-article.html index c323add11c..e1f9730ca7 100644 --- a/layouts/partials/nested-article.html +++ b/layouts/partials/nested-article.html @@ -1,5 +1,5 @@ {{- $page := .page }} -{{- $page.Page.Store.Set (printf "%sIsNested" (($page.Scratch.Get "relearnOutputFormat") | lower)) true }} +{{- $page.Page.Store.Set (printf "%sIsNested" ((partial "output-format.html" $page) | lower)) true }} {{- with $page }} {{- $currentNode := . }} {{- $isActive := .IsHome }} diff --git a/layouts/partials/output-article.html b/layouts/partials/output-article.html index 9092248741..20baf10274 100644 --- a/layouts/partials/output-article.html +++ b/layouts/partials/output-article.html @@ -1,7 +1,7 @@ {{- $page := .page }} {{- $content := .content }} {{- $r_url := $page.RelPermalink }} -{{- with $page.OutputFormats.Get ($page.Scratch.Get "relearnOutputFormat") }} +{{- with $page.OutputFormats.Get (partial "output-format.html" $page) }} {{- $s_url := .RelPermalink }} {{- if not (eq $s_url $r_url) }} {{/* if we have a relative link in output formats with a path setting, our URLs are one level to deep; so we are making them absolute to our page by prepending the page's permalink */}} diff --git a/layouts/partials/output-format.html b/layouts/partials/output-format.html new file mode 100644 index 0000000000..2c1c0b3496 --- /dev/null +++ b/layouts/partials/output-format.html @@ -0,0 +1,46 @@ +{{/* + https://discourse.gohugo.io/t/question-about-printf-v/22923/4 + + GetCurrentOutput + Retrieves the OuputFormat of the page, from a list of predefined outputs + + @author @regisphilibert with modification by @McShelby + + @context Page (.) + + @access public + + @return A String among the predefined list + + @example - Go Template + {{ $currentOutputFormat := partial "func/GetCurrentOutput" . }} + + @warning This partial cannot be cached. +*/}} + +{{/* We create a slice listing the concerned output formats */}} +{{- $outputs := slice }} +{{- range .OutputFormats }} + {{- $outputs = $outputs | append .Name }} +{{- end }} +{{- $alt := slice }} +{{/* We range on the page's Alternative Output Formats which returns all output formats +except the current one. */}} +{{- range .AlternativeOutputFormats }} +{{/* If an output format matches one in the concerned list, we add it to our slice of outputs */}} + {{- if in $outputs .Name }} + {{- $alt = $alt | append .Name }} + {{- end }} +{{- end }} +{{- $current := "default" }} +{{/* If any alternate output formats part of the "concerned" ones have been found, we range on them. */}} +{{- with $alt }} + {{- range $outputs }} + {{/* If the output format is not listed as an "alternate", it means it is the current one. */}} + {{- if not (in $alt .) }} + {{- $current = . }} + {{- end }} + {{- end }} +{{- end }} + +{{- return $current }} \ No newline at end of file diff --git a/layouts/partials/output-partial.html b/layouts/partials/output-partial.html index ae3f9d40bb..0901f142f5 100644 --- a/layouts/partials/output-partial.html +++ b/layouts/partials/output-partial.html @@ -1,10 +1,10 @@ {{- $base := .base }} {{- $page := .page }} {{- $parameter := .parameter }} -{{- if or (not ($page.Scratch.Get "relearnOutputFormat")) (not (fileExists (printf "/layouts/partials/%s.%s.html" $base (($page.Scratch.Get "relearnOutputFormat") | lower)))) }} +{{- if or (not (partial "output-format.html" $page)) (not (fileExists (printf "/layouts/partials/%s.%s.html" $base ((partial "output-format.html" $page) | lower)))) }} {{- if (fileExists (printf "/layouts/partials/%s.html" $base)) }} {{- partial (printf "%s.html" $base) $parameter }} {{- end }} {{- else }} - {{- partial (printf "%s.%s.html" $base ((.page.Scratch.Get "relearnOutputFormat") | lower)) $parameter }} + {{- partial (printf "%s.%s.html" $base ((partial "output-format.html" $page) | lower)) $parameter }} {{- end }} \ No newline at end of file diff --git a/layouts/partials/single-article.html b/layouts/partials/single-article.html index a892bdc5ab..8dbe95cdc4 100644 --- a/layouts/partials/single-article.html +++ b/layouts/partials/single-article.html @@ -1,4 +1,4 @@ {{- $page := .page }} {{- $content := .content }} -{{- $page.Page.Store.Set (printf "%sIsNested" (($page.Scratch.Get "relearnOutputFormat") | lower)) false }} +{{- $page.Page.Store.Set (printf "%sIsNested" ((partial "output-format.html" $page) | lower)) false }} {{- partial "output-partial.html" (dict "base" "output-article" "page" $page "parameter" (dict "page" $page "content" $content)) }} \ No newline at end of file diff --git a/layouts/partials/stylesheet.html b/layouts/partials/stylesheet.html index 2b1435054c..49c8b79131 100644 --- a/layouts/partials/stylesheet.html +++ b/layouts/partials/stylesheet.html @@ -19,8 +19,8 @@ - {{- if (fileExists (printf "/static/css/format-%s.css" ((.Scratch.Get "relearnOutputFormat") | lower))) }} - + {{- if (fileExists (printf "/static/css/format-%s.css" ((partial "output-format.html" .) | lower))) }} + {{- end }}