/
main.go
138 lines (118 loc) · 2.99 KB
/
main.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
// Copyright 2015 Philip Schlump
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
// Pack css Remove extra blank lines.
package main
import (
"fmt"
"io/ioutil"
"os"
"github.com/jessevdk/go-flags"
"github.com/pschlump/css/scanner"
)
var opts struct {
CssFileName string `short:"i" long:"input" description:"Path to css file" default:""`
SourceMapFileName string `short:"s" long:"sourcemap" description:"Path to css file" default:""`
OutFileName string `short:"o" long:"output" description:"Path packed output to css file" default:"out.css"`
Deps string `short:"d" long:"deps" description:"Path packed output to css file" default:""`
Debug bool `short:"D" long:"debug" description:"debug flag" default:"false"`
}
func main() {
_, err := flags.ParseArgs(&opts, os.Args)
if err != nil {
fmt.Printf("Invalid Command Line: %s\n", err)
os.Exit(1)
}
if opts.CssFileName == "" {
fmt.Printf("Usage test1 -s File.css\n")
os.Exit(1)
}
myCSS, err := ioutil.ReadFile(opts.CssFileName)
if err != nil {
fmt.Printf("Errror: %s\n", err)
os.Exit(1)
}
o := ""
d := ""
atKeyword := 0
atImport := 0
s := scanner.New(string(myCSS))
for {
token := s.Next()
if token.Type == scanner.TokenError {
fmt.Printf("Syntax Error: %s\n", token)
os.Exit(2)
} else if token.Type == scanner.TokenEOF {
break
}
if opts.Debug {
fmt.Printf("%+v\n", token)
}
switch token.Type {
case scanner.TokenComment:
case scanner.TokenS:
case scanner.TokenAtKeyword:
o += token.Value
atKeyword = 1
if token.Value == "@import" {
atImport = 1
}
case scanner.TokenString:
o += token.Value
if atImport == 1 {
token.Type = scanner.TokenImportURI
if opts.Deps != "" {
d += "@import " + token.Value + "\n"
}
}
atKeyword, atImport = 0, 0
case scanner.TokenIdent:
fallthrough
case scanner.TokenHash:
fallthrough
case scanner.TokenNumber:
fallthrough
case scanner.TokenPercentage:
fallthrough
case scanner.TokenDimension:
fallthrough
case scanner.TokenUnicodeRange:
fallthrough
case scanner.TokenCDO:
fallthrough
case scanner.TokenCDC:
fallthrough
case scanner.TokenFunction:
fallthrough
case scanner.TokenIncludes:
fallthrough
case scanner.TokenDashMatch:
fallthrough
case scanner.TokenPrefixMatch:
fallthrough
case scanner.TokenSuffixMatch:
fallthrough
case scanner.TokenSubstringMatch:
fallthrough
case scanner.TokenBOM:
o += token.Value
case scanner.TokenChar:
if token.Value == "}" {
o += "}\n"
} else {
o += token.Value
}
case scanner.TokenURI:
o += token.Value
if opts.Deps != "" {
d += token.Value + "\n"
}
}
}
// fmt.Printf("s=%s\n", o)
ioutil.WriteFile(opts.OutFileName, []byte(o), 0644)
if opts.Deps != "" {
ioutil.WriteFile(opts.Deps, []byte(d), 0644)
}
_, _ = atKeyword, atImport
}