theme: robust way to determine output format #308

This commit is contained in:
Sören Weber 2022-07-11 00:59:26 +02:00
parent d60c2988f2
commit b8a49da2f5
No known key found for this signature in database
GPG key ID: 07D17FF580AE7589
17 changed files with 57 additions and 24 deletions

View file

@ -1,4 +1,3 @@
{{- .Scratch.Set "relearnOutputFormat" "HTML" }}
<!DOCTYPE html>
<html lang="{{ .Page.Language | default "en" }}">
<head>

View file

@ -1,2 +1 @@
{{- .Scratch.Set "relearnOutputFormat" "HTML" }}
{{- partial "_main.html" . }}

View file

@ -1,2 +0,0 @@
{{- .Scratch.Set "relearnOutputFormat" "PRINT" }}
{{- partial "_main.html" . }}

View file

@ -1,2 +1 @@
{{- .Scratch.Set "relearnOutputFormat" "HTML" }}
{{- partial "_main.html" . }}

View file

@ -1,2 +0,0 @@
{{- .Scratch.Set "relearnOutputFormat" "PRINT" }}
{{- partial "_main.html" . }}

View file

@ -1,2 +1 @@
{{- .Scratch.Set "relearnOutputFormat" "HTML" }}
{{- partial "_main.html" . }}

View file

@ -1,2 +0,0 @@
{{- .Scratch.Set "relearnOutputFormat" "PRINT" }}
{{- partial "_main.html" . }}

View file

@ -1,2 +1 @@
{{- .Scratch.Set "relearnOutputFormat" "HTML" }}
{{- partial "_taxonomy.html" . }}

View file

@ -1,2 +0,0 @@
{{- .Scratch.Set "relearnOutputFormat" "PRINT" }}
{{- partial "_taxonomy.html" . }}

View file

@ -7,7 +7,7 @@
<script src="{{"js/clipboard.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
<script src="{{"js/perfect-scrollbar.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
<script src="{{"js/featherlight.min.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
{{- $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 @@
<script id="MathJax-script" async src="{{"js/mathjax/tex-mml-chtml.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script>
{{- end }}
{{- end }}
{{- $wantsMermaid := .Scratch.Get (printf "%sHasMermaid" ((.Scratch.Get "relearnOutputFormat") | lower)) }}
{{- $wantsMermaid := .Scratch.Get (printf "%sHasMermaid" ((partial "output-format.html" .) | lower)) }}
{{- if $wantsMermaid }}
<script src="{{"js/jquery.svg.pan.zoom.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}" defer></script>
{{- if isset .Params "custommermaidurl" }}
@ -78,7 +78,7 @@
useMermaid( JSON.parse({{ $.Scratch.Get "mermaidInitialize" }}) );
</script>
{{- 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" }}
<script src="{{ .Params.customSwaggerURL }}" defer></script>

View file

@ -25,7 +25,7 @@
{{- partial "stylesheet.html" . }}
{{- partial "custom-header.html" . }}
</head>
<body class="mobile-support {{ ((.Scratch.Get "relearnOutputFormat") | lower) }}{{- if .Site.Params.disableInlineCopyToClipBoard }} disableInlineCopyToClipboard{{ end }}" data-url="{{ .RelPermalink }}">
<body class="mobile-support {{ ((partial "output-format.html" .) | lower) }}{{- if .Site.Params.disableInlineCopyToClipBoard }} disableInlineCopyToClipboard{{ end }}" data-url="{{ .RelPermalink }}">
<div id="body" class="default-animation">
<div id="sidebar-overlay"></div>
<div id="toc-overlay"></div>

View file

@ -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 }}

View file

@ -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 */}}

View file

@ -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 }}

View file

@ -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 }}

View file

@ -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)) }}

View file

@ -19,8 +19,8 @@
<link href="{{"css/ie.css" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet">
<link href="{{"css/variant.css" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet">
<link href="{{"css/print.css" | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet" media="print">
{{- if (fileExists (printf "/static/css/format-%s.css" ((.Scratch.Get "relearnOutputFormat") | lower))) }}
<link href="{{(printf "css/format-%s.css" ((.Scratch.Get "relearnOutputFormat") | lower)) | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet">
{{- if (fileExists (printf "/static/css/format-%s.css" ((partial "output-format.html" .) | lower))) }}
<link href="{{(printf "css/format-%s.css" ((partial "output-format.html" .) | lower)) | relURL}}{{ if $assetBusting }}?{{ now.Unix }}{{ end }}" rel="stylesheet">
{{- end }}
<script src="{{"js/variant.js" | relURL}}{{ if not .Site.Params.disableAssetsBusting }}?{{ now.Unix }}{{ end }}"></script>
<script>