| 1 | """Wiki main program. Imported and run by cgi3.py."""
|
|---|
| 2 |
|
|---|
| 3 | import os, re, cgi, sys, tempfile
|
|---|
| 4 | escape = cgi.escape
|
|---|
| 5 |
|
|---|
| 6 | def main():
|
|---|
| 7 | form = cgi.FieldStorage()
|
|---|
| 8 | print "Content-type: text/html"
|
|---|
| 9 | print
|
|---|
| 10 | cmd = form.getvalue("cmd", "view")
|
|---|
| 11 | page = form.getvalue("page", "FrontPage")
|
|---|
| 12 | wiki = WikiPage(page)
|
|---|
| 13 | method = getattr(wiki, 'cmd_' + cmd, None) or wiki.cmd_view
|
|---|
| 14 | method(form)
|
|---|
| 15 |
|
|---|
| 16 | class WikiPage:
|
|---|
| 17 |
|
|---|
| 18 | homedir = tempfile.gettempdir()
|
|---|
| 19 | scripturl = os.path.basename(sys.argv[0])
|
|---|
| 20 |
|
|---|
| 21 | def __init__(self, name):
|
|---|
| 22 | if not self.iswikiword(name):
|
|---|
| 23 | raise ValueError, "page name is not a wiki word"
|
|---|
| 24 | self.name = name
|
|---|
| 25 | self.load()
|
|---|
| 26 |
|
|---|
| 27 | def cmd_view(self, form):
|
|---|
| 28 | print "<h1>", escape(self.splitwikiword(self.name)), "</h1>"
|
|---|
| 29 | print "<p>"
|
|---|
| 30 | for line in self.data.splitlines():
|
|---|
| 31 | line = line.rstrip()
|
|---|
| 32 | if not line:
|
|---|
| 33 | print "<p>"
|
|---|
| 34 | else:
|
|---|
| 35 | print self.formatline(line)
|
|---|
| 36 | print "<hr>"
|
|---|
| 37 | print "<p>", self.mklink("edit", self.name, "Edit this page") + ";"
|
|---|
| 38 | print self.mklink("view", "FrontPage", "go to front page") + "."
|
|---|
| 39 |
|
|---|
| 40 | def formatline(self, line):
|
|---|
| 41 | words = []
|
|---|
| 42 | for word in re.split('(\W+)', line):
|
|---|
| 43 | if self.iswikiword(word):
|
|---|
| 44 | if os.path.isfile(self.mkfile(word)):
|
|---|
| 45 | word = self.mklink("view", word, word)
|
|---|
| 46 | else:
|
|---|
| 47 | word = self.mklink("new", word, word + "*")
|
|---|
| 48 | else:
|
|---|
| 49 | word = escape(word)
|
|---|
| 50 | words.append(word)
|
|---|
| 51 | return "".join(words)
|
|---|
| 52 |
|
|---|
| 53 | def cmd_edit(self, form, label="Change"):
|
|---|
| 54 | print "<h1>", label, self.name, "</h1>"
|
|---|
| 55 | print '<form method="POST" action="%s">' % self.scripturl
|
|---|
| 56 | s = '<textarea cols="70" rows="20" name="text">%s</textarea>'
|
|---|
| 57 | print s % self.data
|
|---|
| 58 | print '<input type="hidden" name="cmd" value="create">'
|
|---|
| 59 | print '<input type="hidden" name="page" value="%s">' % self.name
|
|---|
| 60 | print '<br>'
|
|---|
| 61 | print '<input type="submit" value="%s Page">' % label
|
|---|
| 62 | print "</form>"
|
|---|
| 63 |
|
|---|
| 64 | def cmd_create(self, form):
|
|---|
| 65 | self.data = form.getvalue("text", "").strip()
|
|---|
| 66 | error = self.store()
|
|---|
| 67 | if error:
|
|---|
| 68 | print "<h1>I'm sorry. That didn't work</h1>"
|
|---|
| 69 | print "<p>An error occurred while attempting to write the file:"
|
|---|
| 70 | print "<p>", escape(error)
|
|---|
| 71 | else:
|
|---|
| 72 | # Use a redirect directive, to avoid "reload page" problems
|
|---|
| 73 | print "<head>"
|
|---|
| 74 | s = '<meta http-equiv="refresh" content="1; URL=%s">'
|
|---|
| 75 | print s % (self.scripturl + "?cmd=view&page=" + self.name)
|
|---|
| 76 | print "<head>"
|
|---|
| 77 | print "<h1>OK</h1>"
|
|---|
| 78 | print "<p>If nothing happens, please click here:",
|
|---|
| 79 | print self.mklink("view", self.name, self.name)
|
|---|
| 80 |
|
|---|
| 81 | def cmd_new(self, form):
|
|---|
| 82 | self.cmd_edit(form, label="Create")
|
|---|
| 83 |
|
|---|
| 84 | def iswikiword(self, word):
|
|---|
| 85 | return re.match("[A-Z][a-z]+([A-Z][a-z]*)+", word)
|
|---|
| 86 |
|
|---|
| 87 | def splitwikiword(self, word):
|
|---|
| 88 | chars = []
|
|---|
| 89 | for c in word:
|
|---|
| 90 | if chars and c.isupper():
|
|---|
| 91 | chars.append(' ')
|
|---|
| 92 | chars.append(c)
|
|---|
| 93 | return "".join(chars)
|
|---|
| 94 |
|
|---|
| 95 | def mkfile(self, name=None):
|
|---|
| 96 | if name is None:
|
|---|
| 97 | name = self.name
|
|---|
| 98 | return os.path.join(self.homedir, name + ".txt")
|
|---|
| 99 |
|
|---|
| 100 | def mklink(self, cmd, page, text):
|
|---|
| 101 | link = self.scripturl + "?cmd=" + cmd + "&page=" + page
|
|---|
| 102 | return '<a href="%s">%s</a>' % (link, text)
|
|---|
| 103 |
|
|---|
| 104 | def load(self):
|
|---|
| 105 | try:
|
|---|
| 106 | f = open(self.mkfile())
|
|---|
| 107 | data = f.read().strip()
|
|---|
| 108 | f.close()
|
|---|
| 109 | except IOError:
|
|---|
| 110 | data = ""
|
|---|
| 111 | self.data = data
|
|---|
| 112 |
|
|---|
| 113 | def store(self):
|
|---|
| 114 | data = self.data
|
|---|
| 115 | try:
|
|---|
| 116 | f = open(self.mkfile(), "w")
|
|---|
| 117 | f.write(data)
|
|---|
| 118 | if data and not data.endswith('\n'):
|
|---|
| 119 | f.write('\n')
|
|---|
| 120 | f.close()
|
|---|
| 121 | return ""
|
|---|
| 122 | except IOError, err:
|
|---|
| 123 | return "IOError: %s" % str(err)
|
|---|