]> git.plutz.net Git - shellwiki/blobdiff - themes/default.sh
Syntax help fro white space file names
[shellwiki] / themes / default.sh
index 7edd4b9986c60ad8e989f07a2740c7dc327d7c48..23b03f25552b86ba2e6e3fc03a2c46d322e4cbba 100755 (executable)
@@ -1,13 +1,30 @@
 #!/bin/sh
 
+# Copyright 2022 - 2023 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
+# copyright notice and this permission notice appear in all copies.
+# 
+# THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
+# IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+
 . "$_EXEC/tools.sh"
 
 theme_head(){
+  local IFS="$BR"
   printf '
-  <meta name="viewport" content="width=device-width"/>
-  <link rel="stylesheet" type="text/css" href="%s/[.]/cgilite/common.css">
-  <link rel="stylesheet" type="text/css" href="%s/[.]/themes/default.css">
-  ' "$_BASE"
+  <meta name="viewport" content="width=device-width">
+  '
+  for css in "$_BASE/%5B.%5D/cgilite/common.css" "$_BASE/%5B.%5D/themes/default.css" $PAGE_CSS; do
+    printf  '<link rel="stylesheet" type="text/css" href="%s">' \
+            "$(HTML "${css##*//}")"
+  done
 }
 
 theme_header(){
@@ -15,41 +32,50 @@ theme_header(){
 }
 
 theme_footer(){
-  printf '<footer>%s</footer>' "$(wiki '[wiki]/footer/' ||echo No footer)"
+  printf '<footer>%s</footer>' "$(wiki '[wiki]/footer/')"
 }
 
-theme_page(){
-  local page="$1" title
-  title="${page%/}"; title="${title##*/}"
+theme_pagemenu(){
+  local page="$1"
 
-  if [ ! "$(mdfile "$page")" ]; then
-    theme_404
-    return 0
-  elif ! acl_read "$page"; then
-    theme_403
-    return 0
+  if acl_write "$page"; then
+    cat <<-EOF
+       <ul class="pagemenu">
+         <li><a href="./%5Bview%5D">$(_ View)</a></li>
+         <li><a href="./%5Bedit%5D">$(_ Edit)</a></li>
+         <li><a href="./%5Battachment%5D">$(_ Attachments)</a></li>
+         <li><a href="./%5Brevision%5D">$(_ Revisions)</a></li>
+         <li><a href="./%5Brename%5D">$(_ Rename)</a></li>
+         <li><a href="./%5Bmove%5D">$(_ Move)</a></li>
+         <li><a href="./%5Bdelete%5D">$(_ Delete)</a></li>
+       </ul>
+       EOF
   fi
+}
+
+theme_page(){
+  local page="$1" title="$2"
+  title="$(HTML "${title:-"${PAGE_TITLE:-"${page}"}"}")"
 
   # Important! Web Server response including newline
   printf "%s\r\n" "Content-Type: text/html; charset=utf-8" ""
 
   cat <<-EOF
        <!DOCTYPE HTML>
-       <html><head>
+       <html${LANGUAGE:+ lang=\"${LANGUAGE}\"}><head>
          $(theme_head)
-         <title>$(HTML "${title}")</title>
-       </head><body id="$(HTML "$page")">
+         <title>${title}</title>
+       </head><body id="$(HTML "${PATH_INFO}")">
          $(theme_header)
          <main>
-           $(acl_write "$page" && printf %s \
-             '<ul class="pagemenu">
-                <li><a href="[edit]">Edit</a></li>
-                <li><a href="[attachment]/">Attachments</a></li>
-              </ul>'
-           )
-           <article>
-             $(wiki "$page" || printf 'Error while loading page <br> function "wiki" of index.sh returned with an error.')
-           </article>
+           $(theme_pagemenu)
+           $(if [ "$page" = '-' ]; then
+               cat
+             else
+               printf '<article>'
+               wiki "$page"
+               printf '</article>'
+           fi)
          </main>
          $(theme_footer)
        </body></html>
@@ -57,35 +83,74 @@ theme_page(){
 }
 
 theme_editor(){
-  local page="$1" title
-  title="${page%/}"; title="${title##*/}"
-
-  if [ ! "$(mdfile "$page")" ]; then
-    theme_404
-    return 0
-  elif ! acl_write "$page"; then
-    theme_403
-    return 0
-  fi
-
-  # Important! Web Server response including newline
-  printf "%s\r\n" "Content-Type: text/html; charset=utf-8" ""
+  local page="$1" template="$2" title file att
+  title="$(HTML "${PAGE_TITLE:-"${page}"}")"
+
+  [ "$template" ] && acl_read "$template" || template="$page"
+
+  theme_page - "Editor: $title" <<-EOF
+       <input checked type=radio class=tab name=edithelp value=edtr id=editor><label for=editor>$(_ Editor)</label>
+       <input         type=radio class=tab name=edithelp value=help id=syntax><label for=syntax>$(_ Syntax)</label>
+       <input         type=radio class=tab name=edithelp value=att  id=attach><label for=attach>$(_ Attachments)</label>
+       $([ "$LANGUAGE_DEFAULT" -a "$LANGUAGE_DEFAULT" != "$LANGUAGE" ] && printf '
+       <input         type=radio class=tab name=edithelp value=att  id=transl><label for=transl>%s</label>
+       ' "$LANGUAGE_DEFAULT"
+       )
+       <form class="tab editor" method=POST action="$(HTML "${_BASE}${page%/}/[edit]")">
+         <input type=hidden name=session_key value="${SESSION_KEY}"/>
+         <textarea name=pagetext>$({ wiki_text "$page" \
+                                  || wiki_text "$template"; } |HTML)</textarea>
+         <button type=submit name=action value=update>$(_ Update)</button>
+         <button type=submit name=action value=cancel>$(_ Cancel)</button>
+       </form>
+        <div class="tab syntax">$(wiki "/[wiki]/editorhelp/")</div>
+       <div class="tab attach">
+       $(for file in "$_EXEC/pages/${page%/:$LANGUAGE/}/#attachments"/* "$_DATA/pages/${page%/:$LANGUAGE/}/#attachments"/*; do
+         [ "$file" = "$_EXEC/pages/${page%/:$LANGUAGE/}/#attachments/${file##*/}" \
+               -a -f "$_DATA/pages/${page%/:$LANGUAGE/}/#attachments/${file##*/}" ] && continue
+         att="$(HTML "${file##*/}")"
+          url="$(printf %s\\n "${file##*/}" |sed 's;[\\<>];\\&;g' |HTML)"
+         name="$(printf %s\\n "${file##*/}" |sed 's;[]\\[];\\&;g' |HTML)"
+         case ${file##*/} in
+           \*) continue;;
+           *.[pP][nN][gG]|*.[jJ][pP][gG]|*.[jJ][pP][eE][gG]|*.[gG][iI][fF])
+             [ "$page" != "${page%/:$LANGUAGE/}" ] && p=../ || p=''
+             printf '<p class=aimg><img src="%s%s">![](&lt;%s&gt;)</p><ul class=aimg>
+                       <li>[%s](&lt;[attachment]/%s&gt;)</li>
+                       <li>[![%s](&lt;%s&gt;)](&lt;[attachment]/%s&gt;)</li>
+                     </ul>' \
+                    "$p" "$att" "$url" "$name" "$url" "$name" "$url" "$url"
+             ;;
+           *)
+             printf '<p class="adoc">[%s](&lt;%s&gt;)</p>' "$name" "$url"
+             ;;
+         esac
+       done)
+       </div>
+       $(if [ "$LANGUAGE_DEFAULT" -a "$LANGUAGE_DEFAULT" != "$LANGUAGE" ]; then
+         printf '<div class="tab transl">%s</div>' "$(LANGUAGE='' wiki_text "${page%/:$LANGUAGE/}" |HTML)"
+       fi)
+       EOF
+}
 
-  cat <<-EOF
-       <!DOCTYPE HTML>
-       <html><head>
-         $(theme_head)
-         <title>$(HTML "${title}")</title>
-       </head><body id="$(HTML "$page")" class="editor">
-         $(theme_header)
-         <main><form method=POST>
-           <input type=hidden name=session_key value="${SESSION_KEY}"/>
-            <textarea name=pagetext>$(wiki_text "$page" |HTML)</textarea>
-            <button type=submit name=action value=update>Update</button>
-            <button type=submit name=action value=cancel>Cancel</button>
-          </form></main>
-         $(theme_footer)
-       </body></html>
+theme_revisions(){ theme_page "$@"; }
+
+theme_search(){
+  local words="$*"
+  # STDIN: read result pages line by line
+
+  theme_page - <<-EOF
+       <article>
+         <h1>$([ "$words" ] && _ "Search results" || _ "Search" )</h1>
+         <form class="search" method="GET">
+           <input class="search" name="q" value="$(HTML $words)"><button class="search" type="submit">$(_ Search)</button>
+         </form>
+         <ol class="searchresults">
+           $( while read p; do
+             printf '<li><a href="%s">%s</a></li>' "$(URL "$p")" "$(HTML "$p")"
+           done)
+         </ol>
+       </article>
        EOF
 }
 
@@ -93,99 +158,83 @@ theme_attachments(){
   local page="$1" title
   title="${page%/}"; title="${title##*/}"
 
-  if [ ! "$(mdfile "$page")" ]; then
-    theme_404
-    return 0
-  elif ! acl_read "$page"; then
-    theme_403
-    return 0
-  fi
-
-  # Important! Web Server response including newline
-  printf "%s\r\n" "Content-Type: text/html; charset=utf-8" ""
-
   if acl_write "$page"; then
-    cat <<-EOF
-       <!DOCTYPE HTML>
-       <html><head>
-         $(theme_head)
-         <title>Attachments $(HTML "${title}")</title>
-       </head><body id="$(HTML "$page")[attachment]/">
-         $(theme_header)
-         <main>
-           <form class=upload method=POST enctype="multipart/form-data">
-             <input type=file name=file multiple>
-             <button type=submit name=action value=upload>Upload</button>
-           </form>
-
-            <form method=POST><ul class="attachment list">
-           $(for file in "$_EXEC/pages/$page/#attachments"/* "$_DATA/pages/$page/#attachments"/*; do
-             [ "$file" = "$_EXEC/pages/$page/#attachments/${file##*/}" \
-                   -a -f "$_DATA/pages/$page/#attachments/${file##*/}" ] && continue
-             stat="$(stat -c '%s %Y' -- "$file" 2>&-)" || continue
-             size="${stat% *}" date="${stat#* }"
-           
-             printf '<li><button type=submit name=delete value="%s">Delete</button><a class=name href="%s">%s</a>
-                     <span class=size>%s</span><span class=date>%s</span></li>' \
-               "$(HTML "${file##*/}")" "$(HTML "${file##*/}")" "$(HTML "${file##*/}")" \
-               "$(size_human "$size")" "$(date -d @"$date" +"%F %T")"
-           done)
-            </ul></form>
-         </main>
-         $(theme_footer)
-       </body></html>
+    theme_page - "$(_ Attachments): $title" <<-EOF
+       <form class=upload method=POST enctype="multipart/form-data">
+         <h2>$(_ Upload)</h2>
+         <input type=hidden name=session_id value="$SESSION_ID">
+         <input type=file name=file multiple>
+         <button type=submit name=action value=upload>$(_ Upload)</button>
+       </form>
+       
+       <form method=POST>
+         <h2>$(_ Attachments)</h2>
+         <input type=hidden name=session_key value="$SESSION_KEY">
+          <input checked type=radio class=tab name=attachaction value=view id=tview><label for=tview>$(_ View)</label>
+          <input         type=radio class=tab name=attachaction value=del  id=tdel ><label for=tdel >$(_ Delete)</label>
+          <input         type=radio class=tab name=attachaction value=move id=tmove><label for=tmove>$(_ Move)</label>
+          <input         type=radio class=tab name=attachaction value=ren  id=tren ><label for=tren >$(_ Rename)</label>
+          <div class="tab">
+           <ul class="attachment list">
+       $(for file in "$_EXEC/pages/$page/#attachments"/* "$_DATA/pages/$page/#attachments"/*; do
+         [ "$file" = "$_EXEC/pages/$page/#attachments/${file##*/}" \
+               -a -f "$_DATA/pages/$page/#attachments/${file##*/}" ] && continue
+         stat="$(stat -c '%s %Y' -- "$file" 2>&-)" || continue
+         size="${stat% *}" date="${stat#* }"
+          hfile="$(HTML "${file##*/}")"
+       
+         printf '<li><input id="select_%s" type=checkbox name=select value="%s">
+                   <label class=name for="select_%s">%s</label>
+                   <input class=name name="rename_%s" value="" placeholder="%s" />
+                   <a class=name href="[attachment]/%s">%s</a>
+                   <span class=size>%s</span>
+                   <span class=date>%s</span>
+                 </li>' \
+                "$hfile" "$hfile" "$hfile" "$hfile" \
+                "$(slopecode "${file##*/}")" "$hfile" "$hfile" "$hfile" \
+                "$(size_human "$size")" "$(date -d @"$date" +"%F %T")"
+       done)
+           </ul>
+           <button type=submit name=action value=delete>$(_ Delete)</button>
+           <label for=moveto>$(_ Move To:)</label>
+           <input id="moveto" name="moveto" value="$(HTML "$page")" placeholder="$(_ page name)">
+           <button type=submit name=action value=move  >$(_ Move)</button>
+           <button type=submit name=action value=rename>$(_ Rename)</button>
+         </div>
+       </form>
        EOF
   else
-    cat <<-EOF
-       <!DOCTYPE HTML>
-       <html><head>
-         $(theme_head)
-         <title>Attachments $(HTML "${title}")</title>
-       </head><body id="$(HTML "$page")[attachment]/">
-         $(theme_header)
-         <main>
-            <ul class="attachment list">
-           $(for file in "$_EXEC/pages/$page/#attachments"/* "$_DATA/pages/$page/#attachments"/*; do
-             [ "$file" = "$_EXEC/pages/$page/#attachments/${file##*/}" \
-                   -a -f "$_DATA/pages/$page/#attachments/${file##*/}" ] && continue
-             stat="$(stat -c '%s %Y' -- "$file" 2>&-)" || continue
-             size="${stat% *}" date="${stat#* }"
-           
-             printf '<li><a class=name href="%s">%s</a>
-                     <span class=size>%s</span><span class=date>%s</span></li>' \
-               "$(HTML "${file##*/}")" "$(HTML "${file##*/}")" "$(size_human "$size")" "$(date -d @"$date" +"%F %T")"
-           done)
-            </ul>
-         </main>
-         $(theme_footer)
-       </body></html>
+    theme_page - "$(_ Attachments): $title" <<-EOF
+       <ul class="attachment list">
+       $(for file in "$_EXEC/pages/$page/#attachments"/* "$_DATA/pages/$page/#attachments"/*; do
+         [ "$file" = "$_EXEC/pages/$page/#attachments/${file##*/}" \
+               -a -f "$_DATA/pages/$page/#attachments/${file##*/}" ] && continue
+         stat="$(stat -c '%s %Y' -- "$file" 2>&-)" || continue
+         size="${stat% *}" date="${stat#* }"
+          hfile="$(HTML "${file##*/}")"
+       
+         printf '<li><a class=name href="%s">%s</a>
+                 <span class=size>%s</span><span class=date>%s</span></li>' \
+                "$hfile" "$hfile" "$(size_human "$size")" "$(date -d @"$date" +"%F %T")"
+       done)
+       </ul>
        EOF
   fi
 }
 
-theme_login(){
-  theme_page '/[wiki]/login/'
-}
-
-theme_register(){
-  theme_page '/[wiki]/register/'
-}
-
-theme_403(){
-  printf "%s\r\n" "Status: 403 Forbidden"
-
-  if [ "$(mdfile '/[wiki]/403/')" ]; then
-    theme_page '/[wiki]/403/'
-  else
-    printf "Content-Length: 0\r\n\r\n"
-  fi
-}
+theme_error(){
+  local errno="$1"
 
-theme_404(){
-  printf "%s\r\n" "Status: 404 Not Found"
+  case $errno in
+    400) printf "%s\r\n" "Status: 400 Bad Request";;
+    403) printf "%s\r\n" "Status: 403 Forbidden";;
+    404) printf "%s\r\n" "Status: 404 Not Found";;
+    409) printf "%s\r\n" "Status: 409 Conflict";;
+    500) printf "%s\r\n" "Status: 500 Internal Server Error";;
+  esac
 
-  if [ "$(mdfile '/[wiki]/404/')" ]; then
-    theme_page '/[wiki]/404/'
+  if mdfile "/[wiki]/$errno/" >&-; then
+    theme_page "/[wiki]/$errno/"
   else
     printf "Content-Length: 0\r\n\r\n"
   fi