-
Notifications
You must be signed in to change notification settings - Fork 24
/
wallcalendar-helpers.lua
333 lines (268 loc) · 10.1 KB
/
wallcalendar-helpers.lua
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
-- paths are relative to wallcalendar.cls
local csv = require("wallcalendar-csv.lua")
local date = require("wallcalendar-date.lua")
local tp = tex.print
local tsp = tex.sprint
function loadCsv(csv_path)
local f = csv.open(csv_path, {separator = ";", header = true})
local data = {}
for fields in f:lines() do
data[#data + 1] = fields
end
return data
end
function ok(x)
return x ~= nil and x ~= ""
end
function hasNote(event)
return ok(event.note)
end
function monthNameToNum(monthName)
local months = {january = 1, february = 2, march = 3, april = 4, may = 5, june = 6, july = 7, august = 8, september = 9, october = 10, november = 11, december = 12}
return months[string.lower(monthName)]
end
function getMark(idx, events, markDefaults, isNote)
local event = events[idx]
local default_mark = {}
default_mark.number = {}
default_mark.day_text = {}
default_mark.note = {}
default_mark.number.symbol = idx
default_mark.number.above_offset = "\\markNumberAbove"
default_mark.number.right_offset = "\\markNumberRight"
default_mark.day_text.symbol = idx
default_mark.day_text.above_offset = "\\markDayTextAbove"
default_mark.day_text.right_offset = "\\markDayTextRight"
default_mark.note.symbol = idx
default_mark.note.above_offset = "" -- placeholder, not used for note
default_mark.note.right_offset = "" -- placeholder, not used for note
local mark = {}
for k,v in pairs(default_mark.number) do
local m = {}
local mark_key = ""
local csv_key = ""
if ok(isNote) then
mark_key = "note"
elseif ok(event.day_text) then
mark_key = "day_text"
else
mark_key = "number"
end
m = default_mark[mark_key]
csv_key = mark_key .. "_" .. k
if not ok(markDefaults) or not ok(markDefaults[idx]) or not ok(markDefaults[idx][csv_key]) then
mark[k] = m[k]
else
mark[k] = markDefaults[idx][csv_key]
end
end
return mark
end
function getCombinedMark(idx, events, markDefaults, isNote)
local event = events[idx]
local mark = getMark(idx, events, markDefaults, isNote)
mark.symbol = ""
for i,e in pairs(events) do
if e.date == event.date then
local m = getMark(i, events, markDefaults, isNote)
if mark.symbol == "" then
mark.symbol = m.symbol
else
mark.symbol = mark.symbol .. "\\symbolSeparator " .. m.symbol
end
end
end
return mark
end
function collectEvents(byWhat, events, byValue, filterPred)
local data = {}
for idx,row in pairs(events) do
d = date(row.date)
if filterPred ~= nil then
if byWhat == 'month' then
if d:getmonth() == byValue and filterPred(row) then
data[#data + 1] = row
end
elseif byWhat == 'year' then
if d:getyear() == byValue and filterPred(row) then
data[#data + 1] = row
end
end
else
if byWhat == 'month' then
if d:getmonth() == byValue then
data[#data + 1] = row
end
elseif byWhat == 'year' then
if d:getyear() == byValue then
data[#data + 1] = row
end
end
end
end
return data
end
function eventsInMonth(events, month, filterPred)
return collectEvents('month', events, month, filterPred)
end
function eventsInYear(events, year, filterPred)
return collectEvents('year', events, year, filterPred)
end
function formatEvents(events, formatFunc, formatCmd, markDefaultsCsv, minEvents)
if ok(minEvents) and #events < minEvents then
d = minEvents - #events
for i=0,d,1 do
events[#events + 1] = {}
end
end
local markDefaults = nil
if ok(markDefaultsCsv) then
markDefaults = loadCsv(markDefaultsCsv)
end
for idx,event in pairs(events) do
local mark = {}
local d = {}
if ok(event.date) then
-- don't use getCombinedMark here, events on the same day will be printed one after the other
mark = getMark(idx, events, markDefaults, true)
d = date(event.date)
end
if formatFunc then
formatFunc(idx, #events, event, d, mark)
else
tsp("\\def\\eIdx{"..idx .."}") -- \def\eIdx{1}
tsp("\\def\\eMaxIdx{"..#events .."}") -- \def\eMaxIdx{8}
tsp("\\def\\eMark{"..mark.symbol .."}") -- \def\eMark{\kiteMark}
tsp("\\def\\eIsoDate{"..event.date .."}") -- \def\eIsoDate{2018-01-12}
tsp("\\def\\eYear{"..d:getyear() .."}") -- \def\eYear{2018}
tsp("\\def\\eMonth{\\x"..d:fmt("%B") .."}") -- \def\eMonth\xJanuary
tsp("\\def\\eMonthShort{\\x"..d:fmt("%b").."Short" .."}") -- \def\eMonthShort\xJanShort
tsp("\\def\\eDay{"..d:getday() .."}") -- \def\eDay{12}
if ok(event.day_text) then
tsp("\\def\\eDayText{"..event.day_text .."}") -- \def\eDayText{\dejaVuSans\char"263C}
else
tsp("\\def\\eDayText{}")
end
if ok(event.note) then
tsp("\\def\\eNote{"..event.note .."}") -- \def\eNote{Anniversary Day}
else
tsp("\\def\\eNote{}")
end
tsp(formatCmd)
end
end
end
-- It's better to call it with the name of the month than its number because it
-- fits the wrapper commands better.
function monthEvents(monthName, filterPred, formatFunc, formatCmd, eventsCsv, markDefaultsCsv, minEvents)
local monthNum = monthNameToNum(monthName)
local events = eventsInMonth(loadCsv(eventsCsv), monthNum, filterPred)
formatEvents(events, formatFunc, formatCmd, markDefaultsCsv, minEvents)
end
function yearEvents(yearNum, filterPred, formatFunc, formatCmd, eventsCsv, markDefaultsCsv, minEvents)
local events = eventsInYear(loadCsv(eventsCsv), yearNum, filterPred)
formatEvents(events, formatFunc, formatCmd, markDefaultsCsv, minEvents)
end
-- monthName is better for argument than monthNum
function monthMarksDayText(monthName, filterPred, eventsCsv)
local monthNum = monthNameToNum(monthName)
local events = eventsInMonth(loadCsv(eventsCsv), monthNum, filterPred);
for idx,event in pairs(events) do
if ok(event.day_text) then
tsp(string.format(" if (equals=%s) [day text={%s}, xshift=\\dayTextXshift, yshift=\\dayTextYshift] ", event.date, event.day_text))
end
end
end
function monthMarksDayTextInline(monthName, filterPred, eventsCsv)
local monthNum = monthNameToNum(monthName)
local events = eventsInMonth(loadCsv(eventsCsv), monthNum, filterPred);
for idx,event in pairs(events) do
if ok(event.day_text) then
local d = date(event.date)
tsp(string.format(" \\draw node [above right=%s and %s of cal%s-%s.north west, anchor=north west] {%s}; ",
"0.5pt",
"20pt",
d:fmt("%m"),
event.date,
event.day_text))
end
end
end
function yearMarksDayText(yearNum, filterPred, eventsCsv)
local events = eventsInYear(loadCsv(eventsCsv), yearNum, filterPred);
for idx,event in pairs(events) do
if ok(event.day_text) then
tsp(string.format(" if (equals=%s) [day text={%s}, xshift=\\dayTextXshift, yshift=\\dayTextYshift] ", event.date, event.day_text))
end
end
end
function formatMarksNote(events, filterPred, markDefaultsCsv, isOneCalendar)
local markDefaults = nil
if ok(markDefaultsCsv) then
markDefaults = loadCsv(markDefaultsCsv)
end
local alreadyMarkedDates = {}
for idx,event in pairs(events) do
if ok(event.note) and alreadyMarkedDates[event.date] == nil then
alreadyMarkedDates[event.date] = true
local d = date(event.date)
local mark = getCombinedMark(idx, events, markDefaults)
if ok(isOneCalendar) and isOneCalendar == true then
tsp(string.format(" \\draw node [above right=%s and %s of cal-%s.north east] {\\monthMarkFmt %s}; ",
mark.above_offset,
mark.right_offset,
event.date,
mark.symbol))
else
tsp(string.format(" \\draw node [above right=%s and %s of cal%s-%s.north east] {\\monthMarkFmt %s}; ",
mark.above_offset,
mark.right_offset,
d:fmt("%m"),
event.date,
mark.symbol))
end
end
end
end
function formatInlineNotes(events, filterPred, markDefaultsCsv)
local markDefaults = nil
if ok(markDefaultsCsv) then
markDefaults = loadCsv(markDefaultsCsv)
end
local alreadyMarkedDates = {}
for idx,event in pairs(events) do
if ok(event.note) and alreadyMarkedDates[event.date] == nil then
alreadyMarkedDates[event.date] = true
local d = date(event.date)
tsp(string.format(" \\draw node [above right=0pt and 0pt of cal%s-%s.north west, anchor=north west] {\\begin{minipage}[t][\\@t@calendar@dayYshift - 10pt][b]{\\@t@calendar@dayXshift - 8pt}\\eventsFmt %s\\end{minipage}}; ",
d:fmt("%m"),
event.date,
event.note))
end
end
end
-- monthName is better for argument than monthNum
function monthMarksNote(monthName, filterPred, eventsCsv, markDefaultsCsv)
local monthNum = monthNameToNum(monthName)
if not ok(filterPred) then
filterPred = function(e) return ok(e.note) end
end
local events = eventsInMonth(loadCsv(eventsCsv), monthNum, filterPred);
formatMarksNote(events, filterPred, markDefaultsCsv, false)
end
-- monthName is better for argument than monthNum
function monthInlineNotes(monthName, filterPred, eventsCsv, markDefaultsCsv)
local monthNum = monthNameToNum(monthName)
if not ok(filterPred) then
filterPred = function(e) return ok(e.note) end
end
local events = eventsInMonth(loadCsv(eventsCsv), monthNum, filterPred);
formatInlineNotes(events, filterPred, markDefaultsCsv)
end
function yearMarksNote(yearNum, filterPred, eventsCsv, markDefaultsCsv, isOneCalendar)
if not ok(filterPred) then
filterPred = function(e) return ok(e.note) end
end
local events = eventsInYear(loadCsv(eventsCsv), yearNum, filterPred);
formatMarksNote(events, filterPred, markDefaultsCsv, isOneCalendar)
end