-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrender.go
54 lines (46 loc) · 1.27 KB
/
render.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
package main
import (
"bytes"
"fmt"
"text/template"
"time"
)
var SUMMARY_TEMPLATE_FUNCS template.FuncMap = map[string]interface{}{
"time": func(t time.Time) string {
return t.Format(time.RFC1123Z)
},
}
// `SummaryRenderer` turns a `SummaryMessage` into an `OutgoingMessage`.
type SummaryRenderer interface {
Render(*SummaryMessage) OutgoingMessage
}
// `NoRenderer` uses a `SummaryMesssage` as an `OutgoingMessage` directly.
type NoRenderer struct{}
func (r *NoRenderer) Render(s *SummaryMessage) OutgoingMessage {
return s
}
// `TemplateRenderer` generates an `OutgoingMessage` by passing the
// `SummaryMessage` through a template, using the `SummaryMessage`'s envelope
// as the envelope of the generated message.
type TemplateRenderer struct {
Template *template.Template
}
func normalizeNewlines(s string) []byte {
buf := new(bytes.Buffer)
for i, c := range s {
if c == '\n' && i > 0 && s[i-1] != '\r' {
buf.WriteString("\r\n")
} else {
buf.WriteRune(c)
}
}
return buf.Bytes()
}
func (r *TemplateRenderer) Render(s *SummaryMessage) OutgoingMessage {
buf := new(bytes.Buffer)
err := r.Template.Execute(buf, s)
if err != nil {
fmt.Fprintf(buf, "\nError rendering message: %s\n", err)
}
return &message{s.From, s.To, normalizeNewlines(buf.String())}
}