Improve details on the recipe template

This commit is contained in:
Miguel de la Cruz 2024-07-06 06:56:57 +02:00
parent 055358f2db
commit e871c5c3d5
3 changed files with 40 additions and 8 deletions

9
cmd.go
View file

@ -1,10 +1,11 @@
package main package main
type generateCmd struct { type generateCmd struct {
Path string `arg:"" help:"Path to the directory with the recipes."` 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."` 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."` 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."` Title string `default:"Recipes directory" short:"t" help:"The title to use at the recipe index."`
IngredientsTitle string `default:"Ingredients" short:"i" help:"The title to use at the list of ingredients of each recipe"`
} }
var cli struct { var cli struct {

View file

@ -10,6 +10,7 @@ import (
"log/slog" "log/slog"
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
"github.com/alecthomas/kong" "github.com/alecthomas/kong"
@ -69,6 +70,15 @@ var funcMap = template.FuncMap{
return template.HTML(str) return template.HTML(str)
}, },
"sortedMetadataKeys": func(m cooklang.Metadata) []string {
keys := []string{}
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
},
} }
func recipeImageForStep(recipepath string, step int) string { func recipeImageForStep(recipepath string, step int) string {
@ -257,7 +267,14 @@ func (g *generateCmd) Run() error {
htmlPath := filepath.Join(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}) recipeFiles = append(recipeFiles, &RecipeFile{Name: recipeName, Path: htmlPath})
recipeDistPath := filepath.Join(g.Output, htmlPath) recipeDistPath := filepath.Join(g.Output, htmlPath)
data := map[string]any{"name": recipeName, "recipe": recipe, "path": path, "relpath": getRelpath(relpath)} data := map[string]any{
"name": recipeName,
"recipe": recipe,
"path": path,
"relpath": getRelpath(relpath),
"nameKey": g.NameKey,
"ingredientsTitle": g.IngredientsTitle,
}
slog.Debug("Executing template", "recipeName", recipeName, "recipeWebPath", recipeDistPath) slog.Debug("Executing template", "recipeName", recipeName, "recipeWebPath", recipeDistPath)
if err := executeTemplateToFile("recipe", data, recipeDistPath); err != nil { if err := executeTemplateToFile("recipe", data, recipeDistPath); err != nil {
return fmt.Errorf("cannot execute template \"recipe\" to file %q: %w", recipeDistPath, err) return fmt.Errorf("cannot execute template \"recipe\" to file %q: %w", recipeDistPath, err)

View file

@ -15,14 +15,28 @@
<img class="recipeimage" src="{{$recipeImage}}" /> <img class="recipeimage" src="{{$recipeImage}}" />
{{end}} {{end}}
{{if ne (len .recipe.Steps) 0}} {{if ne (len .recipe.Metadata) 0}}
<ul> <ul>
{{range $k, $v := .recipe.Metadata}} {{$m := .recipe.Metadata}}
<li><em>{{$k}}:</em> {{$v}}</li> {{$nameKey := .nameKey}}
{{range $k := sortedMetadataKeys $m}}
{{if ne $k $nameKey}}
<li><em>{{$k}}:</em> {{index $m $k}}</li>
{{end}}
{{end}} {{end}}
</ul> </ul>
{{end}} {{end}}
{{recipeSteps .recipe .path}} {{recipeSteps .recipe .path}}
<h2>{{.ingredientsTitle}}</h2>
<ul>
{{range .recipe.Steps}}
{{range .Ingredients}}
<li>{{.Name}} ({{.Amount.QuantityRaw}}{{if ne .Amount.Unit ""}} {{.Amount.Unit}}{{end}})</li>
{{end}}
{{end}}
</ul>
</body> </body>
</html> </html>