-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
179 lines (151 loc) · 3.3 KB
/
main.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
// You can edit this code!
// Click here and start typing.
package gotml
import (
"fmt"
html "html"
"os"
)
type GotmlTree struct {
terminalTagName *string
gotmlFunc *Component
attrs *AttrList
children []GotmlTree
textContent string
}
type Bag = map[string]interface{}
type AttrNode struct {
Key string
Value interface{}
}
type Component func(*AttrList, ...GotmlTree) GotmlTree
type AttrList struct {
This AttrNode
Next *AttrList
}
func Tree(tagName any) GotmlTree {
var terminalTagName *string
var gotmlFunc *Component
switch any(tagName).(type) {
case string:
m := any(tagName).(string)
if m == "#text" {
fmt.Fprintf(os.Stderr, "Text is not allowed as the top level. Use #fragment instead.")
}
terminalTagName = &m
default:
casted, ok := any(tagName).(Component)
if !ok {
fmt.Fprintf(os.Stderr, "Unsupported tag type. Perhaps you forgot to declare it as a Component?")
} else {
gotmlFunc = &casted
}
}
return GotmlTree{
terminalTagName: terminalTagName,
gotmlFunc: gotmlFunc,
}
}
func (g GotmlTree) Attr(key string, value interface{}) GotmlTree {
copied := g
copied.attrs = &AttrList{
This: AttrNode{Key: key, Value: value},
Next: copied.attrs,
}
return copied
}
func (g GotmlTree) Children(children ...any) GotmlTree {
copied := g
copied.children = []GotmlTree{}
for _, inst := range children {
switch any(inst).(type) {
case GotmlTree:
// Child instruction
casted := any(inst).(GotmlTree)
copied.children = append(copied.children, casted)
case string:
// Child instruction (text node)
casted := any(inst).(string)
t := "#text"
v := GotmlTree{
terminalTagName: &t,
textContent: casted,
}
copied.children = append(copied.children, v)
default:
fmt.Fprintf(os.Stderr, "Unsupported child type. Perhaps you need AsAny(...).")
}
}
return copied
}
func Render(ctx Bag, tree GotmlTree) string {
if tree.gotmlFunc != nil {
f := *tree.gotmlFunc
return Render(
ctx,
f(tree.attrs, tree.children...),
)
}
tagName := *tree.terminalTagName
if tagName == "#text" {
return html.EscapeString(tree.textContent)
}
isFragment := tagName == "#fragment"
result := ""
if !isFragment {
result += "<" + tagName
}
if tree.attrs != nil && isFragment {
fmt.Fprintf(os.Stderr, "Document fragments cannot have attributes")
} else {
cur := tree.attrs
for cur != nil {
name := cur.This.Key
val := cur.This.Value
result += " "
result += name
result += "=\""
valAsString, ok := any(val).(string)
if !ok {
fmt.Fprintf(os.Stderr, "Could not serialize attribute %s", name)
}
result += html.EscapeString(valAsString)
result += "\""
cur = cur.Next
}
}
if len(tree.children) > 0 {
if !isFragment {
result += ">"
}
for _, child := range tree.children {
result += Render(ctx, child)
}
if !isFragment {
result += "</"
result += tagName
result += ">"
}
return result
}
return result + " />"
}
func Attr(k string, v interface{}) AttrNode {
return AttrNode{Key: k, Value: v}
}
func (list *AttrList) ToBag() Bag {
b := Bag{}
cur := list
for cur != nil {
b[cur.This.Key] = cur.This.Value
cur = cur.Next
}
return b
}
func AsAny(children []GotmlTree) []any {
r := make([]any, len(children))
for i, c := range children {
r[i] = c
}
return r
}