From 03faace5d5ef48267bfd0108edf65b29f285b2ac Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Tue, 17 May 2022 23:25:40 +0200 Subject: [PATCH] include macro --- macros/include | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100755 macros/include diff --git a/macros/include b/macros/include new file mode 100755 index 0000000..97f35a4 --- /dev/null +++ b/macros/include @@ -0,0 +1,63 @@ +#!/bin/sh + +. "$_EXEC/cgilite/cgilite.sh" +. "$_EXEC/acl.sh" +. "$_EXEC/tools.sh" + +from=1 +to=\$ +rev='' +items=\$ +path_info="$PATH_INFO" + +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;; + *) page="$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 + +page_glob "$page" \ +| sort $rev \ +| sed "${items}q" \ +| while read glob; do + case $glob in + /*) export PATH_INFO="$(PATH "$glob")";; + *) export PATH_INFO="$(PATH "$path_info/$glob")";; + esac + acl_read || continue + if [ -f "$_DATA/pages/$PATH_INFO/#page.md" ]; then + mdfile="$_DATA/pages/$PATH_INFO/#page.md" + elif [ -f "$_EXEC/pages/$PATH_INFO/#page.md" ]; then + mdfile="$_EXEC/pages/$PATH_INFO/#page.md" + else + continue + fi + printf '
' "$(HTML "$glob")" + ( cd "${mdfile%/*}" + sed -n "${from},${to}p" <"$mdfile" \ + | sed -E '1,20{ /^%[a-z]+/d; }' \ + | md \ + | grep -vx '' + ) | sed -E ' + s;(<[^>]+ )(href|src)="([^#/"][^"]*)"([^>]*>);\1\2="'"$(HTML "$glob")"'\3"\4;g + ' + printf '
' +done -- 2.39.2