]> git.plutz.net Git - shellwiki/blob - index.cgi
wiki framework
[shellwiki] / index.cgi
1 #!/bin/sh
2
3 . "${_EXEC:-${0%/*}}/cgilite/cgilite.sh"
4 set -- nocookie
5 . "${_EXEC}/cgilite/session.sh"
6 . "${_EXEC}/cgilite/file.sh"
7 # . "$_EXEC"/cgilite/users.sh
8
9 . "${_EXEC}/themes/default.sh"
10
11 CACHE_AGE=${CACHE_AGE:-1800}
12 export MD_MACROS="$_EXEC/macros"
13 export MD_HTML="${MD_HTML:-false}"
14
15 if [ "$(which awk)" ]; then
16   md() { awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk"; }
17 elif [ "$(which busybox)" ]; then
18   md() { busybox awk -f "$_EXEC/md_macros.awk" -f "$_EXEC/cgilite/markdown.awk"; }
19 else
20   md() { cat; }
21 fi
22
23 wiki() {
24   # Print content of a wiki page
25   # Get page from data or underlay dir, handle caching
26   local page="$(PATH "$1")" md cache cachetime
27
28   cache="$_DATA/pages/$page/#page.cache"
29   if [ -f "$_DATA/pages/$page/#page.md" ]; then
30     md="$_DATA/pages/$page/#page.md"
31   elif [ -f "$_EXEC/pages/$page/#page.md" ]; then
32     md="$_EXEC/pages/$page/#page.md"
33   else
34     return 1
35   fi
36
37   cachetime="$(stat -c %Y -- "$md" "$cache" 2>/dev/null)"
38
39   if [ "${cachetime#*${BR}}" -gt "${cachetime%${BR}*}" \
40     -a "${cachetime#*${BR}}" -gt "$((_DATE - CACHE_AGE))" ]; then
41     cat "${cache}"
42   else
43     mkdir -p -- "$_DATA/pages/$page/"
44     # Macros expect to find page directory as working dir
45     ( cd -- "$_DATA/pages/$page/";
46       md <"$md" |tee -- "${cache}.$$"
47     )
48     mv -- "${cache}.$$" "${cache}"
49   fi
50 }
51
52 attachment() {
53   local file="$(PATH "$1")"
54
55   # TODO: deliver downscaled images, etc.
56   if [ -f "$_DATA/pages/${file%/*}/#attachments/${file#*/}" ]; then
57     FILE "$_DATA/pages/${file%/*}/#attachments/${file#*/}"
58   elif [ -f "$_EXEC/pages/${file%/*}/#attachments/${file#*/}" ]; then
59     FILE "$_EXEC/pages/${file%/*}/#attachments/${file#*/}"
60   elif [ -d "$_DATA/pages/${file}/" -o -d "$_EXEC/pages/${file}" ]; then
61     # path looks like a rogue page name (without trailing slash), so redirect
62     REDIRECT "$_BASE/${file}/"
63   else
64     return 1
65   fi
66 }
67
68 case "${PATH_INFO}" in
69   /"[wiki]"/*)
70     FILE "${_EXEC}/${PATH_INFO#/\[wiki\]}"
71     ;;
72   */)
73     if [ -f "$_DATA/pages/$PATH_INFO/#page.md" \
74       -o -f "$_EXEC/pages/$PATH_INFO/#page.md" ]; then
75       theme_page "${PATH_INFO}"
76     else
77       theme_404
78     fi
79     ;;
80   */"[attachment]"/*)
81     :  # TODO: Original attachment file
82     ;;
83   */"#"*)
84     :  # TODO: Invalid page name
85     ;;
86   *)
87     attachment "${PATH_INFO}"
88     ;;
89 esac