]> git.plutz.net Git - shellwiki/blob - handlers/10_page.sh
executable flag
[shellwiki] / handlers / 10_page.sh
1 #!/bin/sh
2
3 CACHE_AGE=${CACHE_AGE:-300}
4 export MD_MACROS="$_EXEC/macros"
5 export MD_HTML="${MD_HTML:-false}"
6
7 wiki() {
8   # Print content of a wiki page
9   # Get page from data or underlay dir, handle caching
10   local page="$(PATH "$1")" mdfile cache cachetime
11
12   cache="$_DATA/pages/$page/#page.${USER_ID}.cache"
13
14   mdfile="$(mdfile "$page")" || return 4
15   acl_read "$page" || return 3
16
17   cachetime="$(stat -c %Y -- "$mdfile" "$cache" 2>/dev/null)"
18
19   if [ "${cachetime#*${BR}}" -gt "${cachetime%${BR}*}" \
20     -a "${cachetime#*${BR}}" -gt "$((_DATE - CACHE_AGE))" ]; then
21     cat "${cache}"
22   else
23     mkdir -p -- "$_DATA/pages/$page/"
24     # Macros expect to find page directory as working dir
25     ( cd -- "$_DATA/pages/$page/";
26       md <"$mdfile" \
27       | tee -- "${cache}.$$"
28     )
29     grep -q '^%nocache' "$mdfile" \
30     && rm -- "${cache}.$$" \
31     || mv -- "${cache}.$$" "${cache}"
32   fi
33 }
34
35 case "${PATH_INFO}" in
36   /"[.]"/*)
37     FILE "${_EXEC}/${PATH_INFO#/\[.\]}"
38     return 0
39     ;;
40   *${BR}*)
41     export ERROR_MSG='Page names containing newline character are not allowed'
42     theme_error 400
43     return 0
44     ;;
45   */\#*)
46     export ERROR_MSG='Page names starting with "#" are not allowed'
47     theme_error 400
48     return 0
49     ;;
50   */\[*\]/*)
51     return 1
52     ;;
53   */)
54     theme_page "${PATH_INFO}"
55     return 0
56     ;;
57 esac
58
59 return 1