]> git.plutz.net Git - shellwiki/commitdiff
wiki framework
authorPaul Hänsch <paul@plutz.net>
Thu, 24 Feb 2022 02:42:02 +0000 (03:42 +0100)
committerPaul Hänsch <paul@plutz.net>
Thu, 24 Feb 2022 02:42:02 +0000 (03:42 +0100)
index.cgi [new file with mode: 0755]

diff --git a/index.cgi b/index.cgi
new file mode 100755 (executable)
index 0000000..9d2ed3d
--- /dev/null
+++ b/index.cgi
@@ -0,0 +1,89 @@
+#!/bin/sh
+
+. "${_EXEC:-${0%/*}}/cgilite/cgilite.sh"
+set -- nocookie
+. "${_EXEC}/cgilite/session.sh"
+. "${_EXEC}/cgilite/file.sh"
+# . "$_EXEC"/cgilite/users.sh
+
+. "${_EXEC}/themes/default.sh"
+
+CACHE_AGE=${CACHE_AGE:-1800}
+export MD_MACROS="$_EXEC/macros"
+export MD_HTML="${MD_HTML:-false}"
+
+if [ "$(which awk)" ]; then
+  md() { awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk"; }
+elif [ "$(which busybox)" ]; then
+  md() { busybox awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk"; }
+else
+  md() { cat; }
+fi
+
+wiki() {
+  # Print content of a wiki page
+  # Get page from data or underlay dir, handle caching
+  local page="$(PATH "$1")" md cache cachetime
+
+  cache="$_DATA/pages/$page/#page.cache"
+  if [ -f "$_DATA/pages/$page/#page.md" ]; then
+    md="$_DATA/pages/$page/#page.md"
+  elif [ -f "$_EXEC/pages/$page/#page.md" ]; then
+    md="$_EXEC/pages/$page/#page.md"
+  else
+    return 1
+  fi
+
+  cachetime="$(stat -c %Y -- "$md" "$cache" 2>/dev/null)"
+
+  if [ "${cachetime#*${BR}}" -gt "${cachetime%${BR}*}" \
+    -a "${cachetime#*${BR}}" -gt "$((_DATE - CACHE_AGE))" ]; then
+    cat "${cache}"
+  else
+    mkdir -p -- "$_DATA/pages/$page/"
+    # Macros expect to find page directory as working dir
+    ( cd -- "$_DATA/pages/$page/";
+      md <"$md" |tee -- "${cache}.$$"
+    )
+    mv -- "${cache}.$$" "${cache}"
+  fi
+}
+
+attachment() {
+  local file="$(PATH "$1")"
+
+  # TODO: deliver downscaled images, etc.
+  if [ -f "$_DATA/pages/${file%/*}/#attachments/${file#*/}" ]; then
+    FILE "$_DATA/pages/${file%/*}/#attachments/${file#*/}"
+  elif [ -f "$_EXEC/pages/${file%/*}/#attachments/${file#*/}" ]; then
+    FILE "$_EXEC/pages/${file%/*}/#attachments/${file#*/}"
+  elif [ -d "$_DATA/pages/${file}/" -o -d "$_EXEC/pages/${file}" ]; then
+    # path looks like a rogue page name (without trailing slash), so redirect
+    REDIRECT "$_BASE/${file}/"
+  else
+    return 1
+  fi
+}
+
+case "${PATH_INFO}" in
+  /"[wiki]"/*)
+    FILE "${_EXEC}/${PATH_INFO#/\[wiki\]}"
+    ;;
+  */)
+    if [ -f "$_DATA/pages/$PATH_INFO/#page.md" \
+      -o -f "$_EXEC/pages/$PATH_INFO/#page.md" ]; then
+      theme_page "${PATH_INFO}"
+    else
+      theme_404
+    fi
+    ;;
+  */"[attachment]"/*)
+    :  # TODO: Original attachment file
+    ;;
+  */"#"*)
+    :  # TODO: Invalid page name
+    ;;
+  *)
+    attachment "${PATH_INFO}"
+    ;;
+esac