## ## WebAppWiki: an example program for the webAppWorkshop list ## version: 0.1 (0518.2002) ## ## -------------------------------------------------------------------- ## Copyright (C) 2002 Sabren Enterprises, Inc ## ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, but ## WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ## General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 ## USA ## -------------------------------------------------------------------- ## configuration ################## DATADIR = "./pages" ### code starts here ############## import os.path import re import sys import zebra from sixthday import App class WikiException(Exception): pass class WikiApp(App): ## class variables ###################### # regexp to match WikiWords (stolen from PikiPiki) # note the r'' (raw string) syntax, which keeps # us from having to write \\ for the backslashes. reWikiWord = re.compile(r'^([A-Z][a-z]+){2,}$') ## constructor ########################## def __init__(self, input, datadir): super(WikiApp, self).__init__(input) self.dir = datadir ## helper methods ####################### def error(self, message): raise WikiException, message def ensurePage(self): res = self.input.get("page") if not res: self.error("no PageName provided!") if not self.reWikiWord.match(res): self.error("invalid PageName: " + res) return res def getFileName(self, page): return self.dir + "/" + page + ".txt" def getText(self, page): file = self.getFileName(page) if os.path.exists(file): res = open(file).read() else: res = "Describe %s here." % page return res # This next routine formats the page. # It's very primitive, and destructive with whitespace. # HTML should work fine, (except for PRE tags) # Still, this would be a good place to start improving! def wikify(self, text): res = "" for line in text.split("\n"): if line.strip() == "": res += "
 
" else: # line[:-1] here strips out the newline for now: for word in line[:-1].split(" "): if self.reWikiWord.match(word): res += '%s ' \ % (word, word) else: res += word + ' ' res += "\n" return res # each App has a .model - It's just a normal dictionary, # but handy for passing to template engines. # (it also usually collects error messages, but not in # this app) def prepModel(self, wikify=0): page = self.ensurePage() self.model["page"] = page if wikify: self.model["text"] = self.wikify(self.getText(page)) else: self.model["text"] = self.getText(page) return self.model ## actions ############################## # These methods are special. Apps look for # an "action" argument in the query string or # form data, and invoke the appropriate method. # # eg, wiki.app?action=show calls act_show() # # act_() is the default. def act_(self): self.input.setdefault("page","FrontPage") self.act_show() def act_show(self): print >> self, zebra.fetch("sho_page", self.prepModel(wikify=1)) def act_edit(self): print >> self, zebra.fetch("sho_form", self.prepModel()) def act_save(self): page = self.ensurePage() open(self.getFileName(page), "w").write(self.input["text"]) self.redirect("wiki.app?action=show&page=%s" % page) if __name__=="__main__": try: print >> RES, WikiApp(REQ, DATADIR).act() except WikiException, e: # we do this to hide the detailed stack trace # that weblib normally provides: print >> RES, "Error:", str(e)