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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
|
// Copyright 2019 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 page
import (
"path"
"path/filepath"
"strings"
"github.com/gohugoio/hugo/common/urls"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/output"
)
const slash = "/"
// TargetPathDescriptor describes how a file path for a given resource
// should look like on the file system. The same descriptor is then later used to
// create both the permalinks and the relative links, paginator URLs etc.
//
// The big motivating behind this is to have only one source of truth for URLs,
// and by that also get rid of most of the fragile string parsing/encoding etc.
type TargetPathDescriptor struct {
PathSpec *helpers.PathSpec
Type output.Format
Kind string
Sections []string
// For regular content pages this is either
// 1) the Slug, if set,
// 2) the file base name (TranslationBaseName).
BaseName string
// Source directory.
Dir string
// Typically a language prefix added to file paths.
PrefixFilePath string
// Typically a language prefix added to links.
PrefixLink string
// If in multihost mode etc., every link/path needs to be prefixed, even
// if set in URL.
ForcePrefix bool
// URL from front matter if set. Will override any Slug etc.
URL string
// Used to create paginator links.
Addends string
// The expanded permalink if defined for the section, ready to use.
ExpandedPermalink string
// Some types cannot have uglyURLs, even if globally enabled, RSS being one example.
UglyURLs bool
}
// TODO(bep) move this type.
type TargetPaths struct {
// Where to store the file on disk relative to the publish dir. OS slashes.
TargetFilename string
// The directory to write sub-resources of the above.
SubResourceBaseTarget string
// The base for creating links to sub-resources of the above.
SubResourceBaseLink string
// The relative permalink to this resources. Unix slashes.
Link string
}
func (p TargetPaths) RelPermalink(s *helpers.PathSpec) string {
return s.PrependBasePath(p.Link, false)
}
func (p TargetPaths) PermalinkForOutputFormat(s *helpers.PathSpec, f output.Format) string {
var baseURL urls.BaseURL
var err error
if f.Protocol != "" {
baseURL, err = s.Cfg.BaseURL().WithProtocol(f.Protocol)
if err != nil {
return ""
}
} else {
baseURL = s.Cfg.BaseURL()
}
baseURLstr := baseURL.String()
return s.PermalinkForBaseURL(p.Link, baseURLstr)
}
func isHtmlIndex(s string) bool {
return strings.HasSuffix(s, "/index.html")
}
func CreateTargetPaths(d TargetPathDescriptor) (tp TargetPaths) {
if d.Type.Name == "" {
panic("CreateTargetPath: missing type")
}
// Normalize all file Windows paths to simplify what's next.
if helpers.FilePathSeparator != slash {
d.Dir = filepath.ToSlash(d.Dir)
d.PrefixFilePath = filepath.ToSlash(d.PrefixFilePath)
}
if d.URL != "" && !strings.HasPrefix(d.URL, "/") {
// Treat this as a context relative URL
d.ForcePrefix = true
}
pagePath := slash
fullSuffix := d.Type.MediaType.FirstSuffix.FullSuffix
var (
pagePathDir string
link string
linkDir string
)
// The top level index files, i.e. the home page etc., needs
// the index base even when uglyURLs is enabled.
needsBase := true
isUgly := d.UglyURLs && !d.Type.NoUgly
baseNameSameAsType := d.BaseName != "" && d.BaseName == d.Type.BaseName
if d.ExpandedPermalink == "" && baseNameSameAsType {
isUgly = true
}
if d.Kind != KindPage && d.URL == "" && len(d.Sections) > 0 {
if d.ExpandedPermalink != "" {
pagePath = pjoin(pagePath, d.ExpandedPermalink)
} else {
pagePath = pjoin(d.Sections...)
}
needsBase = false
}
if d.Type.Path != "" {
pagePath = pjoin(pagePath, d.Type.Path)
}
if d.Kind != KindHome && d.URL != "" {
pagePath = pjoin(pagePath, d.URL)
if d.Addends != "" {
pagePath = pjoin(pagePath, d.Addends)
}
pagePathDir = pagePath
link = pagePath
hasDot := strings.Contains(d.URL, ".")
hasSlash := strings.HasSuffix(d.URL, slash)
if hasSlash || !hasDot {
pagePath = pjoin(pagePath, d.Type.BaseName+fullSuffix)
} else if hasDot {
pagePathDir = path.Dir(pagePathDir)
}
if !isHtmlIndex(pagePath) {
link = pagePath
} else if !hasSlash {
link += slash
}
linkDir = pagePathDir
if d.ForcePrefix {
// Prepend language prefix if not already set in URL
if d.PrefixFilePath != "" && !strings.HasPrefix(d.URL, slash+d.PrefixFilePath) {
pagePath = pjoin(d.PrefixFilePath, pagePath)
pagePathDir = pjoin(d.PrefixFilePath, pagePathDir)
}
if d.PrefixLink != "" && !strings.HasPrefix(d.URL, slash+d.PrefixLink) {
link = pjoin(d.PrefixLink, link)
linkDir = pjoin(d.PrefixLink, linkDir)
}
}
} else if d.Kind == KindPage {
if d.ExpandedPermalink != "" {
pagePath = pjoin(pagePath, d.ExpandedPermalink)
} else {
if d.Dir != "" {
pagePath = pjoin(pagePath, d.Dir)
}
if d.BaseName != "" {
pagePath = pjoin(pagePath, d.BaseName)
}
}
if d.Addends != "" {
pagePath = pjoin(pagePath, d.Addends)
}
link = pagePath
// TODO(bep) this should not happen after the fix in https://github.com/gohugoio/hugo/issues/4870
// but we may need some more testing before we can remove it.
if baseNameSameAsType {
link = strings.TrimSuffix(link, d.BaseName)
}
pagePathDir = link
link = link + slash
linkDir = pagePathDir
if isUgly {
pagePath = addSuffix(pagePath, fullSuffix)
} else {
pagePath = pjoin(pagePath, d.Type.BaseName+fullSuffix)
}
if !isHtmlIndex(pagePath) {
link = pagePath
}
if d.PrefixFilePath != "" {
pagePath = pjoin(d.PrefixFilePath, pagePath)
pagePathDir = pjoin(d.PrefixFilePath, pagePathDir)
}
if d.PrefixLink != "" {
link = pjoin(d.PrefixLink, link)
linkDir = pjoin(d.PrefixLink, linkDir)
}
} else {
if d.Addends != "" {
pagePath = pjoin(pagePath, d.Addends)
}
needsBase = needsBase && d.Addends == ""
// No permalink expansion etc. for node type pages (for now)
base := ""
if needsBase || !isUgly {
base = d.Type.BaseName
}
pagePathDir = pagePath
link = pagePath
linkDir = pagePathDir
if base != "" {
pagePath = path.Join(pagePath, addSuffix(base, fullSuffix))
} else {
pagePath = addSuffix(pagePath, fullSuffix)
}
if !isHtmlIndex(pagePath) {
link = pagePath
} else {
link += slash
}
if d.PrefixFilePath != "" {
pagePath = pjoin(d.PrefixFilePath, pagePath)
pagePathDir = pjoin(d.PrefixFilePath, pagePathDir)
}
if d.PrefixLink != "" {
link = pjoin(d.PrefixLink, link)
linkDir = pjoin(d.PrefixLink, linkDir)
}
}
pagePath = pjoin(slash, pagePath)
pagePathDir = strings.TrimSuffix(path.Join(slash, pagePathDir), slash)
hadSlash := strings.HasSuffix(link, slash)
link = strings.Trim(link, slash)
if hadSlash {
link += slash
}
if !strings.HasPrefix(link, slash) {
link = slash + link
}
linkDir = strings.TrimSuffix(path.Join(slash, linkDir), slash)
// if page URL is explicitly set in frontmatter,
// preserve its value without sanitization
if d.Kind != KindPage || d.URL == "" {
// Note: MakePathSanitized will lower case the path if
// disablePathToLower isn't set.
pagePath = d.PathSpec.MakePathSanitized(pagePath)
pagePathDir = d.PathSpec.MakePathSanitized(pagePathDir)
link = d.PathSpec.MakePathSanitized(link)
linkDir = d.PathSpec.MakePathSanitized(linkDir)
}
tp.TargetFilename = filepath.FromSlash(pagePath)
tp.SubResourceBaseTarget = filepath.FromSlash(pagePathDir)
tp.SubResourceBaseLink = linkDir
tp.Link = d.PathSpec.URLizeFilename(link)
if tp.Link == "" {
tp.Link = slash
}
return
}
func addSuffix(s, suffix string) string {
return strings.Trim(s, slash) + suffix
}
// Like path.Join, but preserves one trailing slash if present.
func pjoin(elem ...string) string {
hadSlash := strings.HasSuffix(elem[len(elem)-1], slash)
joined := path.Join(elem...)
if hadSlash && !strings.HasSuffix(joined, slash) {
return joined + slash
}
return joined
}
|