X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=geekigeeki.py;h=8de46ab91cbf4d2b14e4ce25753a0848545c311c;hb=fbc4d6639e75dd726fdcadc1129ae0900468e9d2;hp=adc7d2a88f3bab4e8d2bb28a5fdc263841d7d2db;hpb=2cd7e796c7e07cdfd052afbb9d35a4ded3782ef1;p=geekigeeki.git
diff --git a/geekigeeki.py b/geekigeeki.py
index adc7d2a..8de46ab 100755
--- a/geekigeeki.py
+++ b/geekigeeki.py
@@ -1,9 +1,9 @@
-#! /usr/bin/env python
+#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright 1999, 2000 Martin Pool %d hits out of %d pages searched.'
if msg_type == 'error':
print ' Software Failure. Press left mouse button to continue.\n'
- print msg
+ print msg_text
if msg_type == 'error':
print ' Guru Meditation #DEADBEEF.ABADC0DE'
print '
'
- # FIXME: This simple JS snippet is harder to pass than ACID 3.0
+ # FIXME: This little JS snippet is harder to pass than ACID 3.0
print """
"""
-def send_title(name, text="Limbo", msg=None, msg_type='error'):
+def send_title(name, text="Limbo", msg_text=None, msg_type='error', writable=False):
global title_done
if title_done: return
@@ -113,37 +125,51 @@ def send_title(name, text="Limbo", msg=None, msg_type='error'):
print ' '
if not name:
print ' '
- if globals().has_key('css_url'):
- print ' ' % css_url
+
+ for link in link_urls:
+ rel, href = link
+ print ' ' % (rel, relative_url(href))
+
+ if name and writable and privileged_url is not None:
+ print ' ' \
+ % (privileged_path() + '?edit=' + name)
+
+ if history_url is not None:
+ print ' ' \
+ % relative_url(history_url + '?a=rss')
+
print ''
# Body
- if name and privileged_url is not None:
+ if name and writable and privileged_url is not None:
print ''
else:
print ''
title_done = True
- send_guru(msg, msg_type)
+ send_guru(msg_text, msg_type)
# Navbar
print ' '
-def link_tag(params, text=None, ss_class=None, authentication=False):
+def link_tag(params, text=None, ss_class=None, privileged=False):
if text is None:
text = params # default
classattr = ''
@@ -162,15 +188,13 @@ def link_tag(params, text=None, ss_class=None, authentication=False):
# Prevent crawlers from following links potentially added by spammers or to generated pages
if ss_class == 'external' or ss_class == 'navlink':
classattr += 'rel="nofollow" '
- if authentication:
- path = privileged_path()
- else:
- path = script_name()
- return '%s' % (classattr, path, params, text)
+ elif url_re.match(params):
+ classattr += 'rel="nofollow" '
+ return '%s' % (classattr, relative_url(params, privileged=privileged), text)
# Search ---------------------------------------------------
-def do_fullsearch(needle):
+def handle_fullsearch(needle):
send_title(None, 'Full text search for "%s"' % (needle))
needle_re = re.compile(needle, re.IGNORECASE)
@@ -197,7 +221,7 @@ def do_fullsearch(needle):
print_search_stats(len(hits), len(all_pages))
-def do_titlesearch(needle):
+def handle_titlesearch(needle):
# TODO: check needle is legal -- but probably we can just accept any RE
send_title(None, "Title search for \"" + needle + '"')
@@ -215,45 +239,39 @@ def do_titlesearch(needle):
def print_search_stats(hits, searched):
print "
\n' % self.h_level
self.h_level = 0
else:
self.h_level = len(word) - 1
- self.h_cnt += 1
- #abridged = re.sub('[^a-z_]', '', word.lower().replace(' ', '_'))
- result = '
'
- elif self.in_var:
- self.in_var = False
- return '
'
- return ''
+ return '' + word + ''
def _tr_repl(self, word):
out = ''
@@ -466,6 +451,9 @@ class PageFormatter:
s = ''
while self._indent_level() > new_level:
del(self.list_indents[-1])
+ if self.in_li:
+ s += '\{\{\{|\s*\}\}\})" - + r"|(?P\{\{|\}\})" + + r"|(?P\{\{([^\s\|]+)(?:\s*\|\s*([^\]]+)|)\}\})" # Tables + r"|(?P ^\s*\|\|(=|)\s*)" @@ -525,66 +517,62 @@ class PageFormatter: pre_re = re.compile( r"(?:" + r"(?P \s*\}\}\})" + + r"|(?P' if self.in_table: print '[<>&])" + r")") blank_re = re.compile(r"^\s*$") indent_re = re.compile(r"^\s*") tr_re = re.compile(r"^\s*\|\|") eol_re = re.compile(r"\r?\n") - raw = string.expandtabs(self.raw) - for line in eol_re.split(raw): + for self.line in eol_re.split(self.raw.expandtabs()): # Skip ACLs if self.in_header: - if line.startswith('#'): - continue + if self.line.startswith('#'): + continue self.in_header = False if self.in_pre: - print re.sub(pre_re, self.replace, line) + print re.sub(pre_re, self.replace, self.line) else: - if self.in_table and not tr_re.match(line): + if self.in_table and not tr_re.match(self.line): self.in_table = False print ' ' - if blank_re.match(line): + if blank_re.match(self.line): print '
' else: - indent = indent_re.match(line) + indent = indent_re.match(self.line) print self._indent_to(len(indent.group(0))) - print re.sub(scan_re, self.replace, line) + print re.sub(scan_re, self.replace, self.line) if self.in_pre: print '
' print self._undent() print '
' -# ---------------------------------------------------------- class Page: def __init__(self, page_name): self.page_name = page_name - self.msg = '' + self.msg_text = '' self.msg_type = 'error' - self.attrs = {} def split_title(self): - # look for the end of words and the start of a new word, - # and insert a space there + # look for the end of words and the start of a new word and insert a space there return re.sub('([a-z])([A-Z])', r'\1 \2', self.page_name) - def _text_filename(self): + def _filename(self): return path.join(data_dir, self.page_name) def _tmp_filename(self): - return path.join(data_dir, ('#' + self.page_name + '.' + `os.getpid()` + '#')) + return path.join(data_dir, ('#' + self.page_name.replace('/','_') + '.' + `os.getpid()` + '#')) def exists(self): try: - os.stat(self._text_filename()) + os.stat(self._filename()) return True except OSError, er: if er.errno == errno.ENOENT: return False - else: - raise er + raise er def link_to(self): word = self.page_name @@ -595,17 +583,36 @@ class Page: def get_raw_body(self): try: - return open(self._text_filename(), 'rt').read() + return open(self._filename(), 'rb').read() except IOError, er: if er.errno == errno.ENOENT: return '' # just doesn't exist, use default + if er.errno == errno.EISDIR: + return self.format_dir() raise er + def format_dir(self): + out = '== ' + path = '' + for dir in self.page_name.split('/'): + path = (path + '/' + dir) if path else dir + out += '[[' + path + '|' + dir + ']]/' + out += ' ==\n' + + for file in page_list(self._filename(), file_re): + if img_re.match(file): + if image_maxwidth: + maxwidth_arg = '|maxwidth=' + str(image_maxwidth) + out += '{{' + self.page_name + '/' + file + '|' + file + maxwidth_arg + '}}\n' + else: + out += ' * [[' + self.page_name + '/' + file + ']]\n' + return out def get_attrs(self): - if self.attrs: + if 'attrs' in self.__dict__: return self.attrs + self.attrs = {} try: - file = open(self._text_filename(), 'rt') + file = open(self._filename(), 'rt') attr_re = re.compile(r"^#(\S*)(.*)$") for line in file: m = attr_re.match(line) @@ -614,25 +621,26 @@ class Page: self.attrs[m.group(1)] = m.group(2).strip() #print "bernie: attrs[" + m.group(1) + "] = " + m.group(2) + "
\n" except IOError, er: - if er.errno != errno.ENOENT: + if er.errno != errno.ENOENT and er.errno != errno.EISDIR: raise er return self.attrs + def get_attr(self, name, default): + return self.get_attrs().get(name, default) + def can(self, action, default=True): - attrs = self.get_attrs() + acl = None try: - # SomeUser:read,write All:read - acl = attrs["acl"] + #acl SomeUser:read,write All:read + acl = self.get_attr("acl", None) for rule in acl.split(): - (user,perms) = rule.split(':') + (user, perms) = rule.split(':') if user == remote_user() or user == "All": - if action in perms.split(','): - return True - else: - return False + return action in perms.split(',') return False except Exception, er: - pass + if acl: + self.msg_text = 'Illegal acl line: ' + acl return default def can_write(self): @@ -641,21 +649,28 @@ class Page: def can_read(self): return self.can("read", True) - def send_page(self): - page_name = None - if self.can_write(): - page_name = self.page_name - send_title(page_name, self.split_title(), msg=self.msg, msg_type=self.msg_type) + def send_naked(self): if self.can_read(): - PageFormatter(self.get_raw_body()).print_html() + WikiFormatter(self.get_raw_body()).print_html() else: send_guru("Read access denied by ACLs", "notice") - send_footer(page_name, self._last_modified()) + + def format(self): + #css foo.css + value = self.get_attr("css", None) + if value: + global link_urls + link_urls += { "stylesheet": value } + + send_title(self.page_name, self.split_title(), + msg_text=self.msg_text, msg_type=self.msg_type, writable=self.can_write()) + self.send_naked() + send_footer(self.page_name, self._last_modified()) def _last_modified(self): try: from time import localtime, strftime - modtime = localtime(os.stat(self._text_filename())[stat.ST_MTIME]) + modtime = localtime(os.stat(self._filename())[stat.ST_MTIME]) except OSError, er: if er.errno != errno.ENOENT: raise er @@ -663,18 +678,23 @@ class Page: return strftime(datetime_fmt, modtime) def send_editor(self, preview=None): - send_title(None, 'Edit ' + self.split_title(), msg=self.msg, msg_type=self.msg_type) + send_title(None, 'Edit ' + self.split_title(), msg_text=self.msg_text, msg_type=self.msg_type) if not self.can_write(): send_guru("Write access denied by ACLs", "error") return + file = '' + if 'file' in form: + file = form['file'].value + print ('Editing ' + self.page_name + ' for ' + cgi.escape(remote_user()) + ' from ' + cgi.escape(get_hostname(remote_host())) + '
') - print '