summaryrefslogtreecommitdiffstats
path: root/Demo/cgi/cgi3.py
blob: bdb2cb7aa2a31a483a81218b3b97fe8512287030 (plain)
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
#!/usr/local/bin/python

"""CGI test 3 (persistent data)."""

import cgitb; cgitb.enable()

import os, re, cgi, sys
escape = cgi.escape

def main():
    form = cgi.FieldStorage()
    print "Content-type: text/html"
    print
    cmd = form.getvalue("cmd") or "view"
    page = form.getvalue("page") or "FrontPage"
    wiki = WikiPage(page)
    wiki.load()
    method = getattr(wiki, 'cmd_' + cmd, None) or wiki.cmd_view
    method(form)

class WikiPage:

    homedir = os.path.dirname(sys.argv[0])
    scripturl = os.path.basename(sys.argv[0])

    def __init__(self, name):
        self.name = name
        self.load()

    def cmd_view(self, form):
        print "<h1>", escape(self.splitwikiword(self.name)), "</h1>"
        print "<p>"
        for line in self.data.splitlines():
            line = line.rstrip()
            if not line:
                print "<p>"
                continue
            words = re.split('(\W+)', line)
            for i in range(len(words)):
                word = words[i]
                if self.iswikiword(word):
                    if os.path.isfile(self.mkfile(word)):
                        word = self.mklink("view", word, word)
                    else:
                        word = self.mklink("new", word, word + "*")
                else:
                    word = escape(word)
                words[i] = word
            print "".join(words)
        print "<hr>"
        print "<p>", self.mklink("edit", self.name, "Edit this page") + ","
        print self.mklink("view", "FrontPage", "go to front page") + "."

    def cmd_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>'
        print s % 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>"

    def cmd_create(self, form):
        self.data = form.getvalue("text", "").strip()
        self.store()
        self.cmd_view(form)

    def cmd_new(self, form):
        self.cmd_edit(form, label="Create Page")

    def iswikiword(self, word):
        return re.match("[A-Z][a-z]+([A-Z][a-z]*)+", word)

    def splitwikiword(self, word):
        chars = []
        for c in word:
            if chars and c.isupper():
                chars.append(' ')
            chars.append(c)
        return "".join(chars)

    def mkfile(self, name=None):
        if name is None:
            name = self.name
        return os.path.join(self.homedir, name)

    def mklink(self, cmd, page, text):
        link = self.scripturl + "?cmd=" + cmd + "&page=" + page
        return '<a href="%s">%s</a>' % (link, text)

    def load(self):
        try:
            f = open(self.mkfile())
            data = f.read().strip()
            f.close()
        except IOError:
            data = ""
        self.data = data

    def store(self):
        data = self.data
        try:
            f = open(self.mkfile(), "w")
            f.write(data)
            f.close()
            return ""
        except IOError, err:
            return "IOError: %s" % str(err)

if __name__ == "__main__":
    main()