]> git.plutz.net Git - busy/blob - Wiki.page
490966367705b8b8d744062aa4e6aa007612c0a3
[busy] / Wiki.page
1 #!/bin/zsh
2
3 info="$(egrep -o '(^|&)(i=)[0-9]{10}_[0-9]{3}(&|$)' <<<"${QUERY_STRING}" |tr -d '&i=')"
4 edit="$(egrep -o '(^|&)(e=)[0-9]{10}_[0-9]{3}(&|$)' <<<"${QUERY_STRING}" |tr -d '&e=')"
5 rev="$(egrep -o '(^|&)(r=)[0-9]{10}(&|$)' <<<"${QUERY_STRING}" |tr -d '&r=')"
6 search="$(egrep -o '(^|&)(s=).+(&|$)' <<<"${QUERY_STRING}" |sed -r 's:^&?s=::;s:\+: :g;s:%:\\x:g')"
7 search="$(echo -e "${search}" |sed 's:\t: :g;s:\r::g;s:\\:\\\\:g' |head -n1)"
8
9 [ -n "$rev" ] && rev=".$rev"
10 if [ -n "$search" ]; then
11   info=''
12   edit=''
13   rev=''
14 fi
15
16 lock="Wiki/$info.lock"
17 [ -r "$lock" -o -n "$lock" ] && if [ "$(cut -d: -f2 "$lock")" -lt "$(date +%s)" ]; then
18   rm "$lock"
19   lock=''
20 else
21   lock="$(cut -d: -f1 "$lock")"
22 fi
23
24 if [ -n "$edit" -a -r "Wiki/$edit" ]; then
25   title="$(egrep -x 'title=.+' "Wiki/$edit.meta" |cut -d= -f2- || echo '(Untitled)')"
26   cat <<WikiEND
27     <form class="edit" action="write/scriptedit.cgi?i=$edit" method="post" accept-charset="UTF-8">
28       <div id="LEFT">
29         Editing <b>$title</b><br>
30         <input type="submit" value="Submit">
31         <input type="submit" name="cancel" value="Cancel">
32         <hr>
33       </div>
34       <div id="MAIN">
35         <div class="wiki">
36           <h1>$title</h1>
37           <textarea name="page" rows=30 cols=40>$(cat "Wiki/$edit")</textarea>
38         </div>
39       </div>
40     </form>
41     <div id="RIGHT">
42     $(stat -c '' Wiki/${edit}.comment.<0000000000-9999999999>_<000-999> && for each in $(ls -c Wiki/${edit}.comment.<0000000000-9999999999>_<000-999>); do
43       author=$(grep -m1 '^author=' "$each" |cut -d= -f2-)
44       echo "<div class='comment'><h1>$author wrote:</h1><div class='wikitext'>"
45       tail -n+2 $each |_wiki
46       echo '</div></div>'
47     done)
48     </div>
49 WikiEND
50
51 elif [ -n "$info" -a -r "Wiki/${info}${rev}" ]; then
52   title="$(egrep -x 'title=.+' "Wiki/$info.meta" |cut -d= -f2- || echo '(Untitled)')"
53
54   cat <<WikiEND
55     <div id="LEFT">
56       <form method="GET" action="/${SCRIPTNAME}" accept-charset="utf-8">
57         <input type="hidden" name="p" value="Wiki">
58         <input type="text" name="s" placeholder="Search"><br>
59         <input type="submit" value="Search Wiki">
60       </form>
61       <hr>
62
63       <b>Revisions:</b>
64       $(rno=1; for each in Wiki/$info.<0000000000-9999999999>; do
65         revi=$(cut -d. -f2 <<<"$each")
66         echo "<a class='rev' href='?p=Wiki&amp;i=$info&amp;r=$revi'>$rno</a>"
67         rno=$(($rno+1))
68         done) <a class='rev' href='?p=Wiki&amp;i=$info'>Latest</a><br>
69       <hr>
70       $($LOGIN && [ -z "$rev" ] && if [ -z "$lock" ]; then
71           echo "<a class='function' href='write/lock.cgi?p=Wiki&amp;e=$info'>edit this page</a>"
72         else
73           echo "<b>This page is currently being edited by $lock</b><a class='function' href='write/lock.cgi?p=Wiki&amp;e=$info'>edit anyway</a>"
74         fi
75         $LOGIN && echo '<hr>')
76       <h1>Table of Content</h1>
77       <div class="toc">
78         $(cat Wiki/"${info}${rev}" |while read line; do
79         if (egrep -q '^ *!!!' <<<"$line"); then
80           echo "<a class='i2' href='#$(sed 's,^ *!!! *,,;s,[^a-zA-Z0-9],.,g' <<<"$line")'>$(sed 's,^ *!!!,,' <<<"$line")</a><br>"
81         elif (egrep -q '^ *!!' <<<"$line"); then
82           echo "<a class='i1' href='#$(sed 's,^ *!! *,,;s,[^a-zA-Z0-9],.,g' <<<"$line")'>$(sed 's,^ *!!,,' <<<"$line")</a><br>"
83         elif (egrep -q '^ *!' <<<"$line"); then
84           echo "<a class='i0' href='#$(sed 's,^ *! *,,;s,[^a-zA-Z0-9],.,g' <<<"$line")'>$(sed 's,^ *!,,' <<<"$line")</a><br>"
85           fi
86         done)
87       </div>
88       <hr>
89     </div>
90     <div id="MAIN">
91       <div class="wiki"><div class="wikitext">
92         <h1>$title</h1>
93         $(_wiki Wiki/"${info}${rev}")
94       </div></div>
95     </div>
96 WikiEND
97
98   echo '<div id="RIGHT">'
99 $LOGIN && cat <<WikiEND
100       <a id='comment_button' class='function' href='#' onclick='javascript:show_screen("comment");hide_screen("comment_button");'>Comment / Ask</a>
101       <div id="comment" style="display: none;">
102         <form action="/write/wikimeta.cgi?i=$info" method="post" accept-charset="UTF-8">
103           <b>Comment:</b><br>
104           <textarea name="comm" rows="6" style="width:99%;"></textarea><br>
105           <input type="submit" value="Submit">
106           <a class="function" href="#" onclick="javascript:hide_screen('comment');show_screen('comment_button');">Cancel</a>
107         </form>
108       </div>
109 WikiEND
110   stat -c '' Wiki/${info}.comment.<0000000000-9999999999>_<000-999> && for each in $(ls -c Wiki/${info}.comment.<0000000000-9999999999>_<000-999>); do
111     author=$(grep -m1 '^author=' "$each" |cut -d= -f2-)
112     echo "<div class='comment'><h1>$author wrote:</h1><div class='wikitext'>"
113     tail -n+2 $each |_wiki
114     echo '</div></div>'
115   done
116   echo '</div>'
117   
118 elif [ -z "$info" ]; then
119   cat <<WikiEND
120     <div id="LEFT">
121       <form method="GET" action="/${SCRIPTNAME}" accept-charset="utf-8">
122         <input type="hidden" name="p" value="Wiki">
123         <input type="text" name="s" placeholder="Search"><br>
124         <input type="submit" value="Search Wiki">
125       </form>
126       <hr>
127 WikiEND
128   if [ -n "$search" ]; then
129     echo "<h1>Search results for: $search</h1>"
130     for each in $(grep -ile "$search" Wiki/<0000000000-9999999999>_<000-999>); do
131       echo "<div class='search'><a href='?p=Wiki&amp;i=${each:t}'><h2>$(grep '^title=' "$each.meta" |cut -d= -f2-)</h2></a>"
132       grep -C1 -ie "$search" "$each" |sed "s:$search:<b>&</b>:g;s:$:<br>:g"
133       echo '</div>'
134     done
135     echo '<hr>'
136   fi
137   echo '</div><div id="RIGHT">'
138   $LOGIN && cat <<WikiEND
139     <a id='addp_button' class='function' href='#' onclick='javascript:show_screen("add_page");hide_screen("addp_button");'>Add a new Page</a>
140     <div id="add_page" style="display: none;">
141       <form action="/write/wikimeta.cgi" method="post" accept-charset="UTF-8">
142         <br><b>Title:</b><br>
143         <input type="text" name="title" placeholder="Fill in a title"><br>
144         <b>Description:</b><br>
145         <textarea name="desc" rows="6" ></textarea><br>
146         <input type="submit" value="Submit">
147         <a class="function" href="#" onclick="javascript:hide_screen('add_page');show_screen('addp_button');">Cancel</a>
148       </form>
149     </div>
150 WikiEND
151   echo '</div><div id="MAIN">'
152
153   stat -c '' Wiki/<0000000000-9999999999>_<000-999> && for each in Wiki/<0000000000-9999999999>_<000-999>; do
154     info=$(sed 's,Wiki/,,' <<<"$each")
155     title="$(egrep -x 'title=.+' "$each.meta" |cut -d= -f2- || echo '(Untitled)')"
156     description="$(egrep -x 'description=.+' "$each.meta" |cut -d= -f2-)"
157     owner="$(egrep -x 'owner=.+' "$each.meta" |cut -d= -f2-)"
158
159     cat <<WikiEND
160     <div class="article">
161       <div id="display_$info">
162         <a name="$info" href="/?p=Wiki&amp;i=$info"><h1>$title</h1></a>
163         $description<br><br>
164         <a class="function" href="/?p=Wiki&amp;i=$info">View</a> 
165         $($LOGIN && echo "<a class='function' href='#' onclick=\"javascript:show_screen('change_$info');hide_screen('display_$info');\">Change Info</a>")
166       </div>
167 WikiEND
168     $LOGIN && cat <<WikiEND
169       <div id="change_$info" style="display: none;">
170         <form action="/write/wikimeta.cgi?i=$info" method="post" accept-charset="UTF-8">
171           <b>Title:</b> <input type="text" name="title" value="$title"><br>
172           <b>Description:</b><br>
173           <textarea name="desc" rows="6" style="width:100%;">$description</textarea><br>
174           <input type="submit" value="Submit">
175           <a class="function" href="#" onclick="javascript:hide_screen('change_$info');show_screen('display_$info');">Cancel</a>
176         </form>
177       </div>
178 WikiEND
179     echo '</div>'
180   done
181   echo '</div>'
182 fi