Adds external URL to the recipe

This commit is contained in:
Miguel de la Cruz 2024-08-03 08:48:56 +02:00
parent 7a5ab7cf09
commit 432be8dfb2
3 changed files with 13 additions and 1 deletions

1
cmd.go
View file

@ -4,6 +4,7 @@ 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."`
URLKey string `default:"url" short:"u" help:"The metadata key for the recipe URL."`
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"` IngredientsTitle string `default:"Ingredients" short:"i" help:"The title to use at the list of ingredients of each recipe"`
} }

View file

@ -263,16 +263,22 @@ func (g *generateCmd) Run() error {
if name, ok := recipe.Metadata[g.NameKey]; ok { if name, ok := recipe.Metadata[g.NameKey]; ok {
recipeName = name recipeName = name
} }
recipeURL := ""
if url, ok := recipe.Metadata[g.URLKey]; ok {
recipeURL = url
}
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{ data := map[string]any{
"name": recipeName, "name": recipeName,
"url": recipeURL,
"recipe": recipe, "recipe": recipe,
"path": path, "path": path,
"relpath": getRelpath(relpath), "relpath": getRelpath(relpath),
"nameKey": g.NameKey, "nameKey": g.NameKey,
"urlKey": g.URLKey,
"ingredientsTitle": g.IngredientsTitle, "ingredientsTitle": g.IngredientsTitle,
} }
slog.Debug("Executing template", "recipeName", recipeName, "recipeWebPath", recipeDistPath) slog.Debug("Executing template", "recipeName", recipeName, "recipeWebPath", recipeDistPath)

View file

@ -12,6 +12,10 @@
<div id="content"> <div id="content">
<h1>{{.name}}</h1> <h1>{{.name}}</h1>
{{if ne .url ""}}
<a class="recipe-url" href="{{.url}}">External URL</a>
{{end}}
{{$recipeImage := recipeImage .path}} {{$recipeImage := recipeImage .path}}
{{if ne $recipeImage ""}} {{if ne $recipeImage ""}}
<img class="recipeimage" src="{{$recipeImage}}" /> <img class="recipeimage" src="{{$recipeImage}}" />
@ -21,8 +25,9 @@
<ul> <ul>
{{$m := .recipe.Metadata}} {{$m := .recipe.Metadata}}
{{$nameKey := .nameKey}} {{$nameKey := .nameKey}}
{{$urlKey := .urlKey}}
{{range $k := sortedMetadataKeys $m}} {{range $k := sortedMetadataKeys $m}}
{{if ne $k $nameKey}} {{if and (ne $k $nameKey) (ne $k $urlKey)}}
<li><em>{{$k}}:</em> {{index $m $k}}</li> <li><em>{{$k}}:</em> {{index $m $k}}</li>
{{end}} {{end}}
{{end}} {{end}}