From 71152d935975fa673d7ad4398a1757228cab8b8c Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Sun, 21 May 2023 12:57:20 +0200 Subject: [PATCH] override mdfile function for selecting language based includes and header pages --- handlers/10_translations.sh | 67 ++++++++++++++++++++++++++++++------- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/handlers/10_translations.sh b/handlers/10_translations.sh index e666408..b9d6cb1 100755 --- a/handlers/10_translations.sh +++ b/handlers/10_translations.sh @@ -1,7 +1,44 @@ #!/bin/sh +# Set LANGUAGE_DEFAULT to enable Plugin +[ ! "$LANGUAGE_DEFAULT" ] && return 1 + export LANGUAGE_DEFAULT="${LANGUAGE_DEFAULT:-en}" export HTTP_REFERER="${HTTP_REFERER:-$(HEADER Referer)}" +export LANGUAGE='' + +mdfile(){ + # Override mdfile Selection from tools.sh + local page="$(PATH "$1")" + page="${page%/}" + + # Additional part: + # if PAGE Address is LANGUAGE Page, jump to regular processing + # else if LANGUAGE Page exists, add language tag + # Also the reason why `mdfile` should not be used in `acl.sh` + if [ "${page%/:${LANGUAGE}}" != "$page" ]; then + : + elif [ -f "$_DATA/pages/$page/:$LANGUAGE/#page.md" ]; then + printf %s\\n "$_DATA/pages/$page/:$LANGUAGE/#page.md" + return 0 + elif [ -f "$_EXEC/pages/$page/:$LANGUAGE/#page.md" ]; then + printf %s\\n "$_EXEC/pages/$page/:$LANGUAGE/#page.md" + return 0 + fi 2>&- + + # Regular processing, keep in sync with tools.sh + if [ -f "$_DATA/pages/$page/#page.md" ]; then + printf %s\\n "$_DATA/pages/$page/#page.md" + return 0 + elif [ -f "$_EXEC/pages/$page/#page.md" ]; then + printf %s\\n "$_EXEC/pages/$page/#page.md" + return 0 + else + return 1 + fi 2>&- + # ^^ suppress error messages produced + # by printf when stdout was closed +} case ${HTTP_REFERER} in */:*/*):;; @@ -10,21 +47,23 @@ case ${HTTP_REFERER} in ;; esac -export LANGUAGE="${LANGUAGE_REFERRED:-${LANGUAGE_DEFAULT}}" +LANGUAGE="${LANGUAGE_REFERRED:-${LANGUAGE_DEFAULT}}" case ${PATH_INFO} in */:?*/\[attachment\]/?*) - export LANGUAGE="${PATH_INFO#*/:}" - LANGUAGE="${LANGUAGE%%/*}" + LANGUAGE="${PATH_INFO#*/:}" + LANGUAGE="${LANGUAGE%%/*}" PATH_INFO="${PATH_INFO%%:?*/*}${PATH_INFO#*/:?*/}" ;; */:?*/\[attachment\]) - export LANGUAGE="${PATH_INFO#*/:}" - LANGUAGE="${LANGUAGE%%/*}" + LANGUAGE="${PATH_INFO#*/:}" + LANGUAGE="${LANGUAGE%%/*}" PATH_INFO="${PATH_INFO%:?*/\[attachment\]}[attachment]" ;; - */:*/?*) # Never occurs - :;; + */:?*/\[*\]) + :;; # Default handler, considered valid in most cases + */:?*/?*) + :;; # Default attachment handler */:?*/) # Faulty URL build REDIRECT "${_BASE}${PATH_INFO%/}" ;; @@ -32,24 +71,26 @@ case ${PATH_INFO} in # REDIRECT "${_BASE}${PATH_INFO%:*}" # ;; */:?*) - export LANGUAGE="${PATH_INFO##*/:}" + LANGUAGE="${PATH_INFO##*/:}" + if mdfile "${PATH_INFO}/"; then + # Pretend to deliver Language Page, if Language Tag is valid PATH_INFO="${PATH_INFO}/" elif [ "$LANGUAGE" = "$LANGUAGE_DEFAULT" ]; then + # Pretend Language Tag was omitted for default Language PATH_INFO="${PATH_INFO%:*}" else - export ERROR_MSG="TRANSLATION NOT FOUND" + # Deliver default Page if Language is not found + ERROR_MSG="TRANSLATION NOT FOUND" PATH_INFO="${PATH_INFO%:*}" fi ;; - /|*/*/) + /|*/*/) # Keep Language from Referer if [ "$LANGUAGE_REFERRED" -a "$LANGUAGE_REFERRED" != "$LANGUAGE_DEFAULT" ]; then REDIRECT "${_BASE}${PATH_INFO}:${LANGUAGE_REFERRED}" fi ;; - */:*/\[*\]) # Considered valid in most cases - :;; - */\[*\]) + */\[*\]) # Keep Language from Referer if [ "$LANGUAGE_REFERRED" -a "$LANGUAGE_REFERRED" != "$LANGUAGE_DEFAULT" ]; then REDIRECT "${_BASE}${PATH_INFO%\[*\]}:${LANGUAGE_REFERRED}/[${PATH_INFO##*/\[}" fi -- 2.39.2