-#! /usr/bin/env python
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
#
# Copyright 1999, 2000 Martin Pool <mbp@humbug.org.au>
# Copyright 2002 Gerardo Poggiali
-# Copyright 2007 Bernardo Innocenti
+# Copyright 2007, 2008 Bernardo 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
# 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$")
except:
return addr
+def relative_url(path, privileged=False):
+ if not (url_re.match(path) or path.startswith('/')):
+ if privileged:
+ url = privileged_path()
+ else:
+ url = script_name()
+ path = url + '/' + path
+ return path
+
# Formatting stuff --------------------------------------------------
def emit_header(type="text/html"):
# Head
emit_header()
- print """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
- "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
-"""
+ 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">'
+
+ site_name = globals().get('site_name', 'Unconfigured Site')
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" />'
- if css_url:
- print ' <link rel="stylesheet" type="text/css" href="%s" />' % css_url
+ for css in css_url:
+ print ' <link rel="stylesheet" type="text/css" href="%s" />' % relative_url(css)
print '</head>'
# Body
print '<div class="nav">'
print ' <b>' + site_name + ': ',
if name:
- print link_tag('?fullsearch=' + name, text) + '</b> '
+ print link_tag('?fullsearch=' + name, text, 'navlink') + '</b> '
else:
print text + '</b> '
print ' | ' + link_tag('FrontPage', 'Front Page', 'navlink')
print ' | ' + link_tag('FindPage', 'Find Page', 'navlink')
- print ' | <a href="' + history_url + '" class="navlink">Recent Changes</a>'
+ if globals().has_key('history_url'):
+ print ' | <a href="' + history_url + '" class="navlink">Recent Changes</a>'
+ if name:
+ print ' | <a href="' + history_url + '?a=history;f=' + name + '" class="navlink">Page History</a>'
if name:
- print ' | <a href="' + history_url + '?a=history;f=' + name + '" class="navlink">Page History</a>'
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 ' | <i>Immutable Page</i>'
print '<hr /></div>'
-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 = ''
# 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 '<a %shref="%s/%s">%s</a>' % (classattr, path, params, text)
+ return '<a %shref="%s">%s</a>' % (classattr, relative_url(params, privileged=privileged), text)
# Search ---------------------------------------------------
for (count, page_name) in hits:
print '<li><p>' + Page(page_name).link_to()
print ' . . . . ' + `count`
- print ['match', 'matches'][count <> 1]
+ print ['match', 'matches'][count != 1]
print '</p></li>'
print "</ul>"
def print_search_stats(hits, searched):
print "<p>%d hits out of %d pages searched.</p>" % (hits, searched)
+#TODO: merge into do_savepage()
def do_edit(pagename):
Page(pagename).send_editor()
return s
def page_list():
- return filter(word_re.match, os.listdir(text_dir))
+ return filter(word_re.match, os.listdir(data_dir))
def send_footer(name, mod_string=None):
- if debug_cgi:
+ if globals().get('debug_cgi', False):
cgi.print_arguments()
cgi.print_form(cgi.FieldStorage())
cgi.print_environ()
# set title
for word in all_words:
letter = string.lower(word[0])
- if letter <> last_letter:
+ if letter != last_letter:
s = s + '; <a name="%s"><h3>%s</h3></a>' % (letter, letter)
last_letter = letter
current_letter = None
for name in pages:
letter = string.lower(name[0])
- if letter <> current_letter:
+ if letter != current_letter:
s = s + '<a name="%s"><h3>%s</h3></a>' % (letter, letter)
current_letter = letter
else:
"""
def __init__(self, raw):
self.raw = raw
- self.is_em = self.is_b = 0
self.h_level = 0
- self.h_count = 0
- self.list_indents = []
- self.in_pre = False
- self.in_table = False
- self.tr_cnt = 0
- self.in_var = False
+ self.in_pre = self.in_table = False
self.in_header = True
+ self.list_indents = []
+ self.tr_cnt = self.h_cnt = 0
+ self.styles = {
+ #wiki html enabled?
+ "//": ["em", False],
+ "''": ["em", False],
+ "**": ["b", False],
+ "'''": ["b", False],
+ "##": ["tt", False],
+ "``": ["tt", False],
+ "__": ["u", False],
+ "^^": ["sup", False],
+ ",,": ["sub", False]
+ }
- def _emph_repl(self, word):
- if len(word) == 3:
- self.is_b = not self.is_b
- return ['</strong>', '<strong>'][self.is_b]
- else:
- self.is_em = not self.is_em
- return ['</em>', '<em>'][self.is_em]
+ def _b_repl(self, word):
+ style = self.styles[word]
+ style[1] = not style[1]
+ return ['</','<'][style[1]] + style[0] + '>'
def _tit_repl(self, word):
if self.h_level:
self.h_level = 0
else:
self.h_level = len(word) - 1
- self.h_count += 1
- result = '<h%d id="%d"><a class="heading" href="#%d">*</a> ' % (self.h_level, self.h_count, self.h_count)
+ self.h_cnt += 1
+ #abridged = re.sub('[^a-z_]', '', word.lower().replace(' ', '_'))
+ result = '<h%d id="%d"><a class="heading" href="#%d">ΒΆ</a> ' % (self.h_level, self.h_cnt, self.h_cnt)
return result
+ def _br_repl(self, word):
+ return '<br />'
+
def _rule_repl(self, word):
return self._undent() + '\n<hr size="%d" noshade="noshade" />\n' % (len(word) - 2)
return Page(word).link_to()
def _img_repl(self, word):
- path = script_name() + '/' + word;
+ path = relative_url(word)
return '<a href="%s"><img border="0" src="%s" /></a>' % (path, path)
def _url_repl(self, word):
return '<a href="%s" rel="nofollow" class="external">%s</a>' % (word, word)
def _hurl_repl(self, word):
- m = re.compile("\[\[(\S+)([^\]]*)\]\]").match(word)
+ m = re.compile("\[\[([^ \t\n\r\f\v\|]+)(?:\s*\|\s*([^\]]+)|)\]\]").match(word)
name = m.group(1)
- descr = m.group(2).strip() or name
+ descr = m.group(2) or name
macro = globals().get('_macro_' + name)
if macro:
return apply(macro, (name, descr))
elif img_re.match(name):
- return '<a href="%s"><img border="0" src="%s" alt="%s" /></a>' % (name, name, descr)
- elif url_re.match(name):
- return '<a href="%s" rel="nofollow" class="external">%s</a>' % (name, descr)
- elif name.startswith('/'):
- return '<a href="%s">%s</a>' % (name, descr)
+ # 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, descr, descr)
else:
- return link_tag(name, descr)
+ if img_re.match(descr):
+ descr = '<img border="0" src="' + descr + '" />'
+
+ if url_re.match(name):
+ return '<a href="%s" rel="nofollow" class="external">%s</a>' % (name, descr)
+ elif name.startswith('/'):
+ return '<a href="%s">%s</a>' % (name, descr)
+ else:
+ return link_tag(name, descr, 'wikilink')
def _email_repl(self, word):
return '<a href="mailto:%s">%s</a>' % (word, word)
cl = 'notice'
return '<strong class="highlight ' + cl + '">' + word + '</strong>'
- def _var_repl(self, word):
- if word == '{{' and not self.in_var:
- self.in_var = True
- return '<code>'
- elif self.in_var:
- self.in_var = False
- return '</code>'
- return ''
-
def _tr_repl(self, word):
out = ''
if not self.in_table:
self.tr_cnt = 0
out = '</p><table><tbody>\n'
self.tr_cnt += 1
- return out + '<tr class="' + ['even', 'odd'][self.tr_cnt % 2] + '"><td>'
+ out = out + '<tr class="' + ['even', 'odd'][self.tr_cnt % 2] + '">'
+ return out + ['<td>', '<th>'][word.strip() == '||=']
- def _tre_repl(self, word):
+ def _td_repl(self, word):
if self.in_table:
- return '</td></tr>'
+ return ['</td><td>', '</th><th>'][word.strip() == '||=']
return ''
- def _td_repl(self, word):
+ def _tre_repl(self, word):
if self.in_table:
- return '</td><td>'
+ return ['</td></tr>', '</th></tr>'][word.strip() == '||=']
return ''
def _indent_level(self):
scan_re = re.compile(
r"(?:"
# Formatting
- + r"(?P<emph>'{2,3})"
+ + r"(?P<b>\*\*|'''|//|''|##|``|__|\^\^|,,)"
+ r"|(?P<tit>\={2,6})"
+ + r"|(?P<br>\\\\)"
+ r"|(?P<rule>^-{3,})"
+ r"|(?P<html><(/|)(div|span|iframe)[^<>]*>)"
+ r"|(?P<ent>[<>&])"
# Links
+ r"|(?P<img>\b[a-zA-Z0-9_-]+\.(png|gif|jpg|jpeg|bmp))"
+ r"|(?P<word>\b(?:[A-Z][a-z]+){2,}\b)"
- + r"|(?P<hurl>\[\[\S+[^\]]*\]\])"
+ + r"|(?P<hurl>\[\[([^ \t\n\r\f\v\|]+)(?:\s*\|\s*([^\]]+)|)\]\])"
+ r"|(?P<url>(http|https|ftp|mailto)\:[^\s'\"]+\S)"
+ r"|(?P<email>[-\w._+]+\@[\w.-]+)"
# Lists, divs, spans
- + r"|(?P<li>^\s+\*)"
- + r"|(?P<pre>(\{\{\{|\s*\}\}\}))"
- + r"|(?P<var>(\{\{|\}\}))"
+ + r"|(?P<li>^\s+[\*#] +)"
+ + r"|(?P<pre>\{\{\{|\s*\}\}\})"
# Tables
- + r"|(?P<tr>^\s*\|\|\s*)"
- + r"|(?P<tre>\s*\|\|\s*$)"
- + r"|(?P<td>\s*\|\|\s*)"
+ + r"|(?P<tr>^\s*\|\|(=|)\s*)"
+ + r"|(?P<tre>\s*\|\|(=|)\s*$)"
+ + r"|(?P<td>\s*\|\|(=|)\s*)"
+ r")")
pre_re = re.compile(
r"(?:"
self.page_name = page_name
self.msg = ''
self.msg_type = 'error'
- self.attrs = {}
def split_title(self):
# look for the end of words and the start of a new word,
return re.sub('([a-z])([A-Z])', r'\1 \2', self.page_name)
def _text_filename(self):
- return path.join(text_dir, self.page_name)
+ return path.join(data_dir, self.page_name)
def _tmp_filename(self):
- return path.join(text_dir, ('#' + self.page_name + '.' + `os.getpid()` + '#'))
+ return path.join(data_dir, ('#' + self.page_name + '.' + `os.getpid()` + '#'))
def exists(self):
try:
os.stat(self._text_filename())
- return 1
+ return True
except OSError, er:
if er.errno == errno.ENOENT:
- return 0
+ return False
else:
raise er
return open(self._text_filename(), 'rt').read()
except IOError, er:
if er.errno == errno.ENOENT:
- # just doesn't exist, use default
- return 'Describe %s here.' % self.page_name
- else:
- raise er
+ return '' # just doesn't exist, use default
+ raise er
def get_attrs(self):
- if self.attrs:
+ if self.__dict__.has_key('attrs'):
return self.attrs
+ self.attrs = {}
try:
file = open(self._text_filename(), 'rt')
attr_re = re.compile(r"^#(\S*)(.*)$")
raise er
return self.attrs
+ def get_attr(self, name, default):
+ if self.get_attrs().has_key(name):
+ return self.get_attrs()[name]
+ else:
+ return default
+
def can(self, action, default=True):
- attrs = self.get_attrs()
try:
- # SomeUser:read,write All:read
- acl = attrs["acl"]
+ #acl SomeUser:read,write All:read
+ acl = self.get_attr("acl", None)
for rule in acl.split():
(user,perms) = rule.split(':')
if user == remote_user() or user == "All":
page_name = None
if self.can_write():
page_name = self.page_name
+
+ #FIXME: are there security implications?
+ #css foo.css bar.css
+ global css_url
+ css_url = css_url + self.get_attr("css", "").split()
+
send_title(page_name, self.split_title(), msg=self.msg, msg_type=self.msg_type)
if self.can_read():
PageFormatter(self.get_raw_body()).print_html()
send_footer(page_name, self._last_modified())
def _last_modified(self):
- if not self.exists():
+ try:
+ from time import localtime, strftime
+ modtime = localtime(os.stat(self._text_filename())[stat.ST_MTIME])
+ except OSError, er:
+ if er.errno != errno.ENOENT:
+ raise er
return None
- from time import localtime, strftime
- modtime = localtime(os.stat(self._text_filename())[stat.ST_MTIME])
return strftime(datetime_fmt, modtime)
def send_editor(self, preview=None):
+ ' for ' + cgi.escape(remote_user())
+ ' from ' + cgi.escape(get_hostname(remote_host()))
+ '</b></p>')
- print '<div class="editor"><form method="post" action="%s/%s">' % (script_name(), self.page_name)
+ print '<div class="editor"><form method="post" action="%s">' % relative_url(self.page_name)
print '<input type="hidden" name="savepage" value="%s">' % (self.page_name)
- print """<textarea wrap="virtual" id="editor" name="savetext" rows="17" cols="80">%s</textarea>""" % (preview or self.get_raw_body())
+ print """<textarea wrap="off" spellcheck="true" id="editor" name="savetext" rows="17" cols="100">%s</textarea>""" % (preview or self.get_raw_body())
print """
<br />
<input type="submit" name="save" value="Save" />
try:
os.remove(text)
except OSError, er:
- if er.errno <> errno.ENOENT: raise er
+ if er.errno != errno.ENOENT: raise er
os.rename(tmp_filename, text)
def save_text(self, newtext):
if post_edit_hook:
# FIXME: what's the std way to perform shell quoting in python?
cmd = ( post_edit_hook
- + " '" + text_dir + '/' + self.page_name
+ + " '" + data_dir + '/' + self.page_name
+ "' '" + remote_user()
+ "' '" + remote_host() + "'"
)
self.msg = 'Thank you for your contribution. Your attention to detail is appreciated.'
self.msg_type = 'success'
+#TODO: merge into send_raw()
def send_verbatim(filename, mime_type='application/octet-stream'):
- pathname = path.join(text_dir, filename)
+ pathname = path.join(data_dir, filename)
data = open(pathname, 'rb').read()
emit_header(mime_type)
sys.stdout.write(data)
# Main ---------------------------------------------------------------
try:
- # Configuration values
- site_name = 'Codewiz'
-
- # set to None for read-only sites, leave empty ('') to allow anonymous edits
- # otherwise, set to a URL that requires authentication
- privileged_url = 'https://www.codewiz.org/~bernie/wiki'
-
- data_dir = '/home/bernie/public_html/wiki'
- text_dir = path.join(data_dir, 'text')
- css_url = '../wikidata/geekigeeki.css' # optional stylesheet link
- history_url = '../wikigit/wiki.git'
- post_edit_hook = './post_edit_hook.sh'
- datetime_fmt = '%a %d %b %Y %I:%M %p'
- allow_edit = True # Is it possible to edit pages?
- show_hosts = True # show hostnames?
- nonexist_pfx = '' # prefix before nonexistent link (usually '?')
- debug_cgi = False # Set to True for CGI var dump
+ execfile("geekigeeki.conf.py")
form = cgi.FieldStorage()
else:
send_verbatim(query)
else:
- # TODO: return 404?
+ print "Status: 404 Not Found"
send_title(None, msg='Can\'t work out query: ' + query)
except:
import traceback