X-Git-Url: https://codewiz.org/gitweb?p=geekigeeki.git;a=blobdiff_plain;f=geekigeeki.py;h=5340646c63101b24629d96a26ff2adea8e66a3b7;hp=12b6b7cdaec57d05e14baadd381c17a6431029bb;hb=61f00a0d333aeb1cb45e98ab4c1902ff1981ad6a;hpb=75836706224e9f6b8ede6c59deea646e31672924 diff --git a/geekigeeki.py b/geekigeeki.py index 12b6b7c..5340646 100755 --- a/geekigeeki.py +++ b/geekigeeki.py @@ -1,8 +1,9 @@ -#! /usr/bin/env python +#!/usr/bin/python +# -*- coding: utf-8 -*- # # Copyright 1999, 2000 Martin Pool # Copyright 2002 Gerardo Poggiali -# Copyright 2007 Bernardo Innocenti +# Copyright 2007, 2008 Bernardo Innocenti # # 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 @@ -27,8 +28,8 @@ from os import path, environ # 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$") @@ -59,6 +60,15 @@ def get_hostname(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"): @@ -112,8 +122,8 @@ def send_title(name, text="Limbo", msg=None, msg_type='error'): print ' ' if not name: print ' ' - if globals().has_key('css_url'): - print ' ' % css_url + for css in css_url: + print ' ' % relative_url(css) print '' # Body @@ -142,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: - print ' | ' + link_tag('?edit=' + name, 'Edit Page', 'navlink', authentication=True) + print ' | ' + link_tag('?edit=' + name, 'Edit Page', 'navlink', privileged=True) else: print ' | Immutable Page' @@ -152,7 +162,7 @@ def send_title(name, text="Limbo", msg=None, msg_type='error'): print '
' -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 = '' @@ -161,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" ' - if authentication: - path = privileged_path() - else: - path = script_name() - return '%s' % (classattr, path, params, text) + elif url_re.match(params): + classattr += 'rel="nofollow" ' + return '%s' % (classattr, relative_url(params, privileged=privileged), text) # Search --------------------------------------------------- @@ -317,11 +325,11 @@ def _macro_TitleIndex(*vargs): for name in pages: letter = string.lower(name[0]) if letter != current_letter: - s = s + '

%s

' % (letter, letter) + s += '

%s

' % (letter, letter) current_letter = letter else: - s = s + '
' - s = s + Page(name).link_to() + s += '
' + s += Page(name).link_to() return s @@ -335,18 +343,27 @@ class PageFormatter: def __init__(self, raw): self.raw = raw self.h_level = 0 - self.in_pre = self.in_table = self.in_var = self.in_em = self.in_b = 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 _b_repl(self, word): - self.in_b = not self.in_b - return ['', ''][self.in_b] - - def _em_repl(self, word): - self.in_em = not self.in_em - return ['', ''][self.in_em] + style = self.styles[word] + style[1] = not style[1] + return ['' def _tit_repl(self, word): if self.h_level: @@ -355,7 +372,8 @@ class PageFormatter: else: self.h_level = len(word) - 1 self.h_cnt += 1 - result = '* ' % (self.h_level, self.h_cnt, self.h_cnt) + #abridged = re.sub('[^a-z_]', '', word.lower().replace(' ', '_')) + result = '¶ ' % (self.h_level, self.h_cnt, self.h_cnt) return result def _br_repl(self, word): @@ -368,7 +386,7 @@ class PageFormatter: return Page(word).link_to() def _img_repl(self, word): - path = script_name() + '/' + word; + path = relative_url(word) return '' % (path, path) def _url_repl(self, word): @@ -378,7 +396,7 @@ class PageFormatter: return '%s' % (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 @@ -386,12 +404,13 @@ class PageFormatter: if macro: return apply(macro, (name, descr)) elif img_re.match(name): - return '%s' % (name, name, descr) - elif url_re.match(name): - return '%s' % (name, descr) - elif name.startswith('/'): - return '%s' % (name, descr) + name = relative_url(name) + # The "extthumb" nonsense works around a limitation of the HTML block model + return '
%s
%s
' % (name, name, descr, descr) else: + if img_re.match(descr): + descr = '' + return link_tag(name, descr, 'wikilink') def _email_repl(self, word): @@ -418,22 +437,7 @@ class PageFormatter: return '' def _hi_repl(self, word): - if word == 'FIXME': - cl = 'error' - elif word == 'DONE': - cl = 'success' - elif word == 'TODO': - cl = 'notice' - return '' + word + '' - - def _var_repl(self, word): - if word == '{{' and not self.in_var: - self.in_var = True - return '' - elif self.in_var: - self.in_var = False - return '' - return '' + return '' + word + '' def _tr_repl(self, word): out = '' @@ -494,8 +498,7 @@ class PageFormatter: scan_re = re.compile( r"(?:" # Formatting - + r"(?P\*\*|''')" - + r"|(?P//|'')" + + r"(?P\*\*|'''|//|''|##|``|__|\^\^|,,)" + r"|(?P\={2,6})" + r"|(?P
\\\\)" + r"|(?P^-{3,})" @@ -506,14 +509,13 @@ class PageFormatter: # Links + r"|(?P\b[a-zA-Z0-9_-]+\.(png|gif|jpg|jpeg|bmp))" + r"|(?P\b(?:[A-Z][a-z]+){2,}\b)" - + r"|(?P\[\[(\S+)(?:\s*\|\s*([^\]]*)|)\]\])" + + r"|(?P\[\[([^ \t\n\r\f\v\|]+)(?:\s*\|\s*([^\]]+)|)\]\])" + r"|(?P(http|https|ftp|mailto)\:[^\s'\"]+\S)" + r"|(?P[-\w._+]+\@[\w.-]+)" # Lists, divs, spans + r"|(?P
  • ^\s+[\*#] +)" + r"|(?P
    \{\{\{|\s*\}\}\})"
    -            + r"|(?P\{\{|\}\})"
     
                 # Tables
                 + r"|(?P^\s*\|\|(=|)\s*)"
    @@ -523,6 +525,7 @@ class PageFormatter:
             pre_re = re.compile(
                 r"(?:"
                 + r"(?P
    \s*\}\}\})"
    +            + r"|(?P[<>&])"
                 + r")")
             blank_re = re.compile(r"^\s*$")
             indent_re = re.compile(r"^\s*")
    @@ -561,7 +564,6 @@ class Page:
             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,
    @@ -572,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 + '.' + `os.getpid()` + '#'))
    +        return path.join(data_dir, ('#' + self.page_name.replace('/','_') + '.' + `os.getpid()` + '#'))
     
         def exists(self):
             try:
    @@ -581,8 +583,7 @@ class Page:
             except OSError, er:
                 if er.errno == errno.ENOENT:
                     return False
    -            else:
    -                raise er
    +            raise er
     
         def link_to(self):
             word = self.page_name
    @@ -600,8 +601,9 @@ class Page:
                 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*)(.*)$")
    @@ -616,18 +618,17 @@ class Page:
                     raise er
             return self.attrs
     
    +    def get_attr(self, name, default):
    +        return self.get_attrs().get(name, 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":
    -                    if action in perms.split(','):
    -                        return True
    -                    else:
    -                        return False
    +                    return action in perms.split(',')
                 return False
             except Exception, er:
                 pass
    @@ -643,6 +644,12 @@ class Page:
             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()
    @@ -670,9 +677,9 @@ class Page:
                 + ' for ' + cgi.escape(remote_user())
                 + ' from ' + cgi.escape(get_hostname(remote_host()))
                 + '

    ') - print '
    ' % (script_name(), self.page_name) + print '
    ' % relative_url(self.page_name) print '' % (self.page_name) - print """""" % (preview or self.get_raw_body()) + print """""" % (preview or self.get_raw_body()) print """