From 055358f2dbb7dce78f647833eea874adfe9833fd Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Sat, 6 Jul 2024 06:23:32 +0200 Subject: [PATCH] Generate an index of the recipes --- README.md | 2 +- cmd.go | 1 + sandwitchboard.go | 19 ++++++++++++++++++- templates/index.html.tmpl | 21 +++++++++++++++++++++ templates/style.css | 2 +- 5 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 templates/index.html.tmpl diff --git a/README.md b/README.md index 37d14ed..50a1755 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ A simple CLI tool to generate a static website from a - [ ] Improve the steps rendering logic to avoid doing all of it inside a template function. - [X] Add image support for steps and the recipe. -- [ ] Add an `index.html` file that lists all the recipes. +- [X] Add an `index.html` file that lists all the recipes. - [X] Embed the default `style.css` file. - [ ] Add command to copy the embedded templates to an external directory. diff --git a/cmd.go b/cmd.go index 374607e..0218a30 100644 --- a/cmd.go +++ b/cmd.go @@ -4,6 +4,7 @@ type generateCmd struct { Path string `arg:"" help:"Path to the directory with the recipes."` Output string `default:"dist" help:"Path to the directory where the files will be generated."` NameKey string `default:"name" short:"n" help:"The metadata key for the recipe name."` + Title string `default:"Recipes directory" short:"t" help:"The title to use at the recipe index."` } var cli struct { diff --git a/sandwitchboard.go b/sandwitchboard.go index 0fd56d9..aa73d73 100644 --- a/sandwitchboard.go +++ b/sandwitchboard.go @@ -27,6 +27,11 @@ const ( timerSuffix = "" ) +type RecipeFile struct { + Name string + Path string +} + //go:embed templates var embedTmpl embed.FS var imageExtensions = []string{".jpg", ".jpeg", ".png"} @@ -191,6 +196,7 @@ func (g *generateCmd) Run() error { return fmt.Errorf("cannot create directory on path %q: %w", g.Output, err) } + recipeFiles := []*RecipeFile{} walkErr := filepath.WalkDir(g.Path, func(path string, d fs.DirEntry, err error) error { slog.Debug("Walking through file", "path", path) @@ -248,7 +254,9 @@ func (g *generateCmd) Run() error { recipeName = name } - recipeDistPath := filepath.Join(g.Output, filepath.Dir(relpath), fmt.Sprintf("%s.html", filenameWithoutExt)) + htmlPath := filepath.Join(filepath.Dir(relpath), fmt.Sprintf("%s.html", filenameWithoutExt)) + recipeFiles = append(recipeFiles, &RecipeFile{Name: recipeName, Path: htmlPath}) + recipeDistPath := filepath.Join(g.Output, htmlPath) data := map[string]any{"name": recipeName, "recipe": recipe, "path": path, "relpath": getRelpath(relpath)} slog.Debug("Executing template", "recipeName", recipeName, "recipeWebPath", recipeDistPath) if err := executeTemplateToFile("recipe", data, recipeDistPath); err != nil { @@ -261,6 +269,7 @@ func (g *generateCmd) Run() error { return fmt.Errorf("error while walking directory %q: %w", g.Path, walkErr) } + // copy styles styleFile, err := embedTmpl.Open(filepath.Join("templates", "style.css")) if err != nil { return fmt.Errorf("cannot read style.css embed file: %w", err) @@ -270,5 +279,13 @@ func (g *generateCmd) Run() error { return fmt.Errorf("cannot copy style.css: %w", err) } + // generate index + data := map[string]any{"title": g.Title, "recipes": recipeFiles} + indexpath := filepath.Join(g.Output, "index.html") + slog.Debug("Executing index template", "title", g.Title, "recipes", len(recipeFiles), "indexpath", indexpath) + if err := executeTemplateToFile("index", data, indexpath); err != nil { + return fmt.Errorf("cannot execute template \"index\" to file %q: %w", indexpath, err) + } + return nil } diff --git a/templates/index.html.tmpl b/templates/index.html.tmpl new file mode 100644 index 0000000..bd9f946 --- /dev/null +++ b/templates/index.html.tmpl @@ -0,0 +1,21 @@ + + + + + + {{.title}} + + + + +

{{.title}}

+ + {{if ne (len .recipes) 0}} + + {{end}} + + diff --git a/templates/style.css b/templates/style.css index a811139..f20ce33 100644 --- a/templates/style.css +++ b/templates/style.css @@ -10,7 +10,7 @@ body { .cookware { font-weight: bold; - color: red; + color: brown; } .timer {