Debian Codegraph @

hugolib/shortcode_page.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 "context"
18 "html/template"
19
20 "github.com/gohugoio/hugo/common/types"
21 "github.com/gohugoio/hugo/resources/page"
22 )
23
24 // A placeholder for the TableOfContents markup. This is what we pass to the Goldmark etc. renderers.
25 var tocShortcodePlaceholder = createShortcodePlaceholder("TOC", 0, 0)
26
27 // shortcodeRenderer is typically used to delay rendering of inner shortcodes
28 // marked with placeholders in the content.
29 type shortcodeRenderer interface {
30 renderShortcode(context.Context) ([]byte, bool, error)
31 renderShortcodeString(context.Context) (string, bool, error)
32 }
33
34 type shortcodeRenderFunc func(context.Context) ([]byte, bool, error)
35
36 func (f shortcodeRenderFunc) renderShortcode(ctx context.Context) ([]byte, bool, error) {
37 return f(ctx)
38 }
39
40 func (f shortcodeRenderFunc) renderShortcodeString(ctx context.Context) (string, bool, error) {
41 b, has, err := f(ctx)
42 return string(b), has, err
43 }
44
45 type prerenderedShortcode struct {
46 s string
47 hasVariants bool
48 }
49
50 func (p prerenderedShortcode) renderShortcode(context.Context) ([]byte, bool, error) {
51 return []byte(p.s), p.hasVariants, nil
52 }
53
54 func (p prerenderedShortcode) renderShortcodeString(context.Context) (string, bool, error) {
55 return p.s, p.hasVariants, nil
56 }
57
58 var zeroShortcode = prerenderedShortcode{}
59
60 // This is sent to the shortcodes. They cannot access the content
61 // they're a part of. It would cause an infinite regress.
62 //
63 // Go doesn't support virtual methods, so this careful dance is currently (I think)
64 // the best we can do.
65 type pageForShortcode struct {
66 page.PageWithoutContent
67 page.TableOfContentsProvider
68 page.MarkupProvider
69 page.ContentProvider
70
71 // We need to replace it after we have rendered it, so provide a
72 // temporary placeholder.
73 toc template.HTML
74
75 p *pageState
76 }
77
78 var _ types.Unwrapper = (*pageForShortcode)(nil)
79
80 func newPageForShortcode(p *pageState) page.Page {
81 return &pageForShortcode{
82 PageWithoutContent: p,
83 TableOfContentsProvider: p,
84 MarkupProvider: page.NopPage,
85 ContentProvider: page.NopPage,
86 toc: template.HTML(tocShortcodePlaceholder),
87 p: p,
88 }
89 }
90
91 // For internal use.
92 func (p *pageForShortcode) Unwrapv() any {
93 return p.PageWithoutContent.(page.Page)
94 }
95
96 func (p *pageForShortcode) String() string {
97 return p.p.String()
98 }
99
100 func (p *pageForShortcode) TableOfContents(context.Context) template.HTML {
101 return p.toc
102 }
103
104 var _ types.Unwrapper = (*pageForRenderHooks)(nil)
105
106 // This is what is sent into the content render hooks (link, image).
107 type pageForRenderHooks struct {
108 page.PageWithoutContent
109 page.TableOfContentsProvider
110 page.MarkupProvider
111 page.ContentProvider
112 p *pageState
113 }
114
115 func newPageForRenderHook(p *pageState) page.Page {
116 return &pageForRenderHooks{
117 PageWithoutContent: p,
118 MarkupProvider: page.NopPage,
119 ContentProvider: page.NopPage,
120 TableOfContentsProvider: p,
121 p: p,
122 }
123 }
124
125 func (p *pageForRenderHooks) Unwrapv() any {
126 return p.p
127 }
128
129 func (p *pageForRenderHooks) String() string {
130 return p.p.String()
131 }