github.com/choria-io/go-choria@v0.28.1-0.20240416190746-b3bf9c7d5a45/docs/themes/hugo-theme-relearn/layouts/partials/get-theme-variants.hugo (about) 1 {{- $page := . }} 2 {{- $mod := $page.Site.Params.themeVariantModifier | default "" }} 3 {{- $autores := resources.Get "css/auto.css" }} 4 {{- $printres := resources.Get "css/format-print.css" }} 5 6 {{- /* 7 Config run: 8 - convert from old forms to slice of dicts 9 - convert auto property to slice 10 - add name property 11 */}} 12 {{- $refthemevariants := slice | append "relearn-light" | append "relearn-dark" }} 13 {{- $tempthemevariants := slice | append (.Site.Params.themeVariant | default "auto" ) }} 14 {{- $themevariants := slice }} 15 {{- range $tempthemevariant := $tempthemevariants }} 16 {{- $themevariant := $tempthemevariant }} 17 {{- if not (reflect.IsMap $themevariant) }} 18 {{- $themevariant = dict "identifier" $tempthemevariant }} 19 {{- end }} 20 {{- if not $themevariant.name }} 21 {{- $themevariant = collections.Merge $themevariant (dict "name" ($themevariant.identifier | humanize | strings.Title)) }} 22 {{- end }} 23 {{- if eq $themevariant.identifier "auto" }} 24 {{- $themevariant = collections.Merge $themevariant (dict "auto" ($themevariant.auto | default slice)) }} 25 {{- $refthemevariants = $refthemevariants | append $themevariant.auto }} 26 {{- end }} 27 {{- $themevariant = collections.Merge $themevariant (dict "config" true) }} 28 {{- $themevariants = $themevariants | append $themevariant }} 29 {{- end }} 30 31 {{- /* 32 Ref run: 33 - add non-configured but referenced variants 34 */}} 35 {{- range $refthemevariant := $refthemevariants }} 36 {{- if not (index (collections.Where $themevariants "identifier" $refthemevariant) 0) }} 37 {{- $themevariant := (dict "identifier" $refthemevariant "name" ($refthemevariant | humanize | strings.Title) "config" false) }} 38 {{- $themevariants = $themevariants | append $themevariant }} 39 {{- end }} 40 {{- end }} 41 42 {{- /* 43 Non-auto run 44 - fix and regenerate theme css for advanced config mode 45 - add chroma property from stylesheet 46 */}} 47 {{- $tempthemevariants = $themevariants }} 48 {{- $themevariants = slice }} 49 {{- range $tempthemevariant := $tempthemevariants }} 50 {{- $themevariant := $tempthemevariant }} 51 {{- if not (collections.IsSet $themevariant "auto") }} 52 {{- $themevariant = partial "get-theme-details.html" (dict "themevariant" $themevariant "identifier" $themevariant.identifier "mod" $mod)}} 53 {{- end }} 54 {{- $themevariants = $themevariants | append $themevariant }} 55 {{- end }} 56 57 {{- /* 58 Auto run 59 - fill up auto property 60 - fill up chroma property 61 - generate theme and chroma css files from template 62 */}} 63 {{- $tempthemevariants = $themevariants }} 64 {{- $themevariants = slice }} 65 {{- range $tempthemevariant := $tempthemevariants }} 66 {{- $themevariant := $tempthemevariant }} 67 {{- if collections.IsSet $themevariant "auto" }} 68 {{- $i := 0 }} 69 {{- if eq (int (len $themevariant.auto)) 0 }} 70 {{- range $tempthemevariants }} 71 {{- $i = add $i 1 }} 72 {{- if not (collections.IsSet . "auto") }} 73 {{- $themevariant = collections.Merge $themevariant (dict "auto" ($themevariant.auto | append .identifier)) }} 74 {{- break }} 75 {{- end }} 76 {{- end }} 77 {{- end }} 78 {{- if eq (int (len $themevariant.auto)) 0 }} 79 {{- $themevariant = collections.Merge $themevariant (dict "auto" ($themevariant.auto | append "relearn-light")) }} 80 {{- end }} 81 {{- if eq (int (len $themevariant.auto)) 1 }} 82 {{- $poppedthemevariants := last (sub (len $tempthemevariants) $i) $tempthemevariants }} 83 {{- range $poppedthemevariants }} 84 {{- if not (collections.IsSet . "auto") }} 85 {{- $themevariant = collections.Merge $themevariant (dict "auto" ($themevariant.auto | append .identifier)) }} 86 {{- break }} 87 {{- end }} 88 {{- end }} 89 {{- end }} 90 {{- if eq (int (len $themevariant.auto)) 1 }} 91 {{- $themevariant = collections.Merge $themevariant (dict "auto" ($themevariant.auto | append "relearn-dark")) }} 92 {{- end }} 93 {{- $themevariant = collections.Merge $themevariant (dict "auto" ($themevariant.auto | first 2)) }} 94 {{- $light := index $themevariant.auto 0 }}; 95 {{- $dark := index $themevariant.auto 1 }}; 96 {{- $cssres := $autores | resources.ExecuteAsTemplate (printf "css/theme-%s%s.css" $themevariant.identifier $mod) (dict "prefix" "theme" "suffix" $mod "light" $light "dark" $dark) }} 97 {{- /* the following code causes Hugo to generate our css file - although it is in comments */}}<!-- {{ $cssres.RelPermalink }} --> 98 {{- $themevariant = collections.Merge $themevariant (dict "chroma" $themevariant.identifier) }} 99 {{- $light = (index (collections.Where $tempthemevariants "identifier" $light) 0).chroma }} 100 {{- $dark = (index (collections.Where $tempthemevariants "identifier" $dark) 0).chroma }} 101 {{- $cssres = $autores | resources.ExecuteAsTemplate (printf "css/chroma-%s.css" $themevariant.chroma) (dict "prefix" "chroma" "suffix" "" "light" $light "dark" $dark) }} 102 {{- /* the following code causes Hugo to generate our css file - although it is in comments */}}<!-- {{ $cssres.RelPermalink }} --> 103 {{- end }} 104 {{- $themevariants = $themevariants | append $themevariant }} 105 {{- end }} 106 107 {{- $cssres := $printres | resources.ExecuteAsTemplate "css/format-print.css" (dict "themevariant" (index (collections.Where $themevariants "identifier" "relearn-light") 0) "mod" $mod) }} 108 {{- /* the following code causes Hugo to generate our css file - although it is in comments */}}<!-- {{ $cssres.RelPermalink }} --> 109 110 {{- return collections.Where $themevariants "config" true }} 111 112 {{ define "partials/get-theme-details.html" }} 113 {{- $themevariant := .themevariant }} 114 {{- $identifier := .identifier }} 115 {{- $mod := .mod }} 116 {{- $themecontent := "" }} 117 {{- with resources.Get (printf "css/theme-%s.css" $identifier) }} 118 {{- $themecontent = .Content}} 119 {{- else }} 120 {{- if fileExists (printf "static/css/theme-%s.css" $identifier) }} 121 {{- $themecontent = readFile (printf "static/css/theme-%s.css" $identifier) }} 122 {{- else }} 123 {{- errorf "\"theme-%s.css\": file not found neither in \"static/css\" and \"assets/css\"" $identifier }} 124 {{- end }} 125 {{- end }} 126 {{- replaceRE (printf `([ \t]*@import\s+[^$]*?theme-[^.]*?)(?:%s)?(\.css)` (replace $mod "." "\\.")) (printf "${1}%s${2}" $mod) $themecontent }} 127 {{- $chroma := "" }} 128 {{- range findRESubmatch `[ \t]*--CODE-theme\s*:\s*([^;]*?)\s*;` $themecontent }} 129 {{- $chroma = index . 1 }} 130 {{- else }} 131 {{- range findRESubmatch `[ \t]*@import\s+[^$]*?chroma-([^.]*?)\.css` $themecontent }} 132 {{- $chroma = index . 1 }} 133 {{- warnf "\"theme-%s.css\": DEPRECATED use of @import for chroma stylesheet, instead use '--CODE-theme: %s;'" $identifier $chroma }} 134 {{- $themecontent = replaceRE `[ \t]*@import\s+[^$]*?chroma-[^.]*?\.css.*?\n` "" $themecontent }} 135 {{- else }} 136 {{- range findRESubmatch (printf `[ \t]*@import\s+[^$]*?theme-([^.]*?)%s\.css` (replace $mod "." "\\.")) $themecontent }} 137 {{- $subidentifier := index . 1 }} 138 {{- $themevariant = partial "get-theme-details.html" (dict "themevariant" $themevariant "identifier" $subidentifier "mod" $mod) }} 139 {{- $chroma = $themevariant.chroma }} 140 {{- else }} 141 {{- $chroma = "relearn-light" }} 142 {{- end }} 143 {{- end }} 144 {{- $themecontent = replaceRE `(:root\s*\{[ \t]*)(\s*)` (printf "${1}${2}--CODE-theme: %s;${2}" $chroma) $themecontent }} 145 {{- end }} 146 {{- $themevariant = collections.Merge $themevariant (dict "chroma" $chroma) }} 147 {{- $cssres := $themecontent | resources.FromString (printf "css/theme-%s%s.css" $identifier $mod) }} 148 {{- /* the following code causes Hugo to generate our css file - although it is in comments */}}<!-- {{ $cssres.RelPermalink }} --> 149 {{- return $themevariant }} 150 {{- end }}