*** pwyky.py.txt Wed Apr 28 23:00:00 2004 --- index.cgi Mon Sep 14 18:30:22 2009 *************** *** 1,4 **** ! #!/usr/bin/python """ pwyky - A Simple Wiki in Python. --- 1,4 ---- ! #!/usr/local/bin/python """ pwyky - A Simple Wiki in Python. *************** *** 11,18 **** from cStringIO import StringIO from HTMLParser import HTMLParser config = {} ! configvars = ('default', 'sname', 'lname', 'pedit', 'owner', 'logdir') r_meta = re.compile('(?s)([^\n:]+): (.*?)(?=\n[^ \t]|\Z)') if os.path.exists('config.txt'): --- 11,21 ---- from cStringIO import StringIO from HTMLParser import HTMLParser + os.environ['TZ'] = 'Europe/Oslo' + time.tzset() + config = {} ! configvars = ('default', 'sname', 'lname', 'pedit', 'owner', 'logdir', 'usegit') r_meta = re.compile('(?s)([^\n:]+): (.*?)(?=\n[^ \t]|\Z)') if os.path.exists('config.txt'): *************** *** 29,34 **** --- 32,38 ---- pedit = config.get('pedit', True) owner = config.get('owner', '%s owner' % os.environ.get('SERVER_NAME', 'Site')) logdir = config.get('logdir', False) + usegit = config.get('usegit', False) nedit = 'edit' profile = 'http://infomesh.net/pwyky/profile#' *************** *** 60,65 **** --- 64,91 ---- tdDDtRTE/P/e4l/8SPME""")) f.close() + # support for git + def rungit(*args): + os.spawnlp(os.P_WAIT, 'git', 'git', *args) + if usegit and not os.path.exists('@raw'): + os.mkdir('@raw') + if usegit and not os.path.exists('.git'): + rungit('init', '-q') + rungit('add', '@raw') + old = glob.glob('@raw/*') + if old: + rungit('add', *old) + rungit('commit', '-q', '-m', 'initial import of existing files', *old) + + # extract #id from string + def getid(s): + if s[0] == '#': + if s.find(' ') > 0: (id, s) = s[1:].split(' ', 1) + else: id = s[1:] + cid = ' id="%s"' % id + else: cid = '' + return (cid, s) + class Parser(object): EOF = 0 *************** *** 123,134 **** r_emdash = re.compile(r'[A-Za-z0-9"]--(?=[A-Za-z0-9"{])') r_alpha = re.compile(r'[A-Za-z]+') - def makeID(s, current): - s = (''.join(r_alpha.findall(s)) or 'id') + str(len(s)) - while s in current: - s += 'n' - return s - class TextParser(Parser): LIST = 0 HEADING = 1 --- 149,154 ---- *************** *** 152,158 **** exists = lambda: True self.exists = exists self.rawlinks = [] ! self.ids = [] def __call__(self, s): self.text = s --- 172,178 ---- exists = lambda: True self.exists = exists self.rawlinks = [] ! self.imgfloat = 'left' def __call__(self, s): self.text = s *************** *** 225,235 **** def headingElement(self, content): content = self.wikiParse(content) ! newid = makeID(content, self.ids) ! self.ids.append(newid) ! ! self.write('

' % newid) ! self.write(content) self.write('

') self.write('\n') --- 245,253 ---- def headingElement(self, content): content = self.wikiParse(content) ! x = getid(content) ! self.write('' % x[0]) ! self.write(x[1]) self.write('') self.write('\n') *************** *** 331,348 **** if s.startswith('U+'): try: result = self.unicodeify(s[2:]) except ValueError: result = cgi.escape('{%s}' % s) ! elif s == '$timenow': ! result = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()) elif s == '$datenow': ! result = time.strftime('%Y-%m-%d', time.gmtime()) elif level < 1: result = '{' + self.wikiParse('%s}' % s) elif s.startswith('* '): ! result = '%s' % s[2:] elif s.startswith('#'): i = s.find(' ') href, title = s[:i], s[i+1:] result = '%s' % (href, title) elif not re.compile(r'[A-Za-z0-9_.-]').match(s): result = cgi.escape('{%s}' % s) else: --- 349,387 ---- if s.startswith('U+'): try: result = self.unicodeify(s[2:]) except ValueError: result = cgi.escape('{%s}' % s) ! elif s == '$datetimenow': ! result = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime()) elif s == '$datenow': ! result = time.strftime('%Y-%m-%d', time.localtime()) ! elif s == '$timenow': ! result = time.strftime('%H:%M:%S', time.localtime()) elif level < 1: result = '{' + self.wikiParse('%s}' % s) elif s.startswith('* '): ! x = getid(s[2:]) ! result = '%s' % x ! elif s.startswith('/ '): ! x = getid(s[2:]) ! result = '%s' % x ! elif s.startswith('= '): ! result = '%s' % s[2:] ! elif s.startswith('- '): ! result = '%s' % s[2:] elif s.startswith('#'): i = s.find(' ') href, title = s[:i], s[i+1:] result = '%s' % (href, title) + elif s.startswith('! '): + s = s[2:].strip() + i = s.find(' ') + if i > 0: + src, alt= s[:i], s[i+1:] + else: + src, alt = s, '' + result = '%s' % \ + (self.imgfloat, src, alt) + if self.imgfloat == 'left': self.imgfloat = 'right' + else: self.imgfloat = 'left' elif not re.compile(r'[A-Za-z0-9_.-]').match(s): result = cgi.escape('{%s}' % s) else: *************** *** 381,386 **** --- 420,426 ---- self.block = False self.blockquote = False self.anchor = False + self.image = False self.current = None def handle_starttag(self, tag, attrs): *************** *** 403,408 **** --- 443,450 ---- self.write('* ') elif tag in ('h1', 'h2'): self.write('@ ') + id = attrs.get('id', '') + if tag != 'h1' and id: self.write('#' + id + ' ') elif tag == 'pre' and not self.blockquote: self.write('{{{') elif tag == 'blockquote': *************** *** 410,418 **** --- 452,472 ---- self.write('[[[') elif tag == 'strong': self.write('{* ') + id = attrs.get('id', '') + if id: self.write('#' + id + ' ') + elif tag == 'em': + self.write('{/ ') + id = attrs.get('id', '') + if id: self.write('#' + id + ' ') + elif tag == 'tt': + self.write('{= ') + elif tag == 'strike': + self.write('{- ') elif tag == 'a': self.anchor = attrs self.anchor['_'] = '' + elif tag == 'img': + self.image = attrs def handle_endtag(self, tag): self.current = None *************** *** 447,453 **** if not dual: self.write('{%s}' % title) else: self.write('{%s %s}' % (uri, title)) self.anchor = False ! elif tag == 'strong': self.write('}') elif tag == 'div': self.content = False --- 501,512 ---- if not dual: self.write('{%s}' % title) else: self.write('{%s %s}' % (uri, title)) self.anchor = False ! elif tag == 'img': ! attrs = self.image ! uri, title = attrs.get('src', ''), attrs.get('alt', '') ! self.write('{! %s %s}' % (uri, title)) ! self.image = False ! elif tag in ['strong', 'em', 'tt', 'strike']: self.write('}') elif tag == 'div': self.content = False *************** *** 501,509 **** return [fn[len(wn)+10:] for fn in glob.glob('./@links/%s%%*' % wn)] def html(title, body): ! s = '\n\n' s += '\n' s += '\n' % profile s += '%s\n' % title if '/' in os.environ.get('REQUEST_URI')[len(base)+1:]: # heh s += '\n' --- 560,570 ---- return [fn[len(wn)+10:] for fn in glob.glob('./@links/%s%%*' % wn)] def html(title, body): ! s = '\n' s += '\n' s += '\n' % profile + s += '\n' s += '%s\n' % title if '/' in os.environ.get('REQUEST_URI')[len(base)+1:]: # heh s += '\n' *************** *** 520,528 **** content, rawlinks = wikiParse(text, getlinks=getlinks) else: content = wikiParse(text, getlinks=getlinks) ! s = '
(home |
!
!
''' % (wn, cgi.escape(s))) --- 645,654 ---- return html('Editing %s' % (wn or default), '''
!
! @logmessage:
!
''' % (wn, cgi.escape(s))) *************** *** 609,620 **** except "ParseError": content = open(wn + '.html').read() t = os.stat(wn + '.html').st_mtime ! lastmod = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(t)) s = '

About %s

\n' % (wn, wn) if os.path.exists(wn + '.prev'): pt = os.stat(wn + '.html').st_mtime ! plastmod = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(pt)) s += '

Previous ' % wn s += 'version (%s).

\n' % plastmod s += '

' --- 670,681 ---- except "ParseError": content = open(wn + '.html').read() t = os.stat(wn + '.html').st_mtime ! lastmod = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(t)) s = '

About %s

\n' % (wn, wn) if os.path.exists(wn + '.prev'): pt = os.stat(wn + '.html').st_mtime ! plastmod = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(pt)) s += '

Previous ' % wn s += 'version (%s).

\n' % plastmod s += '

' *************** *** 689,695 **** result = {} for fn in glob.glob('*.html'): t = os.stat(fn).st_mtime ! lastmod = time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(t)) while result.has_key(lastmod): lastmod += '_' result[lastmod] = fn[:-len('.html')] --- 750,756 ---- result = {} for fn in glob.glob('*.html'): t = os.stat(fn).st_mtime ! lastmod = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(t)) while result.has_key(lastmod): lastmod += '_' result[lastmod] = fn[:-len('.html')] *************** *** 698,704 **** keys.reverse() keys = keys[:i] ! today = time.strftime('%Y-%m-%d', time.gmtime()) s = '

Updates: Recently Changed Pages

\n' s += '

Today is %s. ' % today s += 'The %s most recent changes in this site are:

\n' % len(keys) --- 759,765 ---- keys.reverse() keys = keys[:i] ! today = time.strftime('%Y-%m-%d', time.localtime()) s = '

Updates: Recently Changed Pages

\n' s += '

Today is %s. ' % today s += 'The %s most recent changes in this site are:

\n' % len(keys) *************** *** 887,897 **** --- 948,969 ---- form = cgi.parse_qs(sys.stdin.read()) form = dict([(item[0], ''.join(item[1])) for item in form.items()]) s = form.get('text', '') + # for git + logm = form.get('log', '') + rawp = '@raw/%s' % wn # @@ if s and not s.endswith('\n'): s += '\n' if s: # do this before writing out! compiled, rawlinks = compile(wn, s, getlinks=True) open('%s.html' % wn, 'w').write(compiled) + # record changes in git + if not logm: logm = 'edit %s' % wn + if os.path.exists(rawp): old = open(rawp).read() + else: old = '' + if usegit and s.strip() != old.strip(): + open(rawp, 'w').write(s.strip()); + rungit('add', rawp) + rungit('commit', '-q', '-m', logm, rawp) # Now record the links in @links... prev, now = outboundLinks(wn), [] for link in rawlinks: *************** *** 912,917 **** --- 984,997 ---- os.remove('%s.html' % wn) if os.path.exists('%s.prev' % wn): os.remove('%s.prev' % wn) + if os.path.exists('@raw/%s' % wn): + os.remove('@raw/%s' % wn) + # remove from git + if usegit and os.path.exists(rawp): + if not logm: logm = 'delete %s' % wn + rungit('rm', rawp) + os.remove(rawp) + rungit('commit', '-q', '-m', logm, rawp) # Update @links: remove all outbound links for fn in glob.glob('./@links/%s%%*' % wn): os.remove(fn) *************** *** 938,944 **** path = uri[len(base):] if logdir: # Log the request ! t = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.gmtime()) f = open('%s/%s-%s.log' % (logdir, shortname, t[:7]), 'a') addr = env.get('REMOTE_ADDR') referer = env.get('HTTP_REFERER', '[direct]').replace(' ', '') --- 1018,1024 ---- path = uri[len(base):] if logdir: # Log the request ! t = time.strftime('%Y-%m-%dT%H:%M:%SZ', time.localtime()) f = open('%s/%s-%s.log' % (logdir, shortname, t[:7]), 'a') addr = env.get('REMOTE_ADDR') referer = env.get('HTTP_REFERER', '[direct]').replace(' ', '')