From 600a26a91f8f5b23c331e47579ecebec66d829d9 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Mon, 19 Aug 2024 07:17:46 +0200 Subject: [PATCH] Adds category and sorts recipes by name --- README.md | 2 ++ cmd.go | 1 + sandwitchboard.go | 32 ++++++++++++++++++++++++-------- templates/index.html.tmpl | 17 ++++++++++++----- templates/recipe.html.tmpl | 3 ++- 5 files changed, 41 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 50a1755..a22f982 100644 --- a/README.md +++ b/README.md @@ -16,3 +16,5 @@ A simple CLI tool to generate a static website from a - [ ] Add command to copy the embedded templates to an external directory. - [ ] Allow to read the templates from an external directory. +- [X] Add a category metadata key. +- [X] Sort recipes by name key. diff --git a/cmd.go b/cmd.go index 49deccf..7cba28d 100644 --- a/cmd.go +++ b/cmd.go @@ -4,6 +4,7 @@ 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."` NameKey string `default:"name" short:"n" help:"The metadata key for the recipe name."` + CategoryKey string `default:"category" short:"c" help:"The metadata key for the categories."` 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."` IngredientsTitle string `default:"Ingredients" short:"i" help:"The title to use at the list of ingredients of each recipe"` diff --git a/sandwitchboard.go b/sandwitchboard.go index 19435c0..01d015f 100644 --- a/sandwitchboard.go +++ b/sandwitchboard.go @@ -17,6 +17,8 @@ import ( "github.com/aquilax/cooklang-go" ) +// ToDo: añadir la cantidad a los ingredientes y timers, quizá en un +// X-data-quantity const ( stepPrefix = "

" stepSuffix = "

" @@ -28,11 +30,6 @@ const ( timerSuffix = "" ) -type RecipeFile struct { - Name string - Path string -} - //go:embed templates var embedTmpl embed.FS var imageExtensions = []string{".jpg", ".jpeg", ".png"} @@ -98,6 +95,8 @@ func recipeImage(recipepath string) string { return "" } +// ToDo: si tengo "una cebolla en la #olla" me parsea +// "cebolla en la olla" func hydrate(text string, elements []string, prefix, suffix string) string { cursor := 0 for _, el := range elements { @@ -206,7 +205,8 @@ func (g *generateCmd) Run() error { return fmt.Errorf("cannot create directory on path %q: %w", g.Output, err) } - recipeFiles := []*RecipeFile{} + recipeFiles := map[string]string{} + categoryMap := map[string][]string{} walkErr := filepath.WalkDir(g.Path, func(path string, d fs.DirEntry, err error) error { slog.Debug("Walking through file", "path", path) @@ -267,17 +267,24 @@ func (g *generateCmd) Run() error { if url, ok := recipe.Metadata[g.URLKey]; ok { recipeURL = url } + category := "" + if cat, ok := recipe.Metadata[g.CategoryKey]; ok { + category = cat + } htmlPath := filepath.Join(filepath.Dir(relpath), fmt.Sprintf("%s.html", filenameWithoutExt)) - recipeFiles = append(recipeFiles, &RecipeFile{Name: recipeName, Path: htmlPath}) + recipeFiles[recipeName] = htmlPath recipeDistPath := filepath.Join(g.Output, htmlPath) + categoryMap[category] = append(categoryMap[category], recipeName) data := map[string]any{ "name": recipeName, "url": recipeURL, + "category": category, "recipe": recipe, "path": path, "relpath": getRelpath(relpath), "nameKey": g.NameKey, + "categoryKey": g.CategoryKey, "urlKey": g.URLKey, "ingredientsTitle": g.IngredientsTitle, } @@ -302,8 +309,17 @@ func (g *generateCmd) Run() error { return fmt.Errorf("cannot copy style.css: %w", err) } + // sort categories + categoryList := []string{} + for category, recipes := range categoryMap { + categoryList = append(categoryList, category) + sort.Strings(recipes) + categoryMap[category] = recipes + } + sort.Strings(categoryList) + // generate index - data := map[string]any{"title": g.Title, "recipes": recipeFiles} + data := map[string]any{"title": g.Title, "recipes": recipeFiles, "categoryList": categoryList, "categoryMap": categoryMap} indexpath := filepath.Join(g.Output, "index.html") slog.Debug("Executing index template", "title", g.Title, "recipes", len(recipeFiles), "indexpath", indexpath) if err := executeTemplateToFile("index", data, indexpath); err != nil { diff --git a/templates/index.html.tmpl b/templates/index.html.tmpl index 35ad5d9..29d9f3c 100644 --- a/templates/index.html.tmpl +++ b/templates/index.html.tmpl @@ -12,12 +12,19 @@

{{.title}}

- {{if ne (len .recipes) 0}} - + {{$recipeList := index $categoryMap .}} + + {{end}}
diff --git a/templates/recipe.html.tmpl b/templates/recipe.html.tmpl index c1aa431..e1d9fa1 100644 --- a/templates/recipe.html.tmpl +++ b/templates/recipe.html.tmpl @@ -26,9 +26,10 @@