Adds category and sorts recipes by name

This commit is contained in:
Miguel de la Cruz 2024-08-19 07:17:46 +02:00
parent aa02133b9e
commit 600a26a91f
5 changed files with 41 additions and 14 deletions

View file

@ -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.

1
cmd.go
View file

@ -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"`

View file

@ -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 = "<p>"
stepSuffix = "</p>"
@ -28,11 +30,6 @@ const (
timerSuffix = "</span>"
)
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
// "ceb<span>olla</span> 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 {

View file

@ -12,12 +12,19 @@
<div id="content">
<h1>{{.title}}</h1>
{{if ne (len .recipes) 0}}
<ul>
{{range .recipes}}
<li><a href="{{.Path}}">{{.Name}}</a></li>
{{$categoryMap := .categoryMap}}
{{$recipes := .recipes}}
{{range .categoryList}}
{{if (ne . "")}}
<h3>{{.}}</h3>
{{end}}
</ul>
{{$recipeList := index $categoryMap .}}
<ul>
{{range $name := $recipeList}}
<li><a href="{{index $recipes $name}}">{{$name}}</a></li>
{{end}}
</ul>
{{end}}
</div>
</main>

View file

@ -26,9 +26,10 @@
<ul>
{{$m := .recipe.Metadata}}
{{$nameKey := .nameKey}}
{{$categoryKey := .categoryKey}}
{{$urlKey := .urlKey}}
{{range $k := sortedMetadataKeys $m}}
{{if and (ne $k $nameKey) (ne $k $urlKey)}}
{{if and (ne $k $nameKey) (ne $k $categoryKey) (ne $k $urlKey)}}
<li><em>{{$k}}:</em> {{index $m $k}}</li>
{{end}}
{{end}}