From 680b00bd982c2625fab69c45728c653ebd1c0bf0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Tue, 6 Feb 2024 15:41:47 +0100 Subject: [PATCH] universal page_title function --- handlers/20_title.sh | 42 ++++-------------------------------------- tools.sh | 24 ++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 38 deletions(-) diff --git a/handlers/20_title.sh b/handlers/20_title.sh index 3da07ff..35b5d3c 100755 --- a/handlers/20_title.sh +++ b/handlers/20_title.sh @@ -1,6 +1,6 @@ #!/bin/sh -# Copyright 2023 Paul Hänsch +# Copyright 2023, 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 @@ -14,44 +14,10 @@ # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -if acl_read ${PATH_INFO}; then - mdfile="$(mdfile "${PATH_INFO%/*}")" -else - PAGE_TITLE="${SITE_TITLE}" - return 1 +if acl_read "${PATH_INFO}"; then + PAGE_TITLE="$(page_title "${PATH_INFO%/*}")" fi -if [ "$mdfile" ]; then - PAGE_TITLE="$( - sed -nE ' - s;^%title[ \t]+([[:graph:]][[:print:]]+)\r?$;\1;p; tQ; - b; :Q q; - ' "$mdfile" - )" - [ ! "${PAGE_TITLE}" ] && PAGE_TITLE="$( - MD_MACROS="" md <"$mdfile" \ - | sed -nE ' - s;^.*]*>(.*>)?([^<]+)(<.*)?.*$;\2;p; tQ; - s;^.*]*>(.*>)?([^<]+)(<.*)?.*$;\2;p; tQ; - b; :Q q; - ' - )" -else - PAGE_TITLE="${SITE_TITLE}" - return 1 -fi - -case $PATH_INFO in - *"/[attachment]") - PAGE_TITLE="${PAGE_TITLE} ($(_ Attachments))" - ;; - *"/[revision]") - PAGE_TITLE="${PAGE_TITLE} ($(_ Revisions))" - ;; -esac - -[ "$PAGE_TITLE" ] \ -&& PAGE_TITLE="${PAGE_TITLE}${SITE_TITLE:+ - ${SITE_TITLE}}" \ -|| PAGE_TITLE="${SITE_TITLE}" +PAGE_TITLE="${PAGE_TITLE:-${SITE_TITLE:-${PATH_INFO}}}${PAGE_TITLE:+${SITE_TITLE:+ - ${SITE_TITLE}}}" return 1 diff --git a/tools.sh b/tools.sh index a298394..dfa4594 100755 --- a/tools.sh +++ b/tools.sh @@ -185,3 +185,27 @@ has_tag() { done return 1 } + +page_title() { + local mdfile PAGE_TITLE + + if mdfile="$(mdfile "${1:-${PATH_INFO%/*}}")"; then + PAGE_TITLE="$( + sed -nE ' + s;^%title[ \t]+([[:graph:]][[:print:]]+)\r?$;\1;p; tQ; + b; :Q q; + ' "$mdfile" + )" + [ ! "${PAGE_TITLE}" ] && PAGE_TITLE="$( + MD_MACROS="" md <"$mdfile" \ + | sed -nE ' + s;^.*]*>(.*>)?([^<]+)(<.*)?.*$;\2;p; tQ; + s;^.*]*>(.*>)?([^<]+)(<.*)?.*$;\2;p; tQ; + b; :Q q; + ' + )" + printf %s\\n "$PAGE_TITLE" + else + return 1 + fi +} -- 2.39.2