/Demo/cgi/wiki.py

http://unladen-swallow.googlecode.com/ · Python · 123 lines · 105 code · 16 blank · 2 comment · 22 complexity · bc14b50c72536292b5f4068537a7645a MD5 · raw file

  1. """Wiki main program. Imported and run by cgi3.py."""
  2. import os, re, cgi, sys, tempfile
  3. escape = cgi.escape
  4. def main():
  5. form = cgi.FieldStorage()
  6. print "Content-type: text/html"
  7. print
  8. cmd = form.getvalue("cmd", "view")
  9. page = form.getvalue("page", "FrontPage")
  10. wiki = WikiPage(page)
  11. method = getattr(wiki, 'cmd_' + cmd, None) or wiki.cmd_view
  12. method(form)
  13. class WikiPage:
  14. homedir = tempfile.gettempdir()
  15. scripturl = os.path.basename(sys.argv[0])
  16. def __init__(self, name):
  17. if not self.iswikiword(name):
  18. raise ValueError, "page name is not a wiki word"
  19. self.name = name
  20. self.load()
  21. def cmd_view(self, form):
  22. print "<h1>", escape(self.splitwikiword(self.name)), "</h1>"
  23. print "<p>"
  24. for line in self.data.splitlines():
  25. line = line.rstrip()
  26. if not line:
  27. print "<p>"
  28. else:
  29. print self.formatline(line)
  30. print "<hr>"
  31. print "<p>", self.mklink("edit", self.name, "Edit this page") + ";"
  32. print self.mklink("view", "FrontPage", "go to front page") + "."
  33. def formatline(self, line):
  34. words = []
  35. for word in re.split('(\W+)', line):
  36. if self.iswikiword(word):
  37. if os.path.isfile(self.mkfile(word)):
  38. word = self.mklink("view", word, word)
  39. else:
  40. word = self.mklink("new", word, word + "*")
  41. else:
  42. word = escape(word)
  43. words.append(word)
  44. return "".join(words)
  45. def cmd_edit(self, form, label="Change"):
  46. print "<h1>", label, self.name, "</h1>"
  47. print '<form method="POST" action="%s">' % self.scripturl
  48. s = '<textarea cols="70" rows="20" name="text">%s</textarea>'
  49. print s % self.data
  50. print '<input type="hidden" name="cmd" value="create">'
  51. print '<input type="hidden" name="page" value="%s">' % self.name
  52. print '<br>'
  53. print '<input type="submit" value="%s Page">' % label
  54. print "</form>"
  55. def cmd_create(self, form):
  56. self.data = form.getvalue("text", "").strip()
  57. error = self.store()
  58. if error:
  59. print "<h1>I'm sorry. That didn't work</h1>"
  60. print "<p>An error occurred while attempting to write the file:"
  61. print "<p>", escape(error)
  62. else:
  63. # Use a redirect directive, to avoid "reload page" problems
  64. print "<head>"
  65. s = '<meta http-equiv="refresh" content="1; URL=%s">'
  66. print s % (self.scripturl + "?cmd=view&page=" + self.name)
  67. print "<head>"
  68. print "<h1>OK</h1>"
  69. print "<p>If nothing happens, please click here:",
  70. print self.mklink("view", self.name, self.name)
  71. def cmd_new(self, form):
  72. self.cmd_edit(form, label="Create")
  73. def iswikiword(self, word):
  74. return re.match("[A-Z][a-z]+([A-Z][a-z]*)+", word)
  75. def splitwikiword(self, word):
  76. chars = []
  77. for c in word:
  78. if chars and c.isupper():
  79. chars.append(' ')
  80. chars.append(c)
  81. return "".join(chars)
  82. def mkfile(self, name=None):
  83. if name is None:
  84. name = self.name
  85. return os.path.join(self.homedir, name + ".txt")
  86. def mklink(self, cmd, page, text):
  87. link = self.scripturl + "?cmd=" + cmd + "&page=" + page
  88. return '<a href="%s">%s</a>' % (link, text)
  89. def load(self):
  90. try:
  91. f = open(self.mkfile())
  92. data = f.read().strip()
  93. f.close()
  94. except IOError:
  95. data = ""
  96. self.data = data
  97. def store(self):
  98. data = self.data
  99. try:
  100. f = open(self.mkfile(), "w")
  101. f.write(data)
  102. if data and not data.endswith('\n'):
  103. f.write('\n')
  104. f.close()
  105. return ""
  106. except IOError, err:
  107. return "IOError: %s" % str(err)