Merge remote-tracking branch 'giskard/master'
[geekigeeki.git] / geekigeeki.py
index 1891fdd8c5842cea4357d5339009ad40f90134d6..f6163110b35b15c6b8d313158a8f42d9145303ca 100755 (executable)
@@ -3,7 +3,7 @@
 #
 # 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>
+# Copyright (C) 2007, 2008, 2009, 2010, 2011 Bernie Innocenti <bernie@codewiz.org>
 #
 # This program is free software: you can redistribute it and/or modify
 # it under the terms of the GNU Affero General Public License as
@@ -110,8 +110,9 @@ def emit_header(mtime=None, mime_type="text/html"):
         # 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")
+    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):
@@ -125,7 +126,7 @@ def send_guru(msg_text, msg_type):
     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):
+def send_httperror(status="404 Not Found", query="", trace=False):
     print("Status: %s" % status)
     msg_text = "%s: on query '%s'" % (status, query)
     if trace:
@@ -151,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 '<a class="%s" %shref="%s">%s</a>' % (link_class, nofollow, relative_url(dest, privileged=privileged), text)
@@ -230,7 +231,7 @@ def handle_atom(pagename, form):
     Page(pagename).send_atom()
 
 def handle_edit(pagename, form):
-    pg  = Page(form['q'].value)
+    pg  = Page(pagename)
     if 'save' in form:
         if form['file'].value:
             pg.save(form['file'].file.read(), form['changelog'].value)
@@ -305,13 +306,13 @@ class WikiFormatter:
         return '&mdash;'
 
     def _tit_repl(self, word):
+        link = permalink(self.line)
         if self.h_level:
-            result = '</h%d><p>\n' % self.h_level
+            result = '<a class="heading" href="#%s">¶</a></h%d><p>\n' % (link, self.h_level)
             self.h_level = 0
         else:
             self.h_level = len(word) - 1
-            link = permalink(self.line)
-            result = '\n</p><h%d id="%s"><a class="heading" href="#%s">¶</a> ' % (self.h_level, link, link)
+            result = '\n</p><h%d id="%s">' % (self.h_level, link)
         return result
 
     def _br_repl(self, word):
@@ -474,8 +475,8 @@ class WikiFormatter:
             | (?P<hurl>  \[\[[^\]]+\]\])
 
             # Inline HTML
-            | (?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<html>             <(br|hr|small|div|span|form|iframe|input|textarea|a|img|h[1-5])\b )
+            | (?P<htmle> ( /\s*> | </(br|hr|small|div|span|form|iframe|input|textarea|a|img|h[1-5])> ) )
             | (?P<ent>   [<>&] )
 
             # Auto links (LEGACY)
@@ -493,8 +494,6 @@ class WikiFormatter:
             | (?P<tr>    ^\s*\|\|(=|)\s*)
             | (?P<tre>   \s*\|\|(=|)\s*$)
             | (?P<td>    \s*\|\|(=|)\s*)
-
-            # TODO: highlight search words (look at referrer)
           )""", re.VERBOSE)
         pre_re = re.compile("""(?:
               (?P<pre>\s*\}\}\})
@@ -569,14 +568,12 @@ class Page:
             return True
         return False
 
-    def get_raw_body(self, default=None):
+    def get_raw_body(self):
         try:
             return open(self._filename(), 'rb').read()
         except IOError, err:
             if err.errno == errno.ENOENT:
-                if default is None:
-                    default = '//[[?a=edit&q=%s|Describe %s]]//' % (self.page_name, self.page_name)
-                return default
+                return ''
             if err.errno == errno.EISDIR:
                 return self.format_dir()
             raise err
@@ -646,7 +643,7 @@ class Page:
         if title_done: return
 
         # HEAD
-        emit_header(self._mtime())
+        emit_header(name and 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">')
@@ -663,7 +660,7 @@ class Page:
         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))
+                % relative_url(name + '?a=edit', privileged=True))
 
         print(' <link rel="alternate" type="application/atom+xml" title="Atom feed" href="%s" />' \
             % relative_url(name + '?a=atom'))
@@ -672,7 +669,7 @@ class Page:
 
         # BODY
         if editable:
-            print('<body ondblclick="location.href=\'' + relative_url('?a=edit&q=' + name, privileged=True) + '\'">')
+            print('<body ondblclick="location.href=\'' + relative_url(name + '?a=edit', privileged=True) + '\'">')
         else:
             print('<body>')
 
@@ -700,13 +697,13 @@ class Page:
             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))
+                    print(' | ' + link_tag(name + '?a=edit', 'Edit', cssclass='navlink', privileged=True))
                 else:
-                    print(' | ' + link_tag(name, 'Login', cssclass='navlink', privileged=True))
+                    print(' | ' + link_tag(name, 'Login', cssclass='login', privileged=True))
 
         user = remote_user()
         if user != 'AnonymousCoward':
-            print(' | <span class="login"><i><b>' + link_tag('User/' + user, user) + '</b></i></span>')
+            print(' | ' + link_tag('user/' + user, user, cssclass='login'))
 
         print('<hr /></div></nav>')
 
@@ -720,13 +717,16 @@ class Page:
             link_inline(footer, kvargs = {
                 'LAST_MODIFIED': strftime(config_get('datetime_fmt', '%Y-%m-%dT%I:%M:%S%p'), localtime(self._mtime()))
             })
-        print("</body></html>")
+        print('</body></html>')
 
     def send_naked(self, kvargs=None):
         if self.can_read():
-            WikiFormatter(self.get_raw_body(), kvargs).print_html()
+            body = self.get_raw_body()
+            if not body:
+                body = "//[[%s?a=edit|Describe %s]]//" % (self.page_name, self.page_name)
+            WikiFormatter(body, kvargs).print_html()
         else:
-            send_guru("Read access denied by ACLs", "notice")
+            send_guru('Read access denied by ACLs', 'notice')
 
     def send(self):
         #css foo.css
@@ -758,7 +758,7 @@ class Page:
             return
 
         if preview is None:
-            preview = self.get_raw_body(default='')
+            preview = self.get_raw_body()
 
         link_inline("sys/EditPage", kvargs = {
             'EDIT_BODY': cgi.escape(preview),