]> git.plutz.net Git - shellwiki/blob - macros/calendar
5a486756537b452d6e53143dff2223bbef2215c6
[shellwiki] / macros / calendar
1 #!/bin/sh
2 # vi:syntax=bash
3
4 # Copyright 2024 Paul Hänsch
5
6 # Permission to use, copy, modify, and/or distribute this software for any
7 # purpose with or without fee is hereby granted, provided that the above
8 # copyright notice and this permission notice appear in all copies.
9
10 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
13 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
16 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
18
19 . "$_EXEC/cgilite/cgilite.sh"
20 . "$_EXEC/acl.sh"
21 . "$_EXEC/tools.sh"
22 . "$_EXEC/datetime.sh"
23
24 tags='' ntags='' dir='' depth='' glob_system_pages=false
25 label='' labeltype='' altlabel='' cnt=0
26
27 set -- "$@" --
28 while [ $# -gt 0 ]; do case $1 in
29   --system) glob_system_pages=true; shift 1;;
30   --depth)  depth="$2" shift 2;;
31   \#*) tags="${tags}${tags:+ }${1###}"; shift 1;;
32   \!*) ntags="${ntags}${ntags:+ }${1##!}"; shift 1;;
33   --date|--from) fromdate="$2"; shift 2;;
34   --weekstart|--ws|-ws) ws="$2"; shift 2;;
35   --) shift 1; break;;
36   *) if [ ! "$dir" ]; then
37       dir="$1"
38       set -- "$@" "$1"; shift 1;
39     elif [ ! "$depth" ]; then
40       depth="$1"; shift 1;
41     else
42       set -- "$@" "$1"; shift 1;
43     fi;;
44 esac; done
45
46 [ "$*" ] || set -- "*"
47 [ "$depth" -ge 0 -o "$depth" -le 0 ] 2>&- || depth=0
48
49 read DY DM DD <<-EOF
50         $(isdate "$fromdate" \
51           && date -ud "$fromdate" +"%Y %m %d" \
52           || date -u +"%Y %m %d"
53         )
54         EOF
55
56 case $ws in
57   0|[sS]*) ws=0;;
58   1|[mM]*) ws=1;;
59   *) ws=0;;
60 esac
61
62 rrexpand() {
63   # Recurrence Expansion
64   # read recurring event specifications and expand them to a list of
65   # single events within the specified time frame
66
67   local dstart="$1" dend="$2"
68   local junk1 start end rrfreq rrint rrend evtitle evlink junk2
69
70   while read -r junk1 start end rrfreq rrint rrend evtitle evlink junk2; do
71     [ "$rrend" -eq -1 ] && rrend=9999999999
72
73     if [ "$start" -lt "$dend" ] &&
74        [ "$end" -gt "$dstart" -o "$rrend" -gt "$dstart" ]; then
75       case $rrint in
76         day) rrex_day;;
77         week) rrex_week;;
78         month) rrex_month;;
79         year) rrex_year;;
80         weekday)
81           [ "$rrfreq" -ge 0 ] && rrex_weekday \
82                               || rrex_lastweekday
83           ;;
84         *):
85           printf '%i    %i      %s      %s\n' "$start" "$end" "$evtitle" "$evlink"
86           ;;
87       esac
88     fi
89   done
90 }
91
92 rrex_day() {
93   # helper for rrexpand daily/N-day expansion
94   local nstart nend
95
96   nend=$(( rrfreq * 86400 - (dstart - end) % (rrfreq * 86400) + dstart ))
97   nstart=$(( start - end + nend))
98   while [ "$nstart" -lt "$rrend" -a "$nstart" -lt "$dend" ]; do
99     [ "$nstart" -ge "$start" ] \
100     && printf '%i       %i      %s      %s\n' "$nstart" "$nend" "$evtitle" "$evlink"
101     nstart="$((nstart + rrfreq * 86400))"
102       nend="$((nstart - start + end))"
103   done
104 }
105
106 rrex_week() {
107   # helper for rrexpand weekly/N-week expansion
108   local nstart nend
109
110   nend=$(( 0 * 604800 - (dstart - end) % (rrfreq * 604800) + dstart ))
111   nstart=$(( start - end + nend))
112   while [ "$nstart" -lt "$rrend" -a "$nstart" -lt "$dend" ]; do
113     [ "$nstart" -ge "$start" -a "$nstart" -ge "$dstart" ] \
114     && printf '%i       %i      %s      %s\n' "$nstart" "$nend" "$evtitle" "$evlink"
115     nstart="$((nstart + rrfreq * 7 * 86400))"
116       nend="$((nstart - start + end))"
117   done
118 }
119
120 rrex_month() {
121   # helper for rrexpand monthly/N-month expansion
122   local nstart nend
123
124   { read _y _m _d; read y m d start_time; } <<-EOF
125         $(date -ud @$dstart +"%Y %_m %_d"
126           date -ud @$start  +"%Y %_m %_d %T"
127         )
128         EOF
129   _m=$((_y * 12 + _m)) m=$((y * 12 + m))
130   while :; do
131     m=$(( rrfreq - ((_m - m - 1) % rrfreq + 1) + _m ))
132     nstart="$(printf '%04i-%02i-%02i' "$(( (m - 1) / 12 ))" "$(( (m - 1) % 12 + 1 ))" "$d")"
133     if isdate "$nstart" && [ "$(date -ud "$nstart" +%s)" -ge "$dstart" ]; then
134       break
135     fi >/dev/null
136     _m="$((_m + rrfreq))"
137   done
138   nstart="$(date -ud "$nstart $start_time" +%s)"
139     nend="$((end - start + nstart))"
140   while [ "$nstart" -lt "$rrend" -a "$nstart" -lt "$dend" ]; do
141     [ "$nstart" -ge "$start" ] \
142     && printf '%i       %i      %s      %s\n' "$nstart" "$nend" "$evtitle" "$evlink"
143     m="$((m + rrfreq))"
144     nstart="$(printf '%04i-%02i-%02i' "$(( (m - 1) / 12 ))" "$(( (m - 1) % 12 + 1 ))" "$d")"
145     nstart="$(date -ud "$nstart $start_time" +%s)"
146       nend="$((nstart - start + end))"
147   done
148 }
149
150 rrex_year() {
151   # helper for rrexpand yearly/N-year expansion
152   local nstart nend
153
154   { read _y _m _d; read y m d start_time; } <<-EOF
155         $(date -ud @$dstart +"%Y %_m %_d"
156           date -ud @$start  +"%Y %_m %_d %T"
157         )
158         EOF
159   while :; do
160     y=$(( rrfreq - ((_y - y - 1) % rrfreq + 1) + _y ))
161     nstart="$(printf '%04i-%02i-%02i' "$y" "$m" "$d")"
162     if isdate "$nstart" && [ "$(date -ud "$nstart" +%s)" -ge "$dstart" ]; then
163       break
164     fi >/dev/null
165     _y="$((_y + rrfreq))"
166   done
167   nstart="$(date -ud "$nstart $start_time" +%s)"
168     nend="$((end - start + nstart))"
169   while [ "$nstart" -lt "$rrend" -a "$nstart" -lt "$dend" ]; do
170     [ "$nstart" -ge "$start" ] \
171     && printf '%i       %i      %s      %s\n' "$nstart" "$nend" "$evtitle" "$evlink"
172     y="$((y + rrfreq))"
173     nstart="$(printf '%04i-%02i-%02i' "$y" "$m" "$d")"
174     nstart="$(date -ud "$nstart $start_time" +%s)"
175       nend="$((nstart - start + end))"
176   done
177 }
178
179 rrex_weekday() {
180   # helper for rrexpand: Nth weekday of a month (e.g. 2nd tuesday, etc.)
181   local nstart nend
182
183   nth=$(( ( $(date -ud @$start +%_d) - 1) / 7))
184
185   nend=$(( 0 * 604800 - (dstart - end) % 604800 + dstart ))
186   nstart=$(( start - end + nend))
187
188   while [ "$nstart" -lt "$rrend" -a "$nstart" -lt "$dend" ]; do
189     [ "$nstart" -ge "$start" -a "$nstart" -ge "$dstart" ] \
190     && [ "$(( ( $(date -ud @$nstart +%_d) -1) / 7 ))" -eq "$nth" ] \
191     && printf '%i       %i      %s      %s\n' "$nstart" "$nend" "$evtitle" "$evlink"
192     nstart="$((nstart + 7 * 86400))"
193       nend="$((nstart - start + end))"
194   done
195 }
196
197 rrex_lastweekday() {
198   # helper for rrexpand: Nth last weekday of a month (e.g. 2nd last tuesday, etc.)
199   local nstart nend Y m d nth
200
201   read Y m d <<-EOF
202         $(date -ud @$start +"%Y %_m %_d")
203         EOF
204   nth=$(( ( $(numdays $Y $m) - d ) / 7))
205
206   nend=$(( 0 * 604800 - (dstart - end) % 604800 + dstart ))
207   nstart=$(( start - end + nend))
208
209   while [ "$nstart" -lt "$rrend" -a "$nstart" -lt "$dend" ]; do
210     read Y m d <<-EOF
211         $(date -ud @$nstart +"%Y %_m %_d")
212         EOF
213     [ "$nstart" -ge "$start" -a "$nstart" -ge "$dstart" ] \
214     && [ "$(( ( $(numdays $Y $m) - d ) / 7 ))" -eq "$nth" ] \
215     && printf '%i       %i      %s      %s\n' "$nstart" "$nend" "$evtitle" "$evlink"
216     nstart="$((nstart + 7 * 86400))"
217       nend="$((nstart - start + end))"
218   done
219 }
220
221 events="$(
222   for dir in "$@"; do
223     page_glob "$dir" "$depth"
224   done \
225   | sort -u \
226   | while read -r page; do
227     pagedir="$(page_abs "$page")"
228     if [ -f "$_DATA/pages/${pagedir}/#events" ] \
229        && acl_read "$pagedir" \
230        && has_tags "$pagedir" $tags \
231        && ! has_tag "$pagedir" $ntags
232     then
233       cat "$_DATA/pages/${pagedir}/#events"
234     fi
235   done
236 )"
237
238 cal_list() {
239   # Print list view for upcoming events
240   local lday='' events sdate=$(date -ud "${DY}-${DM}-${DD}" +%s)
241
242   events="$(
243     printf %s\\n "$events" \
244     | rrexpand "$sdate" "$((sdate + 42 * 86400))" \
245     | sort -n
246   )"
247   
248   printf '<ul class="macro calendar cal_list">\n'
249   printf '%s\n' "${events}" \
250   | while read start end name link; do
251     day="$((start / 86400))"
252     if [ "$day" != "$lday" ]; then
253       [ "$lday" ] && printf '</ul></li>'
254       date -ud "@$start" +'<li><label>%A, %F</label><ul class="day">'
255       lday="$day"
256     fi
257     printf '<li>%s - <a href="%s">%s</a></li>' \
258            "$(date -ud "@$start" +"%H:%M")" "$(URL "${link%%#*}")#$(URL "${link#*#}")" "$(HTML "${name}")"
259   done
260   printf '</ul></li></ul>'
261 }
262
263 cal_month() {
264   local ws events calmonth
265   local iday idow mname dcnt dow dcal start end title link n
266
267   calmonth="$(GET calmonth || printf %i "$((DY * 12 + DM))")"
268   DY="$(( (calmonth - 1) / 12 ))"
269   DM="$(( (calmonth - 1) % 12 + 1 ))"
270
271   read -r iday idow mname <<-EOF
272         $(date -ud "${DY}-${DM}-01" +"%s        %u      %B")
273         EOF
274   dcnt=$((iday - idow * 86400 + ws * 86400))
275    dow=$ws
276   dcal="$(date -ud @"$dcnt" +%d)"
277
278   events="$(
279     printf %s\\n "$events" \
280     | rrexpand "$dcnt" "$((dcnt + 42 * 86400))" \
281     | sort -n
282   )"
283
284   printf '<table class="macro calendar cal_month">'
285   printf '<thead><tr><th><a href="%s">&lt;</a></th><th colspan=5>%s</th><th><a href="%s">&gt;</a></th></tr><tr>' \
286     "./?calmonth=$((DY * 12 + DM -1))" "$mname" "./?calmonth=$((DY * 12 + DM + 1))"
287   for n in 0 1 2 3 4 5 6; do date -ud @"$((dcnt + n * 86400))" +'<th>%a</th>'; done
288   printf '</tr></thead><tbody>'
289   while :; do
290     [ $dow = $ws ] && printf '<tr>'
291     printf '<td><label>%02i</label>' "$dcal"
292
293     evlist="$(
294       printf %s\\n "$events" \
295       | while read start end title link; do
296         if [ "$((start / 86400))" -lt "$((dcnt / 86400))" -a "$end" -gt "$dcnt" ]; then
297           printf '<li><a href="%s\#%s">%s</a></li>' \
298             "$(UNSTRING "${link%%#*}" |URL)" \
299             "$(UNSTRING "${link#*#}" |URL)" \
300             "$(UNSTRING "$title" |HTML)"
301         elif [ "$((start / 86400))" -eq "$((dcnt / 86400))" ]; then
302           printf '<li>%s - <a href="%s\#%s">%s</a></li>' \
303             "$(date -ud @"$start" +%H:%M)" \
304             "$(UNSTRING "${link%%#*}" |URL)" \
305             "$(UNSTRING "${link#*#}" |URL)" \
306             "$(UNSTRING "$title" |HTML)"
307         fi
308       done
309     )"
310     [ "$evlist" ] && printf '<ul>%s</ul>' "$evlist"
311
312     printf '</td>\n'
313     [ $dow = $(( (ws + 6) % 7)) ] && printf '</tr>\n'
314
315     dcnt=$(( dcnt + 86400 ))
316      dow=$(( (dow + 1) % 7 ))
317     [ $dcal -lt 28 ] \
318     && dcal=$((dcal + 1)) \
319     || dcal=$(date -ud @"$dcnt" +%d)
320     [ $dcnt -gt $((iday + 28 * 86400)) -a $dcal -le 7 -a $dow = $ws ] \
321     && break
322   done
323   printf '</tbody></table>'
324 }
325
326 # cal_list
327 cal_month