]> git.plutz.net Git - shellwiki/blob - themes/default.sh
include charset in html headers
[shellwiki] / themes / default.sh
1 #!/bin/sh
2
3 # Copyright 2022 - 2024 Paul Hänsch
4
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8
9 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17 . "$_EXEC/tools.sh"
18
19 theme_head(){
20   local IFS="$BR"
21   printf '
22   <meta charset="utf-8">
23   <meta name="viewport" content="width=device-width">
24   '
25   for css in "$_BASE/%5B.%5D/cgilite/common.css" "$_BASE/%5B.%5D/themes/default.css" $PAGE_CSS; do
26     printf  '<link rel="stylesheet" type="text/css" href="%s">' \
27             "$(HTML "${css##*//}")"
28   done
29 }
30
31 theme_header(){
32   printf '<header>%s</header>' "$(wiki '[wiki]/header/')"
33 }
34
35 theme_footer(){
36   printf '<footer>%s</footer>' "$(wiki '[wiki]/footer/')"
37 }
38
39 theme_pagemenu(){
40   local page="$1"
41
42   if acl_write "$page"; then
43     cat <<-EOF
44         <ul class="pagemenu">
45           <li><a href="./%5Bview%5D">$(_ View)</a></li>
46           <li><a href="./%5Bedit%5D">$(_ Edit)</a></li>
47           <li><a href="./%5Battachment%5D">$(_ Attachments)</a></li>
48           <li><a href="./%5Brevision%5D">$(_ Revisions)</a></li>
49           <li><a href="./%5Brename%5D">$(_ Rename)</a></li>
50           <li><a href="./%5Bmove%5D">$(_ Move)</a></li>
51           <li><a href="./%5Bdelete%5D">$(_ Delete)</a></li>
52         </ul>
53         EOF
54   fi
55 }
56
57 theme_page(){
58   local page="$1" title="$2"
59   title="$(HTML "${title:-"${PAGE_TITLE:-"${page}"}"}")"
60
61   # Important! Web Server response including newline
62   printf "%s\r\n" "Content-Type: text/html; charset=utf-8" ""
63
64   cat <<-EOF
65         <!DOCTYPE HTML>
66         <html${LANGUAGE:+ lang=\"${LANGUAGE}\"}><head>
67           $(theme_head)
68           <title>${title}</title>
69         </head><body id="$(HTML "${PATH_INFO}")">
70           $(theme_header)
71           <main>
72             $(theme_pagemenu)
73             $(if [ "$page" = '-' ]; then
74                 cat
75               else
76                 printf '<article>'
77                 wiki "$page"
78                 printf '</article>'
79             fi)
80           </main>
81           $(theme_footer)
82         </body></html>
83         EOF
84 }
85
86 theme_editor(){
87   local page="$1" template="$2" file att
88
89   [ "$template" ] && acl_read "$template" || template="$page"
90
91   theme_page - "$(_ Editor): ${PAGE_TITLE:-"${page}"}" <<-EOF
92         <input checked type=radio class=tab name=edithelp value=edtr id=editor><label for=editor>$(_ Editor)</label>
93         <input         type=radio class=tab name=edithelp value=help id=syntax><label for=syntax>$(_ Syntax)</label>
94         <input         type=radio class=tab name=edithelp value=att  id=attach><label for=attach>$(_ Attachments)</label>
95         $([ "$LANGUAGE_DEFAULT" -a "$LANGUAGE_DEFAULT" != "$LANGUAGE" ] && printf '
96         <input         type=radio class=tab name=edithelp value=att  id=transl><label for=transl>%s</label>
97         ' "$LANGUAGE_DEFAULT"
98         )
99         <form class="tab editor" method=POST action="$(HTML "${_BASE}${page%/}/[edit]")">
100           <input type=hidden name=session_key value="${SESSION_KEY}"/>
101           <textarea name=pagetext>$({ wiki_text "$page" \
102                                    || wiki_text "$template"; } |HTML)</textarea>
103           <button type=submit name=action value=update>$(_ Update)</button>
104           <button type=submit name=action value=cancel>$(_ Cancel)</button>
105         </form>
106         <div class="tab syntax">$(wiki "/[wiki]/editorhelp/")</div>
107         <div class="tab attach">
108         $(for file in "$_EXEC/pages/${page%/:$LANGUAGE/}/#attachments"/* "$_DATA/pages/${page%/:$LANGUAGE/}/#attachments"/*; do
109           [ "$file" = "$_EXEC/pages/${page%/:$LANGUAGE/}/#attachments/${file##*/}" \
110                 -a -f "$_DATA/pages/${page%/:$LANGUAGE/}/#attachments/${file##*/}" ] && continue
111           att="$(HTML "${file##*/}")"
112            url="$(printf %s\\n "${file##*/}" |sed 's;[\\<>];\\&;g' |HTML)"
113           name="$(printf %s\\n "${file##*/}" |sed 's;[]\\[];\\&;g' |HTML)"
114           case ${file##*/} in
115             \*) continue;;
116             *.[pP][nN][gG]|*.[jJ][pP][gG]|*.[jJ][pP][eE][gG]|*.[gG][iI][fF])
117               [ "$page" != "${page%/:$LANGUAGE/}" ] && p=../ || p=''
118               printf '<p class=aimg><img src="%s%s">![](&lt;%s&gt;)</p><ul class=aimg>
119                         <li>[%s](&lt;[attachment]/%s&gt;)</li>
120                         <li>[![%s](&lt;%s&gt;)](&lt;[attachment]/%s&gt;)</li>
121                       </ul>' \
122                      "$p" "$att" "$url" "$name" "$url" "$name" "$url" "$url"
123               ;;
124             *)
125               printf '<p class="adoc">[%s](&lt;%s&gt;)</p>' "$name" "$url"
126               ;;
127           esac
128         done)
129         </div>
130         $(if [ "$LANGUAGE_DEFAULT" -a "$LANGUAGE_DEFAULT" != "$LANGUAGE" ]; then
131           printf '<div class="tab transl">%s</div>' "$(LANGUAGE='' wiki_text "${page%/:$LANGUAGE/}" |HTML)"
132         fi)
133         EOF
134 }
135
136 theme_revisions(){ theme_page "$@"; }
137
138 theme_search(){
139   local words="$*"
140   # STDIN: [STRING page][TAB][STRING teaser]
141
142   theme_page - "$(_ Search results): ${words}" <<-EOF
143         <article>
144           <h1>$([ "$words" ] && _ "Search results" || _ "Search" )</h1>
145           <form class="search" method="GET">
146             <input type="search" name="q" value="$(GET q |HTML)"><button class="search" type="submit">$(_ Search)</button>
147           </form>
148           <ol class="searchresults">
149             $(while read -r p t; do
150               path="$(UNSTRING "$p")" pfrag="${path%/}" title=''
151               while [ "$pfrag" ]; do
152                 title="$(page_title "$pfrag")/$title"
153                 pfrag="${pfrag%/*}"
154               done
155               printf '<li><a href="%s">%s</a><p>%s</p></li>' \
156                 "$(URL "$path")" "$(HTML "/$title")" "$(UNSTRING "$t" |HTML)"
157             done)
158           </ol>
159         </article>
160         EOF
161 }
162
163 theme_attachments(){
164   local page="$1"
165
166   if acl_write "$page"; then
167     theme_page - "$(_ Attachments): ${PAGE_TITLE:-"${page}"}" <<-EOF
168         <form class=upload method=POST enctype="multipart/form-data">
169           <h2>$(_ Upload)</h2>
170           <input type=hidden name=session_id value="$SESSION_ID">
171           <input type=file name=file multiple>
172           <button type=submit name=action value=upload>$(_ Upload)</button>
173         </form>
174         
175         <form method=POST>
176           <h2>$(_ Attachments)</h2>
177           <input type=hidden name=session_key value="$SESSION_KEY">
178           <input checked type=radio class=tab name=attachaction value=view id=tview><label for=tview>$(_ View)</label>
179           <input         type=radio class=tab name=attachaction value=del  id=tdel ><label for=tdel >$(_ Delete)</label>
180           <input         type=radio class=tab name=attachaction value=move id=tmove><label for=tmove>$(_ Move)</label>
181           <input         type=radio class=tab name=attachaction value=ren  id=tren ><label for=tren >$(_ Rename)</label>
182           <div class="tab">
183             <ul class="attachment list">
184         $(for file in "$_EXEC/pages/$page/#attachments"/* "$_DATA/pages/$page/#attachments"/*; do
185           [ "$file" = "$_EXEC/pages/$page/#attachments/${file##*/}" \
186                 -a -f "$_DATA/pages/$page/#attachments/${file##*/}" ] && continue
187           stat="$(stat -c '%s %Y' -- "$file" 2>&-)" || continue
188           size="${stat% *}" date="${stat#* }"
189           hfile="$(HTML "${file##*/}")"
190         
191           printf '<li><input id="select_%s" type=checkbox name=select value="%s">
192                     <label class=name for="select_%s">%s</label>
193                     <input class=name name="rename_%s" value="" placeholder="%s" />
194                     <a class=name href="[attachment]/%s">%s</a>
195                     <span class=size>%s</span>
196                     <span class=date>%s</span>
197                   </li>' \
198                  "$hfile" "$hfile" "$hfile" "$hfile" \
199                  "$(slopecode "${file##*/}")" "$hfile" "$hfile" "$hfile" \
200                  "$(size_human "$size")" "$(date -d @"$date" +"%F %T")"
201         done)
202             </ul>
203             <button type=submit name=action value=delete>$(_ Delete)</button>
204             <label for=moveto>$(_ Move To:)</label>
205             <input id="moveto" name="moveto" value="$(HTML "$page")" placeholder="$(_ page name)">
206             <button type=submit name=action value=move  >$(_ Move)</button>
207             <button type=submit name=action value=rename>$(_ Rename)</button>
208           </div>
209         </form>
210         EOF
211   else
212     theme_page - "$(_ Attachments): ${PAGE_TITLE:-"${page}"}" <<-EOF
213         <ul class="attachment list">
214         $(for file in "$_EXEC/pages/$page/#attachments"/* "$_DATA/pages/$page/#attachments"/*; do
215           [ "$file" = "$_EXEC/pages/$page/#attachments/${file##*/}" \
216                 -a -f "$_DATA/pages/$page/#attachments/${file##*/}" ] && continue
217           stat="$(stat -c '%s %Y' -- "$file" 2>&-)" || continue
218           size="${stat% *}" date="${stat#* }"
219           hfile="$(HTML "${file##*/}")"
220         
221           printf '<li><a class=name href="%s">%s</a>
222                   <span class=size>%s</span><span class=date>%s</span></li>' \
223                  "$hfile" "$hfile" "$(size_human "$size")" "$(date -d @"$date" +"%F %T")"
224         done)
225         </ul>
226         EOF
227   fi
228 }
229
230 theme_error(){
231   local errno="$1"
232
233   case $errno in
234     400) printf "%s\r\n" "Status: 400 Bad Request";;
235     403) printf "%s\r\n" "Status: 403 Forbidden";;
236     404) printf "%s\r\n" "Status: 404 Not Found";;
237     409) printf "%s\r\n" "Status: 409 Conflict";;
238     500) printf "%s\r\n" "Status: 500 Internal Server Error";;
239   esac
240
241   if mdfile "/[wiki]/$errno/" >&-; then
242     theme_page "/[wiki]/$errno/"
243   else
244     printf "Content-Length: 0\r\n\r\n"
245   fi
246 }