4 # Copyright 2024 Paul Hänsch
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.
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.
19 . "$_EXEC/cgilite/cgilite.sh"
22 . "$_EXEC/datetime.sh"
24 tags='' ntags='' dir='' depth='' glob_system_pages=false
25 label='' labeltype='' altlabel='' cnt=0
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;;
36 *) if [ ! "$dir" ]; then
38 set -- "$@" "$1"; shift 1;
39 elif [ ! "$depth" ]; then
42 set -- "$@" "$1"; shift 1;
46 [ "$*" ] || set -- "*"
47 [ "$depth" -ge 0 -o "$depth" -le 0 ] 2>&- || depth=0
50 $(isdate "$fromdate" \
51 && date -ud "$fromdate" +"%Y %m %d" \
52 || date -u +"%Y %m %d"
63 # Recurrence Expansion
64 # read recurring event specifications and expand them to a list of
65 # single events within the specified time frame
67 local dstart="$1" dend="$2"
68 local junk1 start end rrfreq rrint rrend evtitle evlink junk2
70 while read -r junk1 start end rrfreq rrint rrend evtitle evlink junk2; do
71 [ "$rrend" -eq -1 ] && rrend=9999999999
73 if [ "$start" -lt "$dend" ] &&
74 [ "$end" -gt "$dstart" -o "$rrend" -gt "$dstart" ]; then
81 [ "$rrfreq" -ge 0 ] && rrex_weekday \
85 printf '%i %i %s %s\n' "$start" "$end" "$evtitle" "$evlink"
93 # helper for rrexpand daily/N-day expansion
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))"
107 # helper for rrexpand weekly/N-week expansion
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))"
121 # helper for rrexpand monthly/N-month expansion
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"
129 _m=$((_y * 12 + _m)) m=$((y * 12 + m))
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
136 _m="$((_m + rrfreq))"
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"
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))"
151 # helper for rrexpand yearly/N-year expansion
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"
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
165 _y="$((_y + rrfreq))"
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"
173 nstart="$(printf '%04i-%02i-%02i' "$y" "$m" "$d")"
174 nstart="$(date -ud "$nstart $start_time" +%s)"
175 nend="$((nstart - start + end))"
180 # helper for rrexpand: Nth weekday of a month (e.g. 2nd tuesday, etc.)
183 nth=$(( ( $(date -ud @$start +%_d) - 1) / 7))
185 nend=$(( 0 * 604800 - (dstart - end) % 604800 + dstart ))
186 nstart=$(( start - end + nend))
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))"
198 # helper for rrexpand: Nth last weekday of a month (e.g. 2nd last tuesday, etc.)
199 local nstart nend Y m d nth
202 $(date -ud @$start +"%Y %_m %_d")
204 nth=$(( ( $(numdays $Y $m) - d ) / 7))
206 nend=$(( 0 * 604800 - (dstart - end) % 604800 + dstart ))
207 nstart=$(( start - end + nend))
209 while [ "$nstart" -lt "$rrend" -a "$nstart" -lt "$dend" ]; do
211 $(date -ud @$nstart +"%Y %_m %_d")
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))"
223 page_glob "$dir" "$depth"
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
233 cat "$_DATA/pages/${pagedir}/#events"
239 # Print list view for upcoming events
240 local lday='' events sdate=$(date -ud "${DY}-${DM}-${DD}" +%s)
243 printf %s\\n "$events" \
244 | rrexpand "$sdate" "$((sdate + 42 * 86400))" \
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">'
257 printf '<li>%s - <a href="%s">%s</a></li>' \
258 "$(date -ud "@$start" +"%H:%M")" "$(URL "${link%%#*}")#$(URL "${link#*#}")" "$(HTML "${name}")"
260 printf '</ul></li></ul>'
264 local ws events calmonth
265 local iday idow mname dcnt dow dcal start end title link n
267 calmonth="$(GET calmonth || printf %i "$((DY * 12 + DM))")"
268 DY="$(( (calmonth - 1) / 12 ))"
269 DM="$(( (calmonth - 1) % 12 + 1 ))"
271 read -r iday idow mname <<-EOF
272 $(date -ud "${DY}-${DM}-01" +"%s %u %B")
274 dcnt=$((iday - idow * 86400 + ws * 86400))
276 dcal="$(date -ud @"$dcnt" +%d)"
279 printf %s\\n "$events" \
280 | rrexpand "$dcnt" "$((dcnt + 42 * 86400))" \
284 printf '<table class="macro calendar cal_month">'
285 printf '<thead><tr><th><a href="%s"><</a></th><th colspan=5>%s</th><th><a href="%s">></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>'
290 [ $dow = $ws ] && printf '<tr>'
291 printf '<td><label>%02i</label>' "$dcal"
294 printf %s "${events}${events:+${BR}}" \
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)"
310 [ "$evlist" ] && printf '<ul>%s</ul>' "$evlist"
313 [ $dow = $(( (ws + 6) % 7)) ] && printf '</tr>\n'
315 dcnt=$(( dcnt + 86400 ))
316 dow=$(( (dow + 1) % 7 ))
318 && dcal=$((dcal + 1)) \
319 || dcal=$(date -ud @"$dcnt" +%d)
320 [ $dcnt -gt $((iday + 28 * 86400)) -a $dcal -le 7 -a $dow = $ws ] \
323 printf '</tbody></table>'