From 49201375614b5e547c3d13a2fbd282483722c157 Mon Sep 17 00:00:00 2001 From: lazaralex98 Date: Fri, 19 Jul 2024 13:34:05 +0300 Subject: [PATCH] extract content processing --- main.go | 96 +++++++++++++++++++++++++-------------------------------- 1 file changed, 42 insertions(+), 54 deletions(-) diff --git a/main.go b/main.go index 692aec4..22c2b46 100644 --- a/main.go +++ b/main.go @@ -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 { @@ -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) -}