Guido van Rossum | f66cccf | 2002-10-17 15:53:02 +0000 | [diff] [blame] | 1 | """Wiki main program. Imported and run by cgi3.py.""" |
| 2 | |
Guido van Rossum | c9776bd | 2002-10-17 16:26:45 +0000 | [diff] [blame^] | 3 | import os, re, cgi, sys, tempfile |
Guido van Rossum | f66cccf | 2002-10-17 15:53:02 +0000 | [diff] [blame] | 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 | wiki.load() |
| 14 | method = getattr(wiki, 'cmd_' + cmd, None) or wiki.cmd_view |
| 15 | method(form) |
| 16 | |
| 17 | class WikiPage: |
| 18 | |
Guido van Rossum | c9776bd | 2002-10-17 16:26:45 +0000 | [diff] [blame^] | 19 | homedir = tempfile.gettempdir() |
Guido van Rossum | f66cccf | 2002-10-17 15:53:02 +0000 | [diff] [blame] | 20 | 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) |