-
Notifications
You must be signed in to change notification settings - Fork 0
/
njn-tagcloud.go
143 lines (115 loc) · 3.45 KB
/
njn-tagcloud.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright (c) 2024 The Go-Enjin Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !exclude_pages_formats && !exclude_pages_format_njn
package njn_tagcloud
import (
"fmt"
"html/template"
"github.com/urfave/cli/v2"
"github.com/go-enjin/be/features/pages/tagcloud"
"github.com/go-enjin/be/pkg/feature"
)
const (
Tag feature.Tag = "tagcloud-njn-block"
)
var (
_ Block = (*CBlock)(nil)
_ MakeBlock = (*CBlock)(nil)
)
type Block interface {
feature.EnjinBlock
}
type MakeBlock interface {
SetTagCloud(tag feature.Tag) MakeBlock
Make() Block
}
type CBlock struct {
feature.CEnjinBlock
providerTag feature.Tag
provider feature.TagCloudProvider
}
func New() (field MakeBlock) {
f := new(CBlock)
f.Init(f)
f.PackageTag = Tag
f.FeatureTag = Tag
f.providerTag = tagcloud.Tag
return f
}
func (f *CBlock) Init(this interface{}) {
f.CEnjinBlock.Init(this)
}
func (f *CBlock) SetTagCloud(tag feature.Tag) MakeBlock {
f.providerTag = tag
return f
}
func (f *CBlock) Make() Block {
return f
}
func (f *CBlock) PostStartup(ctx *cli.Context) (err error) {
if f.providerTag.IsNil() {
err = fmt.Errorf("%v feature requires .SetTagCloud", f.Tag())
return
}
if f.provider, err = feature.GetTyped[feature.TagCloudProvider](f.providerTag, f.Enjin.Features().List()); f.provider == feature.TagCloudProvider(nil) {
err = fmt.Errorf("%v feature did not find the %q feature.TagCloudProvider", f.Tag(), f.providerTag)
}
return
}
func (f *CBlock) NjnClass() (tagClass feature.NjnClass) {
tagClass = feature.InlineNjnClass
return
}
func (f *CBlock) NjnBlockType() (name string) {
name = "tagcloud"
return
}
func (f *CBlock) PrepareBlock(re feature.EnjinRenderer, blockType string, data map[string]interface{}) (block map[string]interface{}, redirect string, err error) {
if blockType != "tagcloud" {
err = fmt.Errorf("%v does not implement %v block type", f.Tag(), blockType)
return
}
var blockDataContent map[string]interface{}
if blockDataContent, err = re.PrepareGenericBlockData(data["content"]); err != nil {
return
}
block = re.PrepareGenericBlock("tagcloud", data)
if heading, ok := re.PrepareBlockHeader(blockDataContent); ok {
block["Heading"] = heading
}
tc := f.provider.GetTagCloud()
tc.Sort()
block["TagCloud"] = tc
if footer, ok := re.PrepareBlockFooter(blockDataContent); ok {
block["Footer"] = footer
}
block["SiteContext"] = re.RequestContext()
return
}
func (f *CBlock) RenderPreparedBlock(re feature.EnjinRenderer, block map[string]interface{}) (html template.HTML, err error) {
html, err = re.RenderNjnTemplate("block/tagcloud", block)
return
}
func (f *CBlock) ProcessBlock(re feature.EnjinRenderer, blockType string, data map[string]interface{}) (html template.HTML, redirect string, err error) {
if block, redir, e := f.PrepareBlock(re, blockType, data); e != nil {
err = e
return
} else if redir != "" {
redirect = redir
return
} else {
html, err = f.RenderPreparedBlock(re, block)
}
return
}