-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstyle.go
92 lines (73 loc) · 1.37 KB
/
style.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
package carbon
import (
_ "embed"
"io"
"net/http"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/css"
)
func init() {
BaseCss += baseGoCss
}
//go:embed css/base.css
var BaseCss string
//go:embed css/base-go.css
var baseGoCss string
//go:embed css/font-family.css
var BaseFontCss string
var _m *minify.M
var _styleCache map[string]string
func init() {
_m = minify.New()
_m.AddFunc("text/css", css.Minify)
// initialize style cache
_styleCache = make(map[string]string)
}
var _ Component = (*style)(nil)
type style struct {
attr []Attr
style string
}
func Style(s string) *style {
s, _ = _m.String("text/css", s)
return &style{
attr: nil,
style: s,
}
}
func InlineStyle(href string) *style {
if s, ok := _styleCache[href]; ok {
return &style{
attr: nil,
style: s,
}
}
r, err := http.Get(href)
if err != nil {
panic(err)
}
defer r.Body.Close()
s, err := io.ReadAll(r.Body)
if err != nil {
panic(err)
}
minified, _ := _m.String("text/css", string(s))
_styleCache[href] = minified
return &style{
attr: nil,
style: minified,
}
}
func (s *style) Attr(name string, value string) *style {
s.attr = append(s.attr, Attr{name, value})
return s
}
func (s *style) Render(w io.Writer) {
w.Write([]byte(`<style`))
renderAttrs(w, s.attr)
w.Write([]byte(`>`))
{
io.WriteString(w, s.style)
}
w.Write([]byte(`</style>`))
}