X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=geekigeeki.py;h=b34f152188b0544747471a796258d521f9eb0aba;hb=014d5c705784bfe7b47731ad5cc50568fc5e63cf;hp=adc7d2a88f3bab4e8d2bb28a5fdc263841d7d2db;hpb=2cd7e796c7e07cdfd052afbb9d35a4ded3782ef1;p=geekigeeki.git
diff --git a/geekigeeki.py b/geekigeeki.py
index adc7d2a..b34f152 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
# Copyright 2002 Gerardo Poggiali
-# Copyright 2007 Bernardo Innocenti
+# Copyright 2007, 2008 Bernardo Innocenti
#
# 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
@@ -23,13 +23,13 @@ __version__ = '$Id$'[4:12]
from time import clock
start_time = clock()
-import cgi, sys, string, os, re, errno, stat
+import cgi, sys, os, re, errno, stat
from os import path, environ
# Regular expression defining a WikiWord
# (but this definition is also assumed in other places)
-file_re = re.compile(r"^\b([A-Za-z0-9_\.\-]+)\b$")
-word_re = re.compile(r"^\b([A-Z][a-z]+){2,}\b$")
+file_re = re.compile(r"^\b([A-Za-z0-9_\.\-/]+)\b$")
+word_re = re.compile(r"^\b((([A-Z][a-z]+){2,}/)*([A-Z][a-z]+){2,})\b$")
img_re = re.compile(r"^.*\.(png|gif|jpg|jpeg)$", re.IGNORECASE)
url_re = re.compile(r"^[a-z]{3,8}://[^\s'\"]+\S$")
@@ -57,25 +57,33 @@ def get_hostname(addr):
try:
from socket import gethostbyaddr
return gethostbyaddr(addr)[0] + ' (' + addr + ')'
- except:
+ except Exception, er:
return addr
+def relative_url(pathname, privileged=False):
+ if not (url_re.match(pathname) or pathname.startswith('/')):
+ if privileged:
+ url = privileged_path()
+ else:
+ url = script_name()
+ pathname = url + '/' + pathname
+ return pathname
+
# Formatting stuff --------------------------------------------------
-def emit_header(type="text/html"):
- print "Content-type: " + type + "; charset=utf-8"
- print
+def emit_header(mime_type="text/html"):
+ print "Content-type: " + mime_type + "; charset=utf-8\n"
-def send_guru(msg, msg_type):
- if msg is None or msg == '': return
+def send_guru(msg_text, msg_type):
+ if not msg_text: return
print ''
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'):
global title_done
if title_done: return
@@ -113,8 +121,8 @@ 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 css in css_url:
+ print ' ' % relative_url(css)
print ''
# Body
@@ -124,7 +132,7 @@ def send_title(name, text="Limbo", msg=None, msg_type='error'):
print ''
title_done = True
- send_guru(msg, msg_type)
+ send_guru(msg_text, msg_type)
# Navbar
print ''
@@ -143,7 +151,7 @@ def send_title(name, text="Limbo", msg=None, msg_type='error'):
if name:
print ' | ' + link_tag('?raw=' + name, 'Raw Text', 'navlink')
if privileged_url is not None:
- print ' | ' + link_tag('?edit=' + name, 'Edit Page', 'navlink', authentication=True)
+ print ' | ' + link_tag('?edit=' + name, 'Edit Page', 'navlink', privileged=True)
else:
print ' | Immutable Page'
@@ -153,7 +161,7 @@ def send_title(name, text="Limbo", msg=None, msg_type='error'):
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,11 +170,9 @@ 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 ---------------------------------------------------
@@ -215,15 +221,10 @@ def do_titlesearch(needle):
def print_search_stats(hits, searched):
print "%d hits out of %d pages searched.
" % (hits, searched)
-#TODO: merge into do_savepage()
-def do_edit(pagename):
- Page(pagename).send_editor()
-
def do_raw(pagename):
Page(pagename).send_raw()
-def do_savepage(pagename):
- global form
+def do_edit(pagename):
pg = Page(pagename)
if 'preview' in form:
pg.send_editor(form['savetext'].value)
@@ -231,19 +232,15 @@ def do_savepage(pagename):
pg.save_text(form['savetext'].value)
pg.send_page()
elif 'cancel' in form:
- pg.msg = 'Editing canceled'
+ pg.msg_text = 'Editing canceled'
pg.msg_type = 'notice'
pg.send_page()
else:
- raise 'What did you press?'
+ pg.send_editor()
def make_index_key():
- s = ''
- links = map(lambda ch: '%s' % (ch, ch),
- string.lowercase)
- s = s + string.join(links, ' | ')
- s = s + ''
- return s
+ links = map(lambda ch: '%s' % (ch, ch), 'abcdefghijklmnopqrstuvwxyz')
+ return ''+ ' | '.join(links) + ''
def page_list():
return filter(word_re.match, os.listdir(data_dir))
@@ -253,7 +250,6 @@ def send_footer(name, mod_string=None):
cgi.print_arguments()
cgi.print_form(cgi.FieldStorage())
cgi.print_environ()
- global __version__
print '