Adds image support for both the recipe and the steps
This commit is contained in:
parent
943e9df086
commit
c251e1543d
3 changed files with 33 additions and 5 deletions
|
@ -10,7 +10,7 @@ A simple CLI tool to generate a static website from a
|
||||||
- [ ] Add a default CSS file.
|
- [ ] Add a default CSS file.
|
||||||
- [ ] Improve the steps rendering logic to avoid doing all of it
|
- [ ] Improve the steps rendering logic to avoid doing all of it
|
||||||
inside a template function.
|
inside a template function.
|
||||||
- [ ] Add image support for steps and the recipe.
|
- [X] Add image support for steps and the recipe.
|
||||||
- [ ] Add an `index.html` file that lists all the recipes.
|
- [ ] Add an `index.html` file that lists all the recipes.
|
||||||
- [ ] Embed the default `style.css` file.
|
- [ ] Embed the default `style.css` file.
|
||||||
- [ ] Add command to copy the embedded templates to an external
|
- [ ] Add command to copy the embedded templates to an external
|
||||||
|
|
|
@ -29,10 +29,12 @@ const (
|
||||||
|
|
||||||
//go:embed templates
|
//go:embed templates
|
||||||
var embedTmpl embed.FS
|
var embedTmpl embed.FS
|
||||||
|
var imageExtensions = []string{".jpg", ".jpeg", ".png"}
|
||||||
var funcMap = template.FuncMap{
|
var funcMap = template.FuncMap{
|
||||||
"recipeSteps": func(r cooklang.Recipe) template.HTML {
|
"recipeImage": recipeImage,
|
||||||
|
"recipeSteps": func(r cooklang.Recipe, path string) template.HTML {
|
||||||
var str = ""
|
var str = ""
|
||||||
for _, step := range r.Steps {
|
for i, step := range r.Steps {
|
||||||
d := step.Directions
|
d := step.Directions
|
||||||
|
|
||||||
ingredients := make([]string, len(step.Ingredients))
|
ingredients := make([]string, len(step.Ingredients))
|
||||||
|
@ -53,6 +55,10 @@ var funcMap = template.FuncMap{
|
||||||
}
|
}
|
||||||
d = hydrate(d, timers, timerPrefix, timerSuffix)
|
d = hydrate(d, timers, timerPrefix, timerSuffix)
|
||||||
|
|
||||||
|
recipeImagePath := recipeImageForStep(path, i)
|
||||||
|
if recipeImagePath != "" {
|
||||||
|
str += fmt.Sprintf("<img class=\"stepimage stepimage-%d\" src=\"%s\" />", i, recipeImagePath)
|
||||||
|
}
|
||||||
str += stepPrefix + d + stepSuffix
|
str += stepPrefix + d + stepSuffix
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,6 +66,23 @@ var funcMap = template.FuncMap{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func recipeImageForStep(recipepath string, step int) string {
|
||||||
|
ext := filepath.Ext(recipepath)
|
||||||
|
imagepath := fmt.Sprintf("%s.%d%s", strings.TrimSuffix(recipepath, ext), step, ext)
|
||||||
|
return recipeImage(imagepath)
|
||||||
|
}
|
||||||
|
|
||||||
|
func recipeImage(recipepath string) string {
|
||||||
|
pathWithoutExt := strings.TrimSuffix(recipepath, filepath.Ext(recipepath))
|
||||||
|
for _, ext := range imageExtensions {
|
||||||
|
imagepath := fmt.Sprintf("%s%s", pathWithoutExt, ext)
|
||||||
|
if fi, err := os.Stat(imagepath); err == nil && !fi.IsDir() {
|
||||||
|
return filepath.Base(imagepath)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
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 {
|
||||||
|
@ -193,7 +216,7 @@ 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))
|
recipeDistPath := filepath.Join(g.Output, filepath.Dir(relpath), fmt.Sprintf("%s.html", recipeName))
|
||||||
data := map[string]any{"name": recipeName, "recipe": recipe}
|
data := map[string]any{"name": recipeName, "recipe": recipe, "path": path}
|
||||||
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 {
|
||||||
return fmt.Errorf("cannot execute template \"recipe\" to file %q: %w", recipeDistPath, err)
|
return fmt.Errorf("cannot execute template \"recipe\" to file %q: %w", recipeDistPath, err)
|
||||||
|
|
|
@ -10,6 +10,11 @@
|
||||||
<body>
|
<body>
|
||||||
<h1>{{.name}}</h1>
|
<h1>{{.name}}</h1>
|
||||||
|
|
||||||
|
{{$recipeImage := recipeImage .path}}
|
||||||
|
{{if ne $recipeImage ""}}
|
||||||
|
<img class="recipeimage" src="{{$recipeImage}}" />
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{if ne (len .recipe.Steps) 0}}
|
{{if ne (len .recipe.Steps) 0}}
|
||||||
<ul>
|
<ul>
|
||||||
{{range $k, $v := .recipe.Metadata}}
|
{{range $k, $v := .recipe.Metadata}}
|
||||||
|
@ -18,6 +23,6 @@
|
||||||
</ul>
|
</ul>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
{{recipeSteps .recipe}}
|
{{recipeSteps .recipe .path}}
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in a new issue