MCPcopy Create free account
hub / github.com/antonmedv/gitmal / postProcessHTML

Function postProcessHTML

post_process.go:22–121  ·  view source on GitHub ↗
(root string, doMinify bool, doGzip bool)

Source from the content-addressed store, hash-verified

20)
21
22func postProcessHTML(root string, doMinify bool, doGzip bool) error {
23 // 1) Collect all HTML files first
24 var files []string
25 if err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
26 if err != nil {
27 return err
28 }
29 if d.IsDir() {
30 return nil
31 }
32 if strings.HasSuffix(d.Name(), ".html") {
33 files = append(files, path)
34 }
35 return nil
36 }); err != nil {
37 return err
38 }
39
40 if len(files) == 0 {
41 return nil
42 }
43
44 // 2) Setup progress bar
45 labels := []string{}
46 if doMinify {
47 labels = append(labels, "minify")
48 }
49 if doGzip {
50 labels = append(labels, "gzip")
51 }
52 pb := progress_bar.NewProgressBar(strings.Join(labels, " + "), len(files))
53 defer pb.Done()
54
55 // 3) Worker pool
56 workers := runtime.NumCPU()
57 if workers < 1 {
58 workers = 1
59 }
60 jobs := make(chan string, workers*2)
61 var wg sync.WaitGroup
62 var mu sync.Mutex
63 var firstErr error
64
65 workerFn := func() {
66 defer wg.Done()
67 var m *minify.M
68 if doMinify {
69 m = minify.New()
70 m.AddFunc("text/html", html.Minify)
71 m.AddFunc("text/css", css.Minify)
72 m.AddFunc("image/svg+xml", svg.Minify)
73 }
74 for path := range jobs {
75 data, err := os.ReadFile(path)
76 if err == nil && doMinify {
77 if md, e := m.Bytes("text/html", data); e == nil {
78 data = md
79 } else {

Callers 1

mainFunction · 0.85

Calls 5

DoneMethod · 0.95
IncMethod · 0.95
NewProgressBarFunction · 0.92
writeGzipFunction · 0.85
NewMethod · 0.80

Tested by

no test coverage detected