-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathemail.go
More file actions
265 lines (220 loc) · 6.18 KB
/
Copy pathemail.go
File metadata and controls
265 lines (220 loc) · 6.18 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package myrtle
import (
"errors"
"strings"
"github.com/gzuidhof/myrtle/theme"
)
// ErrThemeCannotRenderBlock is returned when neither a block-provided renderer nor the active theme can render a block kind.
var ErrThemeCannotRenderBlock = errors.New("myrtle: theme cannot render block")
type htmlBlock interface {
RenderHTML(values theme.Values) (string, error)
}
// Email is an immutable rendered message composed from header metadata, values, and blocks.
type Email struct {
header *HeaderSection
footer *FooterSection
preheader string
preheaderPaddingRepeat int
values theme.Values
blocks []Block
theme theme.Theme
}
// Preheader returns the preheader text.
func (email *Email) Preheader() string {
return email.preheader
}
// Values returns the resolved theme values used while rendering this email.
func (email *Email) Values() theme.Values {
return email.values
}
// HTML renders the email into themed HTML by rendering each block then composing the full layout.
func (email *Email) HTML() (string, error) {
header, err := email.headerHTMLView()
if err != nil {
return "", err
}
footer, err := email.footerHTMLView()
if err != nil {
return "", err
}
fragments := make([]string, 0, len(email.blocks))
blockViews := make([]theme.EmailBlockView, 0, len(email.blocks))
for _, block := range email.blocks {
if block == nil {
continue
}
customHTML, ok, err := email.renderBlockHTML(block)
if err != nil {
return "", err
}
if ok {
spec := normalizedLayoutSpec(block.LayoutSpec())
fragments = append(fragments, customHTML)
blockViews = append(blockViews, theme.EmailBlockView{
Kind: block.Kind(),
HTML: customHTML,
InsetMode: string(spec.InsetMode),
CustomInset: spec.CustomInset,
})
continue
}
return "", errors.Join(ErrThemeCannotRenderBlock, errors.New(string(block.Kind())))
}
return email.theme.RenderHTML(theme.EmailView{
Header: header,
Footer: footer,
Preheader: email.preheader,
PreheaderPaddingRepeat: email.preheaderPaddingRepeat,
Values: email.values,
Blocks: fragments,
BlockViews: blockViews,
})
}
func (email *Email) renderBlockHTML(block Block) (string, bool, error) {
data := block.TemplateData()
if htmlRenderer, ok := block.(htmlBlock); ok {
customHTML, err := htmlRenderer.RenderHTML(email.values)
if err != nil {
return "", false, err
}
return customHTML, true, nil
}
return email.theme.RenderBlockHTML(theme.BlockView{
Kind: block.Kind(),
Data: data,
Values: email.values,
})
}
// Text renders the email into a plain-text fallback representation.
func (email *Email) Text() (string, error) {
textParts := make([]string, 0, len(email.blocks)+2)
for _, block := range email.blocks {
if block == nil {
continue
}
text, err := block.RenderText(RenderContext{
Preheader: email.Preheader(),
Values: email.values,
})
if err != nil {
return "", err
}
if strings.TrimSpace(text) == "" {
continue
}
textParts = append(textParts, text)
}
body := strings.Join(textParts, "\n\n")
textHeader, err := email.textHeaderView()
if err != nil {
return "", err
}
textFooter, err := email.textFooterView()
if err != nil {
return "", err
}
if wrapper, ok := email.theme.(theme.TextWrapper); ok {
return wrapper.WrapText(theme.TextView{
Header: textHeader,
Footer: textFooter,
Preheader: email.preheader,
Values: email.values,
Body: body,
})
}
var outputParts []string
if strings.TrimSpace(email.Preheader()) != "" {
outputParts = append(outputParts, email.Preheader())
}
if strings.TrimSpace(body) != "" {
outputParts = append(outputParts, body)
}
if textFooter != nil && strings.TrimSpace(textFooter.Text) != "" {
outputParts = append(outputParts, textFooter.Text)
}
return strings.Join(outputParts, "\n\n"), nil
}
func (email *Email) textHeaderView() (*theme.HeaderView, error) {
if email.header == nil || !email.header.RenderInText {
return nil, nil
}
if email.header.Block == nil {
return nil, nil
}
text, err := email.header.Block.RenderText(RenderContext{
Preheader: email.Preheader(),
Values: email.values,
})
if err != nil {
return nil, err
}
text = strings.TrimSpace(text)
if text == "" {
return nil, nil
}
return &theme.HeaderView{Text: text}, nil
}
func (email *Email) headerHTMLView() (*theme.HeaderView, error) {
if email.header == nil {
return nil, nil
}
if email.header.Block == nil {
return nil, nil
}
headerHTML, ok, err := email.renderBlockHTML(email.header.Block)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.Join(ErrThemeCannotRenderBlock, errors.New(string(email.header.Block.Kind())))
}
spec := normalizedLayoutSpec(email.header.Block.LayoutSpec())
return &theme.HeaderView{
HTML: headerHTML,
Placement: string(normalizedHeaderPlacement(email.header.Placement)),
InsetMode: string(spec.InsetMode),
CustomInset: spec.CustomInset,
}, nil
}
func (email *Email) textFooterView() (*theme.FooterView, error) {
if email.footer == nil || !email.footer.RenderInText {
return nil, nil
}
if email.footer.Block == nil {
return nil, nil
}
text, err := email.footer.Block.RenderText(RenderContext{
Preheader: email.Preheader(),
Values: email.values,
})
if err != nil {
return nil, err
}
text = strings.TrimSpace(text)
if text == "" {
return nil, nil
}
return &theme.FooterView{Text: text}, nil
}
func (email *Email) footerHTMLView() (*theme.FooterView, error) {
if email.footer == nil {
return nil, nil
}
if email.footer.Block == nil {
return nil, nil
}
footerHTML, ok, err := email.renderBlockHTML(email.footer.Block)
if err != nil {
return nil, err
}
if !ok {
return nil, errors.Join(ErrThemeCannotRenderBlock, errors.New(string(email.footer.Block.Kind())))
}
spec := normalizedLayoutSpec(email.footer.Block.LayoutSpec())
return &theme.FooterView{
HTML: footerHTML,
Placement: string(normalizedFooterPlacement(email.footer.Placement)),
InsetMode: string(spec.InsetMode),
CustomInset: spec.CustomInset,
}, nil
}