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