mail/template.go (view raw)
1package mail
2
3import (
4 "bytes"
5 "html/template"
6 "net/url"
7 "path/filepath"
8
9 "git.icyphox.sh/forlater/navani/reader"
10)
11
12func stripQueryParams(u *url.URL) string {
13 u.RawQuery = ""
14 u.Fragment = ""
15 return u.String()
16}
17
18func RenderTemplate(file string, article *reader.Article) ([]byte, error) {
19 t, err := template.ParseGlob(filepath.Join("templates", "*.tpl"))
20 if err != nil {
21 return nil, err
22 }
23
24 buf := &bytes.Buffer{}
25 if err := t.ExecuteTemplate(buf, file, struct {
26 Content template.HTML
27 Title string
28 Byline string
29 URL string
30 }{
31 template.HTML(article.Content),
32 article.Title,
33 article.Byline,
34 stripQueryParams(article.URL),
35 }); err != nil {
36 return nil, err
37 }
38 return buf.Bytes(), nil
39}