From 5cd7dd9dbb7bac9612753354c6557afecb4d6879 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Sun, 30 Jun 2024 11:08:51 +0200 Subject: [PATCH] Adds function to build the menu and manage relpaths --- gitssg.go | 86 +++++++++++++++++++++++++-------------- templates/file.html.tmpl | 24 ++++------- templates/files.html.tmpl | 6 +-- templates/log.html.tmpl | 6 +-- 4 files changed, 64 insertions(+), 58 deletions(-) diff --git a/gitssg.go b/gitssg.go index 7380b68..ca97c80 100644 --- a/gitssg.go +++ b/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 := "Log" + menu += " | Files" + menu += " | Refs" + + if repoInfo.HasReadme { + menu += "| README" + } + + if repoInfo.HasLicense { + menu += "| LICENSE" + } + + if repoInfo.HasContributing { + menu += "| CONTRIBUTING" + } + + 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 diff --git a/templates/file.html.tmpl b/templates/file.html.tmpl index 64d4b7b..0ed7464 100644 --- a/templates/file.html.tmpl +++ b/templates/file.html.tmpl @@ -4,15 +4,15 @@ {{.filename}} - {{.repoInfo.Name}} - {{.repoInfo.Description}} - - - - + + + + - + @@ -26,18 +26,8 @@ - {{end}} - {{if .repoInfo.HasContributing}} - | CONTRIBUTING - {{end}} + {{menu .repoInfo .relpath}} +

{{.repoInfo.Name}}

{{.repoInfo.Description}}
- Log - | Files - | Refs - {{if .repoInfo.HasReadme}} - | README - {{end}} - {{if .repoInfo.HasLicense}} - | LICENSE

diff --git a/templates/files.html.tmpl b/templates/files.html.tmpl index 1477a15..f482a12 100644 --- a/templates/files.html.tmpl +++ b/templates/files.html.tmpl @@ -27,11 +27,7 @@ - Log - | Files - | Refs - | README - | LICENSE + {{menu .repoInfo .relpath}} diff --git a/templates/log.html.tmpl b/templates/log.html.tmpl index 26e4666..fa48b06 100644 --- a/templates/log.html.tmpl +++ b/templates/log.html.tmpl @@ -26,11 +26,7 @@ - Log - | Files - | Refs - | README - | LICENSE + {{menu .repoInfo .relpath}}