"""Wiki main program. Imported and run by cgi3.py."""
import os, re, cgi, sys, tempfile
form = cgi.FieldStorage()
print "Content-type: text/html"
cmd = form.getvalue("cmd", "view")
page = form.getvalue("page", "FrontPage")
method = getattr(wiki, 'cmd_' + cmd, None) or wiki.cmd_view
homedir = tempfile.gettempdir()
scripturl = os.path.basename(sys.argv[0])
def __init__(self, name):
if not self.iswikiword(name):
raise ValueError, "page name is not a wiki word"
def cmd_view(self, form):
print "<h1>", escape(self.splitwikiword(self.name)), "</h1>"
for line in self.data.splitlines():
print self.formatline(line)
print "<p>", self.mklink("edit", self.name, "Edit this page") + ";"
print self.mklink("view", "FrontPage", "go to front page") + "."
def formatline(self, line):
for word in re.split('(\W+)', line):
if self.iswikiword(word):
if os.path.isfile(self.mkfile(word)):
word = self.mklink("view", word, word)
word = self.mklink("new", word, word + "*")
def cmd_edit(self, form, label="Change"):
print "<h1>", label, self.name, "</h1>"
print '<form method="POST" action="%s">' % self.scripturl
s = '<textarea cols="70" rows="20" name="text">%s</textarea>'
print '<input type="hidden" name="cmd" value="create">'
print '<input type="hidden" name="page" value="%s">' % self.name
print '<input type="submit" value="%s Page">' % label
def cmd_create(self, form):
self.data = form.getvalue("text", "").strip()
print "<h1>I'm sorry. That didn't work</h1>"
print "<p>An error occurred while attempting to write the file:"
print "<p>", escape(error)
# Use a redirect directive, to avoid "reload page" problems
s = '<meta http-equiv="refresh" content="1; URL=%s">'
print s % (self.scripturl + "?cmd=view&page=" + self.name)
print "<p>If nothing happens, please click here:",
print self.mklink("view", self.name, self.name)
self.cmd_edit(form, label="Create")
def iswikiword(self, word):
return re.match("[A-Z][a-z]+([A-Z][a-z]*)+", word)
def splitwikiword(self, word):
if chars and c.isupper():
def mkfile(self, name=None):
return os.path.join(self.homedir, name + ".txt")
def mklink(self, cmd, page, text):
link = self.scripturl + "?cmd=" + cmd + "&page=" + page
return '<a href="%s">%s</a>' % (link, text)
f = open(self.mkfile(), "w")
if data and not data.endswith('\n'):
return "IOError: %s" % str(err)