-
Notifications
You must be signed in to change notification settings - Fork 6
/
Minify.go
54 lines (46 loc) · 1.1 KB
/
Minify.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
package cssminify
import (
"fmt"
"regexp"
"strings"
)
func Minify(blocks []Block, file string) {
for _, block := range blocks {
showSelectors(string(block.selector))
fmt.Print("{")
showPropVals(block.pairs, file)
fmt.Print("}")
}
}
func showSelectors(selector string) {
selectors := strings.Split(selector, ",")
for i, sel := range selectors {
fmt.Printf("%s", minifySelector(sel))
if i != len(selectors)-1 {
fmt.Print(",")
}
}
}
func minifySelector(sel string) string {
return cleanSpaces(sel)
}
func showPropVals(pairs []Pair, file string) {
for i, pair := range pairs {
fmt.Printf("%s:%s", minifyProp(string(pair.property)), minifyVal(string(pair.value), file))
// Let's gain some space: semicolons are optional for the last value
if i != len(pairs)-1 {
fmt.Print(";")
}
}
}
func minifyProp(property string) string {
return cleanSpaces(property)
}
func cleanSpaces(str string) string {
str = strings.TrimSpace(str)
re := regexp.MustCompile(`\s\s`)
for str = re.ReplaceAllString(str, " "); re.Find([]byte(str)) != nil; {
str = re.ReplaceAllString(str, " ")
}
return str
}