From: Paul Hänsch Date: Sun, 21 May 2023 18:46:09 +0000 (+0200) Subject: make tools language aware X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=713a39f5e2b8f99d824273d49e64edb977333f69;p=shellwiki make tools language aware --- diff --git a/handlers/10_translations.sh b/handlers/10_translations.sh index 5ae3b0c..da67f89 100755 --- a/handlers/10_translations.sh +++ b/handlers/10_translations.sh @@ -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 diff --git a/tools.sh b/tools.sh index 2e98d67..7069227 100755 --- 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