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 printf '%i %i %s %s\n' "$start" "$end" "$evtitle" "$evlink"
89 # helper for rrexpand daily/N-day expansion
92 nend=$(( rrfreq * 86400 - (dstart - end) % (rrfreq * 86400) + dstart ))
93 nstart=$(( start - end + nend))
94 while [ "$nstart" -lt "$rrend" -a "$nstart" -lt "$dend" ]; do
95 [ "$nstart" -ge "$start" ] \
96 && printf '%i %i %s %s\n' "$nstart" "$nend" "$evtitle" "$evlink"
97 nstart="$((nstart + rrfreq * 86400))"
98 nend="$((nstart - start + end))"
103 # helper for rrexpand weekly/N-week expansion
106 nend=$(( 0 * 604800 - (dstart - end) % (rrfreq * 604800) + dstart ))
107 nstart=$(( start - end + nend))
108 while [ "$nstart" -lt "$rrend" -a "$nstart" -lt "$dend" ]; do
109 [ "$nstart" -ge "$start" -a "$nstart" -ge "$dstart" ] \
110 && printf '%i %i %s %s\n' "$nstart" "$nend" "$evtitle" "$evlink"
111 nstart="$((nstart + rrfreq * 7 * 86400))"
112 nend="$((nstart - start + end))"
117 # helper for rrexpand monthly/N-month expansion
120 { read _y _m _d; read y m d start_time; } <<-EOF
121 $(date -ud @$dstart +"%Y %_m %_d"
122 date -ud @$start +"%Y %_m %_d %T"
125 _m=$((_y * 12 + _m)) m=$((y * 12 + m))
127 m=$(( rrfreq - ((_m - m - 1) % rrfreq + 1) + _m ))
128 nstart="$(printf '%04i-%02i-%02i' "$(( (m - 1) / 12 ))" "$(( (m - 1) % 12 + 1 ))" "$d")"
129 if isdate "$nstart" && [ "$(date -ud "$nstart" +%s)" -ge "$dstart" ]; then
132 _m="$((_m + rrfreq))"
134 nstart="$(date -ud "$nstart $start_time" +%s)"
135 nend="$((end - start + nstart))"
136 while [ "$nstart" -lt "$rrend" -a "$nstart" -lt "$dend" ]; do
137 [ "$nstart" -ge "$start" ] \
138 && printf '%i %i %s %s\n' "$nstart" "$nend" "$evtitle" "$evlink"
140 nstart="$(printf '%04i-%02i-%02i' "$(( (m - 1) / 12 ))" "$(( (m - 1) % 12 + 1 ))" "$d")"
141 nstart="$(date -ud "$nstart $start_time" +%s)"
142 nend="$((nstart - start + end))"
147 # helper for rrexpand yearly/N-year expansion
150 { read _y _m _d; read y m d start_time; } <<-EOF
151 $(date -ud @$dstart +"%Y %_m %_d"
152 date -ud @$start +"%Y %_m %_d %T"
156 y=$(( rrfreq - ((_y - y - 1) % rrfreq + 1) + _y ))
157 nstart="$(printf '%04i-%02i-%02i' "$y" "$m" "$d")"
158 if isdate "$nstart" && [ "$(date -ud "$nstart" +%s)" -ge "$dstart" ]; then
161 _y="$((_y + rrfreq))"
163 nstart="$(date -ud "$nstart $start_time" +%s)"
164 nend="$((end - start + nstart))"
165 while [ "$nstart" -lt "$rrend" -a "$nstart" -lt "$dend" ]; do
166 [ "$nstart" -ge "$start" ] \
167 && printf '%i %i %s %s\n' "$nstart" "$nend" "$evtitle" "$evlink"
169 nstart="$(printf '%04i-%02i-%02i' "$y" "$m" "$d")"
170 nstart="$(date -ud "$nstart $start_time" +%s)"
171 nend="$((nstart - start + end))"
177 page_glob "$dir" "$depth"
180 | while read -r page; do
181 pagedir="$(page_abs "$page")"
182 if [ -f "$_DATA/pages/${pagedir}/#events" ] \
183 && acl_read "$pagedir" \
184 && has_tags "$pagedir" $tags \
185 && ! has_tag "$pagedir" $ntags
187 cat "$_DATA/pages/${pagedir}/#events"
193 # Print list view for upcoming events
194 local lday='' events sdate=$(date -ud "${DY}-${DM}-${DD}" +%s)
197 printf %s\\n "$events" \
198 | rrexpand "$sdate" "$((sdate + 42 * 86400))" \
202 printf '<ul class="macro calendar cal_list">\n'
203 printf '%s\n' "${events}" \
204 | while read start end name link; do
205 day="$((start / 86400))"
206 if [ "$day" != "$lday" ]; then
207 [ "$lday" ] && printf '</ul></li>'
208 date -ud "@$start" +'<li><label>%A, %F</label><ul class="day">'
211 printf '<li>%s - <a href="%s">%s</a></li>' \
212 "$(date -ud "@$start" +"%H:%M")" "$(URL "${link%%#*}")#$(URL "${link#*#}")" "$(HTML "${name}")"
214 printf '</ul></li></ul>'
218 local ws events calmonth
219 local iday idow mname dcnt dow dcal start end title link n
221 calmonth="$(GET calmonth || printf %i "$((DY * 12 + DM))")"
222 DY="$(( (calmonth - 1) / 12 ))"
223 DM="$(( (calmonth - 1) % 12 + 1 ))"
225 read -r iday idow mname <<-EOF
226 $(date -ud "${DY}-${DM}-01" +"%s %u %B")
228 dcnt=$((iday - idow * 86400 + ws * 86400))
230 dcal="$(date -ud @"$dcnt" +%d)"
233 printf %s\\n "$events" \
234 | rrexpand "$dcnt" "$((dcnt + 42 * 86400))" \
238 printf '<table class="macro calendar cal_month">'
239 printf '<thead><tr><th><a href="%s"><</a></th><th colspan=5>%s</th><th><a href="%s">></a></th></tr><tr>' \
240 "./?calmonth=$((DY * 12 + DM -1))" "$mname" "./?calmonth=$((DY * 12 + DM + 1))"
241 for n in 0 1 2 3 4 5 6; do date -ud @"$((dcnt + n * 86400))" +'<th>%a</th>'; done
242 printf '</tr></thead><tbody>'
244 [ $dow = $ws ] && printf '<tr>'
245 printf '<td><label>%02i</label>' "$dcal"
248 printf %s\\n "$events" \
249 | while read start end title link; do
250 if [ "$((start / 86400))" -lt "$((dcnt / 86400))" -a "$end" -gt "$dcnt" ]; then
251 printf '<li><a href="%s\#%s">%s</a></li>' \
252 "$(UNSTRING "${link%%#*}" |URL)" \
253 "$(UNSTRING "${link#*#}" |URL)" \
254 "$(UNSTRING "$title" |HTML)"
255 elif [ "$((start / 86400))" -eq "$((dcnt / 86400))" ]; then
256 printf '<li>%s - <a href="%s\#%s">%s</a></li>' \
257 "$(date -ud @"$start" +%H:%M)" \
258 "$(UNSTRING "${link%%#*}" |URL)" \
259 "$(UNSTRING "${link#*#}" |URL)" \
260 "$(UNSTRING "$title" |HTML)"
264 [ "$evlist" ] && printf '<ul>%s</ul>' "$evlist"
267 [ $dow = $(( (ws + 6) % 7)) ] && printf '</tr>\n'
269 dcnt=$(( dcnt + 86400 ))
270 dow=$(( (dow + 1) % 7 ))
272 && dcal=$((dcal + 1)) \
273 || dcal=$(date -ud @"$dcnt" +%d)
274 [ $dcnt -gt $((iday + 28 * 86400)) -a $dcal -le 7 -a $dow = $ws ] \
277 printf '</tbody></table>'