-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
148 lines (120 loc) · 2.67 KB
/
template.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package templater
import (
"bytes"
"fmt"
htmltpl "html/template"
"os"
texttpl "text/template"
)
const RecoverErrorMessage = "Template Error"
type Recover struct {
TemplateName string
SourceError error
}
func PrintIfError(err error) error {
if err == nil {
return nil
}
if r, ok := err.(Recover); ok {
fmt.Printf("\n%v\n", r.Format())
} else {
fmt.Printf("\n%v\n", err)
}
return err
}
func DebugOrExit(dest *bytes.Buffer, err error) {
if dest != nil && dest.Len() > 0 {
fmt.Printf("\n%s\n", dest.String())
}
PrintIfErrorAndExit(err)
}
func PrintIfErrorAndExit(err error) {
if PrintIfError(err) != nil {
os.Exit(1)
}
}
func (r Recover) Error() string {
return RecoverErrorMessage
}
func (r Recover) Format() error {
return fmt.Errorf("%s in `%s`: %v", RecoverErrorMessage, r.TemplateName, r.Error())
}
func HtmlFromString(name, content string) *Parser {
return newParser(nil, true, name, content)
}
func TextFromString(name, content string) *Parser {
return newParser(nil, false, name, content)
}
func newParser(err error, ishtml bool, name, content string) *Parser {
return &Parser{
err: err,
html: ishtml,
name: name,
content: content,
}
}
func TextFromFile(filename string) *Parser {
content, err := os.ReadFile(filename)
return newParser(err, false, filename, string(content))
}
func HtmlFromFile(filename string) *Parser {
content, err := os.ReadFile(filename)
return newParser(err, true, filename, string(content))
}
type Parser struct {
err error
name string
content string
html bool
}
func (p *Parser) parseHtml(data interface{}, dest *bytes.Buffer) error {
t := htmltpl.New(p.name).Funcs(functions)
t, err := t.Parse(p.content)
if err != nil {
return err
}
return t.Execute(dest, data)
}
func (p *Parser) parseText(data interface{}, dest *bytes.Buffer) error {
t := texttpl.New(p.name).Funcs(functions)
t, err := t.Parse(p.content)
if err != nil {
return err
}
return t.Execute(dest, data)
}
func (p *Parser) Parse(data interface{}, dest *bytes.Buffer) (err error) {
if p.err != nil {
return p.err
}
defer func(returnError *error) {
if r := recover(); r != nil {
errx, ok := r.(error)
if !ok {
errx = fmt.Errorf("pkg: recovery -- err %v", r)
}
*returnError = Recover{
TemplateName: p.name,
SourceError: errx,
}
}
}(&err)
if p.html {
err = p.parseHtml(data, dest)
} else {
err = p.parseText(data, dest)
}
return
}
func (p *Parser) ParseToOutput(data interface{}) (out *bytes.Buffer, err error) {
if p.err != nil {
return nil, p.err
}
out = &bytes.Buffer{}
if err = p.Parse(data, out); err != nil {
out.Reset()
return nil, err
} else {
return out, nil
}
}