]> git.plutz.net Git - shellwiki/commitdiff
override mdfile function for selecting language based includes and header pages
authorPaul Hänsch <paul@plutz.net>
Sun, 21 May 2023 10:57:20 +0000 (12:57 +0200)
committerPaul Hänsch <paul@plutz.net>
Sun, 21 May 2023 10:57:20 +0000 (12:57 +0200)
handlers/10_translations.sh

index e666408a0147a91482c692e12111e2306e966eab..b9d6cb13db64e9cec2f4af0c7e4003e1e9b685b7 100755 (executable)
@@ -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