| 1 |
// Copyright 2019 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 |
"net/url" |
| 18 |
"path" |
| 19 |
"strings" |
| 20 |
|
| 21 |
"github.com/gohugoio/hugo/output" |
| 22 |
|
| 23 |
"github.com/gohugoio/hugo/resources/kinds" |
| 24 |
"github.com/gohugoio/hugo/resources/page" |
| 25 |
) |
| 26 |
|
| 27 |
func newPagePaths(ps *pageState) (pagePaths, error) { |
| 28 |
s := ps.s |
| 29 |
pm := ps.m |
| 30 |
|
| 31 |
targetPathDescriptor, err := createTargetPathDescriptor(ps) |
| 32 |
if err != nil { |
| 33 |
return pagePaths{}, err |
| 34 |
} |
| 35 |
|
| 36 |
var outputFormats output.Formats |
| 37 |
|
| 38 |
if ps.m.isStandalone() { |
| 39 |
outputFormats = output.Formats{ps.m.standaloneOutputFormat} |
| 40 |
} else { |
| 41 |
outputFormats = ps.outputFormats() |
| 42 |
if len(outputFormats) == 0 { |
| 43 |
return pagePaths{}, nil |
| 44 |
} |
| 45 |
|
| 46 |
if pm.noRender() { |
| 47 |
outputFormats = outputFormats[:1] |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
pageOutputFormats := make(page.OutputFormats, len(outputFormats)) |
| 52 |
targets := make(map[string]targetPathsHolder) |
| 53 |
|
| 54 |
for i, f := range outputFormats { |
| 55 |
desc := targetPathDescriptor |
| 56 |
desc.Type = f |
| 57 |
paths := page.CreateTargetPaths(desc) |
| 58 |
|
| 59 |
var relPermalink, permalink string |
| 60 |
|
| 61 |
// If a page is headless or bundled in another, |
| 62 |
// it will not get published on its own and it will have no links. |
| 63 |
// We also check the build options if it's set to not render or have |
| 64 |
// a link. |
| 65 |
if !pm.noLink() && !pm.bundled { |
| 66 |
relPermalink = paths.RelPermalink(s.PathSpec) |
| 67 |
permalink = paths.PermalinkForOutputFormat(s.PathSpec, f) |
| 68 |
} |
| 69 |
|
| 70 |
pageOutputFormats[i] = page.NewOutputFormat(relPermalink, permalink, len(outputFormats) == 1, f) |
| 71 |
|
| 72 |
// Use the main format for permalinks, usually HTML. |
| 73 |
permalinksIndex := 0 |
| 74 |
if f.Permalinkable { |
| 75 |
// Unless it's permalinkable. |
| 76 |
permalinksIndex = i |
| 77 |
} |
| 78 |
|
| 79 |
targets[f.Name] = targetPathsHolder{ |
| 80 |
relURL: relPermalink, |
| 81 |
paths: paths, |
| 82 |
OutputFormat: pageOutputFormats[permalinksIndex], |
| 83 |
} |
| 84 |
|
| 85 |
} |
| 86 |
|
| 87 |
var out page.OutputFormats |
| 88 |
if !pm.noLink() { |
| 89 |
out = pageOutputFormats |
| 90 |
} |
| 91 |
|
| 92 |
return pagePaths{ |
| 93 |
outputFormats: out, |
| 94 |
firstOutputFormat: pageOutputFormats[0], |
| 95 |
targetPaths: targets, |
| 96 |
targetPathDescriptor: targetPathDescriptor, |
| 97 |
}, nil |
| 98 |
} |
| 99 |
|
| 100 |
// targetPathDir returns the directory part of p's target path, with any |
| 101 |
// slug set in the section, taxonomy or term pages above it applied. |
| 102 |
func targetPathDir(p *pageState) string { |
| 103 |
unnormalized := p.s.Conf.DisablePathToLower() |
| 104 |
pi := p.PathInfo() |
| 105 |
if unnormalized { |
| 106 |
pi = pi.Unnormalized() |
| 107 |
} |
| 108 |
|
| 109 |
var sb strings.Builder |
| 110 |
sb.Grow(len(pi.Base())) |
| 111 |
|
| 112 |
if p.IsBranch() { |
| 113 |
writeBranchDir(&sb, p, unnormalized) |
| 114 |
} else { |
| 115 |
section := p.CurrentSection() |
| 116 |
writeBranchDir(&sb, section, unnormalized) |
| 117 |
sb.WriteString(strings.TrimPrefix(pi.ContainerDir(), pathBase(section, unnormalized))) |
| 118 |
} |
| 119 |
|
| 120 |
return sb.String() |
| 121 |
} |
| 122 |
|
| 123 |
func writeBranchDir(sb *strings.Builder, p page.Page, unnormalized bool) { |
| 124 |
if p.IsHome() { |
| 125 |
return |
| 126 |
} |
| 127 |
parent := p.Parent() |
| 128 |
writeBranchDir(sb, parent, unnormalized) |
| 129 |
rel := strings.TrimPrefix(pathBase(p, unnormalized), pathBase(parent, unnormalized)) |
| 130 |
if slug := p.Slug(); slug != "" { |
| 131 |
sb.WriteString(rel[:strings.LastIndexByte(rel, '/')+1]) |
| 132 |
sb.WriteString(slug) |
| 133 |
return |
| 134 |
} |
| 135 |
sb.WriteString(rel) |
| 136 |
} |
| 137 |
|
| 138 |
func pathBase(p page.Page, unnormalized bool) string { |
| 139 |
pi := p.PathInfo() |
| 140 |
if unnormalized { |
| 141 |
pi = pi.Unnormalized() |
| 142 |
} |
| 143 |
return pi.Base() |
| 144 |
} |
| 145 |
|
| 146 |
type pagePaths struct { |
| 147 |
outputFormats page.OutputFormats |
| 148 |
firstOutputFormat page.OutputFormat |
| 149 |
|
| 150 |
targetPaths map[string]targetPathsHolder |
| 151 |
targetPathDescriptor page.TargetPathDescriptor |
| 152 |
} |
| 153 |
|
| 154 |
func (l pagePaths) OutputFormats() page.OutputFormats { |
| 155 |
return l.outputFormats |
| 156 |
} |
| 157 |
|
| 158 |
func createTargetPathDescriptor(p *pageState) (page.TargetPathDescriptor, error) { |
| 159 |
s := p.s |
| 160 |
d := s.Deps |
| 161 |
pm := p.m |
| 162 |
alwaysInSubDir := p.Kind() == kinds.KindSitemap |
| 163 |
pageInfoPage := p.PathInfo() |
| 164 |
if p.s.Conf.DisablePathToLower() { |
| 165 |
pageInfoPage = pageInfoPage.Unnormalized() |
| 166 |
} |
| 167 |
|
| 168 |
desc := page.TargetPathDescriptor{ |
| 169 |
PathSpec: d.PathSpec, |
| 170 |
Kind: p.Kind(), |
| 171 |
Path: pageInfoPage, |
| 172 |
Dir: targetPathDir(p), |
| 173 |
UglyURLs: s.h.Conf.IsUglyURLs(p.Section()), |
| 174 |
ForcePrefix: s.h.Conf.IsMultihost() || alwaysInSubDir, |
| 175 |
URL: pm.pageConfig.URL, |
| 176 |
} |
| 177 |
|
| 178 |
if pm.Slug() != "" { |
| 179 |
desc.BaseName = pm.Slug() |
| 180 |
} else if pm.isStandalone() && pm.standaloneOutputFormat.BaseName != "" { |
| 181 |
desc.BaseName = pm.standaloneOutputFormat.BaseName |
| 182 |
} else { |
| 183 |
desc.BaseName = pageInfoPage.BaseNameNoIdentifier() |
| 184 |
} |
| 185 |
|
| 186 |
addPrefix := func(filePath, link string) { |
| 187 |
if filePath != "" { |
| 188 |
if desc.PrefixFilePath != "" { |
| 189 |
desc.PrefixFilePath += "/" + filePath |
| 190 |
} else { |
| 191 |
desc.PrefixFilePath += filePath |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
if link != "" { |
| 196 |
if desc.PrefixLink != "" { |
| 197 |
desc.PrefixLink += "/" + link |
| 198 |
} else { |
| 199 |
desc.PrefixLink += link |
| 200 |
} |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
// Add path prefixes. |
| 205 |
// Add any role first, as that is a natural candidate for external ACL checks. |
| 206 |
// For multihost sites, the file path needs to start with the language code. |
| 207 |
if s.h.Conf.IsMultihost() { |
| 208 |
addPrefix(s.getLanguageTargetPathLang(true), "") |
| 209 |
} |
| 210 |
rolePrefix := s.getPrefixRole() |
| 211 |
addPrefix(rolePrefix, rolePrefix) |
| 212 |
versionPrefix := s.getPrefixVersion() |
| 213 |
addPrefix(versionPrefix, versionPrefix) |
| 214 |
if s.h.Conf.IsMultihost() { |
| 215 |
addPrefix("", s.getLanguagePermalinkLang(alwaysInSubDir)) |
| 216 |
} else { |
| 217 |
addPrefix(s.getLanguageTargetPathLang(alwaysInSubDir), s.getLanguagePermalinkLang(alwaysInSubDir)) |
| 218 |
} |
| 219 |
|
| 220 |
if desc.URL != "" && strings.IndexByte(desc.URL, ':') >= 0 { |
| 221 |
// Attempt to parse and expand an url |
| 222 |
opath, err := d.ResourceSpec.Permalinks.ExpandPattern(desc.URL, p) |
| 223 |
if err != nil { |
| 224 |
return desc, err |
| 225 |
} |
| 226 |
|
| 227 |
if opath != "" { |
| 228 |
opath, _ = url.QueryUnescape(opath) |
| 229 |
desc.URL = opath |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
opath, err := d.ResourceSpec.Permalinks.Expand(p) |
| 234 |
if err != nil { |
| 235 |
return desc, err |
| 236 |
} |
| 237 |
|
| 238 |
if opath != "" { |
| 239 |
opath, _ = url.QueryUnescape(opath) |
| 240 |
opath = path.Clean(opath) |
| 241 |
|
| 242 |
desc.ExpandedPermalink = opath |
| 243 |
|
| 244 |
if p.File() != nil { |
| 245 |
s.Log.Debugf("Set expanded permalink path for %s %s to %#v", p.Kind(), p.File().Path(), opath) |
| 246 |
} else { |
| 247 |
s.Log.Debugf("Set expanded permalink path for %s %s to %#v", p.Kind(), p.Path(), opath) |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
return desc, nil |
| 252 |
} |