]> git.plutz.net Git - shellwiki/blobdiff - macros/taglist
Taglist and tag cloud
[shellwiki] / macros / taglist
diff --git a/macros/taglist b/macros/taglist
new file mode 100755 (executable)
index 0000000..576655b
--- /dev/null
@@ -0,0 +1,72 @@
+#!/bin/sh
+
+# Copyright 2024 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
+# copyright notice and this permission notice appear in all copies.
+# 
+# THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+# IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
+. "$_EXEC/cgilite/cgilite.sh"
+. "$_EXEC/cgilite/storage.sh"
+. "$_EXEC/cgilite/db23.sh"
+
+cloud='' link=''
+while [ $# -gt 0 ]; do case $1 in
+  --cloud) cloud=true; shift 1;;
+  --link)  link=true; shift 1;;
+esac; done
+
+taglist="$(
+  max=0
+  for tagfile in "$_DATA"/tags/*; do
+    tag="${tagfile##*/}"
+    [ "$tag" = "*" ] && break
+  
+    n=0; while read -r page date; do
+      if ! pdate="$(DB3 get "$page")"; then
+        if pdate="$(stat -c %Y "$_DATA/pages/$(UNSTRING "$page")/#page.md" )"; then
+          DB3 set "$page" "$pdate"
+        else
+          debug "Remove $page from $tag"
+          # DBM "$tagfile" remove "$(UNSTRING "$page")"
+          date=0
+        fi
+      fi
+      [ "$date" -ge "$pdate" ] && n=$((n + 1))
+    done <"$tagfile"
+    if [ $n -gt 0 ]; then
+      printf '%i       %i      %s\n' "$RANDOM" "$n" "$tag"
+    else
+      debug "Removing tag: $tagfile" # && rm -- "$tagfile"
+    fi
+    [ $n -gt $max ] && max="$n"
+  done
+  printf "%i\n" "$max"
+)"
+max="${taglist##*${BR}}"
+taglist="${taglist%${BR}*}"
+
+if [ "$taglist" ]; then
+  [ "$cloud" ] && printf '<ul class="macro taglist cloud">\n' \
+               || printf '<ul class="macro taglist">\n'
+  printf %s\\n "$taglist" \
+  | { [ "$cloud" ] && sort -n || sort -k 3; } \
+  | while read -r junk inc tag; do
+    if [ "$link" ]; then
+      printf '<li class=tag inc=%i rel=%i ><a href="./[search]?q=%s">#%s</a></li>\n' \
+             "$inc" "$((9 * inc / max))" "$(URL "$tag")" "$(HTML "$tag")"
+    else
+      printf '<li class=tag inc=%i rel=%i >#%s</li>\n' \
+             "$inc" "$((9 * inc / max))" "$(HTML "$tag")"
+    fi
+  done
+  printf '</ul>\n'
+fi