From e85e8b170215fb18259564cf7d790b0d827b6333 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Fri, 5 Jul 2024 17:36:00 +0200 Subject: [PATCH] Allows to use the recipe metadata to get the recipe title --- cmd.go | 5 +++-- sandwitchboard.go | 10 ++++++++-- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/cmd.go b/cmd.go index 8df88dc..374607e 100644 --- a/cmd.go +++ b/cmd.go @@ -1,8 +1,9 @@ package main 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."` + 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."` } var cli struct { diff --git a/sandwitchboard.go b/sandwitchboard.go index 890b067..0fd56d9 100644 --- a/sandwitchboard.go +++ b/sandwitchboard.go @@ -210,7 +210,6 @@ func (g *generateCmd) Run() error { } ext := filepath.Ext(d.Name()) - recipeName := strings.TrimSuffix(d.Name(), ext) if d.IsDir() { dirpath := filepath.Join(g.Output, relpath) slog.Debug("Directory found, creating output dir", "path", path, "dirpath", dirpath) @@ -242,7 +241,14 @@ 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)) + + filenameWithoutExt := strings.TrimSuffix(d.Name(), ext) + recipeName := filenameWithoutExt + if name, ok := recipe.Metadata[g.NameKey]; ok { + recipeName = name + } + + recipeDistPath := filepath.Join(g.Output, filepath.Dir(relpath), fmt.Sprintf("%s.html", filenameWithoutExt)) 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 {