-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Interface.go
87 lines (66 loc) · 1.52 KB
/
Interface.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
package scarlet
import (
"io"
"strings"
"github.com/aerogo/codetree"
)
// Compile compiles the given scarlet code to a CSS string.
func Compile(reader io.Reader, pretty bool) (string, error) {
tree, err := codetree.New(reader)
if err != nil {
return "", err
}
defer tree.Close()
output := strings.Builder{}
state := NewState()
// Parse it
rules, mediaGroups, mediaQueries, animations := compileChildren(tree, nil, state)
// Combine duplicate rules
rules = combineDuplicates(rules)
// CSS variables
if len(state.Variables) > 0 {
if pretty {
output.WriteString(":root {\n")
} else {
output.WriteString(":root{")
}
for _, name := range state.VariableNames {
value := state.Variables[name]
if pretty {
output.WriteString("\t")
}
output.WriteString("--")
output.WriteString(name)
output.WriteByte(':')
if pretty {
output.WriteString(" ")
}
output.WriteString(value)
output.WriteByte(';')
if pretty {
output.WriteString("\n")
}
}
output.WriteString("}")
if pretty {
output.WriteString("\n\n")
}
}
// Render rules
for _, rule := range rules {
rule.Render(&output, pretty)
}
// Render animations
for _, animation := range animations {
animation.Render(&output, pretty)
}
// Render media groups
for _, mediaGroup := range mediaGroups {
mediaGroup.Render(&output, pretty)
}
// Render media queries
for _, mediaQuery := range mediaQueries {
mediaQuery.Render(&output, pretty)
}
return strings.TrimRight(output.String(), "\n"), nil
}