Download code

Jump to: navigation, search

Back to Wiki_(sh)

Download for Windows: zip

Download for UNIX: zip, tar.gz, tar.bz2

wiki.sh

 1 #!/bin/sh
 2 # The authors of this work have released all rights to it and placed it
 3 # in the public domain under the Creative Commons CC0 1.0 waiver
 4 # (http://creativecommons.org/publicdomain/zero/1.0/).
 5 # 
 6 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 7 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 8 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 9 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
10 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
11 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
12 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
13 # 
14 # Retrieved from: http://en.literateprograms.org/Wiki_(sh)?oldid=18667
15 
16 me="wiki.sh"
17 homepage="WelcomePage"
18 reply()      { printf '%s\r\n\r\n' "$1"; }
19 urldecode()  { echo "16i[$(sed -e 's/+/ /g;s/\%\(..\)/]P\1P[/g')]P" | dc; }
20 escapehtml() { sed -e 's/&/\&amp;/g;s/</\&lt;/g;s/>/\&gt;/g'; }
21 wikify() {
22     PAGE="$allpages" awk '
23         BEGIN { split(ENVIRON["PAGE"],rev)
24                 for(r in rev) { page[rev[r]] = r } }
25         listing && $0 !~ /^[#\*]/  { print listing; listing = "" }
26         $0 ~ /^# /   { if(!listing) { print "<ol>"; listing = "</ol>" } }
27         $0 ~ /^\* /  { if(!listing) { print "<ul>"; listing = "</ul>" } }
28         listing      { sub(/./, "<li>") }
29         $0 == ""     { $0 = "<p>" }
30         $0 == "----" { $0 = "<hr>" }
31         { for(i=1; i<=NF; ++i) {
32             if(match($i, /^[A-Z][a-z]+[A-Z][a-z]+/))	{
33               wikiname = substr($i, RSTART, RLENGTH)
34               if(page[wikiname]) l = "<a href='$me'?read+"wikiname">"wikiname"</a>"
35               else               l = wikiname"<a href='$me'?create+"wikiname">?</a>"
36               $i = l substr($i, RSTART+RLENGTH)
37             } else if(match($i, /http:\/\//)) {
38               $i = "<a href=\""$i"\">"$i"</a>"
39             }
40           }
41           print }'
42 }
43 markup() { escapehtml | tr -d "\r" | wikify; }
44 
45 set -- $(echo $QUERY_STRING | urldecode)
46 request=$1
47 wikipage=$2
48 [ $request ] || { reply "Location: $me?read+$homepage"; exit; }
49 [ $request = write ] && sed 's/wikitext=//' | urldecode > $wikipage
50 allpages=$(ls [A-Z][a-z]*[A-Z][a-z]*)
51 reply 'Content-Type: text/html'
52 case $request in
53     read|write) cat <<-EOF
54 	<html><body><h1>$wikipage</h1>
55 	(<a href=$me?edit+$wikipage>edit</a>)
56 	(<a href=$me?link+$wikipage>links</a>)
57 	<hr>
58 	$(markup < "$wikipage")
59 	</body></html>
60 	EOF
61 	;;
62     edit|create) cat <<-EOF
63 	<html><body><h1>editing: $wikipage</h1>
64 	(<a href=$me?read+$wikipage>page</a>)<hr>
65 	<form action=$me?write+$wikipage method=POST>
66 	<textarea name=wikitext rows=20 cols=60>
67 	$(escapehtml < "$wikipage")
68 	</textarea><br><input type=submit>
69 	</form></body></html>
70 	EOF
71 	;;
72     link) cat <<-EOF
73 	<html><body><h1>pages linking to: $wikipage</h1>
74 	(<a href=$me?read+$wikipage>page</a>)<hr>
75 	$(grep -l "$wikipage" $allpages | sed -e 's/^/* /' | markup)
76 	</body></html>
77 	EOF
78 	;;
79     *) cat <<-EOF
80 	<html><body><h1>unknown request</h1>
81 	(<a href=$me?read+$homepage>home</a>)<hr>
82 	didn't grok <code>$QUERY_STRING</code>
83 	</body></html>
84 	EOF
85 	;;
86 esac
87 


hijacker
hijacker
hijacker
hijacker

WelcomePage

Welcome to this bare-bones Wiki.
* click on WikiWords to enter new definitions,
* or to navigate to existing pages, like this WelcomePage.

a (very) few other features:
# numbered lists
# URLs such as http://en.literateprograms.org/ are automatically linked.
# horizontal rules
----