-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmplrun.go
71 lines (61 loc) · 1.76 KB
/
tmplrun.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
package tmplrun
import (
"io"
"io/fs"
"github.com/connormckelvey/tmplrun/ast"
"github.com/connormckelvey/tmplrun/evaluator"
"github.com/connormckelvey/tmplrun/evaluator/driver"
"github.com/connormckelvey/tmplrun/lexer"
"github.com/connormckelvey/tmplrun/parser"
)
// TMPLRun represents a template runner that manages the rendering of templates.
type TMPLRun struct {
fs fs.FS // fs is the filesystem from which templates will be loaded.
}
// New creates a new instance of TMPLRun with the given filesystem.
func New(fsys fs.FS) *TMPLRun {
return &TMPLRun{fsys}
}
// RenderInput contains the input data required for rendering a template.
type RenderInput struct {
Entrypoint string // Entrypoint is the path to the main template file.
Props map[string]any // Props contains the properties to be passed to the template.
}
// Render renders the template specified by the input and writes the result to the given writer.
func (tr *TMPLRun) Render(w io.Writer, input *RenderInput) error {
f, err := tr.fs.Open(input.Entrypoint)
if err != nil {
return err
}
defer f.Close()
doc, err := tr.parse(f)
if err != nil {
return err
}
err = tr.render(w, input.Entrypoint, doc, input.Props)
if err != nil {
return err
}
return nil
}
func (tr *TMPLRun) parse(r io.Reader) (*ast.Document, error) {
lex := lexer.New(r)
par := parser.New(lex)
return par.Parse()
}
func (tr *TMPLRun) render(w io.Writer, currentFile string, doc *ast.Document, props map[string]any) error {
hooks := &hooks{
tr: tr,
currentFile: currentFile,
}
ev := evaluator.New(driver.NewGoja(), hooks)
res, err := ev.Render(doc, evaluator.NewEnvironment(tr.fs, props, hooks))
if err != nil {
return err
}
_, err = w.Write([]byte(res))
if err != nil {
return err
}
return nil
}