-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.go
211 lines (194 loc) · 5.42 KB
/
render.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
204
205
206
207
208
209
210
211
package gats
import (
"exp/html"
"fmt"
"github.com/dunmatt/goquery"
"io"
"os"
"strings"
)
func RenderTemplateFile(filename string, data interface{}, out io.Writer) error {
rootNode, err := parseFile(filename)
if err != nil {
return err
}
cont := makeContext(data, nil)
template := goquery.NewDocumentFromNode(rootNode).Find("html") // wrap goquery around the DOM
handleGatsRemoves(template) // take out everything that definitely won't be shown
err = handleGatsTranscludes(template) // transclude in all the sub-templates
if err != nil {
return err
}
err = fillInTemplate(template, cont) // process the template
handleGatsOmitTag(template) // make sure this one comes last, it can interfere with other attributes
if err == nil {
html.Render(out, rootNode) // render the DOM back to html and send it off
}
return err
}
func parseFile(filename string) (*html.Node, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
return html.Parse(f)
}
func fillInTemplate(scope *goquery.Selection, cont *context) error {
// filling in the template happens children first so that the closure of
// available data is readily available (if the most senior elements were
// done first there'd be no way to know the reflection path within data)
// NOTE: handleGatsRepeatOvers is mutually recursive with fillInTemplate
for sel := scope.Find("[gatsrepeatover]"); sel.Length() > 0; sel = scope.Find("[gatsrepeatover]") {
handleGatsRepeatOvers(sel.First(), cont)
}
// do the actual work of filling in the template
e := handleGatsIf(scope, cont)
if e != nil {
return e
}
e = handleGatsContent(scope, cont)
if e != nil {
return e
}
e = handleGatsText(scope, cont)
if e != nil {
return e
}
e = handleGatsAttributes(scope, cont)
if e != nil {
return e
}
e = handleGatsAttribute(scope, cont)
if e != nil {
return e
}
return nil
}
func handleGatsRepeatOvers(sel *goquery.Selection, cont *context) {
fieldName, _ := sel.Attr("gatsrepeatover")
length, err := getLength(fieldName, cont)
if err == nil {
for i := 0; i < length; i++ {
c, e := getItem(fieldName, i, cont)
if e == nil {
instance := sel.Clone().InsertBefore(sel)
e1 := fillInTemplate(instance, c) // TODO: stop ignoring the returned errors here
if e1 != nil {
panic(e1) // is this really panic worthy?
}
instance.RemoveAttr("gatsrepeatover")
}
}
}
sel.Remove()
}
func handleGatsRemoves(t *goquery.Selection) {
t.Find("[gatsremove]").Remove()
}
func handleGats(t *goquery.Selection, selector string, meat func(string, *goquery.Selection)) {
attribName := selector[1 : len(selector)-1]
t.Find(selector).Each(func(_ int, sel *goquery.Selection) {
fieldName, _ := sel.Attr(attribName)
meat(fieldName, sel)
sel.RemoveAttr(attribName)
})
return
}
func splitString(val string) (string, string, error) {
index := strings.Index(val, ";")
if index == -1 {
return "", "", fmt.Errorf("Invalid transclude string '%v', contains no semicolon.", val)
}
return val[:index], val[index+1:], nil
}
func handleGatsTranscludes(scope *goquery.Selection) (result error) {
for scope.Find("[gatstransclude]").Length() > 0 && result == nil {
handleGats(scope, "[gatstransclude]", func(ts string, sel *goquery.Selection) {
if result != nil {
return
}
filename, selector, res := splitString(ts)
if res != nil {
result = res
return
}
rootNode, res := parseFile(filename)
if res != nil {
result = res
return
}
newKids := goquery.NewDocumentFromNode(rootNode).Find(selector)
sel.Empty().Append(newKids)
})
}
return result
}
func handleGatsIf(t *goquery.Selection, cont *context) (result error) {
handleGats(t, "[gatsif]", func(fieldName string, sel *goquery.Selection) {
show, res := getBool(fieldName, cont)
if !show {
sel.Remove()
}
result = res
})
return result
}
func handleGatsContent(t *goquery.Selection, cont *context) (result error) {
handleGats(t, "[gatscontent]", func(fieldName string, sel *goquery.Selection) {
if isString(fieldName, cont) {
rawHtml, result := getString(fieldName, cont)
if result == nil {
sel.SetHtml(rawHtml)
}
} else {
node, result := getHtmlNode(fieldName, cont)
if result == nil {
sel.AppendClones(node)
}
}
})
return result
}
func handleGatsAttribute(t *goquery.Selection, cont *context) (result error) {
handleGats(t, "[gatsattribute]", func(val string, sel *goquery.Selection) {
attribute, fieldName, err := splitString(val)
if err != nil {
result = err
return
}
attributeValue, err := getString(fieldName, cont)
if err != nil {
result = err
return
}
sel.SetAttr(attribute, attributeValue)
})
return result
}
func handleGatsAttributes(t *goquery.Selection, cont *context) (result error) {
handleGats(t, "[gatsattributes]", func(fieldName string, sel *goquery.Selection) {
attribs, result := getStringMap(fieldName, cont)
if result == nil {
for k, v := range attribs {
sel.SetAttr(k, v)
}
}
})
return result
}
func handleGatsText(t *goquery.Selection, cont *context) (result error) {
handleGats(t, "[gatstext]", func(fieldName string, sel *goquery.Selection) {
text, result := getString(fieldName, cont)
if result == nil {
sel.SetText(text)
}
})
return result
}
func handleGatsOmitTag(t *goquery.Selection) {
t.Find("[gatsomittag]").Each(func(_ int, parent *goquery.Selection) {
parent.Contents().Remove().InsertBefore(parent)
parent.Remove()
})
}