summaryrefslogtreecommitdiffstats
path: root/hugolib/pagecollections.go
blob: 58c646334b4e768bde96b52386447efff8be3ca1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package hugolib

import (
	"fmt"
	"path"
	"path/filepath"
	"strings"

	"github.com/gohugoio/hugo/hugofs"
	"github.com/gohugoio/hugo/hugofs/files"

	"github.com/gohugoio/hugo/common/paths"

	"github.com/gohugoio/hugo/resources/kinds"
	"github.com/gohugoio/hugo/resources/page"
)

// pageFinder provides ways to find a Page in a Site.
type pageFinder struct {
	pageMap *pageMap
}

func newPageFinder(m *pageMap) *pageFinder {
	if m == nil {
		panic("must provide a pageMap")
	}
	c := &pageFinder{pageMap: m}
	return c
}

// getPageRef resolves a Page from ref/relRef, with a slightly more comprehensive
// search path than getPage.
func (c *pageFinder) getPageRef(context page.Page, ref string) (page.Page, error) {
	n, err := c.getContentNode(context, true, ref)
	if err != nil {
		return nil, err
	}

	if p, ok := n.(page.Page); ok {
		return p, nil
	}
	return nil, nil
}

func (c *pageFinder) getPage(context page.Page, ref string) (page.Page, error) {
	n, err := c.getContentNode(context, false, paths.ToSlashTrimTrailing(ref))
	if err != nil {
		return nil, err
	}
	if p, ok := n.(page.Page); ok {
		return p, nil
	}
	return nil, nil
}

// Only used in tests.
func (c *pageFinder) getPageOldVersion(kind string, sections ...string) page.Page {
	refs := append([]string{kind}, path.Join(sections...))
	p, _ := c.getPageForRefs(refs...)
	return p
}

// This is an adapter func for the old API with Kind as first argument.
// This is invoked when you do .Site.GetPage. We drop the Kind and fails
// if there are more than 2 arguments, which would be ambiguous.
func (c *pageFinder) getPageForRefs(ref ...string) (page.Page, error) {
	var refs []string
	for _, r := range ref {
		// A common construct in the wild is
		// .Site.GetPage "home" "" or
		// .Site.GetPage "home" "/"
		if r != "" && r != "/" {
			refs = append(refs, r)
		}
	}

	var key string

	if len(refs) > 2 {
		// This was allowed in Hugo <= 0.44, but we cannot support this with the
		// new API. This should be the most unusual case.
		return nil, fmt.Errorf(`too many arguments to .Site.GetPage: %v. Use lookups on the form {{ .Site.GetPage "/posts/mypage-md" }}`, ref)
	}

	if len(refs) == 0 || refs[0] == kinds.KindHome {
		key = "/"
	} else if len(refs) == 1 {
		if len(ref) == 2 && refs[0] == kinds.KindSection {
			// This is an old style reference to the "Home Page section".
			// Typically fetched via {{ .Site.GetPage "section" .Section }}
			// See https://github.com/gohugoio/hugo/issues/4989
			key = "/"
		} else {
			key = refs[0]
		}
	} else {
		key = refs[1]
	}

	key = filepath.ToSlash(key)
	if !strings.HasPrefix(key, "/") {
		key = "/" + key
	}

	return c.getPage(nil, key)
}

const defaultContentExt = ".md"

func (c *pageFinder) getContentNode(context page.Page, isReflink bool, ref string) (contentNodeI, error) {
	inRef := ref
	if ref == "" {
		ref = "/"
	}

	if paths.HasExt(ref) {
		return c.getContentNodeForRef(context, isReflink, true, inRef, ref)
	}

	var refs []string

	// We are always looking for a content file and having an extension greatly simplifies the code that follows,
	// even in the case where the extension does not match this one.
	if ref == "/" {
		refs = append(refs, "/_index"+defaultContentExt)
	} else if strings.HasSuffix(ref, "/index") {
		refs = append(refs, ref+"/index"+defaultContentExt)
		refs = append(refs, ref+defaultContentExt)
	} else {
		refs = append(refs, ref+defaultContentExt)
	}

	for _, ref := range refs {
		n, err := c.getContentNodeForRef(context, isReflink, false, inRef, ref)
		if n != nil || err != nil {
			return n, err
		}
	}

	return nil, nil
}

func (c *pageFinder) getContentNodeForRef(context page.Page, isReflink, hadExtension bool, inRef, ref string) (contentNodeI, error) {
	s := c.pageMap.s
	contentPathParser := s.Conf.PathParser()

	if context != nil && !strings.HasPrefix(ref, "/") {
		// Try the page-relative path first.
		// Branch pages: /mysection, "./mypage" => /mysection/mypage
		// Regular pages: /mysection/mypage.md, Path=/mysection/mypage, "./someotherpage" => /mysection/mypage/../someotherpage
		// Regular leaf bundles: /mysection/mypage/index.md, Path=/mysection/mypage, "./someotherpage" => /mysection/mypage/../someotherpage
		// Given the above, for regular pages we use the containing folder.
		var baseDir string
		if pi := context.PathInfo(); pi != nil {
			if pi.IsBranchBundle() || (hadExtension) {
				baseDir = pi.Dir()
			} else {
				baseDir = pi.ContainerDir()
			}
		}

		rel := path.Join(baseDir, inRef)

		if !hadExtension && !paths.HasExt(rel) {
			// See comment above.
			rel += defaultContentExt
		}

		relPath := contentPathParser.Parse(files.ComponentFolderContent, rel)

		n, err := c.getContentNodeFromPath(relPath, ref)
		if n != nil || err != nil {
			return n, err
		}

		if hadExtension && context.File() != nil {
			if n, err := c.getContentNodeFromRefReverseLookup(inRef, context.File().FileInfo()); n != nil || err != nil {
				return n, err
			}
		}

	}

	if strings.HasPrefix(ref, ".") {
		// Page relative, no need to look further.
		return nil, nil
	}

	refPath := contentPathParser.Parse(files.ComponentFolderContent, ref)

	n, err := c.getContentNodeFromPath(refPath, ref)

	if n != nil || err != nil {
		return