-
Notifications
You must be signed in to change notification settings - Fork 2
/
renderer.go
70 lines (56 loc) · 1.56 KB
/
renderer.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
package errata
import (
"io"
"strings"
"github.com/flosch/pongo2/v5"
"github.com/iancoleman/strcase"
)
type pongo2Renderer struct {
loader *templateLoader
}
func preparePongo2() {
pongo2.RegisterFilter("constantize", func(in *pongo2.Value, param *pongo2.Value) (out *pongo2.Value, err *pongo2.Error) {
return pongo2.AsValue(strcase.ToCamel(in.String())), nil
})
// backticks can't be escaped in golang multi-line strings
pongo2.RegisterFilter("escape_backtick", func(in *pongo2.Value, param *pongo2.Value) (out *pongo2.Value, err *pongo2.Error) {
splits := strings.Split(in.String(), "`")
return pongo2.AsValue(strings.Join(splits, "` + \"`\" + `")), nil
})
pongo2.SetAutoescape(false)
}
func createPongo2Renderer(loader *templateLoader) *pongo2Renderer {
preparePongo2()
return &pongo2Renderer{
loader: loader,
}
}
func (p *pongo2Renderer) getTemplate() (*pongo2.Template, error) {
if !p.loader.builtin {
tmpl, err := pongo2.DefaultSet.FromFile(p.loader.path)
if err != nil {
return nil, NewInvalidSyntaxErr(err, p.loader.path)
}
return tmpl, nil
}
b, err := templates.ReadFile(p.loader.path)
if err != nil {
return nil, NewFileNotReadableErr(err, p.loader.path)
}
tmpl, err := embeddedFS.FromBytes(b)
if err != nil {
return nil, NewInvalidSyntaxErr(err, p.loader.path)
}
return tmpl, nil
}
func (p *pongo2Renderer) render(ctx map[string]interface{}, w io.Writer) error {
tmpl, err := p.getTemplate()
if err != nil {
return err
}
err = tmpl.ExecuteWriter(ctx, w)
if err != nil {
return NewTemplateExecutionErr(err)
}
return nil
}