]> git.plutz.net Git - shellwiki/commitdiff
make tools language aware
authorPaul Hänsch <paul@plutz.net>
Sun, 21 May 2023 18:46:09 +0000 (20:46 +0200)
committerPaul Hänsch <paul@plutz.net>
Sun, 21 May 2023 18:46:09 +0000 (20:46 +0200)
handlers/10_translations.sh
tools.sh

index 5ae3b0c5442332e553d34607da87a4f8c47e99c1..da67f8947cb6f1b0bb99ac32762fbb1adb4f8c50 100755 (executable)
@@ -7,56 +7,6 @@ export LANGUAGE_DEFAULT="${LANGUAGE_DEFAULT:-en}"
 export HTTP_REFERER="${HTTP_REFERER:-$(HEADER Referer)}"
 export LANGUAGE ERROR_MSG
 
-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
-}
-
-# UGLY! UGLY! UGLY!
-# Override markdown processor from `tools.sh` to add link suffixes
-# -> better implement a parser stack, i.e. pipeline markdown + other parser plugins
-if [ "$(which awk)" ]; then
-  md() {
-    awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk" \
-    | sed -E 's;(<[^>]+ )href="((/[^"/]+|[^"/]+[^:/]|)/([^"/]+/)*)"([^>]*>);\1href="\2:'"${LANGUAGE}"'"\5;g'
-  }
-elif [ "$(which busybox)" ]; then
-  md() {
-    busybox awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk" \
-    | sed -E 's;(<[^>]+ )href="((/[^"/]+|[^"/]+[^:/]|)/([^"/]+/)*)"([^>]*>);\1href="\2:'"${LANGUAGE}"'"\5;g'
-  }
-else
-  md() { cat; }
-fi
-
 case ${HTTP_REFERER} in
   */:*/*):;;
   */:*)
@@ -89,18 +39,17 @@ case ${PATH_INFO} in
 #   ;;
   */:?*)
     LANGUAGE="${PATH_INFO##*/:}"
+    PATH_INFO="${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
-      # Deliver default Page if Language is not found
-      ERROR_MSG="TRANSLATION NOT FOUND"
-      PATH_INFO="${PATH_INFO%:*}"
-    fi
+    [ "$LANGUAGE" != "$LANGUAGE_DEFAULT" ] \
+    && case "$(mdfile "${PATH_INFO}")" in
+      *"/:$LANGUAGE/#page.md")
+        :;;
+      '')
+        :;;
+      *)ERROR_MSG="TRANSLATION NOT FOUND"
+        ;;
+    esac
     ;;
   /|*/*/)  # Keep Language from Referer
     if [ "$LANGUAGE_REFERRED" -a "$LANGUAGE_REFERRED" != "$LANGUAGE_DEFAULT" ]; then
index 2e98d674d764295b3e338fd3ca7a59c8c42ec0d9..7069227d960f71d32fc87cc01ebd2143be8e1d27 100755 (executable)
--- a/tools.sh
+++ b/tools.sh
@@ -4,9 +4,15 @@
 include_tools="$0"
 
 if [ "$(which awk)" ]; then
-  md() { awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk"; }
+  md() {
+    awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk" \
+    | sed -E 's;(<[^>]+ )href="((/[^"/]+|[^"/]+[^:/]|)/([^"/]+/)*)"([^>]*>);\1href="\2:'"${LANGUAGE}"'"\5;g'
+  }
 elif [ "$(which busybox)" ]; then
-  md() { busybox awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk"; }
+  md() {
+    busybox awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk" \
+    | sed -E 's;(<[^>]+ )href="((/[^"/]+|[^"/]+[^:/]|)/([^"/]+/)*)"([^>]*>);\1href="\2:'"${LANGUAGE}"'"\5;g'
+  }
 else
   md() { cat; }
 fi
@@ -15,23 +21,24 @@ mdfile(){
   #  Check if page exists, if possible fall
   #  back to default page from installation
   local page="$(PATH "$1")"
+  page="${page%/}"
 
-  # IMPORTANT: A Copy of this function exists
-  # in `handlers/10_translations.sh`
-  # Look into it now!
-  # Keep it synchronized, when changing anything
-
-  if   [ -f "$_DATA/pages/$page/#page.md" ]; then
+  # Regular processing, keep in sync with tools.sh
+  if   [ -f "$_DATA/pages/$page/:$LANGUAGE/#page.md"  ]; then
+    printf %s\\n "$_DATA/pages/$page/:$LANGUAGE/#page.md"
+  elif [ -f "$_DATA/pages/$page/#page.md" ]; then
     printf %s\\n "$_DATA/pages/$page/#page.md"
-    return 0
+  elif [ -f "$_EXEC/pages/$page/:$LANGUAGE/#page.md"  ]; then
+    printf %s\\n "$_EXEC/pages/$page/:$LANGUAGE/#page.md"
   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
+
+  return 0
 }
 
 size_human(){
@@ -54,6 +61,34 @@ size_human(){
   fi
 }
 
+attachment_glob(){
+  local pattern="${1%/}" IFS=''
+  local glob page pagedir
+
+  page="${pattern%/*}"
+  [ "$page" = "$pattern" ] && page=.
+  [ ! "$page" ] && page=/
+  pattern="${pattern##*/}"
+  [ ! "$pattern" ] && pattern="*"
+
+  case $page in
+  /*)
+    for glob in "$_DATA/pages/$page/#attachments"/$pattern; do printf '%s\n' "${glob#"$_DATA/pages"}"; done
+    for glob in "$_EXEC/pages/$page/#attachments"/$pattern; do printf '%s\n' "${glob#"$_EXEC/pages"}"; done
+    ;;
+  *)
+    for glob in "$_DATA/pages/$PATH_INFO/$page/#attachments"/$pattern; do printf '%s\n' "${glob#"$_DATA/pages/$PATH_INFO/"}"; done
+    for glob in "$_EXEC/pages/$PATH_INFO/$page/#attachments"/$pattern; do printf '%s\n' "${glob#"$_EXEC/pages/$PATH_INFO/"}"; done
+    ;;
+  esac \
+  | sort -u \
+  | while read -r glob; do
+    pagedir="$(page_abs "${glob%%/#attachments/*}/")"
+    [ -d "$_DATA/pages/$pagedir" -o -d "$_EXEC/pages/$pagedir" ] \
+    && printf '%s\n' "${glob%%/#attachments/*}/${glob#*/#attachments/}"
+  done
+}
+
 page_glob(){
   local pattern="${1%/}/" IFS=''
   local glob page pagedir