From: Bernie Innocenti Date: Sat, 25 Apr 2009 07:28:01 +0000 (+0900) Subject: Humanize auto link tags X-Git-Tag: v4.0~11 X-Git-Url: https://codewiz.org/gitweb?p=geekigeeki.git;a=commitdiff_plain;h=b70950f8de51524453f99b4809c726212a5fa2f3 Humanize auto link tags --- diff --git a/geekigeeki.py b/geekigeeki.py index 6170d73..368cf8d 100755 --- a/geekigeeki.py +++ b/geekigeeki.py @@ -25,9 +25,6 @@ start_time = clock() 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) @@ -82,6 +79,9 @@ def relative_url(pathname, privileged=False): def permalink(s): return re.sub(' ', '-', re.sub('[^a-z0-9_ ]', '', s.lower()).strip()) +def humanlink(s): + return re.compile('([^:/\.]+)(?:\.[^/:]+|)$').search(s).group(1).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): @@ -197,7 +197,7 @@ def send_httperror(status="403 Not Found", query=""): def link_tag(params, text=None, link_class=None, privileged=False, **kvargs): if text is None: - text = params # default + text = humanlink(params) elif img_re.match(text): text = '' + text + '' @@ -218,7 +218,7 @@ def link_tag(params, text=None, link_class=None, privileged=False, **kvargs): return '%s' % (classattr, relative_url(params, privileged=privileged), text) def link_inline(name, descr=None, kvargs={}): - if not descr: descr = name + if not descr: descr = humanlink(name) url = relative_url(name) if video_re.match(name): return '' % url @@ -325,7 +325,10 @@ def make_index_key(): links = ['%s' % (ch, ch) for ch in 'abcdefghijklmnopqrstuvwxyz'] return '

' + ' | '.join(links) + '

' -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): @@ -649,7 +652,7 @@ class Page: if img_re.match(filename): if image_maxwidth: maxwidth_arg = '|maxwidth=' + str(image_maxwidth) - out += '{{' + self.page_name + '/' + filename + '|' + filename + maxwidth_arg + '}}\n' + out += '{{' + self.page_name + '/' + filename + '|' + humanlink(filename) + maxwidth_arg + '}}\n' else: out += ' * [[' + self.page_name + '/' + filename + ']]\n' return out