-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
139 lines (119 loc) · 3.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
139
package main
import (
"flag"
"fmt"
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
)
func main() {
var baseDirFlag, iconsDirFlag, outDir string
var attBool, uidBool bool
flag.StringVar(&baseDirFlag, "b", "", "Base directory to scan for icons")
flag.StringVar(&iconsDirFlag, "i", "", "Specific directory within the base directory containing SVG icons")
flag.StringVar(&outDir, "o", "dist", "Output subdirectory to save cleaned icons")
flag.BoolVar(&attBool, "a", true, "Enable or disable writing attribution details to a file")
flag.BoolVar(&uidBool, "u", true, "Enable or disabled keeping the UID of the filename")
flag.Parse()
args := flag.Args()
// Determine base directory
baseDir := baseDirFlag
if baseDir == "" {
if len(args) > 0 {
baseDir = args[0]
} else {
baseDir = "." // Use current directory as default
}
}
// Resolve the absolute path of the base directory
absBaseDir, err := filepath.Abs(baseDir)
if err != nil {
fmt.Printf("Error resolving absolute path of base directory: %v\n", err)
return
}
// Determine icons directory
var iconsFullPath string
if iconsDirFlag != "" {
// If -i is specified, use it as the path for icons
iconsFullPath = filepath.Join(absBaseDir, iconsDirFlag)
} else {
// Use the base directory itself for icons if -i is not provided
iconsFullPath = absBaseDir
}
// Resolve the output directory
outFullPath := filepath.Join(absBaseDir, outDir)
if err := os.MkdirAll(outFullPath, os.ModePerm); err != nil {
fmt.Println("Error creating output directory:", err)
return
}
files, err := os.ReadDir(iconsFullPath)
if err != nil {
fmt.Println("Error reading icons directory:", err)
return
}
var attributionDetails string
for _, file := range files {
if file.IsDir() {
continue
}
fileName := file.Name()
if filepath.Ext(fileName) != ".svg" {
continue
}
content, err := os.ReadFile(filepath.Join(iconsFullPath, fileName))
if err != nil {
fmt.Printf("Error reading file %s: %v\n", fileName, err)
continue
}
attributionNote, cleanedContent := cleanSVG(string(content), fileName)
if attBool {
attributionDetails += attributionNote + "\n"
}
// Remove prefix and write the cleaned file
newFileName := removePrefix(fileName, uidBool)
newFilePath := filepath.Join(outFullPath, newFileName)
if err := os.WriteFile(newFilePath, []byte(cleanedContent), fs.ModePerm); err != nil {
fmt.Printf("Error writing cleaned file %s: %v\n", newFileName, err)
continue
}
fmt.Printf("Cleaned file written: %s\n", newFileName)
}
if attBool && attributionDetails != "" {
attributionPath := filepath.Join(absBaseDir, "attribution.txt")
if err := os.WriteFile(attributionPath, []byte(attributionDetails), fs.ModePerm); err != nil {
fmt.Printf("Error writing attribution details: %v\n", err)
return
}
fmt.Println("Attribution details written to", attributionPath)
}
}
func cleanSVG(content, fileName string) (string, string) {
attributionNote := fileName + ": "
textRegex := regexp.MustCompile(`<text.*?>(.*?)<\/text>`)
matches := textRegex.FindAllStringSubmatch(content, -1)
if len(matches) == 0 {
attributionNote += "No attribution detail"
} else {
for _, match := range matches {
attributionNote += match[1] + " "
}
}
newContent := textRegex.ReplaceAllString(content, "")
newContent = strings.Replace(newContent, "<svg", `<svg preserveAspectRatio="xMidYMid meet" `, 1)
newContent = regexp.MustCompile(`viewBox=".*?"`).ReplaceAllString(newContent, `viewBox="0 0 100 100"`)
return strings.TrimSpace(attributionNote), newContent
}
// removePrefix removes the prefix from the filename, leaving only the main part.
func removePrefix(fileName string, uidBool bool) string {
parts := strings.SplitN(fileName, "-", 3) // Assuming the format "noun-<name>-<id>.svg"
if len(parts) == 3 {
if uidBool {
return parts[1] + "-" + parts[2] + ".svg" // Returns "<name>-<uid>.svg"
} else {
return parts[1] + ".svg" // Return "<name>.svg"
}
}
return fileName // Return original if not matching expected format
}