From c251e1543d6722f36a37099fb028a0313885d300 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Wed, 3 Jul 2024 20:41:28 +0200 Subject: [PATCH] Adds image support for both the recipe and the steps --- README.md | 2 +- sandwitchboard.go | 29 ++++++++++++++++++++++++++--- templates/recipe.html.tmpl | 7 ++++++- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 391428f..d701af4 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ A simple CLI tool to generate a static website from a - [ ] Add a default CSS file. - [ ] Improve the steps rendering logic to avoid doing all of it inside a template function. -- [ ] Add image support for steps and the recipe. +- [X] Add image support for steps and the recipe. - [ ] Add an `index.html` file that lists all the recipes. - [ ] Embed the default `style.css` file. - [ ] Add command to copy the embedded templates to an external diff --git a/sandwitchboard.go b/sandwitchboard.go index 5391d0a..e4714a2 100644 --- a/sandwitchboard.go +++ b/sandwitchboard.go @@ -29,10 +29,12 @@ const ( //go:embed templates var embedTmpl embed.FS +var imageExtensions = []string{".jpg", ".jpeg", ".png"} var funcMap = template.FuncMap{ - "recipeSteps": func(r cooklang.Recipe) template.HTML { + "recipeImage": recipeImage, + "recipeSteps": func(r cooklang.Recipe, path string) template.HTML { var str = "" - for _, step := range r.Steps { + for i, step := range r.Steps { d := step.Directions ingredients := make([]string, len(step.Ingredients)) @@ -53,6 +55,10 @@ var funcMap = template.FuncMap{ } d = hydrate(d, timers, timerPrefix, timerSuffix) + recipeImagePath := recipeImageForStep(path, i) + if recipeImagePath != "" { + str += fmt.Sprintf("", i, recipeImagePath) + } str += stepPrefix + d + stepSuffix } @@ -60,6 +66,23 @@ var funcMap = template.FuncMap{ }, } +func recipeImageForStep(recipepath string, step int) string { + ext := filepath.Ext(recipepath) + imagepath := fmt.Sprintf("%s.%d%s", strings.TrimSuffix(recipepath, ext), step, ext) + return recipeImage(imagepath) +} + +func recipeImage(recipepath string) string { + pathWithoutExt := strings.TrimSuffix(recipepath, filepath.Ext(recipepath)) + for _, ext := range imageExtensions { + imagepath := fmt.Sprintf("%s%s", pathWithoutExt, ext) + if fi, err := os.Stat(imagepath); err == nil && !fi.IsDir() { + return filepath.Base(imagepath) + } + } + return "" +} + func hydrate(text string, elements []string, prefix, suffix string) string { cursor := 0 for _, el := range elements { @@ -193,7 +216,7 @@ func (g *generateCmd) Run() error { slog.Debug("Parsed file", "path", path, "steps", len(recipe.Steps)) recipeDistPath := filepath.Join(g.Output, filepath.Dir(relpath), fmt.Sprintf("%s.html", recipeName)) - data := map[string]any{"name": recipeName, "recipe": recipe} + data := map[string]any{"name": recipeName, "recipe": recipe, "path": path} slog.Debug("Executing template", "recipeName", recipeName, "recipeWebPath", recipeDistPath) if err := executeTemplateToFile("recipe", data, recipeDistPath); err != nil { return fmt.Errorf("cannot execute template \"recipe\" to file %q: %w", recipeDistPath, err) diff --git a/templates/recipe.html.tmpl b/templates/recipe.html.tmpl index 2f413cf..9532c30 100644 --- a/templates/recipe.html.tmpl +++ b/templates/recipe.html.tmpl @@ -10,6 +10,11 @@

{{.name}}

+ {{$recipeImage := recipeImage .path}} + {{if ne $recipeImage ""}} + + {{end}} + {{if ne (len .recipe.Steps) 0}} {{end}} - {{recipeSteps .recipe}} + {{recipeSteps .recipe .path}}