]> git.plutz.net Git - shellwiki/blob - macros/pagelist
allow negative tag selection
[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='' ntags='' 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   \!*) ntags="${ntags}${ntags:+ }${1##!}"; shift 1;;
30   --h1|--h2|--h3|--h4|--h5|--h6|--label)
31     labeltype="${1#--}" label="$2"; shift 2;;
32   --alt-label)
33     altlabel="$2"; shift 2;;
34   --) shift 1; break;;
35   *) if [ ! "$dir" ]; then
36       dir="$1"
37       set -- "$@" "$1"; shift 1;
38     elif [ ! "$depth" ]; then
39       depth="$1"; shift 1;
40     else
41       set -- "$@" "$1"; shift 1;
42     fi;;
43 esac; done
44
45 [ "$*" ] || set -- "*"
46 [ "$depth" -ge 0 -o "$depth" -le 0 ] 2>&- || depth=0
47
48 for dir in "$@"; do
49   page_glob "$dir" "$depth"
50 done \
51 | sort -u \
52 | {
53   while read -r page; do
54     pagedir="$(page_abs "$page")"
55     if [ -f "$_DATA/pages/${pagedir}/#page.md" -o \
56          -f "$_EXEC/pages/${pagedir}/#page.md" ] \
57        && acl_read "$pagedir" \
58        && has_tags "$pagedir" $tags \
59        && ! has_tag "$pagedir" $ntags
60     then
61       [ "$cnt" -eq 0 -a "$label" ] \
62       && printf '<%s class="macro pagelist label">%s</%s>' \
63          "$labeltype" "$(HTML "$label")" "$labeltype"
64       [ "$cnt" -eq 0 ] && printf '<ul class="macro pagelist">\n'
65
66       printf '<li><a href="%s">%s</a></li>' "$(HTML "$page")" "$(HTML "$page")"
67       cnt="$((cnt + 1))"
68     fi
69   done
70
71   if [ "$cnt" -gt 0 ]; then
72     printf '</ul>\n'
73   elif [ "$altlabel" ]; then
74     printf '<label class="macro pagelist altlabel">%s</label>' "$(HTML "$altlabel")"
75   fi
76 }