1
1
package main
2
2
3
3
import (
4
+ "errors"
4
5
"flag"
5
6
"log"
6
7
"os"
@@ -91,7 +92,13 @@ func templCSSSort(flags Flags) {
91
92
originalContent := string (content )
92
93
assert (originalContent != "" , "File is empty" )
93
94
94
- newContent := processContent (originalContent )
95
+ newContent , err := processContent (originalContent )
96
+ if err != nil {
97
+ if err .Error () == "no_classes_found" {
98
+ continue
99
+ }
100
+ log .Fatal (err )
101
+ }
95
102
96
103
// write the modified content back to the file
97
104
err = os .WriteFile (file , []byte (newContent ), 0644 )
@@ -103,29 +110,39 @@ func templCSSSort(flags Flags) {
103
110
log .Println ("Done in" , time .Since (start ))
104
111
}
105
112
106
- func processContent (content string ) string {
113
+ func processContent (content string ) ( string , error ) {
107
114
// find all classes in content
108
115
re := regexp .MustCompile (`class="([^"]+)"` )
109
116
matches := re .FindAllStringSubmatch (content , - 1 )
110
- assert (len (matches ) > 0 , "No classes found" )
117
+ if len (matches ) == 0 {
118
+ return "" , errors .New ("no_classes_found" )
119
+ }
111
120
112
121
for _ , match := range matches {
113
122
classList := match [1 ]
114
- assert (classList != "" , "Class list is empty" )
123
+ if classList == "" {
124
+ continue
125
+ }
115
126
116
127
// trim in place
117
128
classList = strings .TrimSpace (classList )
118
- assert (classList != "" , "Class list is empty" )
129
+ if classList == "" {
130
+ continue
131
+ }
119
132
120
133
// any whitespace bigger then 1 char, reduce to 1 char
121
134
for strings .Contains (classList , " " ) {
122
135
classList = strings .ReplaceAll (classList , " " , " " )
123
136
}
124
- assert (classList != "" , "Class list is empty" )
137
+ if classList == "" {
138
+ continue
139
+ }
125
140
126
141
// split
127
142
classes := strings .Split (classList , " " )
128
- assert (len (classes ) > 0 , "No classes found" )
143
+ if len (classes ) == 0 {
144
+ continue
145
+ }
129
146
130
147
// sort
131
148
sort .Strings (classes )
@@ -135,14 +152,18 @@ func processContent(content string) string {
135
152
136
153
// create new class list string
137
154
newClassList := strings .Join (classes , " " )
138
- assert (newClassList != "" , "New class list is empty" )
155
+ if newClassList == "" {
156
+ continue
157
+ }
139
158
140
159
// replace class list in file
141
160
content = strings .Replace (content , match [0 ], "class=\" " + newClassList + "\" " , - 1 )
142
- assert (content != "" , "New content is empty" )
161
+ if content == "" {
162
+ continue
163
+ }
143
164
}
144
165
145
- return content
166
+ return content , nil
146
167
}
147
168
148
169
func removeDuplicates (slice []string ) []string {
0 commit comments