diff --git a/.gitignore b/.gitignore index de4c613..50a6aae 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/gitstatic \ No newline at end of file +/gitstatic +/index.html \ No newline at end of file diff --git a/README.md b/README.md index 2591d80..1c23bda 100644 --- a/README.md +++ b/README.md @@ -6,11 +6,21 @@ Simple CLI tool to generate webpages from git repositories. - [ ] Embed templates for the index and repository pages. - [ ] Generate the index html file for the -index subcommand. +- [ ] Generate the log html file for a repository. +- [ ] Detect and link README, LICENSE and CONTRIBUTING files. +- [ ] Generate the files html file and file structure. +- [ ] Generate the refs html file. - [ ] Add a proper CLI parsing and subcommands. - [ ] Add a sample CSS file for the default templates. -- [ ] Add a subcommand to dump the embed templates. +- [ ] Add a subcommand to dump the embedded templates so they can be + modified. +- [ ] Take binary files into account. +- [ ] Limit the output for large diffs. +- [ ] Allow to anchor lines. - [ ] Check if the templates exist on a location and use them if so. Allow to change that location through CLI flags or env vars. +- [ ] Optimize repository generation through a cache. +- [ ] Add a flag to regenerate in case a `push -f` comes in. - [ ] Optimize output generation through the use of smaller templates instead of having all the structs in memory when executing one template (for large repos). diff --git a/gitstatic.go b/gitstatic.go index d77c92c..fa1791c 100644 --- a/gitstatic.go +++ b/gitstatic.go @@ -1,8 +1,11 @@ package main import ( + "bytes" + "embed" "flag" "fmt" + "html/template" "log/slog" "os" "path/filepath" @@ -19,6 +22,29 @@ type RepoDir struct { LastCommit time.Time } +//go:embed templates +var embedTmpl embed.FS + +func executeTemplate(name string, data any) (string, error) { + path := filepath.Join("templates", fmt.Sprintf("%s.html.tmpl", name)) + b, err := embedTmpl.ReadFile(path) + if err != nil { + return "", fmt.Errorf("cannot read embedded file %q: %w", path, err) + } + + tmpl, err := template.New("").Parse(string(b)) + if err != nil { + return "", fmt.Errorf("cannot parse template %q: %w", path, err) + } + + var strb bytes.Buffer + if err := tmpl.Execute(&strb, data); err != nil { + return "", fmt.Errorf("cannot execute template %q: %w", path, err) + } + + return strb.String(), nil +} + func errAndExit(msg string, args ...any) { fmt.Fprintf(os.Stderr, msg, args...) os.Exit(1) @@ -120,10 +146,16 @@ func generateIndex(args []string) error { repoDirs = append(repoDirs, repoDir) } - // ToDo: pass repoDirs to a template and generate index.htlm - // ToDo: remove this - for _, rd := range repoDirs { - slog.Info("Repo", "name", rd.Name, "desc", rd.Description, "owner", rd.Owner, "lastcommit", rd.LastCommit) + 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", []byte(contents), 0755); err != nil { + return fmt.Errorf("cannot write index contents to \"index.html\": %w", err) } return nil diff --git a/templates/index.html.tmpl b/templates/index.html.tmpl new file mode 100644 index 0000000..4300a47 --- /dev/null +++ b/templates/index.html.tmpl @@ -0,0 +1,13 @@ + + +
+ + + + +{{ .Name }} :: {{.Owner}} :: {{.Description}} :: {{.LastCommit}}
+ {{- end}} + +