-
Notifications
You must be signed in to change notification settings - Fork 6
/
Value.go
119 lines (104 loc) · 2.27 KB
/
Value.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
package cssminify
import (
"bytes"
"encoding/base64"
"io/ioutil"
"mime"
"net/http"
"path"
"regexp"
"strings"
)
func minifyVal(value string, file string) string {
value = cleanSpaces(value)
// Values need special care
value = cleanHex(value)
value = cleanUrl(value, file)
return value
}
func cleanHex(value string) string {
re := regexp.MustCompile(`#(\d{6})`)
matches := re.FindAllString(value, -1)
for _, hex := range matches {
if isFull(hex) {
r := strings.NewReplacer(hex, newHex(hex))
value = r.Replace(value)
}
}
return value
}
func isFull(hex string) bool {
cmp := []byte(hex)[1:]
for _, l := range cmp {
if cmp[0] != l {
return false
}
}
return true
}
func newHex(hex string) string {
return string([]byte(hex)[:4])
}
func cleanUrl(value string, file string) string {
re := regexp.MustCompile(`url\((.*)\)`)
matches := re.FindStringSubmatch(value)
if len(matches) != 2 {
return value
}
img := removeQuotes(matches[1])
if string([]byte(img)[0:4]) == "http" {
img = getWebImg(img)
} else {
img = getLocalImg(img, file)
}
return img
}
/**
* If there are quotes or double quotes around the url, take them off.
*/
func removeQuotes(img string) string {
bytedImg := []byte(img)
first := bytedImg[0]
if first == '\'' || first == '"' {
return string(bytedImg[1 : len(bytedImg)-1])
}
return img
}
func getWebImg(img string) string {
resp, err := http.Get(img)
if err != nil {
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
panic(err)
}
return writeWebUrl(body, resp)
}
func getLocalImg(img string, file string) string {
body, err := ioutil.ReadFile(path.Dir(file) + "/" + img)
if err != nil {
panic(err)
}
return writeUrl(body, mime.TypeByExtension(img))
}
func base64Encode(data []byte) string {
var buf bytes.Buffer
encoder := base64.NewEncoder(base64.StdEncoding, &buf)
encoder.Write(data)
encoder.Close()
return buf.String()
}
func writeWebUrl(body []byte, resp *http.Response) string {
return writeUrl(body, resp.Header.Get("Content-Type"))
}
func writeUrl(body []byte, mime string) string {
var buf bytes.Buffer
buf.WriteString("url(data:")
buf.WriteString(mime)
buf.WriteString(";base64,")
buf.WriteString(base64Encode(body))
buf.WriteString(")")
return buf.String()
}