Add embedded templates and a test index template
This commit is contained in:
parent
847f5a0629
commit
8be57baa83
4 changed files with 62 additions and 6 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -1 +1,2 @@
|
|||
/gitstatic
|
||||
/gitstatic
|
||||
/index.html
|
12
README.md
12
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).
|
||||
|
|
40
gitstatic.go
40
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
|
||||
|
|
13
templates/index.html.tmpl
Normal file
13
templates/index.html.tmpl
Normal file
|
@ -0,0 +1,13 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h3>Repositories</h3>
|
||||
{{- range .repoDirs}}
|
||||
<p>{{ .Name }} :: {{.Owner}} :: {{.Description}} :: {{.LastCommit}}</p>
|
||||
{{- end}}
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue