/
indexrenderer.go
62 lines (53 loc) · 1.23 KB
/
indexrenderer.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
package httpd
import (
_ "embed"
"fmt"
"html/template"
"strings"
)
//go:embed template.html
var templateRaw string
type fileContentsFragment struct {
Name string `json:"name"`
Link string `json:"link"`
Icon string `json:"icon"`
Annotation string `json:"annotation"`
}
// Used for displaying a directory index.
func renderDirectoryIndexPage(dirName string, files []fileContentsFragment, ReadOnly bool) string {
tpl, err := template.New("directoryIndexPage").Parse(templateRaw)
if err != nil {
return fmt.Sprintf("%v\n", err)
}
type TemplateInputDirComponent struct {
Link string
Name string
}
type TemplateInput struct {
DirName string
DirComponents []TemplateInputDirComponent
Files []fileContentsFragment
ReadOnly bool
}
var components []TemplateInputDirComponent
link := ""
for i, part := range strings.Split(dirName, "/") {
link = link + part + "/"
if i == 0 {
part = "root"
}
components = append(components, TemplateInputDirComponent{
Link: link[:],
Name: part,
})
}
input := TemplateInput{
DirName: dirName,
DirComponents: components,
Files: files,
ReadOnly: ReadOnly,
}
builder := strings.Builder{}
_ = tpl.Execute(&builder, input)
return builder.String()
}