-
Notifications
You must be signed in to change notification settings - Fork 0
/
recipe.go
81 lines (71 loc) · 1.66 KB
/
recipe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package recipe
import (
"bytes"
"html/template"
"log"
"strings"
sprig "github.com/Masterminds/sprig/v3"
"github.com/kr/text"
)
type Recipe struct {
Title string `json:"name"`
Source string
Tags []string `json:"keywords"`
Yield string `json:"recipeYield"`
CookTime string `json:"cookTime"`
PrepTime string `json:"prepTime"`
TotalTime string `json:"totalTime"`
Nutrition struct {
servingSize string `json:"servingSize"`
calories string `json:"calories"`
protein string `json:"proteinContent"`
fat string `json:"fatContent"`
carbohydrate string `json:"carbohydrateContent"`
} `json:"nutrition"`
Ingredients []string `json:"recipeIngredient"`
Instructions string `json:"recipeInstructions"`
}
func (recipe *Recipe) Polish() {
for i, ingredient := range recipe.Ingredients {
recipe.Ingredients[i] = strings.TrimSpace(ingredient)
}
tmp := ""
for _, str := range strings.Split(recipe.Instructions, "\n") {
tmp += text.Wrap(str, 80) + "\n"
}
recipe.Instructions = tmp
}
func (r *Recipe) ToYaml() string {
const tmplString = `title: {{.Title}}
source: {{.Source}}
tags:
{{- range .Tags }}
- {{ . }}
{{- end }}
yield: {{.Yield}}
time:
{{- with .CookTime }}
cooking: {{ . }}
{{- end }}
{{- with .PrepTime }}
preparation: {{ . }}
{{- end }}
{{- with .TotalTime }}
total: {{ . }}
{{- end }}
steps:
- ingredients:
{{- range .Ingredients }}
- {{ . }}
{{- end }}
instructions: |
{{.Instructions | indent 4}}
`
tmpl := template.Must(template.New("recipe.yaml").Funcs(sprig.TxtFuncMap()).Parse(tmplString))
buf := new(bytes.Buffer)
err := tmpl.Execute(buf, r)
if err != nil {
log.Panic(err)
}
return buf.String()
}