projects
/
geekigeeki.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Simplify code in a few places
[geekigeeki.git]
/
geekigeeki.py
diff --git
a/geekigeeki.py
b/geekigeeki.py
index 43b9cc1107ac68b484b205b86e69f79d18107b25..5340646c63101b24629d96a26ff2adea8e66a3b7 100755
(executable)
--- a/
geekigeeki.py
+++ b/
geekigeeki.py
@@
-3,7
+3,7
@@
#
# Copyright 1999, 2000 Martin Pool <mbp@humbug.org.au>
# Copyright 2002 Gerardo Poggiali
#
# Copyright 1999, 2000 Martin Pool <mbp@humbug.org.au>
# Copyright 2002 Gerardo Poggiali
-# Copyright 2007 Bernardo Innocenti <bernie@codewiz.org>
+# 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
#
# 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
@@
-28,8
+28,8
@@
from os import path, environ
# Regular expression defining a WikiWord
# (but this definition is also assumed in other places)
# 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$")
img_re = re.compile(r"^.*\.(png|gif|jpg|jpeg)$", re.IGNORECASE)
url_re = re.compile(r"^[a-z]{3,8}://[^\s'\"]+\S$")
@@
-60,6
+60,15
@@
def get_hostname(addr):
except:
return addr
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"):
# Formatting stuff --------------------------------------------------
def emit_header(type="text/html"):
@@
-114,7
+123,7
@@
def send_title(name, text="Limbo", msg=None, msg_type='error'):
if not name:
print ' <meta name="robots" content="noindex,nofollow" />'
for css in css_url:
if not name:
print ' <meta name="robots" content="noindex,nofollow" />'
for css in css_url:
- print ' <link rel="stylesheet" type="text/css" href="%s" />' %
css
+ print ' <link rel="stylesheet" type="text/css" href="%s" />' %
relative_url(css)
print '</head>'
# Body
print '</head>'
# Body
@@
-143,7
+152,7
@@
def send_title(name, text="Limbo", msg=None, msg_type='error'):
if name:
print ' | ' + link_tag('?raw=' + name, 'Raw Text', 'navlink')
if privileged_url is not None:
if name:
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>'
else:
print ' | <i>Immutable Page</i>'
@@
-153,7
+162,7
@@
def send_title(name, text="Limbo", msg=None, msg_type='error'):
print '<hr /></div>'
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 = ''
if text is None:
text = params # default
classattr = ''
@@
-162,11
+171,9
@@
def link_tag(params, text=None, ss_class=None, authentication=False):
# Prevent crawlers from following links potentially added by spammers or to generated pages
if ss_class == 'external' or ss_class == 'navlink':
classattr += 'rel="nofollow" '
# 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)
+ elif url_re.match(params):
+ classattr += 'rel="nofollow" '
+ return '<a %shref="%s">%s</a>' % (classattr, relative_url(params, privileged=privileged), text)
# Search ---------------------------------------------------
# Search ---------------------------------------------------
@@
-318,11
+325,11
@@
def _macro_TitleIndex(*vargs):
for name in pages:
letter = string.lower(name[0])
if letter != current_letter:
for name in pages:
letter = string.lower(name[0])
if letter != current_letter:
- s
= s +
'<a name="%s"><h3>%s</h3></a>' % (letter, letter)
+ s
+=
'<a name="%s"><h3>%s</h3></a>' % (letter, letter)
current_letter = letter
else:
current_letter = letter
else:
- s
= s +
'<br />'
- s
= s +
Page(name).link_to()
+ s
+=
'<br />'
+ s
+=
Page(name).link_to()
return s
return s
@@
-379,7
+386,7
@@
class PageFormatter:
return Page(word).link_to()
def _img_repl(self, word):
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"><img border="0" src="%s" /></a>' % (path, path)
def _url_repl(self, word):
@@
-389,7
+396,7
@@
class PageFormatter:
return '<a href="%s" rel="nofollow" class="external">%s</a>' % (word, word)
def _hurl_repl(self, word):
return '<a href="%s" rel="nofollow" class="external">%s</a>' % (word, word)
def _hurl_repl(self, word):
- m = re.compile("\[\[(
\S+)(?:\s*\|\s*([^\]]*
)|)\]\]").match(word)
+ m = re.compile("\[\[(
[^ \t\n\r\f\v\|]+)(?:\s*\|\s*([^\]]+
)|)\]\]").match(word)
name = m.group(1)
descr = m.group(2) or name
name = m.group(1)
descr = m.group(2) or name
@@
-397,17
+404,14
@@
class PageFormatter:
if macro:
return apply(macro, (name, descr))
elif img_re.match(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)
+ name = relative_url(name)
+ # 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:
if img_re.match(descr):
descr = '<img border="0" src="' + descr + '" />'
else:
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')
+ return link_tag(name, descr, 'wikilink')
def _email_repl(self, word):
return '<a href="mailto:%s">%s</a>' % (word, word)
def _email_repl(self, word):
return '<a href="mailto:%s">%s</a>' % (word, word)
@@
-433,13
+437,7
@@
class PageFormatter:
return ''
def _hi_repl(self, word):
return ''
def _hi_repl(self, word):
- if word == 'FIXME':
- cl = 'error'
- elif word == 'DONE':
- cl = 'success'
- elif word == 'TODO':
- cl = 'notice'
- return '<strong class="highlight ' + cl + '">' + word + '</strong>'
+ return '<strong class="highlight ' + word + '">' + word + '</strong>'
def _tr_repl(self, word):
out = ''
def _tr_repl(self, word):
out = ''
@@
-511,7
+509,7
@@
class PageFormatter:
# Links
+ r"|(?P<img>\b[a-zA-Z0-9_-]+\.(png|gif|jpg|jpeg|bmp))"
+ r"|(?P<word>\b(?:[A-Z][a-z]+){2,}\b)"
# 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+)(?:\s*\|\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.-]+)"
+ r"|(?P<url>(http|https|ftp|mailto)\:[^\s'\"]+\S)"
+ r"|(?P<email>[-\w._+]+\@[\w.-]+)"
@@
-527,6
+525,7
@@
class PageFormatter:
pre_re = re.compile(
r"(?:"
+ r"(?P<pre>\s*\}\}\})"
pre_re = re.compile(
r"(?:"
+ r"(?P<pre>\s*\}\}\})"
+ + r"|(?P<ent>[<>&])"
+ r")")
blank_re = re.compile(r"^\s*$")
indent_re = re.compile(r"^\s*")
+ r")")
blank_re = re.compile(r"^\s*$")
indent_re = re.compile(r"^\s*")
@@
-575,7
+574,7
@@
class Page:
return path.join(data_dir, self.page_name)
def _tmp_filename(self):
return path.join(data_dir, self.page_name)
def _tmp_filename(self):
- return path.join(data_dir, ('#' + self.page_name + '.' + `os.getpid()` + '#'))
+ return path.join(data_dir, ('#' + self.page_name
.replace('/','_')
+ '.' + `os.getpid()` + '#'))
def exists(self):
try:
def exists(self):
try:
@@
-584,8
+583,7
@@
class Page:
except OSError, er:
if er.errno == errno.ENOENT:
return False
except OSError, er:
if er.errno == errno.ENOENT:
return False
- else:
- raise er
+ raise er
def link_to(self):
word = self.page_name
def link_to(self):
word = self.page_name
@@
-621,10
+619,7
@@
class Page:
return self.attrs
def get_attr(self, name, default):
return self.attrs
def get_attr(self, name, default):
- if self.get_attrs().has_key(name):
- return self.get_attrs()[name]
- else:
- return default
+ return self.get_attrs().get(name, default)
def can(self, action, default=True):
try:
def can(self, action, default=True):
try:
@@
-633,10
+628,7
@@
class Page:
for rule in acl.split():
(user,perms) = rule.split(':')
if user == remote_user() or user == "All":
for rule in acl.split():
(user,perms) = rule.split(':')
if user == remote_user() or user == "All":
- if action in perms.split(','):
- return True
- else:
- return False
+ return action in perms.split(',')
return False
except Exception, er:
pass
return False
except Exception, er:
pass
@@
-685,7
+677,7
@@
class Page:
+ ' for ' + cgi.escape(remote_user())
+ ' from ' + cgi.escape(get_hostname(remote_host()))
+ '</b></p>')
+ ' 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="off" spellcheck="true" id="editor" name="savetext" rows="17" cols="100">%s</textarea>""" % (preview or self.get_raw_body())
print """
print '<input type="hidden" name="savepage" value="%s">' % (self.page_name)
print """<textarea wrap="off" spellcheck="true" id="editor" name="savetext" rows="17" cols="100">%s</textarea>""" % (preview or self.get_raw_body())
print """