| 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 hugolib |
| 15 |
|
| 16 |
import ( |
| 17 |
"fmt" |
| 18 |
"path" |
| 19 |
"path/filepath" |
| 20 |
"strings" |
| 21 |
|
| 22 |
"github.com/gohugoio/hugo/hugofs" |
| 23 |
"github.com/gohugoio/hugo/hugofs/files" |
| 24 |
|
| 25 |
"github.com/gohugoio/hugo/common/paths" |
| 26 |
|
| 27 |
"github.com/gohugoio/hugo/resources/kinds" |
| 28 |
"github.com/gohugoio/hugo/resources/page" |
| 29 |
) |
| 30 |
|
| 31 |
// pageFinder provides ways to find a Page in a Site. |
| 32 |
type pageFinder struct { |
| 33 |
pm *pageMap |
| 34 |
} |
| 35 |
|
| 36 |
func newPageFinder(m *pageMap) *pageFinder { |
| 37 |
if m == nil { |
| 38 |
panic("must provide a pageMap") |
| 39 |
} |
| 40 |
c := &pageFinder{pm: m} |
| 41 |
return c |
| 42 |
} |
| 43 |
|
| 44 |
// getPageRef resolves a Page from ref/relRef, with a slightly more comprehensive |
| 45 |
// search path than getPage. |
| 46 |
func (c *pageFinder) getPageRef(context page.Page, ref string) (page.Page, error) { |
| 47 |
n, err := c.getContentNode(context, true, ref) |
| 48 |
if err != nil { |
| 49 |
return nil, err |
| 50 |
} |
| 51 |
|
| 52 |
if p, ok := n.(page.Page); ok { |
| 53 |
return p, nil |
| 54 |
} |
| 55 |
return nil, nil |
| 56 |
} |
| 57 |
|
| 58 |
func (c *pageFinder) getPage(context page.Page, ref string) (page.Page, error) { |
| 59 |
n, err := c.getContentNode(context, false, ref) |
| 60 |
if err != nil { |
| 61 |
return nil, err |
| 62 |
} |
| 63 |
if p, ok := n.(page.Page); ok { |
| 64 |
return p, nil |
| 65 |
} |
| 66 |
return nil, nil |
| 67 |
} |
| 68 |
|
| 69 |
// Only used in tests. |
| 70 |
func (c *pageFinder) getPageOldVersion(kind string, sections ...string) page.Page { |
| 71 |
refs := append([]string{kind}, path.Join(sections...)) |
| 72 |
p, _ := c.getPageForRefs(refs...) |
| 73 |
return p |
| 74 |
} |
| 75 |
|
| 76 |
// This is an adapter func for the old API with Kind as first argument. |
| 77 |
// This is invoked when you do .Site.GetPage. We drop the Kind and fails |
| 78 |
// if there are more than 2 arguments, which would be ambiguous. |
| 79 |
func (c *pageFinder) getPageForRefs(ref ...string) (page.Page, error) { |
| 80 |
var refs []string |
| 81 |
for _, r := range ref { |
| 82 |
// A common construct in the wild is |
| 83 |
// .Site.GetPage "home" "" or |
| 84 |
// .Site.GetPage "home" "/" |
| 85 |
if r != "" && r != "/" { |
| 86 |
refs = append(refs, r) |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
var key string |
| 91 |
|
| 92 |
if len(refs) > 2 { |
| 93 |
// This was allowed in Hugo <= 0.44, but we cannot support this with the |
| 94 |
// new API. This should be the most unusual case. |
| 95 |
return nil, fmt.Errorf(`too many arguments to .Site.GetPage: %v. Use lookups on the form {{ .Site.GetPage "/posts/mypage-md" }}`, ref) |
| 96 |
} |
| 97 |
|
| 98 |
if len(refs) == 0 || refs[0] == kinds.KindHome { |
| 99 |
key = "/" |
| 100 |
} else if len(refs) == 1 { |
| 101 |
if len(ref) == 2 && refs[0] == kinds.KindSection { |
| 102 |
// This is an old style reference to the "Home Page section". |
| 103 |
// Typically fetched via {{ .Site.GetPage "section" .Section }} |
| 104 |
// See https://github.com/gohugoio/hugo/issues/4989 |
| 105 |
key = "/" |
| 106 |
} else { |
| 107 |
key = refs[0] |
| 108 |
} |
| 109 |
} else { |
| 110 |
key = refs[1] |
| 111 |
} |
| 112 |
|
| 113 |
return c.getPage(nil, key) |
| 114 |
} |
| 115 |
|
| 116 |
const defaultContentExt = ".md" |
| 117 |
|
| 118 |
func (c *pageFinder) getContentNode(context page.Page, isReflink bool, ref string) (contentNode, error) { |
| 119 |
ref = paths.ToSlashTrimTrailing(ref) |
| 120 |
inRef := ref |
| 121 |
if ref == "" { |
| 122 |
ref = "/" |
| 123 |
} |
| 124 |
|
| 125 |
if paths.HasExt(ref) { |
| 126 |
return c.getContentNodeForRef(context, isReflink, true, inRef, ref) |
| 127 |
} |
| 128 |
|
| 129 |
// We are always looking for a content file and having an extension greatly simplifies the code that follows, |
| 130 |
// even in the case where the extension does not match this one. |
| 131 |
if ref == "/" { |
| 132 |
if n, err := c.getContentNodeForRef(context, isReflink, false, inRef, "/_index"+defaultContentExt); n != nil || err != nil { |
| 133 |
return n, err |
| 134 |
} |
| 135 |
} else if strings.HasSuffix(ref, "/index") { |
| 136 |
if n, err := c.getContentNodeForRef(context, isReflink, false, inRef, ref+"/index"+defaultContentExt); n != nil || err != nil { |
| 137 |
return n, err |
| 138 |
} |
| 139 |
if n, err := c.getContentNodeForRef(context, isReflink, false, inRef, ref+defaultContentExt); n != nil || err != nil { |
| 140 |
return n, err |
| 141 |
} |
| 142 |
} else { |
| 143 |
if n, err := c.getContentNodeForRef(context, isReflink, false, inRef, ref+defaultContentExt); n != nil || err != nil { |
| 144 |
return n, err |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
return nil, nil |
| 149 |
} |
| 150 |
|
| 151 |
func (c *pageFinder) getContentNodeForRef(context page.Page, isReflink, hadExtension bool, inRef, ref string) (contentNode, error) { |
| 152 |
s := c.pm.s |
| 153 |
contentPathParser := s.Conf.PathParser() |
| 154 |
|
| 155 |
if context != nil && !strings.HasPrefix(ref, "/") { |
| 156 |
// Try the page-relative path first. |
| 157 |
// Branch pages: /mysection, "./mypage" => /mysection/mypage |
| 158 |
// Regular pages: /mysection/mypage.md, Path=/mysection/mypage, "./someotherpage" => /mysection/mypage/../someotherpage |
| 159 |
// Regular leaf bundles: /mysection/mypage/index.md, Path=/mysection/mypage, "./someotherpage" => /mysection/mypage/../someotherpage |
| 160 |
// Given the above, for regular pages we use the containing folder. |
| 161 |
var baseDir string |
| 162 |
if pi := context.PathInfo(); pi != nil { |
| 163 |
if pi.IsBranchBundle() || (hadExtension && strings.HasPrefix(ref, "../")) { |
| 164 |
baseDir = pi.Dir() |
| 165 |
} else { |
| 166 |
baseDir = pi.ContainerDir() |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
rel := path.Join(baseDir, ref) |
| 171 |
|
| 172 |
relPath, _ := contentPathParser.ParseBaseAndBaseNameNoIdentifier(files.ComponentFolderContent, rel) |
| 173 |
|
| 174 |
n, err := c.getContentNodeFromPath(relPath) |
| 175 |
if n != nil || err != nil { |
| 176 |
return n, err |
| 177 |
} |
| 178 |
|
| 179 |
if hadExtension && context.File() != nil { |
| 180 |
if n, err := c.getContentNodeFromRefReverseLookup(inRef, context.File().FileInfo()); n != nil || err != nil { |
| 181 |
return n, err |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
} |
| 186 |
|
| 187 |
if strings.HasPrefix(ref, ".") { |
| 188 |
// Page relative, no need to look further. |
| 189 |
return nil, nil |
| 190 |
} |
| 191 |
|
| 192 |
relPath, nameNoIdentifier := contentPathParser.ParseBaseAndBaseNameNoIdentifier(files.ComponentFolderContent, ref) |
| 193 |
|
| 194 |
n, err := c.getContentNodeFromPath(relPath) |
| 195 |
|
| 196 |
if n != nil || err != nil { |
| 197 |
return n, err |
| 198 |
} |
| 199 |
|
| 200 |
if hadExtension && s.home != nil && s.home.File() != nil { |
| 201 |
if n, err := c.getContentNodeFromRefReverseLookup(inRef, s.home.File().FileInfo()); n != nil || err != nil { |
| 202 |
return n, err |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
var doSimpleLookup bool |
| 207 |
if isReflink || context == nil { |
| 208 |
slashCount := strings.Count(inRef, "/") |
| 209 |
doSimpleLookup = slashCount == 0 |
| 210 |
} |
| 211 |
|
| 212 |
if !doSimpleLookup { |
| 213 |
return nil, nil |
| 214 |
} |
| 215 |
|
| 216 |
n = c.pm.pageReverseIndex.Get(nameNoIdentifier) |
| 217 |
if n == ambiguousContentNode { |
| 218 |
return nil, fmt.Errorf("page reference %q is ambiguous", inRef) |
| 219 |
} |
| 220 |
|
| 221 |
return n, nil |
| 222 |
} |
| 223 |
|
| 224 |
func (c *pageFinder) getContentNodeFromRefReverseLookup(ref string, fi hugofs.FileMetaInfo) (contentNode, error) { |
| 225 |
s := c.pm.s |
| 226 |
meta := fi.Meta() |
| 227 |
dir := meta.Filename |
| 228 |
if !fi.IsDir() { |
| 229 |
dir = filepath.Dir(meta.Filename) |
| 230 |
} |
| 231 |
|
| 232 |
realFilename := filepath.Join(dir, ref) |
| 233 |
|
| 234 |
pcs, err := s.BaseFs.Content.ReverseLookup(realFilename, true) |
| 235 |
if err != nil { |
| 236 |
return nil, err |
| 237 |
} |
| 238 |
|
| 239 |
// There may be multiple matches, but we will only use the first one. |
| 240 |
for _, pc := range pcs { |
| 241 |
pi := s.Conf.PathParser().Parse(pc.Component, pc.Path) |
| 242 |
if n := c.pm.treePages.Get(pi.Base()); n != nil { |
| 243 |
return n, nil |
| 244 |
} |
| 245 |
} |
| 246 |
return nil, nil |
| 247 |
} |
| 248 |
|
| 249 |
func (c *pageFinder) getContentNodeFromPath(s string) (contentNode, error) { |
| 250 |
n := c.pm.treePages.Get(s) |
| 251 |
if n != nil { |
| 252 |
return n, nil |
| 253 |
} |
| 254 |
|
| 255 |
return nil, nil |
| 256 |
} |