shortcode_page.go (2108B)
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 "html/template"
18
19 "github.com/gohugoio/hugo/resources/page"
20 )
21
22 var tocShortcodePlaceholder = createShortcodePlaceholder("TOC", 0)
23
24 // This is sent to the shortcodes. They cannot access the content
25 // they're a part of. It would cause an infinite regress.
26 //
27 // Go doesn't support virtual methods, so this careful dance is currently (I think)
28 // the best we can do.
29 type pageForShortcode struct {
30 page.PageWithoutContent
31 page.ContentProvider
32
33 // We need to replace it after we have rendered it, so provide a
34 // temporary placeholder.
35 toc template.HTML
36
37 p *pageState
38 }
39
40 func newPageForShortcode(p *pageState) page.Page {
41 return &pageForShortcode{
42 PageWithoutContent: p,
43 ContentProvider: page.NopPage,
44 toc: template.HTML(tocShortcodePlaceholder),
45 p: p,
46 }
47 }
48
49 func (p *pageForShortcode) page() page.Page {
50 return p.PageWithoutContent.(page.Page)
51 }
52
53 func (p *pageForShortcode) TableOfContents() template.HTML {
54 p.p.enablePlaceholders()
55 return p.toc
56 }
57
58 // This is what is sent into the content render hooks (link, image).
59 type pageForRenderHooks struct {
60 page.PageWithoutContent
61 page.TableOfContentsProvider
62 page.ContentProvider
63 }
64
65 func newPageForRenderHook(p *pageState) page.Page {
66 return &pageForRenderHooks{
67 PageWithoutContent: p,
68 ContentProvider: page.NopPage,
69 TableOfContentsProvider: page.NopPage,
70 }
71 }
72
73 func (p *pageForRenderHooks) page() page.Page {
74 return p.PageWithoutContent.(page.Page)
75 }