page__output.go (2726B)
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 "github.com/gohugoio/hugo/output"
18 "github.com/gohugoio/hugo/resources/page"
19 "github.com/gohugoio/hugo/resources/resource"
20 )
21
22 func newPageOutput(
23 ps *pageState,
24 pp pagePaths,
25 f output.Format,
26 render bool) *pageOutput {
27 var targetPathsProvider targetPathsHolder
28 var linksProvider resource.ResourceLinksProvider
29
30 ft, found := pp.targetPaths[f.Name]
31 if !found {
32 // Link to the main output format
33 ft = pp.targetPaths[pp.firstOutputFormat.Format.Name]
34 }
35 targetPathsProvider = ft
36 linksProvider = ft
37
38 var paginatorProvider page.PaginatorProvider = page.NopPage
39 var pag *pagePaginator
40
41 if render && ps.IsNode() {
42 pag = newPagePaginator(ps)
43 paginatorProvider = pag
44 }
45
46 providers := struct {
47 page.PaginatorProvider
48 resource.ResourceLinksProvider
49 targetPather
50 }{
51 paginatorProvider,
52 linksProvider,
53 targetPathsProvider,
54 }
55
56 po := &pageOutput{
57 f: f,
58 pagePerOutputProviders: providers,
59 ContentProvider: page.NopPage,
60 TableOfContentsProvider: page.NopPage,
61 PageRenderProvider: page.NopPage,
62 render: render,
63 paginator: pag,
64 }
65
66 return po
67 }
68
69 // We create a pageOutput for every output format combination, even if this
70 // particular page isn't configured to be rendered to that format.
71 type pageOutput struct {
72 // Set if this page isn't configured to be rendered to this format.
73 render bool
74
75 f output.Format
76
77 // Only set if render is set.
78 // Note that this will be lazily initialized, so only used if actually
79 // used in template(s).
80 paginator *pagePaginator
81
82 // These interface provides the functionality that is specific for this
83 // output format.
84 pagePerOutputProviders
85 page.ContentProvider
86 page.TableOfContentsProvider
87 page.PageRenderProvider
88
89 // May be nil.
90 cp *pageContentOutput
91 }
92
93 func (p *pageOutput) initContentProvider(cp *pageContentOutput) {
94 if cp == nil {
95 return
96 }
97 p.ContentProvider = cp
98 p.TableOfContentsProvider = cp
99 p.PageRenderProvider = cp
100 p.cp = cp
101 }
102
103 func (p *pageOutput) enablePlaceholders() {
104 if p.cp != nil {
105 p.cp.enablePlaceholders()
106 }
107 }