Debian Codegraph @

resources/page/page_lazy_contentprovider.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 page
15
16 import (
17 "context"
18 "html/template"
19
20 "github.com/gohugoio/hugo/common/hsync"
21 "github.com/gohugoio/hugo/markup/converter"
22 "github.com/gohugoio/hugo/markup/tableofcontents"
23 )
24
25 // OutputFormatContentProvider represents the method set that is "outputFormat aware" and that we
26 // provide lazy initialization for in case they get invoked outside of their normal rendering context, e.g. via .Translations.
27 // Note that this set is currently not complete, but should cover the most common use cases.
28 // For the others, the implementation will be from the page.NoopPage.
29 type OutputFormatContentProvider interface {
30 OutputFormatPageContentProvider
31
32 // for internal use.
33 ContentRenderer
34 }
35
36 // OutputFormatPageContentProvider holds the exported methods from Page that are "outputFormat aware".
37 type OutputFormatPageContentProvider interface {
38 MarkupProvider
39 ContentProvider
40 TableOfContentsProvider
41 PageRenderProvider
42 }
43
44 // LazyContentProvider initializes itself when read. Each method of the
45 // ContentProvider interface initializes a content provider and shares it
46 // with other methods.
47 //
48 // Used in cases where we cannot guarantee whether the content provider
49 // will be needed. Must create via NewLazyContentProvider.
50 type LazyContentProvider struct {
51 init hsync.ValueResetter[OutputFormatContentProvider]
52 }
53
54 // NewLazyContentProvider returns a LazyContentProvider initialized with
55 // function f. The resulting LazyContentProvider calls f in order to
56 // retrieve a ContentProvider
57 func NewLazyContentProvider(f func(ctx context.Context) OutputFormatContentProvider) *LazyContentProvider {
58 lcp := LazyContentProvider{
59 init: hsync.OnceMoreValue(f),
60 }
61 return &lcp
62 }
63
64 func (lcp *LazyContentProvider) Reset() {
65 lcp.init.Reset()
66 }
67
68 func (lcp *LazyContentProvider) Markup(opts ...any) Markup {
69 return lcp.init.Value(context.Background()).Markup(opts...)
70 }
71
72 func (lcp *LazyContentProvider) TableOfContents(ctx context.Context) template.HTML {
73 return lcp.init.Value(ctx).TableOfContents(ctx)
74 }
75
76 func (lcp *LazyContentProvider) Fragments(ctx context.Context) *tableofcontents.Fragments {
77 return lcp.init.Value(ctx).Fragments(ctx)
78 }
79
80 func (lcp *LazyContentProvider) Content(ctx context.Context) (any, error) {
81 return lcp.init.Value(ctx).Content(ctx)
82 }
83
84 func (lcp *LazyContentProvider) ContentWithoutSummary(ctx context.Context) (template.HTML, error) {
85 return lcp.init.Value(ctx).ContentWithoutSummary(ctx)
86 }
87
88 func (lcp *LazyContentProvider) Plain(ctx context.Context) string {
89 return lcp.init.Value(ctx).Plain(ctx)
90 }
91
92 func (lcp *LazyContentProvider) PlainWords(ctx context.Context) []string {
93 return lcp.init.Value(ctx).PlainWords(ctx)
94 }
95
96 func (lcp *LazyContentProvider) Summary(ctx context.Context) template.HTML {
97 return lcp.init.Value(ctx).Summary(ctx)
98 }
99
100 func (lcp *LazyContentProvider) Truncated(ctx context.Context) bool {
101 return lcp.init.Value(ctx).Truncated(ctx)
102 }
103
104 func (lcp *LazyContentProvider) FuzzyWordCount(ctx context.Context) int {
105 return lcp.init.Value(ctx).FuzzyWordCount(ctx)
106 }
107
108 func (lcp *LazyContentProvider) WordCount(ctx context.Context) int {
109 return lcp.init.Value(ctx).WordCount(ctx)
110 }
111
112 func (lcp *LazyContentProvider) ReadingTime(ctx context.Context) int {
113 return lcp.init.Value(ctx).ReadingTime(ctx)
114 }
115
116 func (lcp *LazyContentProvider) Len(ctx context.Context) int {
117 return lcp.init.Value(ctx).Len(ctx)
118 }
119
120 func (lcp *LazyContentProvider) Render(ctx context.Context, layout ...string) (template.HTML, error) {
121 return lcp.init.Value(ctx).Render(ctx, layout...)
122 }
123
124 func (lcp *LazyContentProvider) RenderString(ctx context.Context, args ...any) (template.HTML, error) {
125 return lcp.init.Value(ctx).RenderString(ctx, args...)
126 }
127
128 func (lcp *LazyContentProvider) ParseAndRenderContent(ctx context.Context, content []byte, renderTOC bool) (converter.ResultRender, error) {
129 return lcp.init.Value(ctx).ParseAndRenderContent(ctx, content, renderTOC)
130 }
131
132 func (lcp *LazyContentProvider) ParseContent(ctx context.Context, content []byte) (converter.ResultParse, bool, error) {
133 return lcp.init.Value(ctx).ParseContent(ctx, content)
134 }
135
136 func (lcp *LazyContentProvider) RenderContent(ctx context.Context, content []byte, sourceInfo, doc any) (converter.ResultRender, bool, error) {
137 return lcp.init.Value(ctx).RenderContent(ctx, content, sourceInfo, doc)
138 }