""" tw.cgi A Python-based backend for TiddlyWiki. """ __author__ = "Joseph Reisinger (joeraii@cs.utexas.edu)" __copyright__ = "Copyright 2005, Joseph Reisinger" __contributors__ = [ 'Andrew Nelis (andrew.nelis@gmail.com)', 'Devon Jones (soulcatcher@evilsoft.org)', 'Rodney Gomes (rodneygomes@gmail.com)'] __version__ = "1.1.0.1 $Rev:$" __license__ = "GPL" __history__ = """ Version 1.1.0.1: - Cleaned up revisions handling (Rodney) - Fix a newline bug with IE (Devon) - Sync to TW 1.2.38 Version 1.1: - Architecture change: added a type to each tiddler to track what importer handles it. (Joe) - Cleaned up/optimized refresh.js. (Joe) - Bugfix: Fixed a small newline bug (Joe) and a similar bug with \s (Devon) - Bugfix: disabled "save changes" (Devon) - Added a mysql database backend (Devon) - Added a set of plugins for displaying revisions (Rodney) See README for older versions. """ import os, sys, time ############################################################################### # Configuration and Global Parameters ############################################################################### # The base html file from http://www.tiddlywiki.com (sans the last few lines) TWBase = 'tiddlywiki-1.2.38.html' # Javascript plugins. remote.js is mandatory for pytw to work, but can be # removed to disable serverside functionality cleanly. TWPlugins = ['remote.js', 'refresh.js', 'revisions.js'] # Importers for turning other kinds of files into tiddlers. pytw uses pickle # Static importers are cached on write, while Dynamic importers are not. StaticContent = ['pickleimporter', 'lockimporter'] DynamicContent = ['pdfimporter'] # Handlers for cgi requests. Format is CGIFormAttribute:python module . Handlers = {'save':'save', 'delete':'delete', 'refresh':'refresh', 'revisions':'revisions'} # Where the html div cache is stored. CachePath = 'store/tiddler_divs_html.cache' # Used for timestamping on save. Best not to change this. CurrentTime = time.strftime('%Y%m%d%H%M') ############################################################################### # Code ############################################################################### def escape_html(text): """Does the same as cgi.escape but doesn't require the cgi module to be loaded.""" return text.replace('&', '&').replace('<', '<').replace('>', '>') def extract( form, name ): """ Safe Extraction from a CGI form. All unicode encoding is done here. """ return unicode( form.getvalue(name, '').replace(r'\s', '\ss').replace(r'\n','\sn').replace('\n','\\n'), 'utf-8' ).encode('utf-8') def form_to_tiddler( form ): """ Set up the Tiddler content variables. Not Pythonic, I'm sure. """ return {'title': extract( form, 'title' ), 'oldtitle': extract( form, 'oldtitle' ), 'body': extract( form, 'body' ), 'author': extract( form, 'author' ), 'tags': extract( form, 'tags' ), 'modified': CurrentTime} def tiddler_to_markup( content, type='undefined' ): """ Returns the
for the tiddler. """ fields = tuple(map(escape_html, (content['title'], content['modified'], content['author'], content['tags'], type, content['body'].replace('\n','\\n')))) return '