-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.go
94 lines (83 loc) · 1.81 KB
/
index.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
package template
import (
"bytes"
_ "embed"
"html"
"io/fs"
"slices"
"sort"
"strings"
"time"
)
var (
//go:embed index-0.html
indexRaw0 []byte
//go:embed index-1.html
indexRaw1 []byte
//go:embed index-2.html
indexRaw2 []byte
)
func init() {
indexRaw0 = minify(indexRaw0)
indexRaw1 = minify(indexRaw1)
indexRaw2 = minify(indexRaw2)
}
func Index(path string, entrys []fs.FileInfo) []byte {
// Prepare files for index
entrys = slices.DeleteFunc(entrys, func(entry fs.FileInfo) bool {
return strings.HasPrefix(entry.Name(), ".")
})
sort.Slice(entrys, func(i, j int) bool {
if entrys[i].IsDir() != entrys[j].IsDir() {
return entrys[i].IsDir()
}
return entrys[i].Name() < entrys[j].Name()
})
// Headers
buff := bytes.Buffer{}
buff.Write(indexRaw0)
htmlPath(&buff, path)
buff.Write(indexRaw1)
// File list
readme := ""
for _, entry := range entrys {
if entry.IsDir() {
buff.WriteString(`<br><div>-</div>`)
} else {
t := entry.ModTime().UTC().Format(time.RFC3339)
buff.WriteString(`<time datetime="`)
buff.WriteString(t)
buff.WriteString(`">`)
buff.WriteString(t)
buff.WriteString(`</time>`)
buff.WriteString(`<div>`)
htmlSize(&buff, entry.Size())
buff.WriteString(`</div>`)
}
name := entry.Name()
if entry.IsDir() {
name += "/"
}
nameEscaped := html.EscapeString(name)
switch strings.ToLower(name) {
case "readme", "readme.md", "readme.txt":
readme = nameEscaped
}
buff.WriteString(`<a href="`)
buff.WriteString(nameEscaped)
buff.WriteString(`">`)
buff.WriteString(nameEscaped)
buff.WriteString(`</a>`)
buff.WriteString(``)
}
buff.WriteString(`</div>`)
// Readme
if readme != "" {
buff.WriteString(`<pre id=r>`)
buff.WriteString(readme)
buff.WriteString(`</pre>`)
}
// Add script
buff.Write(indexRaw2)
return buff.Bytes()
}