-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.go
More file actions
73 lines (67 loc) · 2.31 KB
/
renderer.go
File metadata and controls
73 lines (67 loc) · 2.31 KB
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
package cooklang
// RecipeRenderer interface defines how recipes can be rendered to different output formats.
// Implementations can render recipes as Markdown, HTML, plain text, or any custom format.
//
// Example implementation:
//
// type JSONRenderer struct{}
// func (jr JSONRenderer) RenderRecipe(recipe *cooklang.Recipe) string {
// data, _ := json.MarshalIndent(recipe, "", " ")
// return string(data)
// }
type RecipeRenderer interface {
RenderRecipe(recipe *Recipe) string
}
// RendererFunc is a function type that implements RecipeRenderer.
// This allows using plain functions as renderers without creating a new type.
//
// Example:
//
// simpleRenderer := cooklang.RendererFunc(func(r *cooklang.Recipe) string {
// return fmt.Sprintf("# %s\n\nServings: %.0f", r.Title, r.Servings)
// })
// output := recipe.RenderWith(simpleRenderer)
type RendererFunc func(*Recipe) string
// RenderRecipe implements the RecipeRenderer interface for RendererFunc.
func (f RendererFunc) RenderRecipe(recipe *Recipe) string {
return f(recipe)
}
// SetRenderer allows setting a custom renderer for a recipe.
// Once set, calling Render() will use this custom renderer instead of the default.
//
// Parameters:
// - renderer: A RecipeRenderer implementation
//
// Example:
//
// recipe, _ := cooklang.ParseFile("recipe.cook")
// recipe.SetRenderer(renderers.MarkdownRenderer{})
// markdown := recipe.Render()
func (r *Recipe) SetRenderer(renderer RecipeRenderer) {
r.RenderFunc = func() string {
return renderer.RenderRecipe(r)
}
}
// SetRendererFunc allows setting a custom renderer function for a recipe
func (r *Recipe) SetRendererFunc(renderFunc func(*Recipe) string) {
r.RenderFunc = func() string {
return renderFunc(r)
}
}
// RenderWith renders the recipe using the provided renderer.
// This allows one-time rendering without setting a permanent renderer on the recipe.
//
// Parameters:
// - renderer: A RecipeRenderer implementation to use for rendering
//
// Returns:
// - string: The rendered recipe in the format defined by the renderer
//
// Example:
//
// recipe, _ := cooklang.ParseFile("recipe.cook")
// markdown := recipe.RenderWith(renderers.MarkdownRenderer{})
// html := recipe.RenderWith(renderers.HTMLRenderer{})
func (r *Recipe) RenderWith(renderer RecipeRenderer) string {
return renderer.RenderRecipe(r)
}