Allows to use the recipe metadata to get the recipe title

This commit is contained in:
Miguel de la Cruz 2024-07-05 17:36:00 +02:00
parent 79de743e58
commit e85e8b1702
2 changed files with 11 additions and 4 deletions

5
cmd.go
View file

@ -1,8 +1,9 @@
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."`
} }
var cli struct { var cli struct {

View file

@ -210,7 +210,6 @@ func (g *generateCmd) Run() error {
} }
ext := filepath.Ext(d.Name()) ext := filepath.Ext(d.Name())
recipeName := strings.TrimSuffix(d.Name(), ext)
if d.IsDir() { if d.IsDir() {
dirpath := filepath.Join(g.Output, relpath) dirpath := filepath.Join(g.Output, relpath)
slog.Debug("Directory found, creating output dir", "path", path, "dirpath", dirpath) 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)) 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)} data := map[string]any{"name": recipeName, "recipe": recipe, "path": path, "relpath": getRelpath(relpath)}
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 {