]> git.plutz.net Git - shellwiki/blob - macros/pagelist
cdc74856369131732c8c4e607026f404518b0286
[shellwiki] / macros / pagelist
1 #!/bin/sh
2
3 # Copyright 2022 - 2024 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 . "$_EXEC/db23.sh"
21
22 tags='' ntags='' dir='' depth='' glob_system_pages=false
23 label='' labeltype='' altlabel='' titles='' db3_data=''
24 DB3 new  # Use DB3 for in-memory cache
25
26 set -- "$@" --
27 while [ $# -gt 0 ]; do case $1 in
28   --system) glob_system_pages=true; shift 1;;
29   --depth)  depth="$2"; shift 2;;
30   --title|--titles) titles=true; shift 1;;
31   \#*) tags="${tags}${tags:+ }${1###}"; shift 1;;
32   \!*) ntags="${ntags}${ntags:+ }${1##!}"; shift 1;;
33   --h1|--h2|--h3|--h4|--h5|--h6|--label)
34     labeltype="${1#--}" label="$2"; shift 2;;
35   --alt-label|--altlabel)
36     altlabel="$2"; shift 2;;
37   --) shift 1; break;;
38   *) if [ ! "$dir" ]; then
39       dir="$1"
40       set -- "$@" "$1"; shift 1;
41     elif [ ! "$depth" ]; then
42       depth="$1"; shift 1;
43     else
44       set -- "$@" "$1"; shift 1;
45     fi;;
46 esac; done
47
48 [ "$*" ] || set -- "*"
49 [ "$depth" -ge 0 -o "$depth" -le 0 ] 2>&- || depth=0
50
51 print_page() {
52   # print page URL and resolve page title (for use with --title flag)
53   # avoid calling this function via a subshell (i.e. $(print_page *))
54   # because it should be able to write its cache variable
55   local page="${1%/}/" title='' pfrag=''
56   pfrag="${page}"
57
58   # resolve name of each path element
59   while [ "${pfrag%/*}" -a "${pfrag%/*}" != "${pfrag}" ]; do
60     pfrag="${pfrag%/*}"
61     debug "$page - $pfrag"
62     title="$(DB3 get "${pfrag}" || ! page_title "$(page_abs "${pfrag}")")/${title}" && break
63   done
64   # keep resolved names in cache
65   DB3 set "${page%/*}" "${title%/}"
66
67   [ "${page#/}" != "${page}" ] && title="/$title"
68   printf '%s    %s\n' "$(URL "$page")" "$(HTML "${title}")"
69 }
70
71 pagelist="$(
72   for dir in "$@"; do
73     page_glob "$dir" "$depth"
74   done \
75   | sort -u \
76   | while read -r page; do
77     pagedir="$(page_abs "$page")"
78     if [ -f "$_DATA/pages/${pagedir}/#page.md" -o \
79          -f "$_EXEC/pages/${pagedir}/#page.md" ] \
80        && acl_read "$pagedir" \
81        && has_tags "$pagedir" $tags \
82        && ! has_tag "$pagedir" $ntags
83     then
84       # Be careful, not to fork the print_page function into a subshell
85       # as it must be able to write its cache to the current context
86       [ "$titles" ] \
87       && print_page "$page" \
88       || printf '%s     %s\n' "$(URL "$page")" "$(HTML "$page")"
89     fi
90   done
91 )"
92
93 if [ "$pagelist" ]; then
94   [ "$label" ] \
95   && printf '<%s class="macro pagelist label">%s</%s>' "$labeltype" "$(HTML "$label")" "$labeltype"
96
97   printf '<ul class="macro pagelist">\n'
98   printf %s\\n "$pagelist" \
99   | sort -k2 \
100   | while read -r url title; do
101     printf '<li><a href="%s">%s</a></li>\n' "${url}" "${title}"
102   done
103   printf '</ul>\n'
104
105 elif [ "$altlabel" ]; then
106   printf '<label class="macro pagelist altlabel">%s</label>' "$(HTML "$altlabel")"
107 fi