Repository navigation
Expand file tree
/
Copy pathruntime.go
More file actions
228 lines (197 loc) · 5.25 KB
/
Copy pathruntime.go
File metadata and controls
228 lines (197 loc) · 5.25 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
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
}
// TemplateFunc adapts a function that renders with the slots assigned to its
// enclosing slot template.
type TemplateFunc func(ctx context.Context, w io.Writer, slots Slots) error
func (f TemplateFunc) Render(ctx context.Context, w io.Writer) error {
return f(ctx, w, Slots{})
}
// Fragment renders templates in slice order.
type Fragment []Template
func (f Fragment) Render(ctx context.Context, w io.Writer) error {
for _, template := range f {
if err := template.Render(ctx, w); err != nil {
return err
}
}
return nil
}
// Slots is the named fragments assigned to a slot template.
type Slots map[string]Fragment
// Has reports whether name has an assigned fragment, including an explicitly
// empty assignment. The returned fragment is a copy.
func (s Slots) Has(name string) (Fragment, bool) {
fragment, ok := s[name]
return slices.Clone(fragment), ok
}
// SlotTemplate is an immutable template value with named template lists.
// Slot returns a copied value with a named list replaced.
type SlotTemplate struct {
template TemplateFunc
declared map[string]struct{}
slots map[string]Fragment
err error
}
// NewSlotTemplate creates a template that accepts the provided slot names.
func NewSlotTemplate(template TemplateFunc, slotNames ...string) SlotTemplate {
declared := make(map[string]struct{}, len(slotNames))
for _, slotName := range slotNames {
declared[slotName] = struct{}{}
}
return SlotTemplate{template: template, declared: declared}
}
// Render fails before rendering when composition contains an unknown slot.
func (t SlotTemplate) Render(ctx context.Context, w io.Writer) error {
if t.err != nil {
return t.err
}
return t.template(ctx, w, t.slots)
}
// Slot returns a copied template with slotName replaced by templates. An
// undeclared slot is recorded and returned from Render before any output.
func (t SlotTemplate) Slot(slotName string, templates ...Template) SlotTemplate {
if _, ok := t.declared[slotName]; !ok {
t.err = errors.Join(t.err, fmt.Errorf("goht: unknown slot %q", slotName))
return t
}
slots := make(map[string]Fragment, len(t.slots)+1)
for name, fragment := range t.slots {
slots[name] = slices.Clone(fragment)
}
fragment := slices.Clone(templates)
if fragment == nil {
fragment = Fragment{}
}
slots[slotName] = fragment
t.slots = slots
return t
}
// little nuke alligators that eat whitespace; silly but important
const (
NukeAfter = "~☢<"
NukeBefore = ">☢~"
)
var nukeWhitespaceRe = regexp.MustCompile(NukeAfter + `\s*|\s*` + NukeBefore)
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 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, "_")
}