]> git.plutz.net Git - shellwiki/commitdiff
Feature: allow globbing in `attachment` macro
authorPaul Hänsch <paul@plutz.net>
Fri, 7 Nov 2025 00:21:14 +0000 (01:21 +0100)
committerPaul Hänsch <paul@plutz.net>
Fri, 7 Nov 2025 00:21:14 +0000 (01:21 +0100)
macros/attachments

index 3a75f8687524113896520f5a09a082a36ee7cd12..4af3c09e2e28b00d0846d42f755bb6d66893bb2b 100755 (executable)
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-# Copyright 2022 Paul Hänsch
+# Copyright 2022, 2025 Paul Hänsch
 # 
 # Permission to use, copy, modify, and/or distribute this software for any
 # purpose with or without fee is hereby granted, provided that the above
 . "$_EXEC/acl.sh"
 . "$_EXEC/tools.sh"
 
-page="$1"
-
-if [ "${page#/}" = "$page" ]; then
-  page="$(PATH "${PATH_INFO}/$page")"
-fi
-
-acl_read "$page" || exit 0
+[ $# = 0 ] && set -- "./*"
 
 printf %s\\n '<ul class="macro attachment list">'
-
-for file in "$_EXEC/pages/$page/#attachments"/* "$_DATA/pages/$page/#attachments"/*; do
-  [ "$file" = "$_EXEC/pages/$page/#attachments/${file##*/}" \
-        -a -f "$_DATA/pages/$page/#attachments/${file##*/}" ] && continue
-  stat="$(stat -c '%s %Y' -- "$file" 2>&-)" || continue
+for glob in "$@"; do
+  page="${glob%/*}"
+  [ "$page" = "$glob" ] && page=./
+  [ ! "$page" ]         && page=/
+
+  acl_read "$(page_abs "${page}")" \
+  && attachment_glob "$glob"
+done \
+| while read attachment; do
+  case $attachment in
+    /*)
+      stat="$( stat -c '%s %Y' -- "$_DATA/pages/${attachment%/*}/#attachments/${attachment##*/}" \
+            || stat -c '%s %Y' -- "$_EXEC/pages/${attachment%/*}/#attachments/${attachment##*/}" \
+           2>&-)" || continue
+      ;;
+    *)
+      stat="$( stat -c '%s %Y' -- "$_DATA/pages/${PATH_INFO}/${attachment%/*}/#attachments/${attachment##*/}" \
+            || stat -c '%s %Y' -- "$_EXEC/pages/${PATH_INFO}/${attachment%/*}/#attachments/${attachment##*/}" \
+           2>&-)" || continue
+      ;;
+  esac
   size="${stat% *}" date="${stat#* }"
-
-  printf '<li><a class=name href="[attachment]/%s">%s</a>
+  printf '<li><a class=name href="%s">%s</a>
           <span class=size>%s</span><span class=date>%s</span></li>' \
-    "$(HTML "${file##*/}")" "$(HTML "${file##*/}")" \
-    "$(size_human "$size")" "$(date -d @"$date" +"%F %T")"
+         "$(HTML "${attachment%/*}/[attachment]/${attachment##*/}")" \
+         "$(HTML "${attachment#./}")" \
+         "$(size_human "$size")" "$(date -d @"$date" +"%F %T")"
 done
-
 printf %s\\n '</ul>'