]> git.plutz.net Git - shellwiki/blob - macros/pagelist
08764aabe4d91c4924557557c609f5037d9d489d
[shellwiki] / macros / pagelist
1 #!/bin/sh
2
3 # Copyright 2022 - 2023 Paul Hänsch
4
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8
9 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17 . "$_EXEC/cgilite/cgilite.sh"
18 . "$_EXEC/acl.sh"
19 . "$_EXEC/tools.sh"
20
21 tags='' dir='' depth='' glob_system_pages=false
22 label='' labeltype='' altlabel='' cnt=0
23
24 set -- "$@" --
25 while [ $# -gt 0 ]; do case $1 in
26   --system) glob_system_pages=true; shift 1;;
27   --depth)  depth="$2" shift 2;;
28   \#*) tags="${tags}${tags:+ }${1###}"; shift 1;;
29   --h1|--h2|--h3|--h4|--h5|--h6|--label)
30     labeltype="${1#--}" label="$2"; shift 2;;
31   --alt-label)
32     altlabel="$2"; shift 2;;
33   --) shift 1; break;;
34   *) if [ ! "$dir" ]; then
35       dir="$1"
36       set -- "$@" "$1"; shift 1;
37     elif [ ! "$depth" ]; then
38       depth="$1"; shift 1;
39     else
40       set -- "$@" "$1"; shift 1;
41     fi;;
42 esac; done
43
44 [ "$*" ] || set -- "*"
45 [ "$depth" -ge 0 -o "$depth" -le 0 ] 2>&- || depth=0
46
47 for dir in "$@"; do
48   page_glob "$dir" "$depth"
49 done \
50 | sort -u \
51 | {
52   while read -r page; do
53     pagedir="$(page_abs "$page")"
54     if [ -f "$_DATA/pages/${pagedir}/#page.md" -o \
55          -f "$_EXEC/pages/${pagedir}/#page.md" ] \
56        && acl_read "$pagedir" \
57        && has_tags "$pagedir" $tags
58     then
59       [ "$cnt" -eq 0 -a "$label" ] \
60       && printf '<%s class="macro pagelist label">%s</%s>' \
61          "$labeltype" "$(HTML "$label")" "$labeltype"
62       [ "$cnt" -eq 0 ] && printf '<ul class="macro pagelist">\n'
63
64       printf '<li><a href="%s">%s</a></li>' "$(HTML "$page")" "$(HTML "$page")"
65       cnt="$((cnt + 1))"
66     fi
67   done
68
69   if [ "$cnt" -gt 0 ]; then
70     printf '</ul>\n'
71   elif [ "$altlabel" ]; then
72     printf '<label class="macro pagelist altlabel">%s</label>' "$(HTML "$altlabel")"
73   fi
74 }