-
Notifications
You must be signed in to change notification settings - Fork 0
/
stylesheet.go
102 lines (99 loc) · 2.21 KB
/
stylesheet.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
package go2css
import "strings"
type (
Stylesheet struct {
name string
universe *Universe
rules []*Rule
traitRules map[int][]string
}
StylesheetConfigurator struct {
stylesheet *Stylesheet
universe *Universe
}
)
func (cfg *StylesheetConfigurator) Module(name string, setup func(*ModuleConfigurator)) {
var (
rule = &Rule{
genesisID: MODULE_RULE_GENESIS_ID,
selectors: []string{
"." + name,
},
declarations: []*Declaration{},
}
mcfg = &ModuleConfigurator{
stylesheet: cfg.stylesheet,
rule: rule,
nested: map[string]*Rule{},
components: []string{},
states: []string{},
modifiers: []string{},
}
)
setup(mcfg)
cfg.stylesheet.rules = append(cfg.stylesheet.rules, rule)
for _, rule := range mcfg.nested {
cfg.stylesheet.rules = append(cfg.stylesheet.rules, rule)
}
}
func (s *Stylesheet) Compile() string {
var sb strings.Builder
for _, rule := range s.rules {
if len(rule.declarations) == 0 {
continue
}
sb.WriteString("\n/* ")
sb.WriteString(rule.genesis())
if len(rule.comment) > 0 {
sb.WriteRune(' ')
sb.WriteString(rule.comment)
}
sb.WriteString(" */\n")
selectors := strings.Join(rule.selectors, ",\n")
sb.WriteString(selectors)
sb.WriteString(" {")
for _, declaration := range rule.declarations {
sb.WriteString("\n\t")
sb.WriteString(declaration.property)
sb.WriteString(": ")
sb.WriteString(declaration.value)
sb.WriteRune(';')
}
sb.WriteString("\n\t}")
}
return sb.String()
}
func (s *Stylesheet) traitRule(path []string) (*Rule, bool) {
outer:
for i, tpath := range s.traitRules {
if len(tpath) != len(path) {
continue
}
for j, chunk := range tpath {
if chunk != path[j] {
continue outer
}
}
return s.rules[i], true
}
return nil, false
}
func (s *Stylesheet) includeTrait(path []string) *Rule {
rule, exists := s.traitRule(path)
if exists {
return rule
}
trait, err := s.universe.trait(path)
if err != nil {
panic(err)
}
rule = &Rule{
genesisID: TRAIT_RULE_GENESIS_ID,
selectors: []string{},
declarations: trait.declarations,
comment: strings.Join(path, "/"),
}
s.rules = append(s.rules, rule)
s.traitRules[len(s.rules)-1] = path
return rule
}