Repository navigation
Expand file tree
/
Copy pathindex.go
More file actions
129 lines (113 loc) · 2.96 KB
/
Copy pathindex.go
File metadata and controls
129 lines (113 loc) · 2.96 KB
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
package main
import (
"os"
"path/filepath"
"sort"
"strings"
"github.com/antonmedv/gitmal/pkg/git"
"github.com/antonmedv/gitmal/pkg/links"
"github.com/antonmedv/gitmal/pkg/templates"
)
func generateIndex(files []git.Blob, params Params) error {
// Build directory indexes
type dirInfo struct {
subdirs map[string]struct{}
files []git.Blob
}
dirs := map[string]*dirInfo{}
ensureDir := func(p string) *dirInfo {
if di, ok := dirs[p]; ok {
return di
}
di := &dirInfo{subdirs: map[string]struct{}{}, files: []git.Blob{}}
dirs[p] = di
return di
}
dirsSet := links.BuildDirSet(files)
filesSet := links.BuildFileSet(files)
for _, b := range files {
// Normalize to forward slash paths for URL construction
p := b.Path
parts := strings.Split(p, "/")
// walk directories
cur := ""
for i := 0; i < len(parts)-1; i++ {
child := parts[i]
ensureDir(cur).subdirs[child] = struct{}{}
if cur == "" {
cur = child
} else {
cur = cur + "/" + child
}
ensureDir(cur) // ensure it exists
}
ensureDir(cur).files = append(ensureDir(cur).files, b)
}
di := dirs[""] // root
outDir := params.OutputDir
if err := os.MkdirAll(outDir, 0o755); err != nil {
return err
}
// Build entries
dirNames := make([]string, 0, len(di.subdirs))
for name := range di.subdirs {
dirNames = append(dirNames, name)
}
// Sort for stable output
sort.Strings(dirNames)
sort.Slice(di.files, func(i, j int) bool {
return di.files[i].FileName < di.files[j].FileName
})
subdirEntries := make([]templates.ListEntry, 0, len(dirNames))
for _, name := range dirNames {
subdirEntries = append(subdirEntries, templates.ListEntry{
Name: name + "/",
Href: "blob/" + params.Ref.DirName() + "/" + name + "/index.html",
IsDir: true,
})
}
fileEntries := make([]templates.ListEntry, 0, len(di.files))
for _, b := range di.files {
fileEntries = append(fileEntries, templates.ListEntry{
Name: b.FileName + "",
Href: "blob/" + params.Ref.DirName() + "/" + b.FileName + ".html",
Mode: b.Mode,
Size: humanizeSize(b.Size),
})
}
// Title and current path label
title := params.Name
f, err := os.Create(filepath.Join(outDir, "index.html"))
if err != nil {
return err
}
rootHref := "./"
readmeHTML := readme(di.files, dirsSet, filesSet, params, rootHref)
err = templates.ListTemplate.ExecuteTemplate(f, "layout.gohtml", templates.ListParams{
LayoutParams: templates.LayoutParams{
Title: title,
Name: params.Name,
Dark: params.Dark,
CSSMarkdown: cssMarkdown(params.Dark),
RootHref: rootHref,
CurrentRefDir: params.Ref.DirName(),
Selected: "code",
},
HeaderParams: templates.HeaderParams{
Ref: params.Ref,
Breadcrumbs: breadcrumbs(params.Name, "", false),
},
Ref: params.Ref,
Dirs: subdirEntries,
Files: fileEntries,
Readme: readmeHTML,
})
if err != nil {
_ = f.Close()
return err
}
if err := f.Close(); err != nil {
return err
}
return nil
}