- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy pathwiki.py
123 lines (107 loc) · 3.94 KB
/
wiki.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
"""Wiki main program. Imported and run by cgi3.py."""
importos, re, cgi, sys, tempfile
escape=cgi.escape
defmain():
form=cgi.FieldStorage()
print"Content-type: text/html"
print
cmd=form.getvalue("cmd", "view")
page=form.getvalue("page", "FrontPage")
wiki=WikiPage(page)
method=getattr(wiki, 'cmd_'+cmd, None) orwiki.cmd_view
method(form)
classWikiPage:
homedir=tempfile.gettempdir()
scripturl=os.path.basename(sys.argv[0])
def__init__(self, name):
ifnotself.iswikiword(name):
raiseValueError, "page name is not a wiki word"
self.name=name
self.load()
defcmd_view(self, form):
print"<h1>", escape(self.splitwikiword(self.name)), "</h1>"
print"<p>"
forlineinself.data.splitlines():
line=line.rstrip()
ifnotline:
print"<p>"
else:
printself.formatline(line)
print"<hr>"
print"<p>", self.mklink("edit", self.name, "Edit this page") +";"
printself.mklink("view", "FrontPage", "go to front page") +"."
defformatline(self, line):
words= []
forwordinre.split('(\W+)', line):
ifself.iswikiword(word):
ifos.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)
defcmd_edit(self, form, label="Change"):
print"<h1>", label, self.name, "</h1>"
print'<form method="POST" action="%s">'%self.scripturl
s='<textarea cols="70" rows="20" name="text">%s</textarea>'
prints%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>"
defcmd_create(self, form):
self.data=form.getvalue("text", "").strip()
error=self.store()
iferror:
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)
else:
# Use a redirect directive, to avoid "reload page" problems
print"<head>"
s='<meta http-equiv="refresh" content="1; URL=%s">'
prints% (self.scripturl+"?cmd=view&page="+self.name)
print"<head>"
print"<h1>OK</h1>"
print"<p>If nothing happens, please click here:",
printself.mklink("view", self.name, self.name)
defcmd_new(self, form):
self.cmd_edit(form, label="Create")
defiswikiword(self, word):
returnre.match("[A-Z][a-z]+([A-Z][a-z]*)+", word)
defsplitwikiword(self, word):
chars= []
forcinword:
ifcharsandc.isupper():
chars.append(' ')
chars.append(c)
return"".join(chars)
defmkfile(self, name=None):
ifnameisNone:
name=self.name
returnos.path.join(self.homedir, name+".txt")
defmklink(self, cmd, page, text):
link=self.scripturl+"?cmd="+cmd+"&page="+page
return'<a href="%s">%s</a>'% (link, text)
defload(self):
try:
f=open(self.mkfile())
data=f.read().strip()
f.close()
exceptIOError:
data=""
self.data=data
defstore(self):
data=self.data
try:
f=open(self.mkfile(), "w")
f.write(data)
ifdataandnotdata.endswith('\n'):
f.write('\n')
f.close()
return""
exceptIOError, err:
return"IOError: %s"%str(err)