from time import clock
start_time = clock()
+title_done = False
import cgi, sys, os, re, errno, stat
-# 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$")
# 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(r"(?:\[\[|{{)([^\s\|]+)(?:\s*\|\s*([^\]]+)|)(?:\]\]|}})")
ext_re = re.compile(r"\.([^\./]+)$")
-title_done = False
-
# CGI stuff ---------------------------------------------------------
def script_name():
return os.environ.get('SCRIPT_NAME', '')
def permalink(s):
return re.sub(' ', '-', re.sub('[^a-z0-9_ ]', '', s.lower()).strip())
+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 = []
+ kwargs = {}
+ for arg in s.strip('<[{}]>').split('|'):
+ m = re.match('\s*(\w+)\s*=\s*(.+)\s*', arg)
+ if m is not None:
+ kwargs[m.group(1)] = m.group(2)
+ else:
+ args.append(arg.strip())
+ return (args, kwargs)
+
+def url_args(kvargs):
+ argv = []
+ for k, v in kvargs.items():
+ argv.append(k + '=' + v)
+ if argv:
+ return '?' + '&'.join(argv)
+ return ''
+
# Formatting stuff --------------------------------------------------
def emit_header(mime_type="text/html"):
print("Content-type: " + mime_type + "; charset=utf-8\n")
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))
+ % (privileged_path() + '?a=edit&q=' + name))
if history_url is not None:
print(' <link rel="alternate" type="application/rss+xml" title="RSS" href="%s" />' \
# Body
if name and writable and privileged_url is not None:
- print('<body ondblclick="location.href=\'' + privileged_path() + '?edit=' + name + '\'">')
+ print('<body ondblclick="location.href=\'' + privileged_path() + '?a=edit&q=' + name + '\'">')
else:
print('<body>')
# Navbar
print('<div class="nav">')
- print link_tag('FrontPage', site_icon or 'Home', 'navlink')
+ print link_tag('FrontPage', site_icon or 'Home', cssclass='navlink')
if name:
- print(' <b>' + link_tag('?fullsearch=' + name, text, 'navlink') + '</b> ')
+ print(' <b>' + link_tag('?fullsearch=' + name, text, cssclass='navlink') + '</b> ')
else:
print(' <b>' + text + '</b> ')
- print(' | ' + link_tag('FindPage', 'Find Page', 'navlink'))
+ print(' | ' + link_tag('FindPage', 'Find Page', cssclass='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'))
+ print(' | ' + link_tag(name + '?a=raw', 'Raw Text', cssclass='navlink'))
if privileged_url is not None:
if writable:
- print(' | ' + link_tag('?edit=' + name, 'Edit', 'navlink', privileged=True))
+ print(' | ' + link_tag('?a=edit&q=' + name, 'Edit', cssclass='navlink', privileged=True))
else:
- print(' | ' + link_tag(name, 'Login', 'navlink', privileged=True))
+ print(' | ' + link_tag(name, 'Login', cssclass='navlink', privileged=True))
else:
print(' | <i>Immutable Page</i>')
send_title(None, msg_text=("%s: on query '%s'" % (status, query)))
send_footer()
-def link_tag(params, text=None, link_class=None, privileged=False):
+def link_tag(dest, text=None, privileged=False, **kvargs):
if text is None:
- text = params # default
+ text = humanlink(dest)
elif img_re.match(text):
text = '<img 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 = 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"'
+ nofollow = 'rel="nofollow" '
- return '<a %shref="%s">%s</a>' % (classattr, relative_url(params, privileged=privileged), text)
+ return '<a class="%s" %shref="%s">%s</a>' % (link_class, nofollow, relative_url(dest, privileged=privileged), text)
-def link_inline(name, descr=None, args=''):
- if not descr: descr = name
+def link_inline(name, descr=None, kvargs={}):
+ if not descr: descr = humanlink(name)
url = relative_url(name)
if video_re.match(name):
- return '<video src="%s">Your browser does not support the HTML5 video tag</video>' % url
+ return '<video controls="1" src="%s">Your browser does not support the HTML5 video tag</video>' % url
elif img_re.match(name):
- return '<a href="%s"><img border="0" src="%s" alt="%s" /></a>' % (url, url + args, descr)
+ 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
return Page(name).send_naked()
else:
# Search ---------------------------------------------------
-def handle_fullsearch(needle):
- send_title(None, 'Full text search for "%s"' % (needle))
+def print_search_stats(hits, searched):
+ print("<p>%d hits out of %d pages searched.</p>" % (hits, searched))
+
+def handle_fullsearch(query, form):
+ needle = form['q'].value
+ send_title(None, 'Full text search for "' + needle + '"')
needle_re = re.compile(needle, re.IGNORECASE)
hits = []
print_search_stats(len(hits), len(all_pages))
-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
+ send_title(None, 'Title search for "' + needle + '"')
needle_re = re.compile(needle, re.IGNORECASE)
all_pages = page_list()
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):
+def handle_raw(pagename, form):
if not file_re.match(pagename):
send_httperror("403 Forbidden", pagename)
return
Page(pagename).send_raw()
-def handle_edit(pagename):
+def handle_edit(pagename, form):
if not file_re.match(pagename):
send_httperror("403 Forbidden", pagename)
return
- pg = Page(pagename)
+ pg = Page(form['q'].value)
if 'save' in form:
if form['file'].value:
pg.save(form['file'].file.read(), form['changelog'].value)
text = form['savetext'].value
pg.send_editor(text)
+def handle_get(pagename, form):
+ if file_re.match(pagename):
+ # FIMXE: this is all bullshit, MimeTypes bases its guess on the extension!
+ from mimetypes import MimeTypes
+ mimetype, encoding = MimeTypes().guess_type(pagename)
+ if mimetype:
+ Page(pagename).send_raw(mimetype=mimetype, args=form)
+ else:
+ Page(pagename).format()
+ else:
+ send_httperror("403 Forbidden", pagename)
+
# Used by macros/WordIndex and macros/TitleIndex
def make_index_key():
links = ['<a href="#%s">%s</a>' % (ch, ch) for ch in 'abcdefghijklmnopqrstuvwxyz']
return '<p style="text-align: center">' + ' | '.join(links) + '</p>'
-def page_list(dirname = None, re = word_re):
+def page_list(dirname = None, re = None):
+ if re is None:
+ # FIXME: WikiWord is too restrictive now!
+ re = re.compile(r"^\b((([A-Z][a-z0-9]+){2,}/)*([A-Z][a-z0-9]+){2,})\b$")
return sorted(filter(re.match, os.listdir(dirname or data_dir)))
def send_footer(mod_string=None):
return self._undent() + '\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 = list(map(str.strip, argv))
-
- macro = globals().get('_macro_' + name)
- if not macro:
- try:
+ try:
+ args, kwargs = parse_args(word)
+ macro = globals().get('_macro_' + args[0])
+ if not macro:
exec(open("macros/" + name + ".py").read(), globals())
- except IOError as err:
- if err.errno == errno.ENOENT: pass
- macro = globals().get('_macro_' + name)
- if macro:
- return macro(argv)
- else:
- msg = '<<' + '|'.join(argv) + '>>'
+ macro = globals().get('_macro_' + name)
+ return macro(*args, **kwargs)
+ except Exception:
+ msg = cgi.escape(word)
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))
+ args, kvargs = parse_args(word)
+ return link_tag(*args, **kvargs)
def _inl_repl(self, word):
- (name, descr) = link_re.match(word).groups()
-
- if descr:
- argv = descr.split('|')
- descr = argv.pop(0)
- args = ''
- if argv:
- args = '?' + '&'.join(argv)
-
- # The "extthumb" nonsense works around a limitation of the HTML block model
- return '<div class="extthumb"><div class="thumb">' \
- + link_inline(name, descr, args) \
+ 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(name, descr, kvargs) \
+ '<div class="caption">' + descr + '</div></div></div>'
else:
- return link_inline(name, name)
+ return link_inline(name, None, kvargs)
def _html_repl(self, word):
if not self.in_html and word.startswith('<div'): word = '</p>' + word
except IOError as err:
if err.errno == errno.ENOENT:
if default is None:
- default = '//[[?edit=%s|Describe %s]]//' % (self.page_name, self.page_name)
+ default = '//[[%s|Describe %s|action=edit]]//' % (self.page_name, self.page_name)
return default
if err.errno == errno.EISDIR:
return self.format_dir()
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'
+ maxwidth_arg = ' | maxwidth=' + str(image_maxwidth)
+ out += '{{' + self.page_name + '/' + filename + ' | ' + humanlink(filename) + maxwidth_arg + ' | class=thumbleft}}\n'
else:
out += ' * [[' + self.page_name + '/' + filename + ']]\n'
return out
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):
- # FIMXE: this is all bullshit, MimeTypes bases its guess on the extension!
- from mimetypes import MimeTypes
- mimetype, encoding = MimeTypes().guess_type(query)
- if mimetype:
- Page(query).send_raw(mimetype=mimetype, args=form)
- else:
- Page(query).format()
- else:
- send_httperror("403 Forbidden", query)
-
try:
exec(open("geekigeeki.conf.py").read())
form = cgi.FieldStorage()
- main()
+ action = form.getvalue('a', 'get')
+ handler = globals().get('handle_' + action)
+ if handler:
+ handler(query_string(), form)
+ else:
+ send_httperror("403 Forbidden", query_string())
+
except Exception:
import traceback
msg_text = traceback.format_exc()