-
Notifications
You must be signed in to change notification settings - Fork 2
/
runtime.go
203 lines (174 loc) · 4.08 KB
/
runtime.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package goht
import (
"bytes"
"context"
"errors"
"fmt"
"html"
"io"
"regexp"
"slices"
"strings"
"sync"
)
// Template is a template that can be rendered into a writer.
type Template interface {
Render(ctx context.Context, w io.Writer) error
}
type TemplateFunc func(ctx context.Context, w io.Writer) error
func (f TemplateFunc) Render(ctx context.Context, w io.Writer) error {
return f(ctx, w)
}
// little nuke alligators that eat whitespace; silly but important
const (
NukeAfter = "~☢<"
NukeBefore = ">☢~"
)
var nukeWhitespaceRe = regexp.MustCompile(NukeAfter + `\s*|\s*` + NukeBefore)
type contextKey int
const (
ctxKey contextKey = iota
)
type ctxValue struct {
children *Template
}
type Buffer struct {
*bytes.Buffer
}
func (b *Buffer) Bytes() []byte {
return nukeWhitespaceRe.ReplaceAll(b.Buffer.Bytes(), nil)
}
var bufferPool = sync.Pool{
New: func() any {
return Buffer{new(bytes.Buffer)}
},
}
func GetBuffer() Buffer {
return bufferPool.Get().(Buffer)
}
func ReleaseBuffer(buf Buffer) {
buf.Reset()
bufferPool.Put(buf)
}
func PopChildren(ctx context.Context) (context.Context, Template) {
var value *ctxValue
ctx, value = getContext(ctx)
if value.children == nil {
return ctx, TemplateFunc(func(ctx context.Context, w io.Writer) error { return nil })
}
children := *value.children
value.children = nil
return ctx, children
}
func PushChildren(ctx context.Context, children Template) context.Context {
value := ctx.Value(ctxKey).(*ctxValue)
value.children = &children
return ctx
}
func initContext(ctx context.Context) context.Context {
if _, ok := ctx.Value(ctxKey).(*ctxValue); ok {
return ctx
}
value := &ctxValue{
children: nil,
}
return context.WithValue(ctx, ctxKey, value)
}
func getContext(ctx context.Context) (context.Context, *ctxValue) {
value, ok := ctx.Value(ctxKey).(*ctxValue)
if !ok {
ctx = initContext(ctx)
value = ctx.Value(ctxKey).(*ctxValue)
}
return ctx, value
}
func CaptureErrors(s string, errs ...error) (string, error) {
return s, errors.Join(errs...)
}
func BuildClassList(classes ...any) (string, error) {
var classList []string
for _, class := range classes {
switch class := class.(type) {
case string:
if class == "" {
continue
}
classList = append(classList, class)
case []string:
classList = append(classList, class...)
case map[string]bool:
for cls, ok := range class {
if ok {
if cls == "" {
continue
}
classList = append(classList, cls)
}
}
default:
return "", fmt.Errorf("goht: invalid class type: %T", class)
}
}
return strings.Join(classList, ` `), nil
}
func BuildAttributeList(attributes ...any) (string, error) {
var attributeList []string
for _, attribute := range attributes {
switch attribute := attribute.(type) {
case map[string]bool:
for key, value := range attribute {
if value {
attributeList = append(attributeList, html.EscapeString(key))
}
}
case map[string]string:
for key, value := range attribute {
attributeList = append(attributeList, html.EscapeString(key)+`="`+html.EscapeString(value)+`"`)
}
default:
return "", fmt.Errorf("goht: invalid attribute type: %T", attribute)
}
}
// for stable ordering of the attributes
slices.Sort(attributeList)
return strings.Join(attributeList, " "), nil
}
func EscapeString(s string) string {
return html.EscapeString(s)
}
func FormatString(format string, value any) string {
return fmt.Sprintf(format, value)
}
type ObjectIDer interface {
ObjectID() string
}
type ObjectClasser interface {
ObjectClass() string
}
func ObjectID(obj any, prefix ...string) string {
ref, ok := obj.(ObjectIDer)
if !ok {
return ""
}
var s []string
if len(prefix) > 0 {
s = append(s, prefix[0])
}
if v, ok := obj.(ObjectClasser); ok {
s = append(s, v.ObjectClass())
}
s = append(s, ref.ObjectID())
return strings.Join(s, "_")
}
func ObjectClass(obj any, prefix ...string) string {
ref, ok := obj.(ObjectClasser)
if !ok {
return ""
}
var s []string
if len(prefix) > 0 {
s = append(s, prefix[0])
}
s = append(s, ref.ObjectClass())
return strings.Join(s, "_")
}