-
-
Notifications
You must be signed in to change notification settings - Fork 138
/
CSS.go
182 lines (144 loc) · 4.8 KB
/
CSS.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
package giu
import (
"fmt"
"strconv"
"strings"
"github.com/mazznoer/csscolorparser"
"github.com/napsy/go-css"
)
// Code in this file allows to apply style using a CSS stylesheet
// The intention of the code looks as follows:
// - The CSS stylesheet is parsed and the resulting rules are stored in giu context
// NOTE: more than one CSS stylesheets can be parsed, however the app could panic if the same tag is present twice
// - CSSTagWidget allows to apply style to a specified layout
// - main tag allows to apply style to the whole application
//
// tools:
// css parser - for now github.com/napsy/go-css. it is a bit poor, but I don't think we need anything more
// css colors - github.com/mazznoer/csscolorparser
//
// docs: docs/css.md
// ErrCSSParse represents a CSS parsing error and includes details about what is failing.
type ErrCSSParse struct {
What string // description of what we are parsing
Value string // the value which failed
Detail error // (optional) error to add extra detail (i.e. result of calling another function like strconv.ParseFloat)
}
func (e ErrCSSParse) Error() string {
errStr := fmt.Sprintf("unable to parse %s: %q", e.What, e.Value)
if e.Detail != nil {
errStr += fmt.Sprintf(" - %s", e.Detail.Error())
}
return errStr
}
// ParseCSSStyleSheet parses CSS stylesheet and stores the rules in giu context.
//
//nolint:gocognit // no
func ParseCSSStyleSheet(data []byte) error {
// css does not support windows formatting
// https://github.com/AllenDang/giu/issues/842
data = []byte(strings.ReplaceAll(string(data), "\r\n", "\n"))
stylesheet, err := css.Unmarshal(data)
if err != nil {
return fmt.Errorf("error marshaling CSS file: %w", err)
}
for rule, style := range stylesheet {
setter := Style()
for styleVarName, styleVarValue := range style {
// convert style variable name to giu style variable name
styleVarID, err := StyleVarIDString(styleVarName)
if err == nil {
if err := parseStyleVar(styleVarValue, func(v float32) {
setter.SetStyleFloat(styleVarID, v)
}, func(x, y float32) {
setter.SetStyle(styleVarID, x, y)
}); err != nil {
return err
}
continue
}
styleColorID, err := StyleColorIDString(styleVarName)
if err == nil {
col, err := csscolorparser.Parse(styleVarValue)
if err != nil {
return ErrCSSParse{What: "color", Value: styleVarValue, Detail: err}
}
setter.SetColor(styleColorID, col)
continue
}
stylePlotVarID, err := StylePlotVarIDString(styleVarName)
if err == nil {
if err := parseStyleVar(styleVarValue, func(v float32) {
setter.SetPlotStyleFloat(stylePlotVarID, v)
}, func(x, y float32) {
setter.SetPlotStyle(stylePlotVarID, x, y)
}); err != nil {
return err
}
continue
}
stylePlotColorID, err := StylePlotColorIDString(styleVarName)
if err == nil {
col, err := csscolorparser.Parse(styleVarValue)
if err != nil {
return ErrCSSParse{What: "color", Value: styleVarValue, Detail: err}
}
setter.SetPlotColor(stylePlotColorID, col)
continue
}
return ErrCSSParse{What: "style variable name", Value: styleVarName}
}
Context.cssStylesheet[string(rule)] = setter
}
return nil
}
func parseStyleVar(styleVarValue string, setFloat func(v float32), setVec2 func(x, y float32)) error {
// the style is StyleVarID - set it
f, err2 := strconv.ParseFloat(styleVarValue, 32)
if err2 == nil {
setFloat(float32(f))
return nil
}
// so maybe it is a vec2 value:
// var-name: x, y;
styleVarValue = strings.ReplaceAll(styleVarValue, " ", "")
vec2 := strings.Split(styleVarValue, ",")
if len(vec2) != 2 {
return ErrCSSParse{What: "value (not float or vec2)", Value: styleVarValue}
}
x, err2 := strconv.ParseFloat(vec2[0], 32)
if err2 != nil {
return ErrCSSParse{What: "value (not float)", Value: vec2[0], Detail: err2}
}
y, err2 := strconv.ParseFloat(vec2[1], 32)
if err2 != nil {
return ErrCSSParse{What: "value (not float)", Value: vec2[1], Detail: err2}
}
setVec2(float32(x), float32(y))
return nil
}
// cssStylesheet is a map tag:StyleSetter.
type cssStylesheet map[string]*StyleSetter
var _ Widget = &CSSTagWidget{}
// CSSTagWidget is a widget that allows to apply CSS style to a specified layout.
type CSSTagWidget struct {
tag string
layout Layout
}
// CSSTag creates CSSTagWidget.
func CSSTag(tag string) *CSSTagWidget {
return &CSSTagWidget{tag: tag}
}
// To specifies a layout to which the style will be applied.
func (c *CSSTagWidget) To(layout ...Widget) *CSSTagWidget {
c.layout = layout
return c
}
// Build implements Widget interface.
func (c *CSSTagWidget) Build() {
// get style from context.
// if it doesn't exist Assert.
style, exists := Context.cssStylesheet[c.tag]
Assert(exists, "CSSTagWidget", "Build", "CSS stylesheet doesn't contain tag: %s", c.tag)
style.To(c.layout).Build()
}