Adds category and sorts recipes by name
This commit is contained in:
parent
aa02133b9e
commit
600a26a91f
5 changed files with 41 additions and 14 deletions
|
@ -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
|
- [ ] Add command to copy the embedded templates to an external
|
||||||
directory.
|
directory.
|
||||||
- [ ] Allow to read the templates from 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
1
cmd.go
|
@ -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."`
|
||||||
|
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."`
|
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"`
|
||||||
|
|
|
@ -17,6 +17,8 @@ import (
|
||||||
"github.com/aquilax/cooklang-go"
|
"github.com/aquilax/cooklang-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ToDo: añadir la cantidad a los ingredientes y timers, quizá en un
|
||||||
|
// X-data-quantity
|
||||||
const (
|
const (
|
||||||
stepPrefix = "<p>"
|
stepPrefix = "<p>"
|
||||||
stepSuffix = "</p>"
|
stepSuffix = "</p>"
|
||||||
|
@ -28,11 +30,6 @@ const (
|
||||||
timerSuffix = "</span>"
|
timerSuffix = "</span>"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RecipeFile struct {
|
|
||||||
Name string
|
|
||||||
Path string
|
|
||||||
}
|
|
||||||
|
|
||||||
//go:embed templates
|
//go:embed templates
|
||||||
var embedTmpl embed.FS
|
var embedTmpl embed.FS
|
||||||
var imageExtensions = []string{".jpg", ".jpeg", ".png"}
|
var imageExtensions = []string{".jpg", ".jpeg", ".png"}
|
||||||
|
@ -98,6 +95,8 @@ func recipeImage(recipepath string) string {
|
||||||
return ""
|
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 {
|
func hydrate(text string, elements []string, prefix, suffix string) string {
|
||||||
cursor := 0
|
cursor := 0
|
||||||
for _, el := range elements {
|
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)
|
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 {
|
walkErr := filepath.WalkDir(g.Path, func(path string, d fs.DirEntry, err error) error {
|
||||||
slog.Debug("Walking through file", "path", path)
|
slog.Debug("Walking through file", "path", path)
|
||||||
|
|
||||||
|
@ -267,17 +267,24 @@ func (g *generateCmd) Run() error {
|
||||||
if url, ok := recipe.Metadata[g.URLKey]; ok {
|
if url, ok := recipe.Metadata[g.URLKey]; ok {
|
||||||
recipeURL = url
|
recipeURL = url
|
||||||
}
|
}
|
||||||
|
category := ""
|
||||||
|
if cat, ok := recipe.Metadata[g.CategoryKey]; ok {
|
||||||
|
category = cat
|
||||||
|
}
|
||||||
|
|
||||||
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[recipeName] = htmlPath
|
||||||
recipeDistPath := filepath.Join(g.Output, htmlPath)
|
recipeDistPath := filepath.Join(g.Output, htmlPath)
|
||||||
|
categoryMap[category] = append(categoryMap[category], recipeName)
|
||||||
data := map[string]any{
|
data := map[string]any{
|
||||||
"name": recipeName,
|
"name": recipeName,
|
||||||
"url": recipeURL,
|
"url": recipeURL,
|
||||||
|
"category": category,
|
||||||
"recipe": recipe,
|
"recipe": recipe,
|
||||||
"path": path,
|
"path": path,
|
||||||
"relpath": getRelpath(relpath),
|
"relpath": getRelpath(relpath),
|
||||||
"nameKey": g.NameKey,
|
"nameKey": g.NameKey,
|
||||||
|
"categoryKey": g.CategoryKey,
|
||||||
"urlKey": g.URLKey,
|
"urlKey": g.URLKey,
|
||||||
"ingredientsTitle": g.IngredientsTitle,
|
"ingredientsTitle": g.IngredientsTitle,
|
||||||
}
|
}
|
||||||
|
@ -302,8 +309,17 @@ func (g *generateCmd) Run() error {
|
||||||
return fmt.Errorf("cannot copy style.css: %w", err)
|
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
|
// 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")
|
indexpath := filepath.Join(g.Output, "index.html")
|
||||||
slog.Debug("Executing index template", "title", g.Title, "recipes", len(recipeFiles), "indexpath", indexpath)
|
slog.Debug("Executing index template", "title", g.Title, "recipes", len(recipeFiles), "indexpath", indexpath)
|
||||||
if err := executeTemplateToFile("index", data, indexpath); err != nil {
|
if err := executeTemplateToFile("index", data, indexpath); err != nil {
|
||||||
|
|
|
@ -12,10 +12,17 @@
|
||||||
<div id="content">
|
<div id="content">
|
||||||
<h1>{{.title}}</h1>
|
<h1>{{.title}}</h1>
|
||||||
|
|
||||||
{{if ne (len .recipes) 0}}
|
{{$categoryMap := .categoryMap}}
|
||||||
|
{{$recipes := .recipes}}
|
||||||
|
{{range .categoryList}}
|
||||||
|
{{if (ne . "")}}
|
||||||
|
<h3>{{.}}</h3>
|
||||||
|
{{end}}
|
||||||
|
{{$recipeList := index $categoryMap .}}
|
||||||
|
|
||||||
<ul>
|
<ul>
|
||||||
{{range .recipes}}
|
{{range $name := $recipeList}}
|
||||||
<li><a href="{{.Path}}">{{.Name}}</a></li>
|
<li><a href="{{index $recipes $name}}">{{$name}}</a></li>
|
||||||
{{end}}
|
{{end}}
|
||||||
</ul>
|
</ul>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
|
@ -26,9 +26,10 @@
|
||||||
<ul>
|
<ul>
|
||||||
{{$m := .recipe.Metadata}}
|
{{$m := .recipe.Metadata}}
|
||||||
{{$nameKey := .nameKey}}
|
{{$nameKey := .nameKey}}
|
||||||
|
{{$categoryKey := .categoryKey}}
|
||||||
{{$urlKey := .urlKey}}
|
{{$urlKey := .urlKey}}
|
||||||
{{range $k := sortedMetadataKeys $m}}
|
{{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>
|
<li><em>{{$k}}:</em> {{index $m $k}}</li>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
Loading…
Reference in a new issue