2002-10-17 12:53:02 -03:00
|
|
|
"""Wiki main program. Imported and run by cgi3.py."""
|
|
|
|
|
2002-10-17 13:26:45 -03:00
|
|
|
import os, re, cgi, sys, tempfile
|
2002-10-17 12:53:02 -03:00
|
|
|
escape = cgi.escape
|
|
|
|
|
|
|
|
def main():
|
|
|
|
form = cgi.FieldStorage()
|
2007-07-17 17:59:35 -03:00
|
|
|
print("Content-type: text/html")
|
|
|
|
print()
|
2002-10-17 12:53:02 -03:00
|
|
|
cmd = form.getvalue("cmd", "view")
|
|
|
|
page = form.getvalue("page", "FrontPage")
|
|
|
|
wiki = WikiPage(page)
|
|
|
|
method = getattr(wiki, 'cmd_' + cmd, None) or wiki.cmd_view
|
|
|
|
method(form)
|
|
|
|
|
|
|
|
class WikiPage:
|
|
|
|
|
2002-10-17 13:26:45 -03:00
|
|
|
homedir = tempfile.gettempdir()
|
2002-10-17 12:53:02 -03:00
|
|
|
scripturl = os.path.basename(sys.argv[0])
|
|
|
|
|
|
|
|
def __init__(self, name):
|
|
|
|
if not self.iswikiword(name):
|
2007-07-17 17:59:35 -03:00
|
|
|
raise ValueError("page name is not a wiki word")
|
2002-10-17 12:53:02 -03:00
|
|
|
self.name = name
|
|
|
|
self.load()
|
|
|
|
|
|
|
|
def cmd_view(self, form):
|
2007-07-17 17:59:35 -03:00
|
|
|
print("<h1>", escape(self.splitwikiword(self.name)), "</h1>")
|
|
|
|
print("<p>")
|
2002-10-17 12:53:02 -03:00
|
|
|
for line in self.data.splitlines():
|
|
|
|
line = line.rstrip()
|
|
|
|
if not line:
|
2007-07-17 17:59:35 -03:00
|
|
|
print("<p>")
|
2002-10-17 18:43:47 -03:00
|
|
|
else:
|
2007-07-17 17:59:35 -03:00
|
|
|
print(self.formatline(line))
|
|
|
|
print("<hr>")
|
|
|
|
print("<p>", self.mklink("edit", self.name, "Edit this page") + ";")
|
|
|
|
print(self.mklink("view", "FrontPage", "go to front page") + ".")
|
2002-10-17 12:53:02 -03:00
|
|
|
|
2002-10-17 18:43:47 -03:00
|
|
|
def formatline(self, line):
|
|
|
|
words = []
|
|
|
|
for word in re.split('(\W+)', line):
|
|
|
|
if self.iswikiword(word):
|
|
|
|
if os.path.isfile(self.mkfile(word)):
|
|
|
|
word = self.mklink("view", word, word)
|
|
|
|
else:
|
|
|
|
word = self.mklink("new", word, word + "*")
|
|
|
|
else:
|
|
|
|
word = escape(word)
|
|
|
|
words.append(word)
|
|
|
|
return "".join(words)
|
|
|
|
|
2002-10-17 12:53:02 -03:00
|
|
|
def cmd_edit(self, form, label="Change"):
|
2007-07-17 17:59:35 -03:00
|
|
|
print("<h1>", label, self.name, "</h1>")
|
|
|
|
print('<form method="POST" action="%s">' % self.scripturl)
|
2002-10-17 12:53:02 -03:00
|
|
|
s = '<textarea cols="70" rows="20" name="text">%s</textarea>'
|
2007-07-17 17:59:35 -03:00
|
|
|
print(s % self.data)
|
|
|
|
print('<input type="hidden" name="cmd" value="create">')
|
|
|
|
print('<input type="hidden" name="page" value="%s">' % self.name)
|
|
|
|
print('<br>')
|
|
|
|
print('<input type="submit" value="%s Page">' % label)
|
|
|
|
print("</form>")
|
2002-10-17 12:53:02 -03:00
|
|
|
|
|
|
|
def cmd_create(self, form):
|
|
|
|
self.data = form.getvalue("text", "").strip()
|
|
|
|
error = self.store()
|
|
|
|
if error:
|
2007-07-17 17:59:35 -03:00
|
|
|
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))
|
2002-10-17 12:53:02 -03:00
|
|
|
else:
|
2002-10-17 18:41:42 -03:00
|
|
|
# Use a redirect directive, to avoid "reload page" problems
|
2007-07-17 17:59:35 -03:00
|
|
|
print("<head>")
|
2002-10-17 18:41:42 -03:00
|
|
|
s = '<meta http-equiv="refresh" content="1; URL=%s">'
|
2007-07-17 17:59:35 -03:00
|
|
|
print(s % (self.scripturl + "?cmd=view&page=" + self.name))
|
|
|
|
print("<head>")
|
|
|
|
print("<h1>OK</h1>")
|
|
|
|
print("<p>If nothing happens, please click here:", end=' ')
|
|
|
|
print(self.mklink("view", self.name, self.name))
|
2002-10-17 12:53:02 -03:00
|
|
|
|
|
|
|
def cmd_new(self, form):
|
2002-10-17 18:41:42 -03:00
|
|
|
self.cmd_edit(form, label="Create")
|
2002-10-17 12:53:02 -03:00
|
|
|
|
|
|
|
def iswikiword(self, word):
|
|
|
|
return re.match("[A-Z][a-z]+([A-Z][a-z]*)+", word)
|
|
|
|
|
|
|
|
def splitwikiword(self, word):
|
|
|
|
chars = []
|
|
|
|
for c in word:
|
|
|
|
if chars and c.isupper():
|
|
|
|
chars.append(' ')
|
|
|
|
chars.append(c)
|
|
|
|
return "".join(chars)
|
|
|
|
|
|
|
|
def mkfile(self, name=None):
|
|
|
|
if name is None:
|
|
|
|
name = self.name
|
|
|
|
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)
|
|
|
|
|
|
|
|
def load(self):
|
|
|
|
try:
|
|
|
|
f = open(self.mkfile())
|
|
|
|
data = f.read().strip()
|
|
|
|
f.close()
|
|
|
|
except IOError:
|
|
|
|
data = ""
|
|
|
|
self.data = data
|
|
|
|
|
|
|
|
def store(self):
|
|
|
|
data = self.data
|
|
|
|
try:
|
|
|
|
f = open(self.mkfile(), "w")
|
|
|
|
f.write(data)
|
|
|
|
if data and not data.endswith('\n'):
|
|
|
|
f.write('\n')
|
|
|
|
f.close()
|
|
|
|
return ""
|
2007-01-10 12:19:56 -04:00
|
|
|
except IOError as err:
|
2002-10-17 12:53:02 -03:00
|
|
|
return "IOError: %s" % str(err)
|