-
Notifications
You must be signed in to change notification settings - Fork 0
/
render.go
107 lines (98 loc) · 1.8 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
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
package mailprint
import (
"embed"
"io"
"net/mail"
"strings"
"text/template"
"time"
)
//go:embed templates/*
var templateFiles embed.FS
var tmpl = template.Must(template.New("main").Funcs(template.FuncMap{
"processBody": processBody,
"escape": groffEscape,
}).ParseFS(templateFiles, "templates/*.mom"))
func Render(em *Email, pageFormat, logoPdf string, w io.Writer) error {
return tmpl.ExecuteTemplate(w, "main", struct {
// Page
PageFormat string
LogoPdfPath string
// Headers
Subject string
From []*mail.Address
To []*mail.Address
Cc []*mail.Address
Date time.Time
// Content
TextBody string
}{
PageFormat: pageFormat,
LogoPdfPath: logoPdf,
Subject: em.Subject,
From: em.From,
To: em.To,
Cc: em.Cc,
Date: em.Date,
TextBody: em.TextBody,
})
}
func groffEscape(s string) string {
prefix := ""
if strings.HasPrefix(s, ".") {
prefix = `\&`
}
return prefix + strings.ReplaceAll(s, `\`, `\\`)
}
func processBody(input string) string {
var out strings.Builder
color := "black"
setColor := func(c string) {
if color == c {
return
}
//out.WriteString(`\*[` + c + `]`)
out.WriteString(".COLOR " + c + "\n")
color = c
}
pos := 0
for _, b := range []byte(input) {
if pos == 0 {
switch b {
case '>':
setColor("quotedmail")
case '+':
setColor("green")
case '-':
setColor("red")
default:
setColor("black")
}
if b == '.' {
// Needs escaping
out.WriteString(`\&`)
}
}
if b == '\\' {
// Needs escaping
out.WriteString(`\`)
}
switch b {
case '\t':
for {
out.WriteByte(' ')
pos++
if pos%8 == 0 {
break
}
}
case '\n':
out.WriteByte(b)
pos = 0
default:
out.WriteByte(b)
pos++
}
}
return out.String()
}