-
Notifications
You must be signed in to change notification settings - Fork 11
/
renderer.go
76 lines (60 loc) · 1.28 KB
/
renderer.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
package set
import (
"bytes"
"fmt"
"sort"
"qlova.org/seed"
"qlova.org/seed/use/css"
)
type harvester struct {
queries map[string]*bytes.Buffer
}
func newHarvester() harvester {
return harvester{
make(map[string]*bytes.Buffer),
}
}
func (h harvester) harvest(c seed.Seed) map[string]*bytes.Buffer {
var data data
c.Load(&data)
//Deterministic render.
keys := make([]string, 0, len(data.queries))
for i := range data.queries {
keys = append(keys, string(i))
}
sort.Strings(keys)
for _, query := range keys {
rules := data.queries[query]
if b, ok := h.queries[query]; !ok {
b = new(bytes.Buffer)
h.queries[query] = b
}
b := h.queries[query]
fmt.Fprintf(b, `%v {`, css.Selector(c))
b.WriteString(rules)
fmt.Fprint(b, `}`)
}
for _, child := range c.Children() {
h.harvest(child)
}
return h.queries
}
func init() {
css.RegisterRenderer(func(c seed.Seed) []byte {
var harvested = newHarvester().harvest(c)
var b bytes.Buffer
//Deterministic render.
keys := make([]string, 0, len(harvested))
for i := range harvested {
keys = append(keys, string(i))
}
sort.Strings(keys)
for _, query := range keys {
rules := harvested[query]
fmt.Fprintf(&b, `%v {`, query)
b.Write(rules.Bytes())
fmt.Fprint(&b, `}`)
}
return b.Bytes()
})
}