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.
|
||||
- [ ] Improve the steps rendering logic to avoid doing all of it
|
||||
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.
|
||||
- [ ] Embed the default `style.css` file.
|
||||
- [ ] Add command to copy the embedded templates to an external
|
||||
|
|
|
@ -29,10 +29,12 @@ const (
|
|||
|
||||
//go:embed templates
|
||||
var embedTmpl embed.FS
|
||||
var imageExtensions = []string{".jpg", ".jpeg", ".png"}
|
||||
var funcMap = template.FuncMap{
|
||||
"recipeSteps": func(r cooklang.Recipe) template.HTML {
|
||||
"recipeImage": recipeImage,
|
||||
"recipeSteps": func(r cooklang.Recipe, path string) template.HTML {
|
||||
var str = ""
|
||||
for _, step := range r.Steps {
|
||||
for i, step := range r.Steps {
|
||||
d := step.Directions
|
||||
|
||||
ingredients := make([]string, len(step.Ingredients))
|
||||
|
@ -53,6 +55,10 @@ var funcMap = template.FuncMap{
|
|||
}
|
||||
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
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
cursor := 0
|
||||
for _, el := range elements {
|
||||
|
@ -193,7 +216,7 @@ func (g *generateCmd) Run() error {
|
|||
|
||||
slog.Debug("Parsed file", "path", path, "steps", len(recipe.Steps))
|
||||
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)
|
||||
if err := executeTemplateToFile("recipe", data, recipeDistPath); err != nil {
|
||||
return fmt.Errorf("cannot execute template \"recipe\" to file %q: %w", recipeDistPath, err)
|
||||
|
|
|
@ -10,6 +10,11 @@
|
|||
<body>
|
||||
<h1>{{.name}}</h1>
|
||||
|
||||
{{$recipeImage := recipeImage .path}}
|
||||
{{if ne $recipeImage ""}}
|
||||
<img class="recipeimage" src="{{$recipeImage}}" />
|
||||
{{end}}
|
||||
|
||||
{{if ne (len .recipe.Steps) 0}}
|
||||
<ul>
|
||||
{{range $k, $v := .recipe.Metadata}}
|
||||
|
@ -18,6 +23,6 @@
|
|||
</ul>
|
||||
{{end}}
|
||||
|
||||
{{recipeSteps .recipe}}
|
||||
{{recipeSteps .recipe .path}}
|
||||
</body>
|
||||
</html>
|
||||
|
|
Loading…
Reference in a new issue