-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
81 lines (76 loc) · 2.16 KB
/
file.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
package main
import (
"fmt"
"html/template"
"io/fs"
"os"
"path"
"path/filepath"
"strings"
)
type filePage struct {
RepoData repoData
Mode string
Name string
Size string
Origin string
Extension string
CanRender bool
Destination string
DestinationDir string
Content template.HTML
}
func (f *filePage) renderPage(t *template.Template) {
debug("file %v %v", f.RepoData.Name, f.Name)
err := os.MkdirAll(f.DestinationDir, 0777)
checkErr(err)
err = os.MkdirAll(filepath.Dir(f.Destination), 0777)
checkErr(err)
output, err := os.Create(f.Destination)
checkErr(err)
err = t.Execute(output, f)
checkErr(err)
}
func renderIndividualFilePages(data repoData) {
t, err := template.ParseFS(htmlTemplates, "template.file.html", "template.partials.html")
checkErr(err)
err = filepath.Walk(data.cloneDir(), func(filename string, info fs.FileInfo, err error) error {
if info.IsDir() && info.Name() == ".git" {
return filepath.SkipDir
}
if !info.IsDir() {
ext := filepath.Ext(filename)
_, canRenderExtension := stt.TextExtensions[ext]
_, canRenderByFullName := stt.PlainFiles[filepath.Base(filename)]
canRender := canRenderExtension || canRenderByFullName
partialPath, _ := strings.CutPrefix(filename, data.cloneDir())
destDir := path.Join(args.OutputDir, data.Name, "files", partialPath)
outputName := path.Join(args.OutputDir, data.Name, "files", partialPath, "index.html")
var content template.HTML
info, err := os.Stat(filename)
checkErr(err)
if canRender {
fileBytes, err := os.ReadFile(filename)
checkErr(err)
fileStr := string(fileBytes)
highlighted := highlight(destDir, &fileStr)
checkErr(err)
content = template.HTML(highlighted)
}
(&filePage{
RepoData: data,
Mode: info.Mode().String(),
Size: fmt.Sprintf("%v", info.Size()),
Name: strings.TrimPrefix(partialPath, "/"),
Extension: ext,
CanRender: canRender,
Origin: filename,
Destination: outputName,
DestinationDir: destDir,
Content: content,
}).renderPage(t)
}
return nil
})
checkErr(err)
}