| 1 |
// Copyright 2025 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package tplimpl |
| 15 |
|
| 16 |
import ( |
| 17 |
"strings" |
| 18 |
|
| 19 |
"github.com/gohugoio/hugo/common/types" |
| 20 |
"github.com/gohugoio/hugo/hugolib/sitesmatrix" |
| 21 |
"github.com/gohugoio/hugo/resources/kinds" |
| 22 |
) |
| 23 |
|
| 24 |
const baseNameBaseof = "baseof" |
| 25 |
|
| 26 |
// This is used both as a key and in lookups. |
| 27 |
type TemplateDescriptor struct { |
| 28 |
// Group 1. |
| 29 |
Kind string // page, home, section, taxonomy, term (and only those) |
| 30 |
LayoutFromTemplate string // list, single, all,mycustomlayout |
| 31 |
LayoutFromUser string // custom layout set in front matter, e.g. list, single, all, mycustomlayout |
| 32 |
|
| 33 |
// Group 2. |
| 34 |
OutputFormat string // rss, csv ... |
| 35 |
MediaType string // text/html, text/plain, ... |
| 36 |
|
| 37 |
SitesHash uint64 |
| 38 |
|
| 39 |
Variant1 string // contextual variant, e.g. "link" in render hooks." |
| 40 |
Variant2 string // contextual variant, e.g. "id" in render. |
| 41 |
|
| 42 |
// Misc. |
| 43 |
LayoutFromUserMustMatch bool // If set, we only look for the exact layout. |
| 44 |
IsPlainText bool // Whether this is a plain text template. |
| 45 |
AlwaysAllowPlainText bool // Whether to e.g. allow plain text templates to be rendered in HTML. |
| 46 |
} |
| 47 |
|
| 48 |
func (d *TemplateDescriptor) normalizeFromFile() { |
| 49 |
d.LayoutFromTemplate = strings.ToLower(d.LayoutFromTemplate) |
| 50 |
|
| 51 |
if d.LayoutFromTemplate == d.OutputFormat { |
| 52 |
d.LayoutFromTemplate = "" |
| 53 |
} |
| 54 |
|
| 55 |
if d.Kind == kinds.KindTemporary { |
| 56 |
d.Kind = "" |
| 57 |
} |
| 58 |
|
| 59 |
if d.LayoutFromTemplate == d.Kind { |
| 60 |
d.LayoutFromTemplate = "" |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
type descriptorHandler struct { |
| 65 |
opts StoreOptions |
| 66 |
} |
| 67 |
|
| 68 |
// Note that this in this setup is usually a descriptor constructed from a page, |
| 69 |
// so we want to find the best match for that page. |
| 70 |
func (s descriptorHandler) compareDescriptors(category Category, this, other TemplateDescriptor, sitesMatrixThis, sitesMatrixOther sitesmatrix.VectorProvider) weight { |
| 71 |
if this.LayoutFromUserMustMatch && this.LayoutFromUser != other.LayoutFromTemplate { |
| 72 |
return weightNoMatch |
| 73 |
} |
| 74 |
|
| 75 |
w := this.doCompare(category, other, sitesMatrixThis, sitesMatrixOther) |
| 76 |
|
| 77 |
if w.w1 <= 0 { |
| 78 |
if sitesMatrixOther != nil { |
| 79 |
if sitesMatrixThis == nil || !sitesMatrixOther.HasAnyVector(sitesMatrixThis) { |
| 80 |
return w |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if category == CategoryMarkup && (this.Variant1 == other.Variant1) && (this.Variant2 == other.Variant2 || this.Variant2 != "" && other.Variant2 == "") { |
| 85 |
// See issue 13242. |
| 86 |
if this.OutputFormat != other.OutputFormat && this.OutputFormat == s.opts.DefaultOutputFormat { |
| 87 |
return w |
| 88 |
} |
| 89 |
|
| 90 |
w.w1 = 1 |
| 91 |
} |
| 92 |
|
| 93 |
if category == CategoryShortcode { |
| 94 |
if (this.IsPlainText == other.IsPlainText || !other.IsPlainText) || this.AlwaysAllowPlainText { |
| 95 |
w.w1 = 1 |
| 96 |
} |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
return w |
| 101 |
} |
| 102 |
|
| 103 |
//lint:ignore ST1006 this vs other makes it easier to reason about. |
| 104 |
func (this TemplateDescriptor) doCompare(category Category, other TemplateDescriptor, sitesMatrixThis, sitesMatrixOther sitesmatrix.VectorProvider) weight { |
| 105 |
w := weightNoMatch |
| 106 |
|
| 107 |
if !this.AlwaysAllowPlainText { |
| 108 |
// HTML in plain text is OK, but not the other way around. |
| 109 |
if other.IsPlainText && !this.IsPlainText { |
| 110 |
return w |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
if other.Kind != "" && other.Kind != this.Kind { |
| 115 |
return w |
| 116 |
} |
| 117 |
|
| 118 |
if other.LayoutFromTemplate != "" && other.LayoutFromTemplate != layoutAll { |
| 119 |
if this.LayoutFromUser == "" || this.LayoutFromUser != other.LayoutFromTemplate { |
| 120 |
if other.LayoutFromTemplate != this.LayoutFromTemplate { |
| 121 |
return w |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if sitesMatrixOther != nil { |
| 127 |
// sitesMatrixThis is usually a single Site. |
| 128 |
// But we also use this method to find all base template variants for a given template, |
| 129 |
// and in that case we may get multiple vectors (e.g. multiple languages). |
| 130 |
if sitesMatrixThis == nil || !sitesMatrixOther.HasAnyVector(sitesMatrixThis) { |
| 131 |
return w |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
if other.OutputFormat != "" && other.OutputFormat != this.OutputFormat { |
| 136 |
if this.MediaType != other.MediaType { |
| 137 |
return w |
| 138 |
} |
| 139 |
|
| 140 |
// We want e.g. home page in amp output format (media type text/html) to |
| 141 |
// find a template even if one isn't specified for that output format, |
| 142 |
// when one exist for the html output format (same media type). |
| 143 |
skip := category != CategoryBaseof && (this.Kind == "" || (this.Kind != other.Kind && (this.LayoutFromTemplate != other.LayoutFromTemplate && other.LayoutFromTemplate != layoutAll))) |
| 144 |
if this.LayoutFromUser != "" { |
| 145 |
skip = skip && this.LayoutFromUser != other.LayoutFromTemplate |
| 146 |
} |
| 147 |
if skip { |
| 148 |
return w |
| 149 |
} |
| 150 |
|
| 151 |
// Continue. |
| 152 |
} |
| 153 |
|
| 154 |
if other.MediaType != this.MediaType { |
| 155 |
return w |
| 156 |
} |
| 157 |
|
| 158 |
// One example of variant1 and 2 is for render codeblocks: |
| 159 |
// variant1=codeblock, variant2=go (language). |
| 160 |
if other.Variant1 != "" { |
| 161 |
if other.Variant1 != this.Variant1 { |
| 162 |
return w |
| 163 |
} |
| 164 |
|
| 165 |
if other.Variant2 != "" && other.Variant2 != this.Variant2 { |
| 166 |
return w |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
const ( |
| 171 |
weightKind = 5 // page, home, section, taxonomy, term (and only those) |
| 172 |
weightcustomLayout = 6 // custom layout (mylayout, set in e.g. front matter) |
| 173 |
weightLayoutStandard = 4 // standard layouts (single,list) |
| 174 |
weightLayoutAll = 2 // the "all" layout |
| 175 |
weightOutputFormat = 4 // a configured output format (e.g. rss, html, json) |
| 176 |
weightMediaType = 1 // a configured media type (e.g. text/html, text/plain) |
| 177 |
weightSitesMatrix = 1 // a configured language (e.g. en, nn, fr, ...) |
| 178 |
weightVariant1 = 6 // currently used for render hooks, e.g. "link", "image" |
| 179 |
weightVariant2 = 4 // currently used for render hooks, e.g. the language "go" in code blocks. |
| 180 |
|
| 181 |
// We will use the values for group 2 and 3 |
| 182 |
// if the distance up to the template is shorter than |
| 183 |
// the one we're comparing with. |
| 184 |
// E.g for a page in /posts/mypage.md with the |
| 185 |
// two templates /layouts/posts/single.html and /layouts/page.html, |
| 186 |
// the first one is the best match even if the second one |
| 187 |
// has a higher w1 value. |
| 188 |
weight2Group1 = 1 // kind, standardl layout (single,list,all) |
| 189 |
weight2Group2 = 2 // custom layout (mylayout) |
| 190 |
|
| 191 |
weight3 = 1 // for media type, output format, and dimensions (if not provided). |
| 192 |
) |
| 193 |
|
| 194 |
// Now we now know that the other descriptor is a subset of this. |
| 195 |
// Now calculate the weights. |
| 196 |
w.w1++ |
| 197 |
|
| 198 |
if other.Kind != "" && other.Kind == this.Kind { |
| 199 |
w.w1 += weightKind |
| 200 |
w.w2 = weight2Group1 |
| 201 |
} |
| 202 |
|
| 203 |
if other.LayoutFromTemplate != "" && (other.LayoutFromTemplate == this.LayoutFromTemplate) { |
| 204 |
w.w1 += weightLayoutStandard |
| 205 |
w.w2 = weight2Group1 |
| 206 |
} else if other.LayoutFromTemplate == layoutAll { |
| 207 |
w.w1 += weightLayoutAll |
| 208 |
w.w2 = weight2Group1 |
| 209 |
} |
| 210 |
|
| 211 |
// LayoutCustom is only set in this (usually from Page.Layout). |
| 212 |
if this.LayoutFromUser != "" && this.LayoutFromUser == other.LayoutFromTemplate { |
| 213 |
w.w1 += weightcustomLayout |
| 214 |
w.w2 = weight2Group2 |
| 215 |
} |
| 216 |
|
| 217 |
if sitesMatrixOther != nil { |
| 218 |
// sitesMatrixThis is usually a single Site. |
| 219 |
if sitesMatrixThis != nil && sitesMatrixOther.HasAnyVector(sitesMatrixThis) { |
| 220 |
w.w1 += weightSitesMatrix |
| 221 |
if wp, ok := sitesMatrixOther.(types.WeightProvider); ok { |
| 222 |
w.wsm = wp.Weight() |
| 223 |
} else { |
| 224 |
w.wsm = weight3 |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
if other.OutputFormat != "" && other.OutputFormat == this.OutputFormat { |
| 230 |
w.w1 += weightOutputFormat |
| 231 |
w.w3 += weight3 |
| 232 |
} |
| 233 |
|
| 234 |
if other.MediaType != "" && other.MediaType == this.MediaType { |
| 235 |
w.w1 += weightMediaType |
| 236 |
w.w3 += weight3 |
| 237 |
} |
| 238 |
|
| 239 |
if other.Variant1 != "" && other.Variant1 == this.Variant1 { |
| 240 |
w.w1 += weightVariant1 |
| 241 |
} |
| 242 |
|
| 243 |
if other.Variant1 != "" && other.Variant2 == this.Variant2 { |
| 244 |
w.w1 += weightVariant2 |
| 245 |
} |
| 246 |
|
| 247 |
return w |
| 248 |
} |
| 249 |
|
| 250 |
func (d TemplateDescriptor) IsZero() bool { |
| 251 |
return d == TemplateDescriptor{} |
| 252 |
} |
| 253 |
|
| 254 |
//lint:ignore ST1006 this vs other makes it easier to reason about. |
| 255 |
func (this TemplateDescriptor) isKindInLayout(layout string) bool { |
| 256 |
if this.Kind == "" { |
| 257 |
return true |
| 258 |
} |
| 259 |
if this.Kind != kinds.KindPage { |
| 260 |
return layout != layoutSingle |
| 261 |
} |
| 262 |
return layout != layoutList |
| 263 |
} |