-
Notifications
You must be signed in to change notification settings - Fork 0
/
templates.go
92 lines (86 loc) · 2.04 KB
/
templates.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
package contacts
import (
"fmt"
"html/template"
"net/url"
"strings"
"time"
)
var (
templateFuncs = map[string]interface{}{
"iterN": iterN,
"months": months,
"monthdays": monthdays,
"years": years,
"makeValues": makeValues,
"mailtoLink": mailtoLink,
"mailtoLinks": mailtoLinks,
}
monthNames = []string{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
}
)
func iterN(start, stop int) []int {
r := []int{}
if start > stop {
for i := start; i >= stop; i-- {
r = append(r, i)
}
} else {
for i := start; i <= stop; i++ {
r = append(r, i)
}
}
return r
}
func months() []string { return monthNames }
func monthdays() []int { return iterN(1, 31) }
func years() []int { return iterN(time.Now().Year()+1, time.Now().Year()-100) }
func makeValues(key, val string) url.Values { v := url.Values{}; v.Set(key, val); return v }
func mailtoLinks(list []*Contact) template.HTML { return mailtoLink(list...) }
func mailtoLink(list ...*Contact) template.HTML {
filtered := contactsWithEmail(list...)
switch len(filtered) {
case 0:
return template.HTML("")
case 1:
contact := filtered[0]
return template.HTML(
fmt.Sprintf(
"<a href='mailto:%s'>%s</a>",
safeEmailAddress(contact.DisplayName(), contact.Email[0]),
contact.Email[0]))
default:
var addr []string
for _, contact := range filtered {
addr = append(addr, safeEmailAddress(contact.DisplayName(), contact.Email[0]))
}
return template.HTML(
fmt.Sprintf(
"<a href='mailto:%s'>Email All</a>",
strings.Join(addr, ",")))
}
}
func contactsWithEmail(list ...*Contact) []*Contact {
var filtered []*Contact
for _, contact := range list {
if contact != nil && len(contact.Email) > 0 {
filtered = append(filtered, contact)
}
}
return filtered
}
func safeEmailAddress(name, email string) string {
return url.PathEscape(fmt.Sprintf("%q <%s>", name, email))
}