]> git.plutz.net Git - shellwiki/blob - macros/calendar
month calendar view, more modular architecture
[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         *):
81           printf '%i    %i      %s      %s\n' "$start" "$end" "$evtitle" "$evlink"
82           ;;
83       esac
84     fi
85   done
86 }
87
88 rrex_day() {
89   # helper for rrexpand daily/N-day expansion
90   local nstart nend
91
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))"
99   done
100 }
101
102 rrex_week() {
103   # helper for rrexpand weekly/N-week expansion
104   local nstart nend
105
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))"
113   done
114 }
115
116 rrex_month() {
117   # helper for rrexpand monthly/N-month expansion
118   local nstart nend
119
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"
123         )
124         EOF
125   _m=$((_y * 12 + _m)) m=$((y * 12 + m))
126   while :; do
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
130       break
131     fi >/dev/null
132     _m="$((_m + rrfreq))"
133   done
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"
139     m="$((m + rrfreq))"
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))"
143   done
144 }
145
146 rrex_year() {
147   # helper for rrexpand yearly/N-year expansion
148   local nstart nend
149
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"
153         )
154         EOF
155   while :; do
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
159       break
160     fi >/dev/null
161     _y="$((_y + rrfreq))"
162   done
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"
168     y="$((y + rrfreq))"
169     nstart="$(printf '%04i-%02i-%02i' "$y" "$m" "$d")"
170     nstart="$(date -ud "$nstart $start_time" +%s)"
171       nend="$((nstart - start + end))"
172   done
173 }
174
175 events="$(
176   for dir in "$@"; do
177     page_glob "$dir" "$depth"
178   done \
179   | sort -u \
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
186     then
187       cat "$_DATA/pages/${pagedir}/#events"
188     fi
189   done
190 )"
191
192 cal_list() {
193   # Print list view for upcoming events
194   local lday='' events sdate=$(date -ud "${DY}-${DM}-${DD}" +%s)
195
196   events="$(
197     printf %s\\n "$events" \
198     | rrexpand "$sdate" "$((sdate + 42 * 86400))" \
199     | sort -n
200   )"
201   
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">'
209       lday="$day"
210     fi
211     printf '<li>%s - <a href="%s">%s</a></li>' \
212            "$(date -ud "@$start" +"%H:%M")" "$(URL "${link%%#*}")#$(URL "${link#*#}")" "$(HTML "${name}")"
213   done
214   printf '</ul></li></ul>'
215 }
216
217 cal_month() {
218   local ws events calmonth
219   local iday idow mname dcnt dow dcal start end title link n
220
221   calmonth="$(GET calmonth || printf %i "$((DY * 12 + DM))")"
222   DY="$(( (calmonth - 1) / 12 ))"
223   DM="$(( (calmonth - 1) % 12 + 1 ))"
224
225   read -r iday idow mname <<-EOF
226         $(date -ud "${DY}-${DM}-01" +"%s        %u      %B")
227         EOF
228   dcnt=$((iday - idow * 86400 + ws * 86400))
229    dow=$ws
230   dcal="$(date -ud @"$dcnt" +%d)"
231
232   events="$(
233     printf %s\\n "$events" \
234     | rrexpand "$dcnt" "$((dcnt + 42 * 86400))" \
235     | sort -n
236   )"
237
238   printf '<table class="macro calendar cal_month">'
239   printf '<thead><tr><th><a href="%s">&lt;</a></th><th colspan=5>%s</th><th><a href="%s">&gt;</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>'
243   while :; do
244     [ $dow = $ws ] && printf '<tr>'
245     printf '<td><label>%02i</label>' "$dcal"
246
247     evlist="$(
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)"
261         fi
262       done
263     )"
264     [ "$evlist" ] && printf '<ul>%s</ul>' "$evlist"
265
266     printf '</td>\n'
267     [ $dow = $(( (ws + 6) % 7)) ] && printf '</tr>\n'
268
269     dcnt=$(( dcnt + 86400 ))
270      dow=$(( (dow + 1) % 7 ))
271     [ $dcal -lt 28 ] \
272     && dcal=$((dcal + 1)) \
273     || dcal=$(date -ud @"$dcnt" +%d)
274     [ $dcnt -gt $((iday + 28 * 86400)) -a $dcal -le 7 -a $dow = $ws ] \
275     && break
276   done
277   printf '</tbody></table>'
278 }
279
280 %rem cal_list
281 cal_month