variant: avoid Hugo permission errors on build #831

This commit is contained in:
Sören Weber 2024-04-19 00:02:28 +02:00
parent e952e148fc
commit bd40625c21
No known key found for this signature in database
GPG key ID: BEC6D55545451B6D
16 changed files with 64 additions and 78 deletions

View file

@ -1,15 +1,19 @@
{{- $page := . }} {{- $page := . }}
{{- $mod := $page.Site.Params.themeVariantModifier | default "" }} {{- $mod := "" }}
{{- $autores := resources.Get "css/auto.css" }} {{- $autores := resources.Get "css/auto.css" }}
{{- $printres := resources.Get "css/format-print.css" }} {{- $printres := resources.Get "css/format-print.css" }}
{{- $nonautothemevariants := slice }}
{{- /* we need relearn-light in every case for the print styles */}}
{{- $refthemevariants := slice | append "relearn-light" }}
{{- /* {{- /*
Config run: Unification run:
- convert from old forms to slice of dicts - convert from old forms to slice of dicts
- add default name property
- convert auto property to slice - convert auto property to slice
- add name property - save non auto variants to later assign them as default for missing auto variants
- mark variants configured by the user
*/}} */}}
{{- $refthemevariants := slice | append "relearn-light" | append "relearn-dark" }}
{{- $tempthemevariants := slice | append (.Site.Params.themeVariant | default "auto" ) }} {{- $tempthemevariants := slice | append (.Site.Params.themeVariant | default "auto" ) }}
{{- $themevariants := slice }} {{- $themevariants := slice }}
{{- range $tempthemevariant := $tempthemevariants }} {{- range $tempthemevariant := $tempthemevariants }}
@ -22,15 +26,47 @@ Config run:
{{- end }} {{- end }}
{{- if eq $themevariant.identifier "auto" }} {{- if eq $themevariant.identifier "auto" }}
{{- $themevariant = collections.Merge $themevariant (dict "auto" ($themevariant.auto | default slice)) }} {{- $themevariant = collections.Merge $themevariant (dict "auto" ($themevariant.auto | default slice)) }}
{{- $refthemevariants = $refthemevariants | append $themevariant.auto }} {{- end }}
{{- if not (isset $themevariant "auto") }}
{{- $nonautothemevariants = $nonautothemevariants | append $themevariant.identifier }}
{{- end }} {{- end }}
{{- $themevariant = collections.Merge $themevariant (dict "config" true) }} {{- $themevariant = collections.Merge $themevariant (dict "config" true) }}
{{- $themevariants = $themevariants | append $themevariant }} {{- $themevariants = $themevariants | append $themevariant }}
{{- end }} {{- end }}
{{- /*
Auto run:
- fill up auto property with defaults
- save variants referenced by auto configuration
- write variant CSS file based on auto template
- add chroma property
- write chroma CSS file based on auto template
*/}}
{{- $defaultautothemevariants := slice }}
{{- $defaultautothemevariants = $defaultautothemevariants | append (index .Site.Params.themeVariantAuto 0 | default (index $nonautothemevariants 0) | default "relearn-light") }}
{{- $defaultautothemevariants = $defaultautothemevariants | append (index .Site.Params.themeVariantAuto 1 | default (index $nonautothemevariants 1) | default "relearn-dark") }}
{{- $tempthemevariants = $themevariants }}
{{- $themevariants = slice }}
{{- range $tempthemevariant := $tempthemevariants }}
{{- $themevariant := $tempthemevariant }}
{{- if collections.IsSet $themevariant "auto" }}
{{- $light := index $themevariant.auto 0 | default (index $defaultautothemevariants 0) }}
{{- $dark := index $themevariant.auto 1 | default (index $defaultautothemevariants 1) }}
{{- $themevariant = collections.Merge $themevariant (dict "auto" (slice | append $light | append $dark)) }}
{{- $refthemevariants = $refthemevariants | append $themevariant.auto }}
{{- $cssres := $autores | resources.ExecuteAsTemplate (printf "css/theme-%s%s.css" $themevariant.identifier $mod) (dict "prefix" "theme" "suffix" $mod "light" $light "dark" $dark) }}
{{- /* the following code causes Hugo to generate our css file - although it is in comments */}}<!-- {{ $cssres.RelPermalink }} -->
{{- $themevariant = collections.Merge $themevariant (dict "chroma" $themevariant.identifier) }}
{{- $cssres = $autores | resources.ExecuteAsTemplate (printf "css/chroma-%s.css" $themevariant.chroma) (dict "prefix" "chroma" "suffix" "" "light" $light "dark" $dark) }}
{{- /* the following code causes Hugo to generate our css file - although it is in comments */}}<!-- {{ $cssres.RelPermalink }} -->
{{- end }}
{{- $themevariants = $themevariants | append $themevariant }}
{{- end }}
{{- /* {{- /*
Ref run: Ref run:
- add non-configured but referenced variants - add variants referenced by auto properties
- add default name property
*/}} */}}
{{- range $refthemevariant := $refthemevariants }} {{- range $refthemevariant := $refthemevariants }}
{{- if not (index (collections.Where $themevariants "identifier" $refthemevariant) 0) }} {{- if not (index (collections.Where $themevariants "identifier" $refthemevariant) 0) }}
@ -40,9 +76,10 @@ Ref run:
{{- end }} {{- end }}
{{- /* {{- /*
Non-auto run Non-auto run:
- fix and regenerate theme css for advanced config mode - write variant CSS file and all imported variant CSS files
- add chroma property from stylesheet - add chroma property from deepest imported variant
- write chroma CSS file (just the root one because they are not allowed to be nested and the deepest one wins)
*/}} */}}
{{- $tempthemevariants = $themevariants }} {{- $tempthemevariants = $themevariants }}
{{- $themevariants = slice }} {{- $themevariants = slice }}
@ -50,55 +87,10 @@ Non-auto run
{{- $themevariant := $tempthemevariant }} {{- $themevariant := $tempthemevariant }}
{{- if not (collections.IsSet $themevariant "auto") }} {{- if not (collections.IsSet $themevariant "auto") }}
{{- $themevariant = partial "get-theme-details.html" (dict "themevariant" $themevariant "identifier" $themevariant.identifier "mod" $mod)}} {{- $themevariant = partial "get-theme-details.html" (dict "themevariant" $themevariant "identifier" $themevariant.identifier "mod" $mod)}}
{{- end }} {{- $cssres := resources.Get (printf "css/chroma-%s.css" $themevariant.chroma) }}
{{- $themevariants = $themevariants | append $themevariant }} {{- if not $cssres }}
{{- end }} {{- errorf "\"chroma-%s.css\": file not found in \"assets/css\"; if you are migrating from a theme version older thant 6.0.0, you have to move it over from \"static/css\"" $themevariant.chroma }}
{{- /*
Auto run
- fill up auto property
- fill up chroma property
- generate theme and chroma css files from template
*/}}
{{- $tempthemevariants = $themevariants }}
{{- $themevariants = slice }}
{{- range $tempthemevariant := $tempthemevariants }}
{{- $themevariant := $tempthemevariant }}
{{- if collections.IsSet $themevariant "auto" }}
{{- $i := 0 }}
{{- if eq (int (len $themevariant.auto)) 0 }}
{{- range $tempthemevariants }}
{{- $i = add $i 1 }}
{{- if not (collections.IsSet . "auto") }}
{{- $themevariant = collections.Merge $themevariant (dict "auto" ($themevariant.auto | append .identifier)) }}
{{- break }}
{{- end }}
{{- end }}
{{- end }} {{- end }}
{{- if eq (int (len $themevariant.auto)) 0 }}
{{- $themevariant = collections.Merge $themevariant (dict "auto" ($themevariant.auto | append "relearn-light")) }}
{{- end }}
{{- if eq (int (len $themevariant.auto)) 1 }}
{{- $poppedthemevariants := last (sub (len $tempthemevariants) $i) $tempthemevariants }}
{{- range $poppedthemevariants }}
{{- if not (collections.IsSet . "auto") }}
{{- $themevariant = collections.Merge $themevariant (dict "auto" ($themevariant.auto | append .identifier)) }}
{{- break }}
{{- end }}
{{- end }}
{{- end }}
{{- if eq (int (len $themevariant.auto)) 1 }}
{{- $themevariant = collections.Merge $themevariant (dict "auto" ($themevariant.auto | append "relearn-dark")) }}
{{- end }}
{{- $themevariant = collections.Merge $themevariant (dict "auto" ($themevariant.auto | first 2)) }}
{{- $light := index $themevariant.auto 0 }};
{{- $dark := index $themevariant.auto 1 }};
{{- $cssres := $autores | resources.ExecuteAsTemplate (printf "css/theme-%s%s.css" $themevariant.identifier $mod) (dict "prefix" "theme" "suffix" $mod "light" $light "dark" $dark) }}
{{- /* the following code causes Hugo to generate our css file - although it is in comments */}}<!-- {{ $cssres.RelPermalink }} -->
{{- $themevariant = collections.Merge $themevariant (dict "chroma" $themevariant.identifier) }}
{{- $light = (index (collections.Where $tempthemevariants "identifier" $light) 0).chroma }}
{{- $dark = (index (collections.Where $tempthemevariants "identifier" $dark) 0).chroma }}
{{- $cssres = $autores | resources.ExecuteAsTemplate (printf "css/chroma-%s.css" $themevariant.chroma) (dict "prefix" "chroma" "suffix" "" "light" $light "dark" $dark) }}
{{- /* the following code causes Hugo to generate our css file - although it is in comments */}}<!-- {{ $cssres.RelPermalink }} --> {{- /* the following code causes Hugo to generate our css file - although it is in comments */}}<!-- {{ $cssres.RelPermalink }} -->
{{- end }} {{- end }}
{{- $themevariants = $themevariants | append $themevariant }} {{- $themevariants = $themevariants | append $themevariant }}
@ -109,38 +101,32 @@ Auto run
{{- return collections.Where $themevariants "config" true }} {{- return collections.Where $themevariants "config" true }}
{{ define "partials/get-theme-details.html" }} {{- define "partials/get-theme-details.html" }}
{{- $themevariant := .themevariant }} {{- $themevariant := .themevariant }}
{{- $identifier := .identifier }} {{- $identifier := .identifier }}
{{- $mod := .mod }} {{- $mod := .mod }}
{{- $themecontent := "" }} {{- $themecontent := "" }}
{{- with resources.Get (printf "css/theme-%s.css" $identifier) }} {{- with resources.Get (printf "css/theme-%s.css" $identifier) }}
{{- $themecontent = .Content}} {{- $themecontent = .Content }}
{{- else }} {{- else }}
{{- if fileExists (printf "static/css/theme-%s.css" $identifier) }} {{- errorf "\"theme-%s.css\": file not found in \"assets/css\"; if you are migrating from a theme version older thant 6.0.0, you have to move it over from \"static/css\"" $identifier }}
{{- $themecontent = readFile (printf "static/css/theme-%s.css" $identifier) }}
{{- else }}
{{- errorf "\"theme-%s.css\": file not found neither in \"static/css\" and \"assets/css\"" $identifier }}
{{- end }}
{{- end }} {{- end }}
{{- replaceRE (printf `([ \t]*@import\s+[^$]*?theme-[^.]*?)(?:%s)?(\.css)` (replace $mod "." "\\.")) (printf "${1}%s${2}" $mod) $themecontent }} {{- replaceRE (printf `([ \t]*@import\s+[^$]*?theme-[^.]*?)(?:%s)?(\.css)` (replace $mod "." "\\.")) (printf "${1}%s${2}" $mod) $themecontent }}
{{- $chroma := "" }} {{- $chroma := "" }}
{{- range findRESubmatch `[ \t]*@import\s+[^$]*?chroma-([^.]*?)\.css` $themecontent }}
{{- $chroma = index . 1 }}
{{- errorf "\"theme-%s.css\": UNSUPPORTED use of @import for chroma stylesheet, instead use '--CODE-theme: %s;'" $identifier $chroma }}
{{- end }}
{{- range findRESubmatch (printf `[ \t]*@import\s+[^$]*?theme-([^.]*?)%s\.css` (replace $mod "." "\\.")) $themecontent }}
{{- $subidentifier := index . 1 }}
{{- $themevariant = partial "get-theme-details.html" (dict "themevariant" $themevariant "identifier" $subidentifier "mod" $mod) }}
{{- $chroma = $themevariant.chroma }}
{{- end }}
{{- range findRESubmatch `[ \t]*--CODE-theme\s*:\s*([^;]*?)\s*;` $themecontent }} {{- range findRESubmatch `[ \t]*--CODE-theme\s*:\s*([^;]*?)\s*;` $themecontent }}
{{- $chroma = index . 1 }} {{- $chroma = index . 1 }}
{{- else }} {{- end }}
{{- range findRESubmatch `[ \t]*@import\s+[^$]*?chroma-([^.]*?)\.css` $themecontent }} {{- if not $chroma }}
{{- $chroma = index . 1 }} {{- $chroma = "relearn-light" }}
{{- warnf "\"theme-%s.css\": DEPRECATED use of @import for chroma stylesheet, instead use '--CODE-theme: %s;'" $identifier $chroma }}
{{- $themecontent = replaceRE `[ \t]*@import\s+[^$]*?chroma-[^.]*?\.css.*?\n` "" $themecontent }}
{{- else }}
{{- range findRESubmatch (printf `[ \t]*@import\s+[^$]*?theme-([^.]*?)%s\.css` (replace $mod "." "\\.")) $themecontent }}
{{- $subidentifier := index . 1 }}
{{- $themevariant = partial "get-theme-details.html" (dict "themevariant" $themevariant "identifier" $subidentifier "mod" $mod) }}
{{- $chroma = $themevariant.chroma }}
{{- else }}
{{- $chroma = "relearn-light" }}
{{- end }}
{{- end }}
{{- $themecontent = replaceRE `(:root\s*\{[ \t]*)(\s*)` (printf "${1}${2}--CODE-theme: %s;${2}" $chroma) $themecontent }} {{- $themecontent = replaceRE `(:root\s*\{[ \t]*)(\s*)` (printf "${1}${2}--CODE-theme: %s;${2}" $chroma) $themecontent }}
{{- end }} {{- end }}
{{- $themevariant = collections.Merge $themevariant (dict "chroma" $chroma) }} {{- $themevariant = collections.Merge $themevariant (dict "chroma" $chroma) }}