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