/
calfmt.go
276 lines (239 loc) · 6.63 KB
/
calfmt.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
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
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"fmt"
"sort"
"strings"
"time"
"google.golang.org/api/calendar/v3"
)
func datesToOrg(start, end *calendar.EventDateTime) string {
final := ""
if start == nil { // this event has dates! hurrah!
return "\n"
}
if start.Date != "" { // all day event!
ts, _ := time.Parse("2006-01-02", start.Date)
tsf := ts.Format("2006-01-02")
final = final + fmt.Sprintf("<%s", tsf)
if end == nil {
return final + ">"
}
te, _ := time.Parse("2006-01-02", end.Date)
te = te.AddDate(0, 0, -1)
// The end date is "exclusive", so we should subtract a day, and
// if the day is equivalent to start, then we should just print
// start.
if te.Equal(ts) {
return final + ">"
}
tef := te.Format("2006-01-02")
return final + fmt.Sprintf(">--<%s>", tef)
}
ts, _ := time.Parse(time.RFC3339, start.DateTime)
ts = ts.In(time.Local)
tsf := ts.Format("2006-01-02 Mon 15:04")
final = final + fmt.Sprintf("<%s", tsf)
if end == nil {
return final + fmt.Sprintf(">")
}
te, _ := time.Parse(time.RFC3339, end.DateTime)
te = te.In(time.Local)
if te.Day() != ts.Day() {
tef := te.Format("2006-01-02 Mon 15:04")
return final + fmt.Sprintf(">--<%s>", tef)
}
tef := te.Format("15:04")
return final + fmt.Sprintf("-%s>", tef)
}
func datesToInactiveOrg(start, end *calendar.EventDateTime) string {
datestr := datesToOrg(start, end)
left := strings.ReplaceAll(datestr, "<", "[")
return strings.ReplaceAll(left, ">", "]")
}
func noTodoKwds(s string) string {
states := []string{"TODO", "NEXT", "STARTED", "WAITING", "PROJECT", "DONE", "NVM"}
for _, state := range states {
// We essentially reimplement TrimPrefix, so we don't waste time
// running HasPrefix multiple times. We also don't remove the
// space in the prefix.
if strings.HasPrefix(s, state+" ") {
s = s[len(state):]
s = "/" + state + "/" + s
// Only the actual prefix matters, we don't need to
// check the remaining keywords.
return s
}
}
return s
}
// cleanString removes special characters for org-mode, as almost no one will be
// using org-mode formatting.
func cleanString(s string) string {
s = strings.Replace(s, "[", "{", -1)
s = strings.Replace(s, "]", "}", -1)
s = strings.Replace(s, "\n*", "\n,*", -1)
return s
}
func isDeclinedByMe(e *calendar.Event) bool {
for _, a := range e.Attendees {
if a != nil && a.Self && a.ResponseStatus == "declined" {
return true
}
}
return false
}
func fmtOrgHeader(e *calendar.Event) string {
var buf string
buf += fmt.Sprintf("** ")
if e.Status == "tenative" || e.Status == "cancelled" {
buf += fmt.Sprintf("(%s) ", e.Status)
}
summary := e.Summary
if summary == "" {
summary = "busy"
}
buf += fmt.Sprintf("%s\n", noTodoKwds(summary))
buf += fmt.Sprintf(":PROPERTIES:\n")
buf += fmt.Sprintf(":ID: %s\n", e.ICalUID)
buf += fmt.Sprintf(":GCALLINK: %s\n", e.HtmlLink)
if e.Creator != nil {
buf += fmt.Sprintf(":CREATOR: [[mailto:%s][%s]]\n", e.Creator.Email, cleanString(e.Creator.DisplayName))
}
if e.Organizer != nil {
buf += fmt.Sprintf(":ORGANIZER: [[mailto:%s][%s]]\n", e.Organizer.Email, cleanString(e.Organizer.DisplayName))
}
buf += fmt.Sprintf(":END:\n\n")
return buf
}
func fmtOrgDate(e *calendar.Event) string {
return fmt.Sprintf("%s\n", datesToOrg(e.Start, e.End))
}
func fmtInactiveOrgDate(e *calendar.Event) string {
return fmt.Sprintf("%s\n", datesToInactiveOrg(e.Start, e.End))
}
func fmtOrgAttendees(e *calendar.Event) string {
var buf string
attendees := e.Attendees
if len(attendees) == 0 {
return ""
}
canonical_id := func(ea *calendar.EventAttendee) string {
if ea.Id != "" {
return ea.Id
} else if ea.Email != "" {
return ea.Email
} else if ea.DisplayName != "" {
return cleanString(ea.DisplayName)
}
return "sadness"
}
sort.SliceStable(attendees, func(i, j int) bool {
return canonical_id(attendees[i]) < canonical_id(attendees[j])
})
if len(attendees) > 20 {
buf += fmt.Sprintf("Attendees: ... Many\n")
return buf
}
buf += fmt.Sprintf("Attendees:\n")
for _, a := range attendees {
if a == nil {
continue
}
// ResponseStatus: The attendee's response status. Possible values are:
//
// - "needsAction" - The attendee has not responded to the invitation.
//
// - "declined" - The attendee has declined the invitation.
// - "tentative" - The attendee has tentatively accepted the invitation.
//
// - "accepted" - The attendee has accepted the invitation.
// ResponseStatus string `json:"responseStatus,omitempty"`
statuschar := " "
switch a.ResponseStatus {
case "":
case "NeedsAction":
case "declined":
statuschar = "✗"
case "tenative":
statuschar = "☐"
case "accepted":
statuschar = "✓"
}
linkname := cleanString(a.DisplayName)
if linkname == "" {
linkname = a.Email
}
buf += fmt.Sprintf(" %s [[mailto:%s][%s]]\n", statuschar, a.Email, linkname)
}
return buf
}
func fmtOrgBody(e *calendar.Event) string {
var buf string
to_p := fmt.Sprintf("\nSummary: %s\n%s\n", e.Summary, e.Description)
buf += cleanString(to_p)
buf += "\n"
attachment_title := "\nAttachments:\n"
attachment_entries := ""
for _, a := range e.Attachments {
if a == nil {
continue
}
attachment_entries += fmt.Sprintf("- [[%s][%s]]\n", a.FileUrl,
cleanString(a.Title))
}
if len(attachment_entries) > 0 {
buf += attachment_title + attachment_entries
}
return buf
}
func fmtEventGroup(calid string, events []*calendar.Event) string {
var buf string
// take the last header of the set, has the most recent summary info.
buf = fmtOrgHeader(events[len(events)-1])
// Put the dates from each event repeat
unique_attendees := make(map[string]struct{})
for _, i := range events {
if attendingEvent(calid, *i) {
buf += fmtOrgDate(i)
} else {
buf += fmtInactiveOrgDate(i)
}
attendee := fmtOrgAttendees(i)
if _, ok := unique_attendees[attendee]; !ok {
unique_attendees[attendee] = struct{}{}
buf += attendee
}
}
unique_bodies := make(map[string]struct{})
// Remove duplicate bodies
for _, i := range events {
body := fmtOrgBody(i)
if _, ok := unique_bodies[body]; !ok {
unique_bodies[body] = struct{}{}
buf += body
}
}
return buf
}
func filteredEvent(calid, summary string) bool {
for _, title := range titleFilters[calid] {
if strings.Contains(summary, title) {
return true
}
}
return false
}
func attendingEvent(calid string, event calendar.Event) bool {
for _, email := range attendeeFilters[calid] {
for _, att := range event.Attendees {
if (att.Email == email ||
att.Id == email ||
att.DisplayName == email) &&
att.ResponseStatus == "declined" {
return false
}
}
}
// include by default rather than not include.
return true
}