From 3d34cb0400fc26eaefaf1b0299ddf9dcc0a090b9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Thu, 24 Feb 2022 03:42:02 +0100 Subject: [PATCH] wiki framework --- index.cgi | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 index.cgi diff --git a/index.cgi b/index.cgi new file mode 100755 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 -- 2.39.2