Autorotate image thumbnails.
[geekigeeki.git] / geekigeeki.py
index 38870c8539c1bb39fc29b61be63878cefe0f635d..1891fdd8c5842cea4357d5339009ad40f90134d6 100755 (executable)
@@ -1,46 +1,44 @@
 #!/usr/bin/python
 # -*- coding: utf-8 -*-
 #
-# Copyright 1999, 2000 Martin Pool <mbp@humbug.org.au>
-# Copyright 2002 Gerardo Poggiali
-# Copyright 2007, 2008 Bernie Innocenti <bernie@codewiz.org>
+# Copyright (C) 1999, 2000 Martin Pool <mbp@humbug.org.au>
+# Copyright (C) 2002 Gerardo Poggiali
+# Copyright (C) 2007, 2008, 2009, 2010 Bernie Innocenti <bernie@codewiz.org>
 #
 # 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 <http://www.gnu.org/licenses/>.
 
-__version__ = '$Id$'[4:12]
+__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
+import cgi, sys, os, re, errno, stat, glob
 
-# Regular expression defining a WikiWord
-# (but this definition is also assumed in other places)
-word_re = re.compile(r"^\b((([A-Z][a-z0-9]+){2,}/)*([A-Z][a-z0-9]+){2,})\b$")
+image_ext = 'png|gif|jpg|jpeg|bmp|ico'
+video_ext = "ogg|ogv|oga|webm" # Not supported by Firefox 3.6: 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)
 # FIXME: we accept stuff like foo/../bar and we shouldn't
-file_re = re.compile(r"^\b([A-Za-z0-9_\-][A-Za-z0-9_\.\-/]*)\b$")
-img_re = re.compile(r"^.*\.(png|gif|jpg|jpeg|bmp|ico|ogm|ogg|mkv|mpg|mpeg|mp4|avi|asf|flv|wmv|qt)$", re.IGNORECASE)
-video_re = re.compile(r"^.*\.(ogm|ogg|mkv|mpg|mpeg|mp4|avi|asf|flv|wmv|qt)$", re.IGNORECASE)
-url_re = re.compile(r"^[a-z]{3,8}://[^\s'\"]+\S$")
-link_re = re.compile("(?:\[\[|{{)([^\s\|]+)(?:\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"\.([^\./]+)$")
 
-title_done = False
+def config_get(key, default=None):
+    return globals().get(key, default)
 
-# CGI stuff ---------------------------------------------------------
 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] == '/':
@@ -48,8 +46,9 @@ def query_string():
     else:
         return os.environ.get('QUERY_STRING', '') or 'FrontPage'
 
-def privileged_path():
-    return 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', '')
@@ -73,143 +72,121 @@ 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
-    return pathname
+    return cgi.escape(pathname, quote=True)
 
 def permalink(s):
     return re.sub(' ', '-', re.sub('[^a-z0-9_ ]', '', s.lower()).strip())
 
-# Formatting stuff --------------------------------------------------
-def emit_header(mime_type="text/html"):
-    print "Content-type: " + mime_type + "; charset=utf-8\n"
-
-def sendfile(dest_file, src_file):
-    """Efficiently copy file data between file descriptors"""
-    while 1:
-        data = src_file.read(65536)
-        if not data: break
-        dest_file.write(data)
+def humanlink(s):
+    return re.sub(r'(?:.*[/:]|)([^:/\.]+)(?:\.[^/:]+|)$', r'\1', s.replace('_', ' '))
+
+# Split arg lists like "blah|blah blah| width=100 | align = center",
+# return a list containing anonymous arguments and a map containing the named arguments
+def parse_args(s):
+    args = []
+    kvargs = {}
+    for arg in s.strip('<[{}]>').split('|'):
+        m = re.match('\s*(\w+)\s*=\s*(.+)\s*', arg)
+        if m is not None:
+            kvargs[m.group(1)] = m.group(2)
+        else:
+            args.append(arg.strip())
+    return (args, kvargs)
+
+def url_args(kvargs):
+    argv = []
+    for k, v in kvargs.items():
+        argv.append(k + '=' + v)
+    if argv:
+        return '?' + '&amp;'.join(argv)
+    return ''
+
+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)))
+    if mime_type:
+        print("Content-type: " + mime_type + "; charset=utf-8")
+    print('')
 
 def send_guru(msg_text, msg_type):
     if not msg_text: return
-    print '<pre id="guru" onclick="this.style.display = \'none\'" class="' + msg_type + '">'
+    print('<pre id="guru" onclick="this.style.display = \'none\'" class="' + msg_type + '">')
     if msg_type == 'error':
-        print '    Software Failure.  Press left mouse button to continue.\n'
-    print msg_text
+        print('    Software Failure.  Press left mouse button to continue.\n')
+    print(cgi.escape(msg_text))
     if msg_type == 'error':
-        print '\n      Guru Meditation #DEADBEEF.ABADC0DE'
-    print '</pre>'
-    try:
-        sendfile(sys.stdout, open('gurumeditation.js', 'rb'))
-    except IOError, err:
-        pass
-
-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 '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"'
-    print '  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
-    print '<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">'
-
-    print "<head><title>%s: %s</title>" % (site_name, text)
-    print ' <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />'
-    if not name:
-        print ' <meta name="robots" content="noindex,nofollow" />'
-
-    for meta in meta_urls:
-        http_equiv, content = meta
-        print ' <meta http-equiv="%s" content="%s" />' % (http_equiv, relative_url(content))
-
-    for link in link_urls:
-        rel, href = link
-        print ' <link rel="%s" href="%s" />' % (rel, relative_url(href))
-
-    if name and writable and privileged_url is not None:
-        print ' <link rel="alternate" type="application/x-wiki" title="Edit this page" href="%s" />' \
-            % (privileged_path() + '?edit=' + name)
-
-    if history_url is not None:
-        print ' <link rel="alternate" type="application/rss+xml" title="RSS" href="%s" />' \
-            % relative_url(history_url + '?a=rss')
-
-    print '</head>'
-
-    # Body
-    if name and writable and privileged_url is not None:
-        print '<body ondblclick="location.href=\'' + privileged_path() + '?edit=' + name + '\'">'
-    else:
-        print '<body>'
-
-    title_done = True
-    send_guru(msg_text, msg_type)
-
-    # Navbar
-    print '<div class="nav">'
-    if name:
-        print '  <b>' + link_tag('?fullsearch=' + name, text, 'navlink') + '</b> '
-    else:
-        print '  <b>' + text + '</b> '
-    print ' | ' + link_tag('FrontPage', 'Home', 'navlink')
-    print ' | ' + link_tag('FindPage', 'Find Page', 'navlink')
-    if 'history_url' in globals():
-        print ' | <a href="' + relative_url(history_url) + '" class="navlink">Recent Changes</a>'
-        if name:
-            print ' | <a href="' + relative_url(history_url + '?a=history;f=' + name) + '" class="navlink">Page History</a>'
-
-    if name:
-        print ' | ' + link_tag('?raw=' + name, 'Raw Text', 'navlink')
-        if privileged_url is not None:
-            if writable:
-                print ' | ' + link_tag('?edit=' + name, 'Edit', 'navlink', privileged=True)
-            else:
-                print ' | ' + link_tag(name, 'Login', 'navlink', privileged=True)
-
-    else:
-        print ' | <i>Immutable Page</i>'
-
-    user = remote_user()
-    if user != 'AnonymousCoward':
-        print ' | <span class="login"><i>logged in as <b>' + cgi.escape(user) + '</b></i></span>'
-
-    print '<hr /></div>'
-
-def send_httperror(status="403 Not Found", query=""):
-    print "Status: %s" % status
-    send_title(None, msg_text=("%s: on query '%s'" % (status, query)))
-    send_footer()
-
-def link_tag(params, text=None, link_class=None, privileged=False):
+        print '\n           Guru Meditation #DEADBEEF.ABADC0DE'
+    print('</pre><script type="text/javascript" src="%s" defer="defer"></script>' \
+        % relative_url('sys/GuruMeditation.js'))
+
+def send_httperror(status="403 Not Found", query="", trace=False):
+    print("Status: %s" % status)
+    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 = params # default
-    elif img_re.match(text):
-        text = '<img border="0" src="' + text + '" />'
+        text = humanlink(dest)
+    elif image_re.match(text):
+        text = '<img style="border: 0" src="' + relative_url(text) + '" alt="' + text + '" />'
 
+    link_class = kvargs.get('class', kvargs.get('cssclass', None))
     if not link_class:
-        if is_external_url(params):
+        if is_external_url(dest):
             link_class = 'external'
-        elif file_re.match(params) and Page(params).exists():
+        elif file_re.match(dest) and Page(dest).exists():
             link_class = 'wikilink'
         else:
-            params = nonexist_pfx + params
+            text = config_get('nonexist_pfx', '') + text
             link_class = 'nonexistent'
 
-    classattr = 'class="%s" ' % link_class
     # Prevent crawlers from following links potentially added by spammers or to generated pages
+    nofollow = ''
     if link_class == 'external' or link_class == 'navlink':
-        classattr += 'rel="nofollow"'
-
-    return '<a %shref="%s">%s</a>' % (classattr, relative_url(params, privileged=privileged), text)
+        nofollow = 'rel="nofollow" '
+
+    return '<a class="%s" %shref="%s">%s</a>' % (link_class, nofollow, relative_url(dest, privileged=privileged), text)
+
+def link_inline(name, descr=None, kvargs={}):
+    if not descr: descr = humanlink(name)
+    url = relative_url(name)
+    if video_re.match(name):
+        return '<video controls="1" src="%s">Your browser does not support HTML5 video</video>' % url
+    elif image_re.match(name):
+        return '<a href="%s"><img border="0" src="%s" alt="%s" /></a>' % (url, url + url_args(kvargs), descr)
+    elif file_re.match(name) and not ext_re.search(name): # FIXME: this guesses a wiki page
+        Page(name).send_naked(kvargs) # FIXME: we should return the page as a string rather than print it
+        return ''
+    else:
+        return '<iframe width="100%%" scrolling="auto" frameborder="0" src="%s"><a href="%s">%s</a></iframe>' \
+            % (url, url, name)
+
+def link_inline_glob(pattern, descr=None, kvargs={}):
+    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)
 
-# Search ---------------------------------------------------
+def search_stats(hits, searched):
+    return "%d hits out of %d pages searched.\n" % (hits, searched)
 
-def handle_fullsearch(needle):
-    send_title(None, 'Full text search for "%s"' % (needle))
+def handle_fullsearch(query, form):
+    needle = form['q'].value
+    Page().send_title(text='Full text search for "' + needle + '"')
 
     needle_re = re.compile(needle, re.IGNORECASE)
     hits = []
@@ -220,103 +197,91 @@ def handle_fullsearch(needle):
         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 "<ul>"
+    out = ''
     for (count, page_name) in hits:
-        print '<li><p>' + link_tag(page_name)
-        print ' . . . . ' + `count`
-        print ['match', 'matches'][count != 1]
-        print '</p></li>'
-    print "</ul>"
+        out += ' * [[' + page_name + ']] . . . ' + str(count) + ' ' + ['match', 'matches'][count != 1] + '\n'
 
-    print_search_stats(len(hits), len(all_pages))
+    out += search_stats(len(hits), len(all_pages))
+    WikiFormatter(out).print_html()
 
-def handle_titlesearch(needle):
-    # TODO: check needle is legal -- but probably we can just accept any RE
-    send_title(None, "Title search for \"" + needle + '"')
+def handle_titlesearch(query, form):
+    needle = form['q'].value
+    Page().send_title(text='Title search for "' + needle + '"')
 
     needle_re = re.compile(needle, re.IGNORECASE)
     all_pages = page_list()
-    hits = filter(needle_re.search, all_pages)
+    hits = list(filter(needle_re.search, all_pages))
 
-    print "<ul>"
+    out = ''
     for filename in hits:
-        print '<li><p>' + link_tag(filename) + "</p></li>"
-    print "</ul>"
+        out += ' * [[' + filename + ']]\n'
 
-    print_search_stats(len(hits), len(all_pages))
-
-def print_search_stats(hits, searched):
-    print "<p>%d hits out of %d pages searched.</p>" % (hits, searched)
-
-def handle_raw(pagename):
-    if not file_re.match(pagename):
-        send_httperror("403 Forbidden", pagename)
-        return
+    out += search_stats(len(hits), len(all_pages))
+    WikiFormatter(out).print_html()
 
+def handle_raw(pagename, form):
     Page(pagename).send_raw()
 
-def handle_edit(pagename):
-    if not file_re.match(pagename):
-        send_httperror("403 Forbidden", pagename)
-        return
+def handle_atom(pagename, form):
+    Page(pagename).send_atom()
 
-    pg = Page(pagename)
+def handle_edit(pagename, form):
+    pg  = Page(form['q'].value)
     if 'save' in form:
         if form['file'].value:
             pg.save(form['file'].file.read(), form['changelog'].value)
         else:
             pg.save(form['savetext'].value.replace('\r\n', '\n'), form['changelog'].value)
-        pg.format()
+        pg.send()
     elif 'cancel' in form:
         pg.msg_text = 'Editing canceled'
         pg.msg_type = 'notice'
-        pg.format()
+        pg.send()
     else: # preview or edit
         text = None
         if 'preview' in form:
             text = form['savetext'].value
         pg.send_editor(text)
 
+def handle_get(pagename, form):
+    if not ext_re.search(pagename): # FIXME: no extension guesses a wiki page
+        Page(pagename).send()
+    else:
+        # FIMXE: this is all bullshit, MimeTypes bases its guess on the extension!
+        from mimetypes import MimeTypes
+        mimetype, encoding = MimeTypes().guess_type(pagename)
+        Page(pagename).send_raw(mimetype=mimetype, args=form)
+
+# Used by sys/macros/WordIndex and sys/macros/TitleIndex
 def make_index_key():
-    links = map(lambda ch: '<a href="#%s">%s</a>' % (ch, ch), 'abcdefghijklmnopqrstuvwxyz')
-    return '<p><center>'+ ' | '.join(links) + '</center></p>'
-
-def page_list(dirname = None, re = word_re):
-    return sorted(filter(re.match, os.listdir(dirname or data_dir)))
-
-def send_footer(mod_string=None):
-    if globals().get('debug_cgi', False):
-        cgi.print_arguments()
-        cgi.print_form(form)
-        cgi.print_environ()
-    print '''
-<div id="footer"><hr />
-<p class="copyright">
-<a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/"><img class="license" alt="Creative Commons License" src="%s" /></a>
-<span class="benchmark">generated in %0.3fs</span> by <a href="http://www.codewiz.org/wiki/GeekiGeeki">GeekiGeeki</a> version %s
-</p>
-''' % (relative_url('cc-by-sa.png'), clock() - start_time, __version__)
-    if mod_string:
-        print '<p class="modified">last modified %s</p>' % mod_string
-    print '</div></body></html>'
+    links = ['<a href="#%s">%s</a>' % (ch, ch) for ch in 'abcdefghijklmnopqrstuvwxyz']
+    return '<p style="text-align: center">' + ' | '.join(links) + '</p>'
 
-class WikiFormatter:
-    """Object that turns Wiki markup into HTML.
+def page_list(dirname=None, search_re=None):
+    if search_re is None:
+        # FIXME: WikiWord is too restrictive now!
+        search_re = re.compile(r"^\b((([A-Z][a-z0-9]+){2,}/)*([A-Z][a-z0-9]+){2,})\b$")
+    return sorted(filter(search_re.match, os.listdir(dirname or '.')))
+
+def _macro_ELAPSED_TIME(*args, **kvargs):
+    return "%03f" % (clock() - start_time)
+
+def _macro_VERSION(*args, **kvargs):
+    return __version__
 
-    All formatting commands can be parsed one line at a time, though
-    some state is carried over between lines.
-    """
-    def __init__(self, raw):
+class WikiFormatter:
+    """Object that turns Wiki markup into HTML."""
+    def __init__(self, raw, kvargs=None):
         self.raw = raw
+        self.kvargs = kvargs or {}
         self.h_level = 0
         self.in_pre = self.in_html = self.in_table = self.in_li = False
         self.in_header = True
-        self.list_indents = []
+        self.list_indents = [] # a list of pairs (indent_level, list_type) to track nested lists
         self.tr_cnt = 0
         self.styles = {
             #wiki   html   enabled?
@@ -324,11 +289,11 @@ class WikiFormatter:
             "**":  ["b",   False],
             "##":  ["tt",  False],
             "__":  ["u",   False],
+            "--":  ["del", False],
             "^^":  ["sup", False],
             ",,":  ["sub", False],
-            "''":  ["em",  False], # OBSOLETE
-            "'''": ["b",   False], # OBSOLETE
-            "``":  ["tt",  False], # OBSOLETE
+            "''":  ["em",  False], # LEGACY
+            "'''": ["b",   False], # LEGACY
         }
 
     def _b_repl(self, word):
@@ -336,6 +301,9 @@ class WikiFormatter:
         style[1] = not style[1]
         return ['</', '<'][style[1]] + style[0] + '>'
 
+    def _glyph_repl(self, word):
+        return '&mdash;'
+
     def _tit_repl(self, word):
         if self.h_level:
             result = '</h%d><p>\n' % self.h_level
@@ -350,73 +318,54 @@ class WikiFormatter:
         return '<br />'
 
     def _rule_repl(self, word):
-        return self._undent() + '\n<hr size="%d" noshade="noshade" />\n' % (len(word) - 2)
+        return '\n<hr size="%d" noshade="noshade" />\n' % (len(word) - 2)
 
     def _macro_repl(self, word):
-        m = re.compile("\<\<([^\s\|\>]+)(?:\s*\|\s*([^\>]+)|)\>\>").match(word)
-        name = m.group(1)
-        argv = [name]
-        if m.group(2):
-            argv.extend(m.group(2).split('|'))
-        argv = map(str.strip, argv)
-
-        macro = globals().get('_macro_' + name)
-        if not macro:
-            try:
-                execfile("macros/" + name + ".py", globals())
-            except IOError, err:
-                if err.errno == errno.ENOENT: pass
-            macro = globals().get('_macro_' + name)
-        if macro:
-            return macro(argv)
-        else:
-            msg = '&lt;&lt;' + '|'.join(argv) + '&gt;&gt;'
+        try:
+            args, macro_kvargs = parse_args(word)
+            # Is this a parameter given to the current page?
+            if args[0] in self.kvargs:
+                return self.kvargs[args[0]]
+            # Is this an internal macro?
+            macro = globals().get('_macro_' + args[0])
+            if not macro:
+                # Can we load (and cache) an external macro?
+                exec(open("sys/macros/" + args[0] + ".py").read(), globals())
+                macro = globals().get('_macro_' + args[0])
+            # Invoke macro passing both macro args augmented by page args
+            macro_kvargs.update(self.kvargs)
+            return macro(*args, **macro_kvargs)
+        except Exception, e:
+            msg = cgi.escape(word) + ": " + cgi.escape(str(e))
             if not self.in_html:
                 msg = '<strong class="error">' + msg + '</strong>'
             return msg
 
     def _hurl_repl(self, word):
-        m = link_re.match(word)
-        return link_tag(m.group(1), m.group(2))
-
-    def _url_repl(self, word):
-        return link_tag(word)
-
-    def _word_repl(self, word):
-        return link_tag(word)
+        args, kvargs = parse_args(word)
+        return link_tag(*args, **kvargs)
 
     def _inl_repl(self, word):
-        m = link_re.match(word)
-        name = relative_url(m.group(1))
-        descr = m.group(2)
-
-        if descr:
-            argv = descr.split('|')
-            descr = argv.pop(0)
-            args = ''
-            if argv:
-                args = '?' + '&amp;'.join(argv)
-
-            # The "extthumb" nonsense works around a limitation of the HTML block model
-            return '<div class="extthumb"><div class="thumb"><a href="%s"><img border="0" src="%s" alt="%s" /></a><div class="caption">%s</div></div></div>' \
-                    % (name, name + args, descr, descr)
-        elif video_re.match(name):
-            return '<video src="%s">Your browser does not support the HTML5 video tag</video>' % name
+        args, kvargs = parse_args(word)
+        name = args.pop(0)
+        if len(args):
+            descr = args.pop(0)
+            # This double div nonsense works around a limitation of the HTML block model
+            return '<div class="' + kvargs.get('class', 'thumb') + '">' \
+                + '<div class="innerthumb">' \
+                + link_inline_glob(name, descr, kvargs) \
+                + '<div class="caption">' + descr + '</div></div></div>'
         else:
-            return '<a href="%s"><img border="0" src="%s" /></a>' % (name, name)
-
-    def _img_repl(self, word):
-        return self._inl_repl('{{' + word + '}}')
-
-    def _email_repl(self, word):
-        return '<a href="mailto:%s">%s</a>' % (word, word)
+            return link_inline_glob(name, None, kvargs)
 
     def _html_repl(self, word):
+        if not self.in_html and word.startswith('<div'): word = '</p>' + word
         self.in_html += 1
         return word; # Pass through
 
     def _htmle_repl(self, word):
         self.in_html -= 1
+        if not self.in_html and word.startswith('</div'): word += '<p>'
         return word; # Pass through
 
     def _ent_repl(self, s):
@@ -426,6 +375,21 @@ class WikiFormatter:
                 '<': '&lt;',
                 '>': '&gt;'}[s]
 
+    def _img_repl(self, word): # LEGACY
+        return self._inl_repl('{{' + word + '}}')
+
+    def _word_repl(self, word): # LEGACY
+        if self.in_html: return word # pass through
+        return link_tag(word)
+
+    def _url_repl(self, word): # LEGACY
+        if self.in_html: return word # pass through
+        return link_tag(word)
+
+    def _email_repl(self, word): # LEGACY
+        if self.in_html: return word # pass through
+        return '<a href="mailto:%s">%s</a>' % (word, word)
+
     def _li_repl(self, match):
         if self.in_li:
             return '</li><li>'
@@ -466,68 +430,64 @@ class WikiFormatter:
         return ''
 
     def _indent_level(self):
-        return len(self.list_indents) and self.list_indents[-1]
+        return len(self.list_indents) and self.list_indents[-1][0]
 
-    def _indent_to(self, new_level):
+    def _indent_to(self, new_level, list_type=''):
         if self._indent_level() == new_level:
             return ''
         s = '</p>'
         while self._indent_level() > new_level:
-            del(self.list_indents[-1])
             if self.in_li:
                 s += '</li>'
                 self.in_li = False # FIXME
-            s += '</ul>\n'
+            s += '</' + self.list_indents[-1][1] + '>\n'
+            del(self.list_indents[-1])
+
+        list_type = ('ul', 'ol')[list_type == '#']
         while self._indent_level() < new_level:
-            self.list_indents.append(new_level)
-            s += '<ul>\n'
+            self.list_indents.append((new_level, list_type))
+            s += '<' + list_type + '>\n'
         s += '<p>'
         return s
 
-    def _undent(self):
-        res = '</p>'
-        res += '</ul>' * len(self.list_indents)
-        res += '<p>'
-        self.list_indents = []
-        return res
-
     def replace(self, match):
-        for rule, hit in match.groupdict().items():
+        for rule, hit in list(match.groupdict().items()):
             if hit:
                 return getattr(self, '_' + rule + '_repl')(hit)
         else:
-            raise "Can't handle match " + repr(match)
+            raise Exception("Can't handle match " + repr(match))
 
     def print_html(self):
-        print '<div class="wiki"><p>'
+        print('<div class="wiki"><p>')
 
         scan_re = re.compile(r"""(?:
-            # Styles and formatting
-              (?P<b>     \*\*|'''|//|''|\#\#|``|__|\^\^|,,)
+            # Styles and formatting ("--" must cling to a word to disambiguate it from the dash)
+              (?P<b>     \*\* | // | \#\# | __ | --\b | \b-- | \^\^ | ,, | ''' | '' )
             | (?P<tit>   \={2,6})
             | (?P<br>    \\\\)
             | (?P<rule>  ^-{3,})
             | (?P<hi>    \b( FIXME | TODO | DONE )\b )
+            | (?P<glyph> --)
 
             # Links
-            | (?P<macro> \<\<([^\s\|\>]+)(?:\s*\|\s*([^\>]+)|)\>\>)
-            | (?P<hurl>  \[\[([^\s\|]+)(?:\s*\|\s*([^\]]+)|)\]\])
+            | (?P<macro> \<\<[^\>]+\>\>)
+            | (?P<hurl>  \[\[[^\]]+\]\])
 
             # Inline HTML
-            | (?P<html>  <(br|hr|div|span|form|iframe|input|textarea|a|img|h[1-5])\b )
+            | (?P<html>             <(br|hr|div|span|form|iframe|input|textarea|a|img|h[1-5])\b )
             | (?P<htmle> ( /\s*> | </(br|hr|div|span|form|iframe|input|textarea|a|img|h[1-5])> ) )
             | (?P<ent>   [<>&] )
 
             # Auto links (LEGACY)
-            | (?P<img>   \b[a-zA-Z0-9_/-]+\.(png|gif|jpg|jpeg|bmp|ico|ogm|ogg|mkv|mpg|mpeg|mp4|avi|asf|flv|wmv|qt))
+            | (?P<img>   \b[a-zA-Z0-9_/-]+\.(""" + image_ext + "|" + video_ext + r"""))
             | (?P<word>  \b(?:[A-Z][a-z]+){2,}\b)
             | (?P<url>   (http|https|ftp|mailto)\:[^\s'\"]+\S)
             | (?P<email> [-\w._+]+\@[\w.-]+)
 
-            # Lists, divs, spans
+            # Lists, divs, spans and inline objects
             | (?P<li>    ^\s+[\*\#]\s+)
             | (?P<pre>   \{\{\{|\s*\}\}\})
-            | (?P<inl>   \{\{([^\s\|]+)(?:\s*\|\s*([^\]]+)|)\}\})
+            | (?P<inl>   \{\{[^\}]+\}\})
 
             # Tables
             | (?P<tr>    ^\s*\|\|(=|)\s*)
@@ -541,12 +501,12 @@ class WikiFormatter:
             | (?P<ent>[<>&])"
             )""", 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
-        for self.line in eol_re.split(self.raw.expandtabs()):
+        #3.0: for self.line in eol_re.split(str(self.raw.expandtabs(), 'utf-8')):
+        for self.line in eol_re.split(str(self.raw.expandtabs())):
             # Skip pragmas
             if self.in_header:
                 if self.line.startswith('#'):
@@ -554,55 +514,69 @@ class WikiFormatter:
                 self.in_header = False
 
             if self.in_pre:
-                print re.sub(pre_re, self.replace, self.line)
+                print(re.sub(pre_re, self.replace, self.line))
             else:
                 if self.in_table and not tr_re.match(self.line):
                     self.in_table = False
-                    print '</tbody></table><p>'
+                    print('</tbody></table><p>')
 
                 if blank_re.match(self.line):
-                    print '</p><p>'
+                    print('</p><p>')
                 else:
                     indent = indent_re.match(self.line)
-                    print self._indent_to(len(indent.group(0))) ,
-                    print re.sub(scan_re, self.replace, self.line)
+                    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('</pre>')
+        if self.in_table: print('</tbody></table><p>')
+        print(self._indent_to(0))
+        print('</p></div>')
 
-        if self.in_pre: print '</pre>'
-        if self.in_table: print '</tbody></table><p>'
-        print self._undent()
-        print '</p></div>'
+class HttpException(Exception):
+    def __init__(self, error, query):
+        self.error = error
+        self.query = query
 
 class Page:
-    def __init__(self, page_name):
-        self.page_name = page_name
+    def __init__(self, page_name="Limbo"):
+        self.page_name = page_name.rstrip('/');
         self.msg_text = ''
         self.msg_type = 'error'
+        if not file_re.match(self.page_name):
+            raise HttpException("403 Forbidden", self.page_name)
 
     def split_title(self):
         # 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 _filename(self):
-        return os.path.join(data_dir, self.page_name)
+        return self.page_name
 
     def _tmp_filename(self):
-        return os.path.join(data_dir, ('#' + self.page_name.replace('/','_') + '.' + `os.getpid()` + '#'))
+        return self.page_name + '.tmp' + str(os.getpid()) + '#'
 
-    def exists(self):
+    def _mtime(self):
         try:
-            os.stat(self._filename())
-            return True
+            return os.stat(self._filename()).st_mtime
         except OSError, err:
             if err.errno == errno.ENOENT:
-                return False
+                return None
             raise err
 
-    def get_raw_body(self):
+    def exists(self):
+        if self._mtime():
+            return True
+        return False
+
+    def get_raw_body(self, default=None):
         try:
             return open(self._filename(), 'rb').read()
         except IOError, err:
             if err.errno == errno.ENOENT:
-                return '' # just doesn't exist, use default
+                if default is None:
+                    default = '//[[?a=edit&q=%s|Describe %s]]//' % (self.page_name, self.page_name)
+                return default
             if err.errno == errno.EISDIR:
                 return self.format_dir()
             raise err
@@ -610,32 +584,34 @@ class Page:
     def format_dir(self):
         out = '== '
         pathname = ''
-        for dirname in self.page_name.split('/'):
-            pathname = (pathname + '/' + dirname) if pathname else dirname
+        for dirname in self.page_name.strip('/').split('/'):
+            pathname = (pathname and pathname + '/' ) + dirname
             out += '[[' + pathname + '|' + dirname + ']]/'
         out += ' ==\n'
+        images_out = '\n'
  
         for filename in page_list(self._filename(), file_re):
-            if img_re.match(filename):
-                if image_maxwidth:
-                    maxwidth_arg = '|maxwidth=' + str(image_maxwidth)
-                out += '{{' + self.page_name + '/' + filename + '|' + filename + maxwidth_arg + '}}\n'
+            if image_re.match(filename):
+                maxwidth = config_get('image_maxwidth', '400')
+                if maxwidth:
+                    maxwidth = ' | maxwidth=' + str(maxwidth)
+                images_out += '{{' + self.page_name + '/' + filename + ' | ' + humanlink(filename) + maxwidth + ' | class=thumbleft}}\n'
             else:
                 out += ' * [[' + self.page_name + '/' + filename + ']]\n'
-        return out
+        return out + images_out
 
     def pragmas(self):
         if not '_pragmas' in self.__dict__:
             self._pragmas = {}
             try:
-                f = open(self._filename(), 'rt')
+                file = open(self._filename(), 'rt')
                 attr_re = re.compile(r"^#(\S*)(.*)$")
-                for line in f:
+                for line in file:
                     m = attr_re.match(line)
                     if not m:
                         break
                     self._pragmas[m.group(1)] = m.group(2).strip()
-                    #print "bernie: _pragmas[" + m.group(1) + "] = " + m.group(2) + "<br>\n"
+                    #print "bernie: pragmas[" + m.group(1) + "] = " + m.group(2) + "<br>\n"
             except IOError, err:
                 if err.errno != errno.ENOENT and err.errno != errno.EISDIR:
                     raise err
@@ -665,91 +641,151 @@ class Page:
     def can_read(self):
         return self.can("read", True)
 
-    def send_naked(self):
+    def send_title(self, name=None, text="Limbo", msg_text=None, msg_type='error'):
+        global title_done
+        if title_done: return
+
+        # HEAD
+        emit_header(self._mtime())
+        print('<!doctype html>\n<html lang="en">')
+        print("<head><title>%s: %s</title>" % (config_get('site_name', "Unconfigured Wiki"), text))
+        print(' <meta charset="utf-8">')
+        if not name:
+            print(' <meta name="robots" content="noindex,nofollow" />')
+
+        for http_equiv, content in config_get('meta_urls', {}):
+            print(' <meta http-equiv="%s" content="%s" />' % (http_equiv, relative_url(content)))
+
+        for link in config_get('link_urls', {}):
+            rel, href = link
+            print(' <link rel="%s" href="%s" />' % (rel, relative_url(href)))
+
+        editable = name and self.can_write() and is_privileged()
+        if editable:
+            print(' <link rel="alternate" type="application/x-wiki" title="Edit this page" href="%s" />' \
+                % relative_url('?a=edit&q=' + name, privileged=True))
+
+        print(' <link rel="alternate" type="application/atom+xml" title="Atom feed" href="%s" />' \
+            % relative_url(name + '?a=atom'))
+
+        print('</head>')
+
+        # BODY
+        if editable:
+            print('<body ondblclick="location.href=\'' + relative_url('?a=edit&q=' + name, privileged=True) + '\'">')
+        else:
+            print('<body>')
+
+        title_done = True
+        send_guru(msg_text, msg_type)
+
+        if self.pragma("navbar", "on") != "on":
+            return
+
+        # NAVBAR
+        print('<nav><div class="nav">')
+        print link_tag('FrontPage', config_get('site_icon', 'Home'), cssclass='navlink')
+        if name:
+            print('  <b>' + link_tag('?a=titlesearch&q=' + name, text, cssclass='navlink') + '</b> ')
+        else:
+            print('  <b>' + text + '</b> ')
+        print(' | ' + link_tag('FindPage', 'Find Page', cssclass='navlink'))
+        history = config_get('history_url')
+        if history:
+            print(' | <a href="' + relative_url(history) + '" class="navlink">Recent Changes</a>')
+            if name:
+                print(' | <a href="' + relative_url(history + '?a=history;f=' + name) + '" class="navlink">Page History</a>')
+
+        if name:
+            print(' | ' + link_tag(name + '?a=raw', 'Raw Text', cssclass='navlink'))
+            if config_get('privileged_url') is not None:
+                if self.can_write():
+                    print(' | ' + link_tag('?a=edit&q=' + name, 'Edit', cssclass='navlink', privileged=True))
+                else:
+                    print(' | ' + link_tag(name, 'Login', cssclass='navlink', privileged=True))
+
+        user = remote_user()
+        if user != 'AnonymousCoward':
+            print(' | <span class="login"><i><b>' + link_tag('User/' + user, user) + '</b></i></span>')
+
+        print('<hr /></div></nav>')
+
+    def send_footer(self):
+        if config_get('debug_cgi', False):
+            cgi.print_arguments()
+            cgi.print_form(form)
+            cgi.print_environ()
+        footer = self.pragma("footer", "sys/footer")
+        if footer != "off":
+            link_inline(footer, kvargs = {
+                'LAST_MODIFIED': strftime(config_get('datetime_fmt', '%Y-%m-%dT%I:%M:%S%p'), localtime(self._mtime()))
+            })
+        print("</body></html>")
+
+    def send_naked(self, kvargs=None):
         if self.can_read():
-            WikiFormatter(self.get_raw_body()).print_html()
+            WikiFormatter(self.get_raw_body(), kvargs).print_html()
         else:
             send_guru("Read access denied by ACLs", "notice")
 
-    def format(self):
+    def send(self):
         #css foo.css
         value = self.pragma("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_title(name=self.page_name, text=self.split_title(), msg_text=self.msg_text, msg_type=self.msg_type)
         self.send_naked()
-        send_footer(self._last_modified())
-
-    def _last_modified(self):
-        try:
-            from time import localtime, strftime
-            modtime = localtime(os.stat(self._filename())[stat.ST_MTIME])
-        except OSError, err:
-            if err.errno != errno.ENOENT:
-                raise err
-            return None
-        return strftime(datetime_fmt, modtime)
+        self.send_footer()
+
+    def send_atom(self):
+        emit_header(self._mtime(), 'application/atom+xml')
+        self.in_html = True
+        link_inline("sys/atom_header", kvargs = {
+            'LAST_MODIFIED': strftime(config_get('datetime_fmt', '%a, %d %b %Y %I:%M:%S %p'), localtime(self._mtime()))
+        })
+        self.in_html = False
+        self.send_naked()
+        self.in_html = True
+        link_inline("sys/atom_footer")
+        self.in_html = False
 
     def send_editor(self, preview=None):
-        send_title(None, 'Edit ' + self.split_title(), msg_text=self.msg_text, msg_type=self.msg_type)
+        self.send_title(text='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
 
-        filename = ''
-        if 'file' in form:
-            filename = form['file'].value
-
-        print ('<p><b>Editing ' + self.page_name
-            + ' for ' + cgi.escape(remote_user())
-            + ' from ' + cgi.escape(get_hostname(remote_host()))
-            + '</b></p>')
-        print '<div class="editor"><form name="editform" method="post" enctype="multipart/form-data" action="%s">' % relative_url(self.page_name)
-        print '<input type="hidden" name="edit" value="%s">' % (self.page_name)
-        print '<input type="input" id="editor" name="changelog" value="Edit page %s" accesskey="c" /><br />' % (self.page_name)
-        print '<textarea wrap="off" spellcheck="true" id="editor" name="savetext" rows="17" cols="100" accesskey="e">%s</textarea>' % cgi.escape(preview or self.get_raw_body())
-        print '<label for="file" accesskey="u">Or Upload a file:</label> <input type="file" name="file" value="%s" />' % filename
-        print """
-            <br />
-            <input type="submit" name="save" value="Save" accesskey="s">
-            <input type="submit" name="preview" value="Preview" accesskey="p" />
-            <input type="reset" value="Reset" />
-            <input type="submit" name="cancel" value="Cancel" />
-            <br />
-            </form></div>
-            <script language="javascript">
-            <!--
-            document.editform.savetext.focus()
-            //-->
-            </script>
-            """
-        print "<p>" + link_tag('EditingTips') + "</p>"
+        if preview is None:
+            preview = self.get_raw_body(default='')
+
+        link_inline("sys/EditPage", kvargs = {
+            'EDIT_BODY': cgi.escape(preview),
+            #'EDIT_PREVIEW': WikiFormatter(preview).print_html(),
+        })
+
         if preview:
-            print "<div class='preview'>"
+            print("<div class='preview'>")
             WikiFormatter(preview).print_html()
-            print "</div>"
-        send_footer()
+            print("</div>")
+        self.send_footer()
 
-    def send_raw(self, mimetype='text/plain'):
-        if self.can_read():
-            body = self.get_raw_body()
-            emit_header(mimetype)
-            print body
-        else:
-            send_title(None, msg_text='Read access denied by ACLs', msg_type='notice')
+    def send_raw(self, mimetype='text/plain', args=[]):
+        if not self.can_read():
+            self.send_title(msg_text='Read access denied by ACLs', msg_type='notice')
+            return
 
-    def send_image(self, mimetype, args=[]):
+        emit_header(self._mtime(), mimetype)
         if 'maxwidth' in args:
             import subprocess
-            emit_header(mimetype)
             sys.stdout.flush()
-            subprocess.check_call(['gm', 'convert', self._filename(),
+            subprocess.check_call(['convert', self._filename(),
+                '-auto-orient', '-orient', 'TopLeft',
                 '-scale', args['maxwidth'].value + ' >', '-'])
         else:
-            self.send_raw(mimetype)
+            body = self.get_raw_body()
+            print(body)
 
     def _write_file(self, data):
         tmp_filename = self._tmp_filename()
@@ -761,71 +797,52 @@ class Page:
                 os.remove(name)
             except OSError, err:
                 if err.errno != errno.ENOENT: raise err
+        path = os.path.split(name)[0]
+        if path and not os.path.exists(path):
+            os.makedirs(path)
         os.rename(tmp_filename, name)
 
     def save(self, newdata, changelog):
         if not self.can_write():
-            self.msg_text = 'Write access denied by ACLs'
-            self.msg_type = 'error'
+            self.msg_text = 'Write access denied by Access Control List'
+            return
+        if not is_privileged():
+            self.msg_text = 'Unauthenticated access denied'
             return
 
         self._write_file(newdata)
         rc = 0
-        if post_edit_hook:
-            # FIXME: what's the std way to perform shell quoting in python?
-            cmd = ( post_edit_hook
-                + " '" + data_dir + '/' + self.page_name
-                + "' '" + remote_user()
-                + "' '" + remote_host()
-               + "' '" + changelog + "'"
-            )
-            out = os.popen(cmd)
-            output = out.read()
-            rc = out.close()
+        if config_get('post_edit_hook'):
+            import subprocess
+            cmd = [
+                config_get('post_edit_hook'),
+                self.page_name, remote_user(),
+                remote_host(), changelog ]
+            child = subprocess.Popen(cmd, stdout=subprocess.PIPE, close_fds=True)
+            output = child.stdout.read()
+            rc = child.wait()
         if rc:
-            self.msg_text += "Post-editing hook returned %d.\n" % rc
-            self.msg_text += 'Command was: ' + cmd + '\n'
+            self.msg_text += "Post-editing hook returned %d. Command was:\n'%s'\n" % (rc, "' '".join(cmd))
             if output:
                 self.msg_text += 'Output follows:\n' + output
         else:
             self.msg_text = 'Thank you for your contribution.  Your attention to detail is appreciated.'
             self.msg_type = 'success'
 
-def main():
-    for cmd in form:
-        handler = globals().get('handle_' + cmd)
-        if handler:
-            handler(form[cmd].value)
-            break
-    else:
-        query = query_string()
-        if file_re.match(query):
-            if word_re.match(query):
-                Page(query).format()
-            else:
-                from mimetypes import MimeTypes
-                mimetype, encoding = MimeTypes().guess_type(query)
-                if mimetype:
-                    if mimetype.startswith('image/'):
-                        Page(query).send_image(mimetype=mimetype, args=form)
-                    else:
-                        Page(query).send_raw(mimetype=mimetype)
-                else:
-                    Page(query).format()
-        else:
-            send_httperror("403 Forbidden", query)
-
 try:
-    execfile("geekigeeki.conf.py")
+    exec(open("geekigeeki.conf.py").read())
+    os.chdir(config_get('data_dir', 'data'))
     form = cgi.FieldStorage()
-    main()
-except Exception:
-    import traceback
-    msg_text = traceback.format_exc()
-    if title_done:
-        send_guru(msg_text, "error")
+    action = form.getvalue('a', 'get')
+    handler = globals().get('handle_' + action)
+    if handler:
+        handler(query_string(), form)
     else:
-        send_title(None, msg_text=msg_text)
-    send_footer()
+        send_httperror("403 Forbidden", query_string())
+
+except HttpException, e:
+    send_httperror(e.error, query=e.query)
+except Exception:
+    send_httperror("500 Internal Server Error", query=query_string(), trace=True)
 
 sys.stdout.flush()