| 1 |
// Copyright 2024 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 paths |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"path" |
| 19 |
"path/filepath" |
| 20 |
"runtime" |
| 21 |
"strings" |
| 22 |
"sync" |
| 23 |
|
| 24 |
"github.com/gohugoio/hugo/common/hmaps" |
| 25 |
"github.com/gohugoio/hugo/common/types" |
| 26 |
"github.com/gohugoio/hugo/hugofs/files" |
| 27 |
"github.com/gohugoio/hugo/hugolib/sitesmatrix" |
| 28 |
"github.com/gohugoio/hugo/resources/kinds" |
| 29 |
) |
| 30 |
|
| 31 |
const ( |
| 32 |
identifierBaseof = "baseof" |
| 33 |
identifierCustomWrapper = "_" |
| 34 |
) |
| 35 |
|
| 36 |
// Known prefixes for ._prefix_value_. identifiers. |
| 37 |
const ( |
| 38 |
prefixLanguage = "language_" |
| 39 |
prefixVersion = "version_" |
| 40 |
prefixRole = "role_" |
| 41 |
prefixOutputFormat = "outputformat_" |
| 42 |
prefixKind = "kind_" |
| 43 |
prefixLayout = "layout_" |
| 44 |
) |
| 45 |
|
| 46 |
// isCustomWrapperIdentifier tells whether a supplied path is of the form _xyz_. |
| 47 |
// must have non-empty content between the identifierCustomWrapper's to pass. |
| 48 |
func isCustomWrapperIdentifier(s string) bool { |
| 49 |
return len(s) > 2*len(identifierCustomWrapper) && |
| 50 |
strings.HasPrefix(s, identifierCustomWrapper) && |
| 51 |
strings.HasSuffix(s, identifierCustomWrapper) |
| 52 |
} |
| 53 |
|
| 54 |
// parsePrefixIdentifier parses inner content of a wrapper block (with outer underscores stripped) |
| 55 |
// and returns the prefix. The prefix match is case-insensitive. |
| 56 |
// Returns empty string if not a known prefix. |
| 57 |
func parsePrefixIdentifier(inner string) string { |
| 58 |
innerLower := strings.ToLower(inner) |
| 59 |
for _, p := range []string{prefixLanguage, prefixVersion, prefixRole, prefixOutputFormat, prefixKind, prefixLayout} { |
| 60 |
if strings.HasPrefix(innerLower, p) { |
| 61 |
return p |
| 62 |
} |
| 63 |
} |
| 64 |
return "" |
| 65 |
} |
| 66 |
|
| 67 |
// findWrapperDotPositions returns positions of dots inside ._..._. wrapper blocks. |
| 68 |
// These dots should be skipped during the main backward dot-scanning loop, |
| 69 |
// so that wrapper blocks are treated as single opaque segments. |
| 70 |
func findWrapperDotPositions(s string) []int { |
| 71 |
lastSlash := strings.LastIndex(s, "/") |
| 72 |
if lastSlash == -1 { |
| 73 |
return nil |
| 74 |
} |
| 75 |
|
| 76 |
var skipDots []int |
| 77 |
i := lastSlash |
| 78 |
for i < len(s) { |
| 79 |
// Look for ._ which could start a wrapper block. |
| 80 |
if i+2 < len(s) && s[i] == '.' && s[i+1] == '_' { |
| 81 |
// Find the closing _. or _ at end of string. |
| 82 |
end := -1 |
| 83 |
for j := i + 2; j < len(s); j++ { |
| 84 |
if s[j] == '/' { |
| 85 |
break |
| 86 |
} |
| 87 |
if s[j] == '_' && (j+1 >= len(s) || s[j+1] == '.') { |
| 88 |
end = j |
| 89 |
break |
| 90 |
} |
| 91 |
} |
| 92 |
if end != -1 { |
| 93 |
// Record dot positions inside the wrapper block. |
| 94 |
for j := i + 2; j < end; j++ { |
| 95 |
if s[j] == '.' { |
| 96 |
skipDots = append(skipDots, j) |
| 97 |
} |
| 98 |
} |
| 99 |
i = end + 1 |
| 100 |
continue |
| 101 |
} |
| 102 |
} |
| 103 |
i++ |
| 104 |
} |
| 105 |
return skipDots |
| 106 |
} |
| 107 |
|
| 108 |
// isSkippedDot reports whether position i is in the sorted skipDots slice. |
| 109 |
func isSkippedDot(skipDots []int, i int) bool { |
| 110 |
for _, d := range skipDots { |
| 111 |
if d == i { |
| 112 |
return true |
| 113 |
} |
| 114 |
if d > i { |
| 115 |
break |
| 116 |
} |
| 117 |
} |
| 118 |
return false |
| 119 |
} |
| 120 |
|
| 121 |
// applyPrefixIdentifier validates and stores a prefix identifier. |
| 122 |
// id is the value-only LowHigh (e.g. pointing at "v1.0.0" in p.s, not the full wrapper). |
| 123 |
// Returns true if the identifier was recognized and applied. |
| 124 |
func (pp *PathParser) applyPrefixIdentifier(component, prefix string, p *Path, id types.LowHigh[string]) bool { |
| 125 |
value := strings.ToLower(p.s[id.Low:id.High]) |
| 126 |
switch prefix { |
| 127 |
case prefixLanguage: |
| 128 |
if pp.LanguageIndex != nil { |
| 129 |
if _, ok := pp.LanguageIndex[value]; ok { |
| 130 |
p.identifiersKnown = append(p.identifiersKnown, id) |
| 131 |
p.posIdentifierPrefixLanguages = append(p.posIdentifierPrefixLanguages, len(p.identifiersKnown)-1) |
| 132 |
return true |
| 133 |
} |
| 134 |
if pp.IsLangDisabled != nil && pp.IsLangDisabled(value) { |
| 135 |
p.identifiersKnown = append(p.identifiersKnown, id) |
| 136 |
p.posIdentifierPrefixLanguages = append(p.posIdentifierPrefixLanguages, len(p.identifiersKnown)-1) |
| 137 |
p.disabled = true |
| 138 |
return true |
| 139 |
} |
| 140 |
} |
| 141 |
case prefixVersion: |
| 142 |
if pp.ConfiguredDimensions != nil { |
| 143 |
if idx := pp.ConfiguredDimensions.ConfiguredVersions.ResolveIndex(value); idx >= 0 { |
| 144 |
p.identifiersKnown = append(p.identifiersKnown, id) |
| 145 |
p.posIdentifierVersions = append(p.posIdentifierVersions, len(p.identifiersKnown)-1) |
| 146 |
return true |
| 147 |
} |
| 148 |
} |
| 149 |
case prefixRole: |
| 150 |
if pp.ConfiguredDimensions != nil { |
| 151 |
if idx := pp.ConfiguredDimensions.ConfiguredRoles.ResolveIndex(value); idx >= 0 { |
| 152 |
p.identifiersKnown = append(p.identifiersKnown, id) |
| 153 |
p.posIdentifierRoles = append(p.posIdentifierRoles, len(p.identifiersKnown)-1) |
| 154 |
return true |
| 155 |
} |
| 156 |
} |
| 157 |
case prefixOutputFormat: |
| 158 |
if component == files.ComponentFolderLayouts && pp.IsOutputFormat != nil { |
| 159 |
if pp.IsOutputFormat(value, "") { |
| 160 |
p.identifiersKnown = append(p.identifiersKnown, id) |
| 161 |
p.posIdentifierPrefixOutputFormat = len(p.identifiersKnown) - 1 |
| 162 |
return true |
| 163 |
} |
| 164 |
} |
| 165 |
case prefixKind: |
| 166 |
if component == files.ComponentFolderLayouts { |
| 167 |
if kinds.GetKindMain(value) != "" { |
| 168 |
p.identifiersKnown = append(p.identifiersKnown, id) |
| 169 |
p.posIdentifierPrefixKind = len(p.identifiersKnown) - 1 |
| 170 |
return true |
| 171 |
} |
| 172 |
} |
| 173 |
case prefixLayout: |
| 174 |
if component == files.ComponentFolderLayouts { |
| 175 |
p.identifiersKnown = append(p.identifiersKnown, id) |
| 176 |
p.posIdentifierPrefixLayout = len(p.identifiersKnown) - 1 |
| 177 |
return true |
| 178 |
} |
| 179 |
} |
| 180 |
return false |
| 181 |
} |
| 182 |
|
| 183 |
// PathParser parses and manages paths. |
| 184 |
type PathParser struct { |
| 185 |
// Maps the language code to its index in the languages/sites slice. |
| 186 |
LanguageIndex map[string]int |
| 187 |
|
| 188 |
// Reports whether the given language is disabled. |
| 189 |
IsLangDisabled func(string) bool |
| 190 |
|
| 191 |
// IsOutputFormat reports whether the given name is a valid output format. |
| 192 |
// The second argument is optional. |
| 193 |
IsOutputFormat func(name, ext string) bool |
| 194 |
|
| 195 |
// Reports whether the given ext is a content file. |
| 196 |
IsContentExt func(string) bool |
| 197 |
|
| 198 |
// The configured sites matrix. |
| 199 |
ConfiguredDimensions *sitesmatrix.ConfiguredDimensions |
| 200 |
|
| 201 |
// Below gets created on demand. |
| 202 |
initOnce sync.Once |
| 203 |
sitesMatrixCache *hmaps.Cache[string, sitesmatrix.VectorStore] // Maps language index to sites matrix vector store. |
| 204 |
} |
| 205 |
|
| 206 |
func (pp *PathParser) init() { |
| 207 |
pp.initOnce.Do(func() { |
| 208 |
pp.sitesMatrixCache = hmaps.NewCache[string, sitesmatrix.VectorStore]() |
| 209 |
}) |
| 210 |
} |
| 211 |
|
| 212 |
// NormalizePathString returns a normalized path string using the very basic Hugo rules. |
| 213 |
func NormalizePathStringBasic(s string) string { |
| 214 |
// All lower case. |
| 215 |
s = strings.ToLower(s) |
| 216 |
|
| 217 |
// Replace spaces with hyphens. |
| 218 |
s = strings.ReplaceAll(s, " ", "-") |
| 219 |
|
| 220 |
return s |
| 221 |
} |
| 222 |
|
| 223 |
func (pp *PathParser) SitesMatrixFromPath(p *Path) sitesmatrix.VectorStore { |
| 224 |
pp.init() |
| 225 |
langs := p.Langs() |
| 226 |
versions := p.Versions() |
| 227 |
roles := p.Roles() |
| 228 |
lang := p.Lang() // First or dot-based language. |
| 229 |
// Cache by the full site-selection identity derived from the path: |
| 230 |
// languages, selected language, versions, and roles. |
| 231 |
cacheKey := strings.Join(langs, ",") + "/" + lang + "/" + strings.Join(versions, ",") + "|" + strings.Join(roles, ",") |
| 232 |
v, _ := pp.sitesMatrixCache.GetOrCreate(cacheKey, func() (sitesmatrix.VectorStore, error) { |
| 233 |
builder := sitesmatrix.NewIntSetsBuilder(pp.ConfiguredDimensions) |
| 234 |
if len(langs) > 0 { |
| 235 |
for _, l := range langs { |
| 236 |
if idx, ok := pp.LanguageIndex[l]; ok { |
| 237 |
builder.WithLanguageIndices(idx) |
| 238 |
} |
| 239 |
} |
| 240 |
} else if lang != "" { |
| 241 |
if idx, ok := pp.LanguageIndex[lang]; ok { |
| 242 |
builder.WithLanguageIndices(idx) |
| 243 |
} |
| 244 |
} |
| 245 |
for _, version := range versions { |
| 246 |
if idx := pp.ConfiguredDimensions.ConfiguredVersions.ResolveIndex(version); idx >= 0 { |
| 247 |
builder.WithVersionIndices(idx) |
| 248 |
} |
| 249 |
} |
| 250 |
for _, role := range roles { |
| 251 |
if idx := pp.ConfiguredDimensions.ConfiguredRoles.ResolveIndex(role); idx >= 0 { |
| 252 |
builder.WithRoleIndices(idx) |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
switch p.Component() { |
| 257 |
case files.ComponentFolderContent: |
| 258 |
builder.WithDefaultsIfNotSet() |
| 259 |
case files.ComponentFolderLayouts: |
| 260 |
builder.WithAllIfNotSet() |
| 261 |
case files.ComponentFolderStatic: |
| 262 |
builder.WithDefaultsAndAllLanguagesIfNotSet() |
| 263 |
} |
| 264 |
|
| 265 |
return builder.Build(), nil |
| 266 |
}) |
| 267 |
|
| 268 |
return v |
| 269 |
} |
| 270 |
|
| 271 |
// ParseBaseAndBaseNameNoIdentifier parses component c with path s into a base and a base name without any identifier. |
| 272 |
func (pp *PathParser) ParseBaseAndBaseNameNoIdentifier(c, s string) (string, string) { |
| 273 |
p := pp.parsePooled(c, s) |
| 274 |
defer putPath(p) |
| 275 |
return p.Base(), p.BaseNameNoIdentifier() |
| 276 |
} |
| 277 |
|
| 278 |
func (pp *PathParser) parsePooled(c, s string) *Path { |
| 279 |
s = NormalizePathStringBasic(s) |
| 280 |
p := getPath() |
| 281 |
p.component = c |
| 282 |
p, err := pp.doParse(c, s, p) |
| 283 |
if err != nil { |
| 284 |
panic(err) |
| 285 |
} |
| 286 |
return p |
| 287 |
} |
| 288 |
|
| 289 |
// Parse parses component c with path s into Path using Hugo's content path rules. |
| 290 |
func (pp *PathParser) Parse(c, s string) *Path { |
| 291 |
p, err := pp.parse(c, s) |
| 292 |
if err != nil { |
| 293 |
panic(err) |
| 294 |
} |
| 295 |
return p |
| 296 |
} |
| 297 |
|
| 298 |
func (pp *PathParser) newPath(component string) *Path { |
| 299 |
p := &Path{} |
| 300 |
p.reset() |
| 301 |
p.component = component |
| 302 |
return p |
| 303 |
} |
| 304 |
|
| 305 |
func (pp *PathParser) parse(component, s string) (*Path, error) { |
| 306 |
ss := NormalizePathStringBasic(s) |
| 307 |
|
| 308 |
p, err := pp.doParse(component, ss, pp.newPath(component)) |
| 309 |
if err != nil { |
| 310 |
return nil, err |
| 311 |
} |
| 312 |
|
| 313 |
if s != ss { |
| 314 |
var err error |
| 315 |
// Preserve the original case for titles etc. |
| 316 |
p.unnormalized, err = pp.doParse(component, s, pp.newPath(component)) |
| 317 |
if err != nil { |
| 318 |
return nil, err |
| 319 |
} |
| 320 |
} else { |
| 321 |
p.unnormalized = p |
| 322 |
} |
| 323 |
|
| 324 |
return p, nil |
| 325 |
} |
| 326 |
|
| 327 |
func (pp *PathParser) parseIdentifier(component, s string, p *Path, i, lastDot, numDots int, isLast bool) { |
| 328 |
if p.posContainerHigh != -1 { |
| 329 |
return |
| 330 |
} |
| 331 |
mayHaveLang := numDots > 1 && p.posIdentifierLanguage == -1 && pp.LanguageIndex != nil |
| 332 |
mayHaveLang = mayHaveLang && (component == files.ComponentFolderContent || component == files.ComponentFolderLayouts) |
| 333 |
mayHaveOutputFormat := component == files.ComponentFolderLayouts |
| 334 |
mayHaveKind := p.posIdentifierKind == -1 && mayHaveOutputFormat |
| 335 |
var mayHaveLayout bool |
| 336 |
if p.pathType == TypeShortcode { |
| 337 |
mayHaveLayout = !isLast && component == files.ComponentFolderLayouts |
| 338 |
} else { |
| 339 |
mayHaveLayout = component == files.ComponentFolderLayouts |
| 340 |
} |
| 341 |
|
| 342 |
var found bool |
| 343 |
var high int |
| 344 |
if len(p.identifiersKnown) > 0 { |
| 345 |
high = lastDot |
| 346 |
} else { |
| 347 |
high = len(p.s) |
| 348 |
} |
| 349 |
id := types.LowHigh[string]{Low: i + 1, High: high} |
| 350 |
sid := p.s[id.Low:id.High] |
| 351 |
|
| 352 |
if isCustomWrapperIdentifier(sid) { |
| 353 |
inner := sid[1 : len(sid)-1] |
| 354 |
if prefix := parsePrefixIdentifier(inner); prefix != "" { |
| 355 |
// Value-only LowHigh: skip leading _ + prefix, trailing _ |
| 356 |
valueID := types.LowHigh[string]{Low: id.Low + 1 + len(prefix), High: id.High - 1} |
| 357 |
if pp.applyPrefixIdentifier(component, prefix, p, valueID) { |
| 358 |
found = true |
| 359 |
} |
| 360 |
} |
| 361 |
if !found { |
| 362 |
p.identifiersKnown = append(p.identifiersKnown, id) |
| 363 |
p.posIdentifierCustom = len(p.identifiersKnown) - 1 |
| 364 |
found = true |
| 365 |
} |
| 366 |
} |
| 367 |
|
| 368 |
if found { |
| 369 |
// Already handled (e.g. prefix wrapper). |
| 370 |
} else if len(p.identifiersKnown) == 0 { |
| 371 |
// The first is always the extension. |
| 372 |
p.identifiersKnown = append(p.identifiersKnown, id) |
| 373 |
found = true |
| 374 |
|
| 375 |
// May also be the output format. |
| 376 |
if mayHaveOutputFormat && pp.IsOutputFormat(sid, "") { |
| 377 |
p.posIdentifierOutputFormat = 0 |
| 378 |
} |
| 379 |
} else { |
| 380 |
var langFound bool |
| 381 |
|
| 382 |
if mayHaveLang { |
| 383 |
var disabled bool |
| 384 |
_, langFound = pp.LanguageIndex[sid] |
| 385 |
|
| 386 |
if !langFound { |
| 387 |
disabled = pp.IsLangDisabled != nil && pp.IsLangDisabled(sid) |
| 388 |
if disabled { |
| 389 |
p.disabled = true |
| 390 |
langFound = true |
| 391 |
} |
| 392 |
} |
| 393 |
found = langFound |
| 394 |
if langFound { |
| 395 |
p.identifiersKnown = append(p.identifiersKnown, id) |
| 396 |
p.posIdentifierLanguage = len(p.identifiersKnown) - 1 |
| 397 |
} |
| 398 |
|
| 399 |
} |
| 400 |
|
| 401 |
if !found && mayHaveOutputFormat { |
| 402 |
// At this point we may already have resolved an output format, |
| 403 |
// but we need to keep looking for a more specific one, e.g. amp before html. |
| 404 |
// Use both name and extension to prevent |
| 405 |
// false positives on the form css.html. |
| 406 |
if pp.IsOutputFormat(sid, p.Ext()) { |
| 407 |
found = true |
| 408 |
p.identifiersKnown = append(p.identifiersKnown, id) |
| 409 |
p.posIdentifierOutputFormat = len(p.identifiersKnown) - 1 |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
if !found && mayHaveKind { |
| 414 |
if kinds.GetKindMain(sid) != "" { |
| 415 |
found = true |
| 416 |
p.identifiersKnown = append(p.identifiersKnown, id) |
| 417 |
p.posIdentifierKind = len(p.identifiersKnown) - 1 |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
if !found && sid == identifierBaseof { |
| 422 |
found = true |
| 423 |
p.identifiersKnown = append(p.identifiersKnown, id) |
| 424 |
p.posIdentifierBaseof = len(p.identifiersKnown) - 1 |
| 425 |
} |
| 426 |
|
| 427 |
if !found && mayHaveLayout { |
| 428 |
if p.posIdentifierLayout != -1 { |
| 429 |
// Move it to identifiersUnknown. |
| 430 |
p.identifiersUnknown = append(p.identifiersUnknown, p.identifiersKnown[p.posIdentifierLayout]) |
| 431 |
p.identifiersKnown[p.posIdentifierLayout] = id |
| 432 |
} else { |
| 433 |
p.identifiersKnown = append(p.identifiersKnown, id) |
| 434 |
p.posIdentifierLayout = len(p.identifiersKnown) - 1 |
| 435 |
} |
| 436 |
found = true |
| 437 |
} |
| 438 |
|
| 439 |
if !found { |
| 440 |
p.identifiersUnknown = append(p.identifiersUnknown, id) |
| 441 |
} |
| 442 |
|
| 443 |
} |
| 444 |
|
| 445 |
if found { |
| 446 |
if isLast { |
| 447 |
// The isLast identifier starts right at the container boundary. |
| 448 |
// Treat it as part of the name (e.g. layout name "list" in list.no.html). |
| 449 |
if p.posNameHigh <= 0 { |
| 450 |
p.posNameHigh = lastDot |
| 451 |
} |
| 452 |
} else { |
| 453 |
p.posNameHigh = i // The '.' before this identifier. |
| 454 |
} |
| 455 |
} |
| 456 |
} |
| 457 |
|
| 458 |
func (pp *PathParser) doParse(component, s string, p *Path) (*Path, error) { |
| 459 |
if runtime.GOOS == "windows" { |
| 460 |
s = path.Clean(filepath.ToSlash(s)) |
| 461 |
if s == "." { |
| 462 |
s = "" |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
if s == "" { |
| 467 |
s = "/" |
| 468 |
} |
| 469 |
|
| 470 |
// Leading slash, no trailing slash. |
| 471 |
if !strings.HasPrefix(s, "/") { |
| 472 |
s = "/" + s |
| 473 |
} |
| 474 |
|
| 475 |
if s != "/" && s[len(s)-1] == '/' { |
| 476 |
s = s[:len(s)-1] |
| 477 |
} |
| 478 |
|
| 479 |
p.s = s |
| 480 |
|
| 481 |
// Find dots inside ._..._. wrapper blocks that must be skipped. |
| 482 |
skipDots := findWrapperDotPositions(s) |
| 483 |
|
| 484 |
slashCount := 0 |
| 485 |
lastDot := 0 |
| 486 |
lastSlashIdx := strings.LastIndex(s, "/") |
| 487 |
numDots := strings.Count(s[lastSlashIdx+1:], ".") - len(skipDots) |
| 488 |
if strings.Contains(s, "/_shortcodes/") { |
| 489 |
p.pathType = TypeShortcode |
| 490 |
} |
| 491 |
|
| 492 |
for i := len(s) - 1; i >= 0; i-- { |
| 493 |
c := s[i] |
| 494 |
|
| 495 |
switch c { |
| 496 |
case '.': |
| 497 |
if isSkippedDot(skipDots, i) { |
| 498 |
continue |
| 499 |
} |
| 500 |
pp.parseIdentifier(component, s, p, i, lastDot, numDots, false) |
| 501 |
lastDot = i |
| 502 |
case '/': |
| 503 |
slashCount++ |
| 504 |
if p.posContainerHigh == -1 { |
| 505 |
if lastDot > 0 { |
| 506 |
pp.parseIdentifier(component, s, p, i, lastDot, numDots, true) |
| 507 |
} |
| 508 |
p.posContainerHigh = i + 1 |
| 509 |
} else if p.posContainerLow == -1 { |
| 510 |
p.posContainerLow = i + 1 |
| 511 |
} |
| 512 |
if i > 0 { |
| 513 |
p.posSectionHigh = i |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
// Compute the name boundary. |
| 519 |
if p.posNameHigh >= p.posContainerHigh { |
| 520 |
p.posIdentifierName = types.LowHigh[string]{Low: p.posContainerHigh, High: p.posNameHigh} |
| 521 |
} else { |
| 522 |
p.posIdentifierName = types.LowHigh[string]{Low: p.posContainerHigh, High: len(p.s)} |
| 523 |
} |
| 524 |
|
| 525 |
if len(p.identifiersKnown) > 0 { |
| 526 |
isContentComponent := p.component == files.ComponentFolderContent || p.component == files.ComponentFolderArchetypes |
| 527 |
isContent := isContentComponent && pp.IsContentExt(p.Ext()) |
| 528 |
|
| 529 |
if p.posIdentifierName.Low >= p.posContainerHigh && p.posIdentifierName.High > p.posIdentifierName.Low { |
| 530 |
b := p.s[p.posIdentifierName.Low:p.posIdentifierName.High] |
| 531 |
if isContent { |
| 532 |
switch b { |
| 533 |
case "index": |
| 534 |
p.pathType = TypeLeaf |
| 535 |
case "_index": |
| 536 |
p.pathType = TypeBranch |
| 537 |
default: |
| 538 |
p.pathType = TypeContentSingle |
| 539 |
} |
| 540 |
|
| 541 |
if slashCount == 2 && p.IsLeafBundle() { |
| 542 |
p.posSectionHigh = 0 |
| 543 |
} |
| 544 |
} else if b == files.NameContentData && files.IsContentDataExt(p.Ext()) { |
| 545 |
p.pathType = TypeContentData |
| 546 |
} |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
if p.pathType < TypeMarkup && component == files.ComponentFolderLayouts { |
| 551 |
if p.posIdentifierBaseof != -1 { |
| 552 |
p.pathType = TypeBaseof |
| 553 |
} else { |
| 554 |
pth := p.Path() |
| 555 |
if strings.Contains(pth, "/_shortcodes/") { |
| 556 |
p.pathType = TypeShortcode |
| 557 |
} else if strings.Contains(pth, "/_markup/") { |
| 558 |
p.pathType = TypeMarkup |
| 559 |
} else if strings.HasPrefix(pth, "/_partials/") { |
| 560 |
p.pathType = TypePartial |
| 561 |
} |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
if p.pathType == TypeShortcode && p.posIdentifierLayout != -1 { |
| 566 |
id := p.identifiersKnown[p.posIdentifierLayout] |
| 567 |
if id.Low == p.posContainerHigh { |
| 568 |
// First identifier is shortcode name. |
| 569 |
p.posIdentifierLayout = -1 |
| 570 |
} |
| 571 |
} |
| 572 |
|
| 573 |
return p, nil |
| 574 |
} |
| 575 |
|
| 576 |
func ModifyPathBundleTypeResource(p *Path) { |
| 577 |
if p.IsContent() { |
| 578 |
p.pathType = TypeContentResource |
| 579 |
} else { |
| 580 |
p.pathType = TypeFile |
| 581 |
} |
| 582 |
} |
| 583 |
|
| 584 |
//go:generate stringer -type Type |
| 585 |
|
| 586 |
type Type int |
| 587 |
|
| 588 |
const ( |
| 589 |
|
| 590 |
// A generic file, e.g. a JSON file. |
| 591 |
TypeFile Type = iota |
| 592 |
|
| 593 |
// All below are content files. |
| 594 |
// A resource of a content type with front matter. |
| 595 |
TypeContentResource |
| 596 |
|
| 597 |
// E.g. /blog/my-post.md |
| 598 |
TypeContentSingle |
| 599 |
|
| 600 |
// All below are bundled content files. |
| 601 |
|
| 602 |
// Leaf bundles, e.g. /blog/my-post/index.md |
| 603 |
TypeLeaf |
| 604 |
|
| 605 |
// Branch bundles, e.g. /blog/_index.md |
| 606 |
TypeBranch |
| 607 |
|
| 608 |
// Content data file, _content.gotmpl. |
| 609 |
TypeContentData |
| 610 |
|
| 611 |
// Layout types. |
| 612 |
TypeMarkup |
| 613 |
TypeShortcode |
| 614 |
TypePartial |
| 615 |
TypeBaseof |
| 616 |
) |
| 617 |
|
| 618 |
type Path struct { |
| 619 |
// Note: Any additions to this struct should also be added to the pathPool. |
| 620 |
s string |
| 621 |
|
| 622 |
posContainerLow int |
| 623 |
posContainerHigh int |
| 624 |
posSectionHigh int |
| 625 |
|
| 626 |
component string |
| 627 |
pathType Type |
| 628 |
|
| 629 |
identifiersKnown []types.LowHigh[string] |
| 630 |
identifiersUnknown []types.LowHigh[string] |
| 631 |
|
| 632 |
posIdentifierLanguage int |
| 633 |
posIdentifierOutputFormat int |
| 634 |
posIdentifierKind int |
| 635 |
posIdentifierLayout int |
| 636 |
posIdentifierBaseof int |
| 637 |
posIdentifierCustom int |
| 638 |
|
| 639 |
// Prefix identifier positions (indices into identifiersKnown). |
| 640 |
posIdentifierPrefixLanguages []int |
| 641 |
posIdentifierVersions []int |
| 642 |
posIdentifierRoles []int |
| 643 |
posIdentifierPrefixOutputFormat int |
| 644 |
posIdentifierPrefixKind int |
| 645 |
posIdentifierPrefixLayout int |
| 646 |
|
| 647 |
// Name boundary, computed during parse. |
| 648 |
posIdentifierName types.LowHigh[string] |
| 649 |
// Position of the dot before the leftmost known identifier. |
| 650 |
// Set during parseIdentifier, used to compute posIdentifierName. |
| 651 |
posNameHigh int |
| 652 |
|
| 653 |
disabled bool |
| 654 |
|
| 655 |
trimLeadingSlash bool |
| 656 |
|
| 657 |
unnormalized *Path |
| 658 |
} |
| 659 |
|
| 660 |
var pathPool = &sync.Pool{ |
| 661 |
New: func() any { |
| 662 |
p := &Path{} |
| 663 |
p.reset() |
| 664 |
return p |
| 665 |
}, |
| 666 |
} |
| 667 |
|
| 668 |
func getPath() *Path { |
| 669 |
return pathPool.Get().(*Path) |
| 670 |
} |
| 671 |
|
| 672 |
func putPath(p *Path) { |
| 673 |
p.reset() |
| 674 |
pathPool.Put(p) |
| 675 |
} |
| 676 |
|
| 677 |
func (p *Path) reset() { |
| 678 |
p.s = "" |
| 679 |
p.posContainerLow = -1 |
| 680 |
p.posContainerHigh = -1 |
| 681 |
p.posSectionHigh = -1 |
| 682 |
p.component = "" |
| 683 |
p.pathType = 0 |
| 684 |
p.identifiersKnown = p.identifiersKnown[:0] |
| 685 |
p.posIdentifierLanguage = -1 |
| 686 |
p.posIdentifierOutputFormat = -1 |
| 687 |
p.posIdentifierKind = -1 |
| 688 |
p.posIdentifierLayout = -1 |
| 689 |
p.posIdentifierBaseof = -1 |
| 690 |
p.posIdentifierCustom = -1 |
| 691 |
p.posIdentifierPrefixLanguages = p.posIdentifierPrefixLanguages[:0] |
| 692 |
p.posIdentifierVersions = p.posIdentifierVersions[:0] |
| 693 |
p.posIdentifierRoles = p.posIdentifierRoles[:0] |
| 694 |
p.posIdentifierPrefixOutputFormat = -1 |
| 695 |
p.posIdentifierPrefixKind = -1 |
| 696 |
p.posIdentifierPrefixLayout = -1 |
| 697 |
p.posIdentifierName = types.LowHigh[string]{} |
| 698 |
p.posNameHigh = -1 |
| 699 |
p.disabled = false |
| 700 |
p.trimLeadingSlash = false |
| 701 |
p.unnormalized = nil |
| 702 |
} |
| 703 |
|
| 704 |
// TrimLeadingSlash returns a copy of the Path with the leading slash removed. |
| 705 |
func (p Path) TrimLeadingSlash() *Path { |
| 706 |
p.trimLeadingSlash = true |
| 707 |
return &p |
| 708 |
} |
| 709 |
|
| 710 |
func (p *Path) norm(s string) string { |
| 711 |
if p.trimLeadingSlash { |
| 712 |
s = strings.TrimPrefix(s, "/") |
| 713 |
} |
| 714 |
return s |
| 715 |
} |
| 716 |
|
| 717 |
// IdentifierBase satisfies identity.Identity. |
| 718 |
func (p *Path) IdentifierBase() string { |
| 719 |
if p.Component() == files.ComponentFolderLayouts { |
| 720 |
return p.Path() |
| 721 |
} |
| 722 |
return p.Base() |
| 723 |
} |
| 724 |
|
| 725 |
// Component returns the component for this path (e.g. "content"). |
| 726 |
func (p *Path) Component() string { |
| 727 |
return p.component |
| 728 |
} |
| 729 |
|
| 730 |
// Container returns the base name of the container directory for this path. |
| 731 |
func (p *Path) Container() string { |
| 732 |
if p.posContainerLow == -1 { |
| 733 |
return "" |
| 734 |
} |
| 735 |
return p.norm(p.s[p.posContainerLow : p.posContainerHigh-1]) |
| 736 |
} |
| 737 |
|
| 738 |
func (p *Path) String() string { |
| 739 |
if p == nil { |
| 740 |
return "<nil>" |
| 741 |
} |
| 742 |
return p.Path() |
| 743 |
} |
| 744 |
|
| 745 |
// ContainerDir returns the container directory for this path. |
| 746 |
// For content bundles this will be the parent directory. |
| 747 |
func (p *Path) ContainerDir() string { |
| 748 |
if p.posContainerLow == -1 || !p.IsBundle() { |
| 749 |
return p.Dir() |
| 750 |
} |
| 751 |
return p.norm(p.s[:p.posContainerLow-1]) |
| 752 |
} |
| 753 |
|
| 754 |
// Section returns the first path element (section). |
| 755 |
func (p *Path) Section() string { |
| 756 |
if p.posSectionHigh <= 0 { |
| 757 |
return "" |
| 758 |
} |
| 759 |
return p.norm(p.s[1:p.posSectionHigh]) |
| 760 |
} |
| 761 |
|
| 762 |
// IsContent returns true if the path is a content file (e.g. mypost.md). |
| 763 |
// Note that this will also return true for content files in a bundle. |
| 764 |
func (p *Path) IsContent() bool { |
| 765 |
return p.Type() >= TypeContentResource && p.Type() <= TypeContentData |
| 766 |
} |
| 767 |
|
| 768 |
// isContentPage returns true if the path is a content file (e.g. mypost.md), |
| 769 |
// but nof if inside a leaf bundle. |
| 770 |
func (p *Path) isContentPage() bool { |
| 771 |
return p.Type() >= TypeContentSingle && p.Type() <= TypeContentData |
| 772 |
} |
| 773 |
|
| 774 |
// Name returns the last element of path. |
| 775 |
func (p *Path) Name() string { |
| 776 |
if p.posContainerHigh > 0 { |
| 777 |
return p.s[p.posContainerHigh:] |
| 778 |
} |
| 779 |
return p.s |
| 780 |
} |
| 781 |
|
| 782 |
// Name returns the last element of path without any extension. |
| 783 |
func (p *Path) NameNoExt() string { |
| 784 |
if i := p.identifierIndex(0); i != -1 { |
| 785 |
return p.s[p.posContainerHigh : p.identifiersKnown[i].Low-1] |
| 786 |
} |
| 787 |
return p.s[p.posContainerHigh:] |
| 788 |
} |
| 789 |
|
| 790 |
// BaseNameNoIdentifier returns the logical base name for a resource without any identifier (e.g. no extension). |
| 791 |
// For bundles this will be the containing directory's name, e.g. "blog". |
| 792 |
func (p *Path) BaseNameNoIdentifier() string { |
| 793 |
if p.IsBundle() { |
| 794 |
return p.Container() |
| 795 |
} |
| 796 |
return p.NameNoIdentifier() |
| 797 |
} |
| 798 |
|
| 799 |
// NameNoIdentifier returns the last element of path without any identifier (e.g. no extension). |
| 800 |
func (p *Path) NameNoIdentifier() string { |
| 801 |
lowHigh := p.nameLowHigh() |
| 802 |
return p.s[lowHigh.Low:lowHigh.High] |
| 803 |
} |
| 804 |
|
| 805 |
func (p *Path) nameLowHigh() types.LowHigh[string] { |
| 806 |
return p.posIdentifierName |
| 807 |
} |
| 808 |
|
| 809 |
// Dir returns all but the last element of path, typically the path's directory. |
| 810 |
func (p *Path) Dir() (d string) { |
| 811 |
if p.posContainerHigh > 0 { |
| 812 |
d = p.s[:p.posContainerHigh-1] |
| 813 |
} |
| 814 |
if d == "" { |
| 815 |
d = "/" |
| 816 |
} |
| 817 |
d = p.norm(d) |
| 818 |
return |
| 819 |
} |
| 820 |
|
| 821 |
// Path returns the full path. |
| 822 |
func (p *Path) Path() (d string) { |
| 823 |
return p.norm(p.s) |
| 824 |
} |
| 825 |
|
| 826 |
// PathNoLeadingSlash returns the full path without the leading slash. |
| 827 |
func (p *Path) PathNoLeadingSlash() string { |
| 828 |
return p.Path()[1:] |
| 829 |
} |
| 830 |
|
| 831 |
// Unnormalized returns the Path with the original case preserved. |
| 832 |
func (p *Path) Unnormalized() *Path { |
| 833 |
return p.unnormalized |
| 834 |
} |
| 835 |
|
| 836 |
// PathNoLang returns the Path but with any language identifier removed. |
| 837 |
func (p *Path) PathNoLang() string { |
| 838 |
if len(p.posIdentifierPrefixLanguages) > 0 { |
| 839 |
return p.base(true, false) |
| 840 |
} |
| 841 |
if p.identifierIndex(p.posIdentifierLanguage) == -1 { |
| 842 |
return p.Path() |
| 843 |
} |
| 844 |
return p.base(true, false) |
| 845 |
} |
| 846 |
|
| 847 |
// PathNoIdentifier returns the Path but with any identifier (ext, lang) removed. |
| 848 |
func (p *Path) PathNoIdentifier() string { |
| 849 |
return p.base(false, false) |
| 850 |
} |
| 851 |
|
| 852 |
// PathBeforeLangAndOutputFormatAndExt returns the path up to the first identifier that is not a language or output format. |
| 853 |
func (p *Path) PathBeforeLangAndOutputFormatAndExt() string { |
| 854 |
if len(p.identifiersKnown) == 0 { |
| 855 |
return p.norm(p.s) |
| 856 |
} |
| 857 |
i := p.identifierIndex(0) |
| 858 |
|
| 859 |
if j := p.posIdentifierOutputFormat; i == -1 || (j != -1 && j < i) { |
| 860 |
i = j |
| 861 |
} |
| 862 |
if j := p.posIdentifierLanguage; i == -1 || (j != -1 && j < i) { |
| 863 |
i = j |
| 864 |
} |
| 865 |
|
| 866 |
if i == -1 { |
| 867 |
return p.norm(p.s) |
| 868 |
} |
| 869 |
|
| 870 |
id := p.identifiersKnown[i] |
| 871 |
return p.norm(p.s[:id.Low-1]) |
| 872 |
} |
| 873 |
|
| 874 |
// PathRel returns the path relative to the given owner. |
| 875 |
func (p *Path) PathRel(owner *Path) string { |
| 876 |
ob := owner.Base() |
| 877 |
if !strings.HasSuffix(ob, "/") { |
| 878 |
ob += "/" |
| 879 |
} |
| 880 |
return strings.TrimPrefix(p.Path(), ob) |
| 881 |
} |
| 882 |
|
| 883 |
// BaseRel returns the base path relative to the given owner. |
| 884 |
func (p *Path) BaseRel(owner *Path) string { |
| 885 |
ob := owner.Base() |
| 886 |
if ob == "/" { |
| 887 |
ob = "" |
| 888 |
} |
| 889 |
return p.Base()[len(ob)+1:] |
| 890 |
} |
| 891 |
|
| 892 |
// For content files, Base returns the path without any identifiers (extension, language code etc.). |
| 893 |
// Any 'index' as the last path element is ignored. |
| 894 |
// |
| 895 |
// For other files (Resources), any extension is kept. |
| 896 |
func (p *Path) Base() string { |
| 897 |
s := p.base(!p.isContentPage(), p.IsBundle()) |
| 898 |
if s == "/" && p.isContentPage() { |
| 899 |
// The content home page is represented as "". |
| 900 |
s = "" |
| 901 |
} |
| 902 |
return s |
| 903 |
} |
| 904 |
|
| 905 |
// Used in template lookups. |
| 906 |
// For pages with Type set, we treat that as the section. |
| 907 |
func (p *Path) BaseReTyped(typ string) (d string) { |
| 908 |
base := p.Base() |
| 909 |
if typ == "" || p.Section() == typ { |
| 910 |
return base |
| 911 |
} |
| 912 |
d = "/" + typ |
| 913 |
if p.posSectionHigh != -1 { |
| 914 |
d += base[p.posSectionHigh:] |
| 915 |
} |
| 916 |
d = p.norm(d) |
| 917 |
return |
| 918 |
} |
| 919 |
|
| 920 |
func (p *Path) base(preserveExt, isBundle bool) string { |
| 921 |
if len(p.identifiersKnown) == 0 { |
| 922 |
return p.norm(p.s) |
| 923 |
} |
| 924 |
|
| 925 |
if preserveExt && len(p.identifiersKnown) == 1 { |
| 926 |
// Preserve extension. |
| 927 |
return p.norm(p.s) |
| 928 |
} |
| 929 |
|
| 930 |
var high int |
| 931 |
|
| 932 |
if isBundle { |
| 933 |
high = p.posContainerHigh - 1 |
| 934 |
} else { |
| 935 |
high = p.nameLowHigh().High |
| 936 |
} |
| 937 |
|
| 938 |
if high == 0 { |
| 939 |
high++ |
| 940 |
} |
| 941 |
|
| 942 |
if !preserveExt { |
| 943 |
return p.norm(p.s[:high]) |
| 944 |
} |
| 945 |
|
| 946 |
// For txt files etc. we want to preserve the extension. |
| 947 |
id := p.identifiersKnown[0] |
| 948 |
|
| 949 |
return p.norm(p.s[:high] + p.s[id.Low-1:id.High]) |
| 950 |
} |
| 951 |
|
| 952 |
func (p *Path) Ext() string { |
| 953 |
return p.identifierAsString(0) |
| 954 |
} |
| 955 |
|
| 956 |
func (p *Path) OutputFormat() string { |
| 957 |
if p.posIdentifierPrefixOutputFormat != -1 { |
| 958 |
return p.identifierAsString(p.posIdentifierPrefixOutputFormat) |
| 959 |
} |
| 960 |
return p.identifierAsString(p.posIdentifierOutputFormat) |
| 961 |
} |
| 962 |
|
| 963 |
func (p *Path) Kind() string { |
| 964 |
if p.posIdentifierPrefixKind != -1 { |
| 965 |
return p.identifierAsString(p.posIdentifierPrefixKind) |
| 966 |
} |
| 967 |
return p.identifierAsString(p.posIdentifierKind) |
| 968 |
} |
| 969 |
|
| 970 |
func (p *Path) Layout() string { |
| 971 |
if p.posIdentifierPrefixLayout != -1 { |
| 972 |
return p.identifierAsString(p.posIdentifierPrefixLayout) |
| 973 |
} |
| 974 |
return p.identifierAsString(p.posIdentifierLayout) |
| 975 |
} |
| 976 |
|
| 977 |
func (p *Path) Lang() string { |
| 978 |
if len(p.posIdentifierPrefixLanguages) > 0 { |
| 979 |
return p.identifierAsString(p.posIdentifierPrefixLanguages[0]) |
| 980 |
} |
| 981 |
return p.identifierAsString(p.posIdentifierLanguage) |
| 982 |
} |
| 983 |
|
| 984 |
func (p *Path) Langs() []string { |
| 985 |
return p.identifiersAsStrings(p.posIdentifierPrefixLanguages) |
| 986 |
} |
| 987 |
|
| 988 |
func (p *Path) Versions() []string { |
| 989 |
return p.identifiersAsStrings(p.posIdentifierVersions) |
| 990 |
} |
| 991 |
|
| 992 |
func (p *Path) Roles() []string { |
| 993 |
return p.identifiersAsStrings(p.posIdentifierRoles) |
| 994 |
} |
| 995 |
|
| 996 |
func (p *Path) identifiersAsStrings(positions []int) []string { |
| 997 |
if len(positions) == 0 { |
| 998 |
return nil |
| 999 |
} |
| 1000 |
ids := make([]string, len(positions)) |
| 1001 |
for i, pos := range positions { |
| 1002 |
ids[i] = p.identifierAsString(pos) |
| 1003 |
} |
| 1004 |
return ids |
| 1005 |
} |
| 1006 |
|
| 1007 |
func (p *Path) Custom() string { |
| 1008 |
return strings.TrimSuffix(strings.TrimPrefix(p.identifierAsString(p.posIdentifierCustom), identifierCustomWrapper), identifierCustomWrapper) |
| 1009 |
} |
| 1010 |
|
| 1011 |
func (p *Path) Disabled() bool { |
| 1012 |
return p.disabled |
| 1013 |
} |
| 1014 |
|
| 1015 |
func (p *Path) Identifiers() []string { |
| 1016 |
ids := make([]string, len(p.identifiersKnown)) |
| 1017 |
for i, id := range p.identifiersKnown { |
| 1018 |
ids[i] = p.s[id.Low:id.High] |
| 1019 |
} |
| 1020 |
return ids |
| 1021 |
} |
| 1022 |
|
| 1023 |
func (p *Path) IdentifiersUnknown() []string { |
| 1024 |
ids := make([]string, len(p.identifiersUnknown)) |
| 1025 |
for i, id := range p.identifiersUnknown { |
| 1026 |
ids[i] = p.s[id.Low:id.High] |
| 1027 |
} |
| 1028 |
return ids |
| 1029 |
} |
| 1030 |
|
| 1031 |
func (p *Path) Type() Type { |
| 1032 |
return p.pathType |
| 1033 |
} |
| 1034 |
|
| 1035 |
func (p *Path) IsBundle() bool { |
| 1036 |
return p.pathType >= TypeLeaf && p.pathType <= TypeContentData |
| 1037 |
} |
| 1038 |
|
| 1039 |
func (p *Path) IsBranchBundle() bool { |
| 1040 |
return p.pathType == TypeBranch |
| 1041 |
} |
| 1042 |
|
| 1043 |
func (p *Path) IsLeafBundle() bool { |
| 1044 |
return p.pathType == TypeLeaf |
| 1045 |
} |
| 1046 |
|
| 1047 |
func (p *Path) IsContentData() bool { |
| 1048 |
return p.pathType == TypeContentData |
| 1049 |
} |
| 1050 |
|
| 1051 |
func (p Path) ForType(t Type) *Path { |
| 1052 |
p.pathType = t |
| 1053 |
return &p |
| 1054 |
} |
| 1055 |
|
| 1056 |
func (p *Path) identifierAsString(i int) string { |
| 1057 |
i = p.identifierIndex(i) |
| 1058 |
if i == -1 { |
| 1059 |
return "" |
| 1060 |
} |
| 1061 |
|
| 1062 |
id := p.identifiersKnown[i] |
| 1063 |
return p.s[id.Low:id.High] |
| 1064 |
} |
| 1065 |
|
| 1066 |
func (p *Path) identifierIndex(i int) int { |
| 1067 |
if i < 0 || i >= len(p.identifiersKnown) { |
| 1068 |
return -1 |
| 1069 |
} |
| 1070 |
return i |
| 1071 |
} |
| 1072 |
|
| 1073 |
// HasExt returns true if the Unix styled path has an extension. |
| 1074 |
func HasExt(p string) bool { |
| 1075 |
for i := len(p) - 1; i >= 0; i-- { |
| 1076 |
if p[i] == '.' { |
| 1077 |
return true |
| 1078 |
} |
| 1079 |
if p[i] == '/' { |
| 1080 |
return false |
| 1081 |
} |
| 1082 |
} |
| 1083 |
return false |
| 1084 |
} |
| 1085 |
|
| 1086 |
// ValidateIdentifier returns true if the given string is a valid identifier according |
| 1087 |
// to Hugo's basic path normalization rules. |
| 1088 |
func ValidateIdentifier(s string) error { |
| 1089 |
if s == NormalizePathStringBasic(s) { |
| 1090 |
return nil |
| 1091 |
} |
| 1092 |
return fmt.Errorf("must be all lower case and no spaces") |
| 1093 |
} |