X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;ds=sidebyside;f=geekigeeki.py;h=79960d45422760c24f867238426ee76b8a905af4;hb=HEAD;hp=f906c07c53e638a08e55d9062aa0d10b95cfcbf6;hpb=68d4178e022cfe5946b7a574fb20aaae41d98f14;p=geekigeeki.git
diff --git a/geekigeeki.py b/geekigeeki.py
index f906c07..79960d4 100755
--- a/geekigeeki.py
+++ b/geekigeeki.py
@@ -1,47 +1,44 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
-# Copyright 1999, 2000 Martin Pool
-# Copyright 2002 Gerardo Poggiali
-# Copyright 2007, 2008, 2009 Bernie Innocenti
+# Copyright (C) 1999, 2000 Martin Pool
+# Copyright (C) 2002 Gerardo Poggiali
+# Copyright (C) 2007, 2008, 2009, 2010, 2011 Bernie 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
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful, but
-# WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-# General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
+# it under the terms of the GNU Affero General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see .
__version__ = '4.0-' + '$Id$'[4:11]
-from time import clock
+from time import clock, localtime, gmtime, strftime
start_time = clock()
title_done = False
import cgi, sys, os, re, errno, stat, glob
-image_ext = 'png|gif|jpg|jpeg|bmp|ico'
-video_ext = "ogg|ogv|oga" # Not supported by Firefox 3.5: mkv|mpg|mpeg|mp4|avi|asf|flv|wmv|qt
-image_re = re.compile(r".*\.(" + image_ext + "|" + video_ext + ")", re.IGNORECASE)
-video_re = re.compile(r".*\.(" + video_ext + ")", re.IGNORECASE)
+image_ext = 'png|gif|jpg|jpeg|svg|bmp|ico'
+video_ext = 'avi|webm|mkv|ogv'
+image_re = re.compile(r".*\.(" + image_ext + ")$", re.IGNORECASE)
+video_re = re.compile(r".*\.(" + video_ext + ")$", re.IGNORECASE)
# FIXME: we accept stuff like foo/../bar and we shouldn't
-file_re = re.compile(r"([A-Za-z0-9_\-][A-Za-z0-9_\.\-/]*)")
-url_re = re.compile(r"[a-z]{3,8}://[^\s'\"]+\S")
+file_re = re.compile(r"([A-Za-z0-9_\-][A-Za-z0-9_\.\-/ ]*)$")
+url_re = re.compile(r"[a-z]{3,8}://[^\s'\"]+\S$")
ext_re = re.compile(r"\.([^\./]+)$")
-# CGI stuff ---------------------------------------------------------
def config_get(key, default=None):
return globals().get(key, default)
def script_name():
return os.environ.get('SCRIPT_NAME', '')
+#TODO: move post-edit hook into wiki, then kill this
+def script_path():
+ return os.path.split(os.environ.get('SCRIPT_FILENAME', ''))[0]
+
def query_string():
path_info = os.environ.get('PATH_INFO', '')
if len(path_info) and path_info[0] == '/':
@@ -49,8 +46,9 @@ def query_string():
else:
return os.environ.get('QUERY_STRING', '') or 'FrontPage'
-def privileged_path():
- return config_get('privileged_url') or script_name()
+def is_privileged():
+ purl = config_get('privileged_url')
+ return (purl is not None) and os.environ.get('SCRIPT_URI', '').startswith(purl)
def remote_user():
user = os.environ.get('REMOTE_USER', '')
@@ -74,7 +72,7 @@ def is_external_url(pathname):
def relative_url(pathname, privileged=False):
if not is_external_url(pathname):
if privileged:
- url = privileged_path()
+ url = config_get('privileged_url') or script_name()
else:
url = script_name()
pathname = url + '/' + pathname
@@ -107,9 +105,15 @@ def url_args(kvargs):
return '?' + '&'.join(argv)
return ''
-# Formatting stuff --------------------------------------------------
-def emit_header(mime_type="text/html"):
- print("Content-type: " + mime_type + "; charset=utf-8\n")
+def emit_header(mtime=None, mime_type="text/html"):
+ if mtime:
+ # Prevent caching when the wiki engine gets updated
+ mtime = max(mtime, os.stat(__file__).st_mtime)
+ print("Last-Modified: " + strftime("%a, %d %b %Y %H:%M:%S GMT", gmtime(mtime)))
+ else:
+ print("Cache-Control: must-revalidate, max-age=0")
+ print("Content-type: " + mime_type + "; charset=utf-8")
+ print('')
def send_guru(msg_text, msg_type):
if not msg_text: return
@@ -119,92 +123,24 @@ def send_guru(msg_text, msg_type):
print(cgi.escape(msg_text))
if msg_type == 'error':
print '\n Guru Meditation #DEADBEEF.ABADC0DE'
- print('' \
+ print('' \
% relative_url('sys/GuruMeditation.js'))
-def send_title(name, text="Limbo", msg_text=None, msg_type='error', writable=False):
- global title_done
- if title_done: return
-
- # Head
- emit_header()
- print('')
- print('')
-
- print("%s: %s" % (config_get('site_name', "Unconfigured Wiki"), text))
- print(' ')
- if not name:
- print(' ')
-
- for http_equiv, content in config_get('meta_urls', {}):
- print(' ' % (http_equiv, relative_url(content)))
-
- for link in config_get('link_urls', {}):
- rel, href = link
- print(' ' % (rel, relative_url(href)))
-
- editable = name and writable and config_get('privileged_url') is not None
- if editable:
- print(' ' \
- % (privileged_path() + '?a=edit&q=' + name))
-
- history = config_get('history_url')
- if history is not None:
- print(' ' \
- % relative_url(history + '?a=rss'))
-
- print('')
-
- # Body
- if editable:
- print('')
- else:
- print('')
-
- title_done = True
- send_guru(msg_text, msg_type)
-
- # Navbar
- print('
')
-
-def send_httperror(status="403 Not Found", query=""):
+def send_httperror(status="404 Not Found", query="", trace=False):
print("Status: %s" % status)
- send_title(None, msg_text=("%s: on query '%s'" % (status, query)))
- send_footer()
+ msg_text = "%s: on query '%s'" % (status, query)
+ if trace:
+ import traceback
+ msg_text += '\n\n' + traceback.format_exc()
+ page = Page()
+ page.send_title(msg_text=msg_text)
+ page.send_footer()
def link_tag(dest, text=None, privileged=False, **kvargs):
if text is None:
text = humanlink(dest)
elif image_re.match(text):
- text = ''
+ text = ''
link_class = kvargs.get('class', kvargs.get('cssclass', None))
if not link_class:
@@ -216,9 +152,9 @@ def link_tag(dest, text=None, privileged=False, **kvargs):
text = config_get('nonexist_pfx', '') + text
link_class = 'nonexistent'
- # Prevent crawlers from following links potentially added by spammers or to generated pages
+ # Prevent crawlers from following links potentially added by spammers and to autogenerated pages
nofollow = ''
- if link_class == 'external' or link_class == 'navlink':
+ if link_class in ('external', 'navlink', 'nonexistent'):
nofollow = 'rel="nofollow" '
return '%s' % (link_class, nofollow, relative_url(dest, privileged=privileged), text)
@@ -227,29 +163,34 @@ def link_inline(name, descr=None, kvargs={}):
if not descr: descr = humanlink(name)
url = relative_url(name)
if video_re.match(name):
- return '' % url
+ args = ''
+ if 'maxwidth' in kvargs:
+ args += 'width=' + kvargs['maxwidth']
+ return '' % (url, args)
elif image_re.match(name):
return '' % (url, url + url_args(kvargs), descr)
elif file_re.match(name) and not ext_re.search(name): # FIXME: this guesses a wiki page
- return Page(name).send_naked(kvargs)
+ Page(name).send_naked(kvargs) # FIXME: we should return the page as a string rather than print it
+ return ''
else:
return '' \
% (url, url, name)
def link_inline_glob(pattern, descr=None, kvargs={}):
- s = ''
- for name in glob.glob(pattern):
- s += link_inline(name, descr, kvargs)
- return s
-
-# Search ---------------------------------------------------
+ if not url_re.match(pattern) and bool(set(pattern) & set('?*[')):
+ s = ''
+ for name in sorted(glob.glob(pattern), reverse=bool(int(kvargs.get('reverse', '0'))) ):
+ s += link_inline(name, descr, kvargs)
+ return s
+ else:
+ return link_inline(pattern, descr, kvargs)
-def print_search_stats(hits, searched):
- print("
%d hits out of %d pages searched.
" % (hits, searched))
+def search_stats(hits, searched):
+ return "%d hits out of %d pages searched.\n" % (hits, searched)
def handle_fullsearch(query, form):
needle = form['q'].value
- send_title(None, 'Full text search for "' + needle + '"')
+ Page().send_title(text='Full text search for "' + needle + '"')
needle_re = re.compile(needle, re.IGNORECASE)
hits = []
@@ -260,59 +201,50 @@ def handle_fullsearch(query, form):
if count:
hits.append((count, page_name))
- # The default comparison for tuples compares elements in order,
- # so this sorts by number of hits
+ # The default comparison for tuples compares elements in order, so this sorts by number of hits
hits.sort()
hits.reverse()
- print("
")
+ out = ''
for (count, page_name) in hits:
- print('
\s*\|\|(=|)\s*)
-
- # TODO: highlight search words (look at referrer)
)""", re.VERBOSE)
pre_re = re.compile("""(?:
(?P
\s*\}\}\})
| (?P[<>&])"
)""", re.VERBOSE)
blank_re = re.compile(r"^\s*$")
- indent_re = re.compile(r"^\s*")
+ indent_re = re.compile(r"^(\s*)(\*|\#|)")
tr_re = re.compile(r"^\s*\|\|")
eol_re = re.compile(r"\r?\n")
# For each line, we scan through looking for magic strings, outputting verbatim any intervening text
@@ -612,20 +526,27 @@ class WikiFormatter:
print('
')
else:
indent = indent_re.match(self.line)
- #3.0: print(self._indent_to(len(indent.group(0))), end=' ')
- print(self._indent_to(len(indent.group(0))))
+ print(self._indent_to(len(indent.group(1)), indent.group(2)))
+ # Stand back! Here we apply the monster regex that does all the parsing
print(re.sub(scan_re, self.replace, self.line))
if self.in_pre: print('