Debian Codegraph @

hugolib/page__output.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 "fmt"
18 "path"
19 "strings"
20
21 "github.com/gohugoio/hugo/identity"
22 "github.com/gohugoio/hugo/output"
23 "github.com/gohugoio/hugo/resources/page"
24 "github.com/gohugoio/hugo/resources/resource"
25 )
26
27 var paginatorNotSupported = page.PaginatorNotSupportedFunc(func() error {
28 return fmt.Errorf("pagination not supported for this page")
29 })
30
31 func newPageOutput(
32 ps *pageState,
33 pp pagePaths,
34 f output.Format,
35 render bool,
36 ) *pageOutput {
37 var targetPathsProvider targetPathsHolder
38 var linksProvider resource.ResourceLinksProvider
39
40 ft, found := pp.targetPaths[f.Name]
41 if !found {
42 // Link to the main output format
43 ft = pp.targetPaths[pp.firstOutputFormat.Format.Name]
44 }
45 targetPathsProvider = ft
46 linksProvider = ft
47
48 var paginatorProvider page.PaginatorProvider
49 var pag *pagePaginator
50
51 if render && ps.IsBranch() {
52 pag = newPagePaginator(ps)
53 paginatorProvider = pag
54 } else {
55 paginatorProvider = paginatorNotSupported
56 }
57
58 providers := struct {
59 page.PaginatorProvider
60 resource.ResourceLinksProvider
61 targetPather
62 }{
63 paginatorProvider,
64 linksProvider,
65 targetPathsProvider,
66 }
67
68 po := &pageOutput{
69 p: ps,
70 f: f,
71 pagePerOutputProviders: providers,
72 MarkupProvider: page.NopPage,
73 ContentProvider: page.NopPage,
74 PageRenderProvider: page.NopPage,
75 TableOfContentsProvider: page.NopPage,
76 render: render,
77 paginator: pag,
78 dependencyManagerOutput: ps.s.Conf.NewIdentityManager(),
79 }
80
81 return po
82 }
83
84 // We create a pageOutput for every output format combination, even if this
85 // particular page isn't configured to be rendered to that format.
86 type pageOutput struct {
87 p *pageState
88
89 // Set if this page is configured to be rendered to this format.
90 render bool
91
92 f output.Format
93
94 // Only set if render is set.
95 // Note that this will be lazily initialized, so only used if actually
96 // used in template(s).
97 paginator *pagePaginator
98
99 // These interface provides the functionality that is specific for this
100 // output format.
101 contentRenderer page.ContentRenderer
102 pagePerOutputProviders
103 page.MarkupProvider
104 page.ContentProvider
105 page.PageRenderProvider
106 page.TableOfContentsProvider
107 page.RenderShortcodesProvider
108
109 // May be nil.
110 pco *pageContentOutput
111
112 dependencyManagerOutput identity.Manager
113
114 renderState int // Reset when it needs to be rendered again.
115 renderOnce bool // To make sure we at least try to render it once.
116 }
117
118 func (po *pageOutput) Aliases() []string {
119 conf := po.p.s.conf
120 p := po.p
121 f := po.f
122
123 // This is relatively cheap to create, and the common case is to call this once.
124 // So avoid caching this value.
125 aliases := make([]string, len(po.p.m.pageConfig.Aliases))
126 for i, a := range po.p.m.pageConfig.Aliases {
127 isRelative := !strings.HasPrefix(a, "/")
128 var baseDir string
129 if isRelative {
130 // Form the baseDir by taking the resource's base target
131 // and moving up one level to the parent.
132 parentContext := path.Join(p.targetPaths().SubResourceBaseTarget, "..")
133 baseDir = parentContext
134
135 } else {
136 // Form the baseDir by prepending the content dimension
137 // prefixes with the Output Format path.
138 baseDir = path.Join("/", p.targetPathDescriptor.PrefixFilePath, f.Path)
139 }
140
141 a = path.Join(baseDir, a)
142
143 if conf.C.IsUglyURLSection(p.Section()) && !pathHasOutputFormatSuffix(a, f) {
144 a += f.MediaType.FirstSuffix.FullSuffix
145 }
146
147 aliases[i] = a
148 }
149 return aliases
150 }
151
152 // Key returns a unique key for this page output, used to e.g. hashing.
153 func (po *pageOutput) Key() string {
154 return po.p.Path() + po.f.Name
155 }
156
157 func (po *pageOutput) incrRenderState() {
158 po.renderState++
159 po.renderOnce = true
160 }
161
162 // isRendered reports whether this output format or its content has been rendered.
163 func (po *pageOutput) isRendered() bool {
164 if po.renderState > 0 {
165 return true
166 }
167 if po.pco != nil && po.pco.contentRendered.Load() {
168 return true
169 }
170 return false
171 }
172
173 func (po *pageOutput) IdentifierBase() string {
174 return po.p.f.Name
175 }
176
177 func (po *pageOutput) GetDependencyManager() identity.Manager {
178 return po.dependencyManagerOutput
179 }
180
181 func (p *pageOutput) setContentProvider(cp *pageContentOutput) {
182 if cp == nil {
183 return
184 }
185 p.contentRenderer = cp
186 p.ContentProvider = cp
187 p.MarkupProvider = cp
188 p.PageRenderProvider = cp
189 p.TableOfContentsProvider = cp
190 p.RenderShortcodesProvider = cp
191 p.pco = cp
192 }