Debian Codegraph @

hugolib/page__paths.go

raw
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 type pagePaths struct {
101 outputFormats page.OutputFormats
102 firstOutputFormat page.OutputFormat
103
104 targetPaths map[string]targetPathsHolder
105 targetPathDescriptor page.TargetPathDescriptor
106 }
107
108 func (l pagePaths) OutputFormats() page.OutputFormats {
109 return l.outputFormats
110 }
111
112 func createTargetPathDescriptor(p *pageState) (page.TargetPathDescriptor, error) {
113 s := p.s
114 d := s.Deps
115 pm := p.m
116 alwaysInSubDir := p.Kind() == kinds.KindSitemap
117 pageInfoPage := p.PathInfo()
118
119 pageInfoCurrentSection := p.CurrentSection().PathInfo()
120 if p.s.Conf.DisablePathToLower() {
121 pageInfoPage = pageInfoPage.Unnormalized()
122 pageInfoCurrentSection = pageInfoCurrentSection.Unnormalized()
123 }
124
125 desc := page.TargetPathDescriptor{
126 PathSpec: d.PathSpec,
127 Kind: p.Kind(),
128 Path: pageInfoPage,
129 Section: pageInfoCurrentSection,
130 UglyURLs: s.h.Conf.IsUglyURLs(p.Section()),
131 ForcePrefix: s.h.Conf.IsMultihost() || alwaysInSubDir,
132 URL: pm.pageConfig.URL,
133 }
134
135 if pm.Slug() != "" {
136 desc.BaseName = pm.Slug()
137 } else if pm.isStandalone() && pm.standaloneOutputFormat.BaseName != "" {
138 desc.BaseName = pm.standaloneOutputFormat.BaseName
139 } else {
140 desc.BaseName = pageInfoPage.BaseNameNoIdentifier()
141 }
142
143 addPrefix := func(filePath, link string) {
144 if filePath != "" {
145 if desc.PrefixFilePath != "" {
146 desc.PrefixFilePath += "/" + filePath
147 } else {
148 desc.PrefixFilePath += filePath
149 }
150 }
151
152 if link != "" {
153 if desc.PrefixLink != "" {
154 desc.PrefixLink += "/" + link
155 } else {
156 desc.PrefixLink += link
157 }
158 }
159 }
160
161 // Add path prefixes.
162 // Add any role first, as that is a natural candidate for external ACL checks.
163 // For multihost sites, the file path needs to start with the language code.
164 if s.h.Conf.IsMultihost() {
165 addPrefix(s.getLanguageTargetPathLang(true), "")
166 }
167 rolePrefix := s.getPrefixRole()
168 addPrefix(rolePrefix, rolePrefix)
169 versionPrefix := s.getPrefixVersion()
170 addPrefix(versionPrefix, versionPrefix)
171 if s.h.Conf.IsMultihost() {
172 addPrefix("", s.getLanguagePermalinkLang(alwaysInSubDir))
173 } else {
174 addPrefix(s.getLanguageTargetPathLang(alwaysInSubDir), s.getLanguagePermalinkLang(alwaysInSubDir))
175 }
176
177 if desc.URL != "" && strings.IndexByte(desc.URL, ':') >= 0 {
178 // Attempt to parse and expand an url
179 opath, err := d.ResourceSpec.Permalinks.ExpandPattern(desc.URL, p)
180 if err != nil {
181 return desc, err
182 }
183
184 if opath != "" {
185 opath, _ = url.QueryUnescape(opath)
186 desc.URL = opath
187 }
188 }
189
190 opath, err := d.ResourceSpec.Permalinks.Expand(p)
191 if err != nil {
192 return desc, err
193 }
194
195 if opath != "" {
196 opath, _ = url.QueryUnescape(opath)
197 opath = path.Clean(opath)
198
199 desc.ExpandedPermalink = opath
200
201 if p.File() != nil {
202 s.Log.Debugf("Set expanded permalink path for %s %s to %#v", p.Kind(), p.File().Path(), opath)
203 } else {
204 s.Log.Debugf("Set expanded permalink path for %s in %v to %#v", p.Kind(), desc.Section.Path(), opath)
205 }
206 }
207
208 return desc, nil
209 }