Adds function to build the menu and manage relpaths
This commit is contained in:
parent
1da04d0f93
commit
5cd7dd9dbb
4 changed files with 64 additions and 58 deletions
86
gitssg.go
86
gitssg.go
|
@ -22,6 +22,7 @@ type RepoDir struct {
|
|||
LastCommit string
|
||||
}
|
||||
|
||||
// ToDo: replace has* with the filename, as it can be bare or .md
|
||||
type RepoInfo struct {
|
||||
Name string
|
||||
Description string
|
||||
|
@ -52,8 +53,31 @@ type CommitLog struct {
|
|||
|
||||
//go:embed templates
|
||||
var embedTmpl embed.FS
|
||||
var funcMap = template.FuncMap{"inc": func(i int) int { return i + 1 }}
|
||||
var timeFormat = "2006-01-02 15:04"
|
||||
var funcMap = template.FuncMap{
|
||||
"inc": func(i int) int {
|
||||
return i + 1
|
||||
},
|
||||
"menu": func(repoInfo *RepoInfo, relpath string) template.HTML {
|
||||
menu := "<a href=\"" + relpath + "log.html\">Log</a>"
|
||||
menu += " | <a href=\"" + relpath + "files.html\">Files</a>"
|
||||
menu += " | <a href=\"" + relpath + "refs.html\">Refs</a>"
|
||||
|
||||
if repoInfo.HasReadme {
|
||||
menu += "| <a href=\"" + relpath + "file/README.html\">README</a>"
|
||||
}
|
||||
|
||||
if repoInfo.HasLicense {
|
||||
menu += "| <a href=\"" + relpath + "file/LICENSE.html\">LICENSE</a>"
|
||||
}
|
||||
|
||||
if repoInfo.HasContributing {
|
||||
menu += "| <a href=\"" + relpath + "file/CONTRIBUTING.html\">CONTRIBUTING</a>"
|
||||
}
|
||||
|
||||
return template.HTML(menu)
|
||||
},
|
||||
}
|
||||
|
||||
// ToDo: add a map function to generate the menu
|
||||
|
||||
|
@ -77,6 +101,19 @@ func executeTemplate(name string, data any) ([]byte, error) {
|
|||
return strb.Bytes(), nil
|
||||
}
|
||||
|
||||
func executeTemplateToFile(tmplName string, data any, filename string) error {
|
||||
contents, err := executeTemplate(tmplName, data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot execute %q template: %w", tmplName, err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(filename, contents, 0755); err != nil {
|
||||
return fmt.Errorf("cannot write %q contents to %q: %w", tmplName, filename, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func readFile(path string) (string, error) {
|
||||
if fi, err := os.Stat(path); err == nil && !fi.IsDir() {
|
||||
b, err := os.ReadFile(path)
|
||||
|
@ -102,6 +139,10 @@ func readRepoFile(dirname, name string) (contents string, err error) {
|
|||
return
|
||||
}
|
||||
|
||||
func getRelpath(name string) string {
|
||||
return strings.Repeat("../", strings.Count(name, "/"))
|
||||
}
|
||||
|
||||
func generateIndex(args []string) error {
|
||||
slog.Debug("Generating index", "args", args)
|
||||
|
||||
|
@ -146,13 +187,8 @@ func generateIndex(args []string) error {
|
|||
}
|
||||
|
||||
data := map[string]any{"repoDirs": repoDirs}
|
||||
contents, err := executeTemplate("index", data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot execute index template: %w", err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile("index.html", contents, 0755); err != nil {
|
||||
return fmt.Errorf("cannot write index contents to \"index.html\": %w", err)
|
||||
if err := executeTemplateToFile("index", data, "index.html"); err != nil {
|
||||
return fmt.Errorf("cannot execute template %q to file %q: %w", "index", "index.html", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -230,6 +266,7 @@ func generateRepo(path string) error {
|
|||
}
|
||||
|
||||
data := map[string]any{
|
||||
"relpath": getRelpath(dirpath),
|
||||
"repoInfo": repoInfo,
|
||||
"filename": filename,
|
||||
"filesize": o.Size,
|
||||
|
@ -251,14 +288,9 @@ func generateRepo(path string) error {
|
|||
data["filelines"] = lines
|
||||
}
|
||||
|
||||
contents, err := executeTemplate("file", data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot execute file template for file %q: %w", o.Name, err)
|
||||
}
|
||||
|
||||
dstpath := filepath.Join(dirpath, fmt.Sprintf("%s.html", filename))
|
||||
if err := os.WriteFile(dstpath, contents, 0755); err != nil {
|
||||
return fmt.Errorf("cannot write file contents to %q: %w", dstpath, err)
|
||||
if err := executeTemplateToFile("file", data, dstpath); err != nil {
|
||||
return fmt.Errorf("cannot execute template %q to file %q: %w", "file", dstpath, err)
|
||||
}
|
||||
|
||||
file := &CommitFile{
|
||||
|
@ -279,14 +311,10 @@ func generateRepo(path string) error {
|
|||
|
||||
// generate the files index file
|
||||
// ToDo: bundle execute and write into a function
|
||||
filesData := map[string]any{"repoInfo": repoInfo, "files": files}
|
||||
filesContents, err := executeTemplate("files", filesData)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot execute files template for repository %q: %w", repoInfo.Name, err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(filepath.Join(repoInfo.Name, "files.html"), filesContents, 0755); err != nil {
|
||||
return fmt.Errorf("cannot write files contents to \"files.html\": %w", err)
|
||||
filesData := map[string]any{"repoInfo": repoInfo, "files": files, "relpath": ""}
|
||||
filesDstpath := filepath.Join(repoInfo.Name, "files.html")
|
||||
if err := executeTemplateToFile("files", filesData, filesDstpath); err != nil {
|
||||
return fmt.Errorf("cannot execute template %q to file %q: %w", "file", filesDstpath, err)
|
||||
}
|
||||
|
||||
// generate the log file
|
||||
|
@ -327,14 +355,10 @@ func generateRepo(path string) error {
|
|||
return fmt.Errorf("error while processing log: %w", iErr)
|
||||
}
|
||||
|
||||
logData := map[string]any{"repoInfo": repoInfo, "loglines": loglines}
|
||||
logContents, err := executeTemplate("log", logData)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot execute logs template for repository %q: %w", repoInfo.Name, err)
|
||||
}
|
||||
|
||||
if err := os.WriteFile(filepath.Join(repoInfo.Name, "log.html"), logContents, 0755); err != nil {
|
||||
return fmt.Errorf("cannot write log contents to \"log.html\": %w", err)
|
||||
logData := map[string]any{"repoInfo": repoInfo, "loglines": loglines, "relpath": ""}
|
||||
logDstpath := filepath.Join(repoInfo.Name, "log.html")
|
||||
if err := executeTemplateToFile("log", logData, logDstpath); err != nil {
|
||||
return fmt.Errorf("cannot execute template %q to file %q: %w", "file", logDstpath, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
|
@ -4,15 +4,15 @@
|
|||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>{{.filename}} - {{.repoInfo.Name}} - {{.repoInfo.Description}}</title>
|
||||
<link rel="icon" type="image/png" href="../favicon.png" />
|
||||
<link rel="alternate" type="application/atom+xml" title="bag Atom Feed" href="../atom.xml" />
|
||||
<link rel="alternate" type="application/atom+xml" title="bag Atom Feed (tags)" href="../tags.xml" />
|
||||
<link rel="stylesheet" type="text/css" href="../style.css" />
|
||||
<link rel="icon" type="image/png" href="{{.relpath}}favicon.png" />
|
||||
<link rel="alternate" type="application/atom+xml" title="bag Atom Feed" href="{{.relpath}}atom.xml" />
|
||||
<link rel="alternate" type="application/atom+xml" title="bag Atom Feed (tags)" href="{{.relpath}}tags.xml" />
|
||||
<link rel="stylesheet" type="text/css" href="{{.relpath}}style.css" />
|
||||
</head>
|
||||
<body>
|
||||
<table>
|
||||
<tr>
|
||||
<td><a href="../../"><img src="../logo.png" alt="" width="32" height="32" /></a></td>
|
||||
<td><a href="{{.relpath}}../"><img src="{{.relpath}}logo.png" alt="" width="32" height="32" /></a></td>
|
||||
<td><h1>{{.repoInfo.Name}}</h1><span class="desc">{{.repoInfo.Description}}</span></td>
|
||||
</tr>
|
||||
<tr class="url">
|
||||
|
@ -26,18 +26,8 @@
|
|||
<tr>
|
||||
<td></td>
|
||||
<td>
|
||||
<a href="../log.html">Log</a>
|
||||
| <a href="../files.html">Files</a>
|
||||
| <a href="../refs.html">Refs</a>
|
||||
{{if .repoInfo.HasReadme}}
|
||||
| <a href="../file/README.html">README</a>
|
||||
{{end}}
|
||||
{{if .repoInfo.HasLicense}}
|
||||
| <a href="../file/LICENSE.html">LICENSE</a></td>
|
||||
{{end}}
|
||||
{{if .repoInfo.HasContributing}}
|
||||
| <a href="../file/CONTRIBUTING.html">CONTRIBUTING</a></td>
|
||||
{{end}}
|
||||
{{menu .repoInfo .relpath}}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<hr/>
|
||||
|
|
|
@ -27,11 +27,7 @@
|
|||
<tr>
|
||||
<td></td>
|
||||
<td>
|
||||
<a href="log.html">Log</a>
|
||||
| <a href="files.html">Files</a>
|
||||
| <a href="refs.html">Refs</a>
|
||||
| <a href="file/README.html">README</a>
|
||||
| <a href="file/LICENSE.html">LICENSE</a>
|
||||
{{menu .repoInfo .relpath}}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
|
|
@ -26,11 +26,7 @@
|
|||
<tr>
|
||||
<td></td>
|
||||
<td>
|
||||
<a href="log.html">Log</a>
|
||||
| <a href="files.html">Files</a>
|
||||
| <a href="refs.html">Refs</a>
|
||||
| <a href="file/README.html">README</a>
|
||||
| <a href="file/LICENSE.html">LICENSE</a>
|
||||
{{menu .repoInfo .relpath}}
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
|
|
Loading…
Reference in a new issue