1 """Wiki main program. Imported and run by cgi3.py."""
3 import os
, re
, cgi
, sys
, tempfile
7 form
= cgi
.FieldStorage()
8 print "Content-type: text/html"
10 cmd
= form
.getvalue("cmd", "view")
11 page
= form
.getvalue("page", "FrontPage")
13 method
= getattr(wiki
, 'cmd_' + cmd
, None) or wiki
.cmd_view
18 homedir
= tempfile
.gettempdir()
19 scripturl
= os
.path
.basename(sys
.argv
[0])
21 def __init__(self
, name
):
22 if not self
.iswikiword(name
):
23 raise ValueError, "page name is not a wiki word"
27 def cmd_view(self
, form
):
28 print "<h1>", escape(self
.splitwikiword(self
.name
)), "</h1>"
30 for line
in self
.data
.splitlines():
35 print self
.formatline(line
)
37 print "<p>", self
.mklink("edit", self
.name
, "Edit this page") + ";"
38 print self
.mklink("view", "FrontPage", "go to front page") + "."
40 def formatline(self
, line
):
42 for word
in re
.split('(\W+)', line
):
43 if self
.iswikiword(word
):
44 if os
.path
.isfile(self
.mkfile(word
)):
45 word
= self
.mklink("view", word
, word
)
47 word
= self
.mklink("new", word
, word
+ "*")
53 def cmd_edit(self
, form
, label
="Change"):
54 print "<h1>", label
, self
.name
, "</h1>"
55 print '<form method="POST" action="%s">' % self
.scripturl
56 s
= '<textarea cols="70" rows="20" name="text">%s</textarea>'
58 print '<input type="hidden" name="cmd" value="create">'
59 print '<input type="hidden" name="page" value="%s">' % self
.name
61 print '<input type="submit" value="%s Page">' % label
64 def cmd_create(self
, form
):
65 self
.data
= form
.getvalue("text", "").strip()
68 print "<h1>I'm sorry. That didn't work</h1>"
69 print "<p>An error occurred while attempting to write the file:"
70 print "<p>", escape(error
)
72 # Use a redirect directive, to avoid "reload page" problems
74 s
= '<meta http-equiv="refresh" content="1; URL=%s">'
75 print s
% (self
.scripturl
+ "?cmd=view&page=" + self
.name
)
78 print "<p>If nothing happens, please click here:",
79 print self
.mklink("view", self
.name
, self
.name
)
81 def cmd_new(self
, form
):
82 self
.cmd_edit(form
, label
="Create")
84 def iswikiword(self
, word
):
85 return re
.match("[A-Z][a-z]+([A-Z][a-z]*)+", word
)
87 def splitwikiword(self
, word
):
90 if chars
and c
.isupper():
95 def mkfile(self
, name
=None):
98 return os
.path
.join(self
.homedir
, name
+ ".txt")
100 def mklink(self
, cmd
, page
, text
):
101 link
= self
.scripturl
+ "?cmd=" + cmd
+ "&page=" + page
102 return '<a href="%s">%s</a>' % (link
, text
)
106 f
= open(self
.mkfile())
107 data
= f
.read().strip()
116 f
= open(self
.mkfile(), "w")
118 if data
and not data
.endswith('\n'):
123 return "IOError: %s" % str(err
)