]> git.plutz.net Git - shellwiki/commitdiff
macro for displaying page teasers, mostly a copy of include macro lds
authorPaul Hänsch <paul@plutz.net>
Tue, 4 Nov 2025 12:08:50 +0000 (13:08 +0100)
committerPaul Hänsch <paul@plutz.net>
Tue, 4 Nov 2025 12:08:50 +0000 (13:08 +0100)
macros/Teaser [new file with mode: 0755]

diff --git a/macros/Teaser b/macros/Teaser
new file mode 100755 (executable)
index 0000000..55ae1fb
--- /dev/null
@@ -0,0 +1,122 @@
+#!/bin/sh
+
+# Copyright 2022 - 2023 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/acl.sh"
+. "$_EXEC/tools.sh"
+
+from='/^::: {\([^}]* \)*teaser\( [^}]*\)*}/'; to='/^:::/'; rev=''; items='$'; hl=0; link='true'; depth=0; tags=''; ntags=''; page='';
+
+set -- "$@" --
+while [ $# -gt 0 ]; do case $1 in
+  --from) from="$2"; shift 2;;
+  from=*) from="${1#*=}"; shift 1;;
+  --to) to="$2"; shift 2;;
+  to=*) to="${1#*=}"; shift 1;;
+  --items) items="$2"; shift 2;;
+  items=*) items="${1#*=}"; shift 1;;
+  --rev|--reverse) rev="-r"; shift 1;;
+  --nolink) link=""; shift 1;;
+  --hl|-hl) hl=$2; shift 2;;
+  --depth) depth=$2; shift 2;;
+  \#*) tags="${tags}${tags:+ }${1}"; shift 1;;
+  \!*) ntags="${ntags}${ntags:+ }${1}"; shift 1;;
+  --) shift 1; break;;
+  *) set -- "$@" "$1"; shift 1;;
+esac; done
+
+if ! printf %s\\n "$from" |grep -qEx '[0-9]+|/([^/\\]|\\/|\\.)*/'; then
+  debug 'Include macro invalid argument: "from"'
+  exit 1
+fi
+if ! printf %s\\n "$to" |grep -qEx '\$|[0-9]+|/([^/\\]|\\/|\\.)*/'; then
+  debug 'Include macro Invalid argument: "to"'
+  exit 1
+fi
+if ! printf %s\\n "$items" |grep -qEx '\$|[0-9]+'; then
+  debug 'Include macro Invalid argument: "items"'
+  exit 1
+fi
+
+for page in "$@"; do
+  page_glob "$page" "$depth"
+done \
+| sort $rev \
+| sed "${items}q" \
+| while read glob; do
+  page="$(page_abs "$glob")"
+  mdfile="$(mdfile "$page")" || continue
+  acl_read "$page" || continue
+  has_tags "$page" $tags || continue
+  has_tag "$page" $ntags && continue
+  printf %s\\n "$INCLUDE_LIST" |grep -qxF "$page" && continue
+  export INCLUDE_LIST="${INCLUDE_LIST}${INCLUDE_LIST:+${BR}}$page"
+  hglob="$(HTML "$glob")"
+  refpfx="$(printf %s\\n "$hglob" |sed 's;[\;&\;];\\&;g')"
+  [ "$link" ] \
+  && printf '<div class="macro Teaser">
+               <a class="include link" href="%s">%s</a>
+               <div class="include" id="include_%s">' \
+            "${hglob}" "${hglob}" "${hglob}" \
+  || printf '<div class="macro Teaser">
+               <div class="include" id="include_%s">' \
+            "${hglob}"
+  ( # PATH_INFO may be used by macros in the included page
+    export PATH_INFO="$page"
+    cd -- "${mdfile%/*}/"
+    sed -n "${from},${to}p" <"$mdfile" \
+    | md \
+    | grep -vx ''
+  ) | sed -E '
+    s;(<[^>]+ )(href|src)="([^"]+://[^"]*|[mM][aA][iI][lL][tT][oO]:[^"]*)"([^>]*>);\1\2="/#safe/\3"\4;g
+    s;(<[^>]+ )(href|src)="([^#/"][^"]*)"([^>]*>);\1\2="'"${refpfx}"'\3"\4;g
+    s;(<[^>]+ )(href|src)="/#safe/([^"]*)"([^>]*>);\1\2="\3"\4;g
+  ' | case $hl in
+      1) sed -E 's;(<h|</h)5( |>);\16\2;g;
+                 s;(<h|</h)4( |>);\15\2;g;
+                 s;(<h|</h)3( |>);\14\2;g;
+                 s;(<h|</h)2( |>);\13\2;g;
+                 s;(<h|</h)1( |>);\12\2;g;
+         ';;
+      2) sed -E 's;(<h|</h)5( |>);\16\2;g;
+                 s;(<h|</h)4( |>);\16\2;g;
+                 s;(<h|</h)3( |>);\15\2;g;
+                 s;(<h|</h)2( |>);\14\2;g;
+                 s;(<h|</h)1( |>);\13\2;g;
+         ';;
+      3) sed -E 's;(<h|</h)5( |>);\16\2;g;
+                 s;(<h|</h)4( |>);\16\2;g;
+                 s;(<h|</h)3( |>);\16\2;g;
+                 s;(<h|</h)2( |>);\15\2;g;
+                 s;(<h|</h)1( |>);\14\2;g;
+         ';;
+      4) sed -E 's;(<h|</h)5( |>);\16\2;g;
+                 s;(<h|</h)4( |>);\16\2;g;
+                 s;(<h|</h)3( |>);\16\2;g;
+                 s;(<h|</h)2( |>);\16\2;g;
+                 s;(<h|</h)1( |>);\15\2;g;
+         ';;
+      5|[6789])
+         sed -E 's;(<h|</h)5( |>);\16\2;g;
+                 s;(<h|</h)4( |>);\16\2;g;
+                 s;(<h|</h)3( |>);\16\2;g;
+                 s;(<h|</h)2( |>);\16\2;g;
+                 s;(<h|</h)1( |>);\16\2;g;
+         ';;
+      *) cat;;
+    esac
+  printf '</div></div>'
+done