shroomgit

generate static pages of git repos
git clone https://git.davidvoz.net/shroomgit.git
index
logs
tree

tree.go
1package main
2
3import (
4 "fmt"
5 "io/fs"
6 "log"
7 "os"
8 "path/filepath"
9 "strings"
10)
11
12// TODO make it more tree like
13func treeRepo(topPart string, repo string, output string) {
14 logsDir := filepath.Join(output, "files")
15
16 err := os.MkdirAll(logsDir, 0o755)
17 if err != nil {
18 panic(err)
19 }
20
21 outputFile := filepath.Join(output, "tree.html")
22 file, err := os.Create(outputFile)
23 if err != nil {
24 log.Fatal(err)
25 }
26 defer file.Close()
27
28 file.WriteString(topPart)
29
30 file.WriteString("<table>\n")
31
32 file.WriteString("\n<tr>")
33 file.WriteString("<td>Mode</td>")
34 file.WriteString("<td>Bytes</td>")
35 file.WriteString("</tr>")
36
37 err = filepath.WalkDir(repo, func(path string, d fs.DirEntry, err error) error {
38 relPath, _ := filepath.Rel(repo, path)
39
40 if d.Name() == ".git" {
41 return filepath.SkipDir
42 } else if relPath == "." {
43 return nil
44 } else if d.IsDir() {
45 return nil
46 }
47 info, _ := d.Info()
48
49 file.WriteString("<tr>")
50 file.WriteString("<td>")
51 file.WriteString(info.Mode().String())
52 file.WriteString("&nbsp;</td>")
53 file.WriteString("<td valign=\"top\" align=\"right\">")
54 fmt.Fprintf(file, " %d", info.Size())
55 file.WriteString("</td>\n")
56 file.WriteString("<td>")
57 file.WriteString("<a href=\"files/")
58 file.WriteString(relPath)
59 file.WriteString(".html\">")
60 file.WriteString(relPath)
61 treeFiles(path, output, relPath, repo)
62 file.WriteString("</a>")
63 file.WriteString("</td>")
64
65 file.WriteString("</tr>")
66
67 return nil
68 })
69 if err != nil {
70 log.Fatal(err)
71 }
72
73 file.WriteString("</table>\n")
74
75 file.WriteString("</body>\n")
76 file.WriteString("</html>")
77}
78
79func treeFiles(path string, output string, relPath string, repo string) {
80 outputFile := filepath.Join(output, "files", fmt.Sprintf("%s.html", relPath))
81 os.MkdirAll(filepath.Dir(outputFile), 0o755)
82 file, _ := os.Create(outputFile)
83 defer file.Close()
84
85 depth := strings.Count(relPath, "/")
86 topPart := genTopPart(repo, depth+1)
87
88 file.WriteString(topPart)
89 writePerFile(file, path)
90}