]> git.plutz.net Git - shellwiki/blob - themes/default.sh
Merge commit '8ed7045912937148dd46554178e675df8493f46c'
[shellwiki] / themes / default.sh
1 #!/bin/sh
2
3 . "$_EXEC/tools.sh"
4
5 theme_head(){
6   local IFS="$BR"
7   printf '
8   <meta name="viewport" content="width=device-width"/>
9   '
10   for css in "$_BASE/[.]/cgilite/common.css" "$_BASE/[.]/themes/default.css" $PAGE_CSS; do
11     printf  '<link rel="stylesheet" type="text/css" href="%s">' \
12             "$(HTML "${css##*//}")"
13   done
14 }
15
16 theme_header(){
17   printf '<header>%s</header>' "$(wiki '[wiki]/header/')"
18 }
19
20 theme_footer(){
21   printf '<footer>%s</footer>' "$(wiki '[wiki]/footer/')"
22 }
23
24 theme_pagemenu(){
25   local page="$1"
26
27   if acl_write "$page"; then
28     printf  '
29     <ul class="pagemenu">
30       <li><a href="./[view]">View</a></li>
31       <li><a href="./[edit]">Edit</a></li>
32       <li><a href="./[attachment]">Attachments</a></li>
33       <li><a href="./[revision]">Revisions</a></li>
34       <li><a href="./[rename]">Rename</a></li>
35       <li><a href="./[move]">Move</a></li>
36       <li><a href="./[delete]">Delete</a></li>
37     </ul>'
38   fi
39 }
40
41 theme_page(){
42   local page="$1" title="$2"
43   title="$(HTML "${title:-"${page}"}")"
44
45   # Important! Web Server response including newline
46   printf "%s\r\n" "Content-Type: text/html; charset=utf-8" ""
47
48   cat <<-EOF
49         <!DOCTYPE HTML>
50         <html><head>
51           $(theme_head)
52           <title>${title}</title>
53         </head><body id="$(HTML "${PATH_INFO}")">
54           $(theme_header)
55           <main>
56             $(theme_pagemenu)
57             $(if [ "$page" = '-' ]; then
58                 cat
59               else
60                 printf '<article>'
61                 wiki "$page"
62                 printf '</article>'
63             fi)
64           </main>
65           $(theme_footer)
66         </body></html>
67         EOF
68 }
69
70 theme_editor(){
71   local page="$1" template="$2" title
72   title="${page%/}"; title="${title##*/}"
73
74   [ "$template" ] && acl_read "$template" || template="$page"
75
76   theme_page - "Editor: $title" <<-EOF
77         <form method=POST action="$(HTML "${_BASE}${page%/}/[edit]")">
78           <input type=hidden name=session_key value="${SESSION_KEY}"/>
79           <textarea name=pagetext>$({ wiki_text "$page" \
80                                    || wiki_text "$template"; } |HTML)</textarea>
81           <button type=submit name=action value=update>Update</button>
82           <button type=submit name=action value=cancel>Cancel</button>
83         </form>
84         EOF
85 }
86
87 theme_revisions(){
88   local page="$1" title
89   title="${page%/}"; title="${title##*/}"
90
91  "$_EXEC/macros/revisions" "$page" \
92   | theme_page - "Revisions: $title"
93 }
94
95 theme_attachments(){
96   local page="$1" title
97   title="${page%/}"; title="${title##*/}"
98
99   if acl_write "$page"; then
100     theme_page - "Attachments: $title" <<-EOF
101         <form class=upload method=POST enctype="multipart/form-data">
102           <input type=hidden name=session_id value="$SESSION_ID">
103           <input type=file name=file multiple>
104           <button type=submit name=action value=upload>Upload</button>
105         </form>
106         
107         <form method=POST><ul class="attachment list">
108           <input type=hidden name=session_key value="$SESSION_KEY">
109         $(for file in "$_EXEC/pages/$page/#attachments"/* "$_DATA/pages/$page/#attachments"/*; do
110           [ "$file" = "$_EXEC/pages/$page/#attachments/${file##*/}" \
111                 -a -f "$_DATA/pages/$page/#attachments/${file##*/}" ] && continue
112           stat="$(stat -c '%s %Y' -- "$file" 2>&-)" || continue
113           size="${stat% *}" date="${stat#* }"
114           hfile="$(HTML "${file##*/}")"
115         
116           printf '<li><button type=submit name=delete value="%s">Delete</button><a class=name href="[attachment]/%s">%s</a>
117                   <span class=size>%s</span><span class=date>%s</span></li>' \
118                  "$hfile" "$hfile" "$hfile" "$(size_human "$size")" "$(date -d @"$date" +"%F %T")"
119         done)
120         </ul></form>
121         EOF
122   else
123     theme_page - "Attachments: $title" <<-EOF
124         <ul class="attachment list">
125         $(for file in "$_EXEC/pages/$page/#attachments"/* "$_DATA/pages/$page/#attachments"/*; do
126           [ "$file" = "$_EXEC/pages/$page/#attachments/${file##*/}" \
127                 -a -f "$_DATA/pages/$page/#attachments/${file##*/}" ] && continue
128           stat="$(stat -c '%s %Y' -- "$file" 2>&-)" || continue
129           size="${stat% *}" date="${stat#* }"
130           hfile="$(HTML "${file##*/}")"
131         
132           printf '<li><a class=name href="%s">%s</a>
133                   <span class=size>%s</span><span class=date>%s</span></li>' \
134                  "$hfile" "$hfile" "$(size_human "$size")" "$(date -d @"$date" +"%F %T")"
135         done)
136         </ul>
137         EOF
138   fi
139 }
140
141 theme_error(){
142   local errno="$1"
143
144   case $errno in
145     400) printf "%s\r\n" "Status: 400 Bad Request";;
146     403) printf "%s\r\n" "Status: 403 Forbidden";;
147     404) printf "%s\r\n" "Status: 404 Not Found";;
148     409) printf "%s\r\n" "Status: 409 Conflict";;
149     500) printf "%s\r\n" "Status: 500 Internal Server Error";;
150   esac
151
152   if [ "$(mdfile "/[wiki]/$errno/")" ]; then
153     theme_page "/[wiki]/$errno/"
154   else
155     printf "Content-Length: 0\r\n\r\n"
156   fi
157 }