Skip to content

Commit

Permalink
extract content processing
Browse files Browse the repository at this point in the history
  • Loading branch information
mauricedesaxe committed Jul 19, 2024
1 parent 9f0d119 commit 4920137
Showing 1 changed file with 42 additions and 54 deletions.
96 changes: 42 additions & 54 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,57 +91,58 @@ func templCSSSort(flags Flags) {
originalContent := string(content)
assert(originalContent != "", "File is empty")

// find all classes in templ file
re := regexp.MustCompile(`class="([^"]+)"`)
matches := re.FindAllStringSubmatch(originalContent, -1)
assert(len(matches) > 0, "No classes found")

for _, match := range matches {
classList := match[1]
assert(classList != "", "Class list is empty")

// trim in place
classList = strings.TrimSpace(classList)
assert(classList != "", "Class list is empty")

// any whitespace bigger then 1 char, reduce to 1 char
for strings.Contains(classList, " ") {
classList = strings.ReplaceAll(classList, " ", " ")
}
assert(classList != "", "Class list is empty")
newContent := processContent(originalContent)

// split
classes := strings.Split(classList, " ")
assert(len(classes) > 0, "No classes found")
// write the modified content back to the file
err = os.WriteFile(file, []byte(newContent), 0644)
if err != nil {
log.Fatal(err)
}
}

// sort
sort.Strings(classes)
log.Println("Done in", time.Since(start))
}

// remove duplicates
classes = removeDuplicates(classes)
func processContent(content string) string {
// find all classes in content
re := regexp.MustCompile(`class="([^"]+)"`)
matches := re.FindAllStringSubmatch(content, -1)
assert(len(matches) > 0, "No classes found")

// create new class list string
newClassList := strings.Join(classes, " ")
assert(newClassList != "", "New class list is empty")
for _, match := range matches {
classList := match[1]
assert(classList != "", "Class list is empty")

// log diff
if flags.dev {
logDiff(file, classList, newClassList)
}
// trim in place
classList = strings.TrimSpace(classList)
assert(classList != "", "Class list is empty")

// replace class list in file
originalContent = strings.Replace(originalContent, match[0], "class=\""+newClassList+"\"", -1)
assert(originalContent != "", "New content is empty")
// any whitespace bigger then 1 char, reduce to 1 char
for strings.Contains(classList, " ") {
classList = strings.ReplaceAll(classList, " ", " ")
}
assert(classList != "", "Class list is empty")

// write the modified content back to the file
err = os.WriteFile(file, []byte(originalContent), 0644)
if err != nil {
log.Fatal(err)
}
// split
classes := strings.Split(classList, " ")
assert(len(classes) > 0, "No classes found")

// sort
sort.Strings(classes)

// remove duplicates
classes = removeDuplicates(classes)

// create new class list string
newClassList := strings.Join(classes, " ")
assert(newClassList != "", "New class list is empty")

// replace class list in file
content = strings.Replace(content, match[0], "class=\""+newClassList+"\"", -1)
assert(content != "", "New content is empty")
}

log.Println("Done in", time.Since(start))
return content
}

func removeDuplicates(slice []string) []string {
Expand All @@ -156,16 +157,3 @@ func removeDuplicates(slice []string) []string {
assert(len(list) > 0, "No classes found")
return list
}

func logDiff(file, oldClassList, newClassList string) {
const (
red = "\033[31m"
green = "\033[32m"
reset = "\033[0m"
)

log.Println("===")
log.Println("File:", file)
log.Println("Old:", red, oldClassList, reset)
log.Println("New:", green, newClassList, reset)
}

0 comments on commit 4920137

Please sign in to comment.