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