-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
247 lines (202 loc) · 5.28 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"bufio"
"flag"
"log"
"os"
"regexp"
"strings"
) //import
type CssRule struct {
Rule string
Value string
} //struct
// Global variables
var (
I_FILENAME string
O_FILENAME string
) //var
func init() {
// Retrieve flags and set globals
flag.StringVar(&I_FILENAME, "i", "", "'i' parameter must be specificed.")
flag.StringVar(&O_FILENAME, "o", "", "'o' parameter must be specificed.")
flag.Parse()
/*
if strings.TrimSpace(I_FILENAME) == "" {
log.Fatalln("'i' parameter must be specificed.")
} //if
if strings.TrimSpace(O_FILENAME) == "" {
log.Fatalln("'o' parameter must be specificed.")
} //if
*/
} //init
func main() {
var (
err error
cssToAdd string
lines []string
allCssRules = make(map[string][]CssRule)
findInlineCSS = regexp.MustCompile("<(\\w+)\\s.*?style=\"(.*?)\".*?>")
findClasses = regexp.MustCompile("class=\"(.*?)\"")
findId = regexp.MustCompile("id=\"([\\w\\-]+)\"")
) //var
// Extract each line of the file
if lines, err = readFile(I_FILENAME); err != nil {
log.Fatalln(err)
} //if
for _, line := range lines {
temp := extractInlineCSS(line, findInlineCSS, findId, findClasses)
for k, v := range temp {
if _, isSet := allCssRules[k]; isSet {
allCssRules[k] = append(allCssRules[k], v...)
} else {
allCssRules[k] = v
} //else
} //for
} //for
for element, rules := range allCssRules {
cssToAdd += element + " {"
for _, val := range rules {
cssToAdd += "\n\t" + val.Rule + ":\t" + val.Value
} //for
cssToAdd += "\n}\n"
} //for
if err = createCssFile(O_FILENAME, cssToAdd); err != nil {
log.Fatalln(err)
} //if
if err = createSourceFile(
I_FILENAME,
RemoveStyleTags(lines),
); err != nil {
log.Fatalln(err)
} //if
} //main
func extractInlineCSS(line string, findInlineCSS, findId, findClasses *regexp.Regexp) (cssRules map[string][]CssRule) {
cssRules = make(map[string][]CssRule)
styles := findInlineCSS.FindAllStringSubmatch(line, -1)
for _, v := range styles {
var (
id, tag string
eachClass []string
) //var
for pos, _ := range v {
// First position is entire capture
if pos == 0 {
ids := findId.FindAllStringSubmatch(v[pos], -1)
if len(ids) > 0 && len(ids[0]) > 0 {
id = ids[0][1]
} else {
classes := findClasses.FindAllStringSubmatch(v[pos], -1)
for _, allClasses := range classes {
for pos2, classes2 := range allClasses {
if pos2 != 0 {
eachClass = strings.Split(classes2, " ")
} //for
} //if
} //for
} //else
} else if pos == 1 { // Second position is tag name
element := strings.TrimSpace(v[pos])
if len(id) > 0 {
tag = element + "#" + id
} else {
for _, class := range eachClass {
tag += " " + element + "." + class
} //for
} //else
} else if len(tag) > 0 { // pos = 3 is the style tag. We are ignoring tags w/out a class or ID right now
var (
items []string
) //var
items = strings.Split(v[pos], ";")
for _, item := range items {
if len(item) > 0 {
rule := strings.Split(item, ":")
rule[0] = strings.TrimSpace(rule[0])
rule[1] = strings.TrimSpace(rule[1])
if rule[1][len(rule[1])-1:] != ";" {
rule[1] += ";"
} //if
cssRules[strings.TrimSpace(tag)] = append(cssRules[tag],
CssRule{
Rule: rule[0],
Value: rule[1],
}) //append
} //if
} //for
} //else
} //for
} //for
return cssRules
} //extractInlineCSS
func readFile(filename string) (lines []string, err error) {
var (
iFile *os.File
scanner *bufio.Scanner
) //var
// Attempt to open the file
if iFile, err = os.Open(I_FILENAME); err != nil {
return lines, err
} //if
defer iFile.Close()
scanner = bufio.NewScanner(iFile)
// Read in each line of file
for scanner.Scan() {
lines = append(lines, scanner.Text())
} //for
return lines, scanner.Err()
} //readFile
func createCssFile(filename, css string) (err error) {
var (
oFile *os.File
) //var
// Attempt to create the file
if oFile, err = os.OpenFile(filename, os.O_CREATE|os.O_RDWR, 0660); err != nil {
return err
} //if
defer oFile.Close()
// Write to new CSS file
if _, err = oFile.Write([]byte(strings.TrimSpace(css))); err != nil {
return err
} //if
return nil
} //createCssFile
func RemoveStyleTags(lines []string) (output string) {
var (
replaceStyleTags = regexp.MustCompile("(.*?)style=\".*?\"(.*)")
) //var
for _, line := range lines {
var (
styles [][]string
) //var
styles = replaceStyleTags.FindAllStringSubmatch(line, -1)
for _, v := range styles {
for pos, w := range v {
// Ignore first element which is entire line
if pos != 0 {
output += w
} //if
} //for
} //for
// If no style tags were found, use original line
if output == "" {
output += line
} //if
} //for
return output
} //removeStyleTags
func createSourceFile(filename, content string) (err error) {
var (
oFile *os.File
) //var
// Attempt to create the file
if oFile, err = os.OpenFile(filename+"_backup", os.O_CREATE|os.O_RDWR|os.O_APPEND, 0660); err != nil {
return err
} //if
defer oFile.Close()
// Append line to file
if _, err = oFile.Write([]byte(strings.TrimSpace(content) + "\n")); err != nil {
return err
} //if
return err
} //createSourceFile