#!/usr/bin/python """A Little Wiki Script Thing.""" import sys, re, os, cgi def quoteHTML(s): s = s.replace('&', '&') s = s.replace('<', '<') return s r_wikiname = re.compile(r'(^|[^A-Za-z0-9?])(([A-Z][a-z]+){2,})') def wikiLinkify(s): def htmlify(m): return '%s%s' % (m.group(1), m.group(2), m.group(2)) return r_wikiname.sub(htmlify, s) def wikiParse(s): s = quoteHTML(s) # must quote HTML first... return wikiLinkify(s) def get(wn): if os.path.exists(wn + '.txt'): s = wikiParse(open(wn + '.txt').read()) return s + '
(edit me)
' % wn else: return 'Create me' % wn def edit(wn): if os.path.exists(wn + '.txt'): s = quoteHTML(open(wn + '.txt').read()) else: s = '' return ''' ''' % (wn, s) def post(wn): form = cgi.parse_qs(sys.stdin.read()) form = dict([(item[0], ''.join(item[1])) for item in form.items()]) s = form.get('text') or '' open('%s.txt' % wn, 'w').write(s) return 'Wrote %s bytes to %s' % (len(s), wn, wn) def main(env=None): if env is None: env = os.environ method = env.get('REQUEST_METHOD') if method not in ('GET', 'POST'): raise "UnsupportedMethodError", "Unsupported method: %s" % method wn = env.get('PATH_INFO') or 'HomePage' wn = wn.split('/').pop() if wn.endswith('.edit'): action = 'edit' wn = wn[:-5] elif wn.endswith('.txt'): action = 'get' wn = wn[:-4] # @@ disallow using the .txt files? unless POSTing? # but then that messes up making new files. argh! else: action = 'get' if not re.compile(r'^[A-Za-z0-9-]+$').match(wn): raise "ScriptError", "Invalid filename: %s" % wn print "Content-Type: text/html; charset=utf-8" print if method == 'POST': print post(wn) elif action == 'edit': print edit(wn) else: print get(wn) if __name__=="__main__": try: main() except: cgi.print_exception()