all repos — navani @ 57138aa32c9fa1554d3eb783367906b810b98f66

forlater's primary mail processing service

mail/template.go (view raw)

 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
package mail

import (
	"bytes"
	"html/template"
	"net/url"
	"path/filepath"

	"git.icyphox.sh/forlater/navani/reader"
)

func stripQueryParams(u *url.URL) string {
	u.RawQuery = ""
	u.Fragment = ""
	return u.String()
}

func RenderTemplate(file string, article *reader.Article) ([]byte, error) {
	t, err := template.ParseGlob(filepath.Join("templates", "*.tpl"))
	if err != nil {
		return nil, err
	}

	buf := &bytes.Buffer{}
	if err := t.ExecuteTemplate(buf, file, struct {
		Content template.HTML
		Title   string
		Byline  string
		URL     string
	}{
		template.HTML(article.Content),
		article.Title,
		article.Byline,
		stripQueryParams(article.URL),
	}); err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}