From 0c86a68ece1de5bdc559419ef52e83611e30998a Mon Sep 17 00:00:00 2001 From: Bernie Innocenti Date: Wed, 8 Apr 2009 16:25:24 +0200 Subject: [PATCH] Fold page existance logic into link_tag() --- geekigeeki.py | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/geekigeeki.py b/geekigeeki.py index f89a844..774225b 100755 --- a/geekigeeki.py +++ b/geekigeeki.py @@ -62,8 +62,11 @@ def get_hostname(addr): except Exception: return addr +def is_external_url(pathname): + return (url_re.match(pathname) or pathname.startswith('/')) + def relative_url(pathname, privileged=False): - if not (url_re.match(pathname) or pathname.startswith('/')): + if not is_external_url(pathname): if privileged: url = privileged_path() else: @@ -187,17 +190,24 @@ def send_httperror(status="403 Not Found", query=""): send_title(None, msg_text=("%s: on query '%s'" % (status, query))) send_footer() -def link_tag(params, text=None, ss_class=None, privileged=False): +def link_tag(params, text=None, link_class=None, privileged=False): if text is None: text = params # default - classattr = '' - if ss_class: - classattr += 'class="%s" ' % ss_class - # Prevent crawlers from following links potentially added by spammers or to generated pages - if ss_class == 'external' or ss_class == 'navlink': - classattr += 'rel="nofollow" ' - elif url_re.match(params): - classattr += 'rel="nofollow" ' + + if not link_class: + if is_external_url(params): + link_class = 'external' + elif file_re.match(params) and Page(params).exists(): + link_class = 'wikilink' + else: + params = nonexist_pfx + params + link_class = 'nonexistent' + + classattr = 'class="%s" ' % link_class + # Prevent crawlers from following links potentially added by spammers or to generated pages + if link_class == 'external' or link_class == 'navlink': + classattr += 'rel="nofollow"' + return '%s' % (classattr, relative_url(params, privileged=privileged), text) # Search --------------------------------------------------- @@ -221,7 +231,7 @@ def handle_fullsearch(needle): print "