From 5b3a951be3cb06fcf44b5643476cb34d8cf773bd Mon Sep 17 00:00:00 2001 From: substantialnoninfringinguser Date: Thu, 16 Jul 2009 05:37:33 +0100 Subject: [svn] * Make descriptions parser also replace links to descriptions from Cave models' underground_descriptions with wikilinks for valid (existing) links * Make entrances searchable in admin by cave kataster number --- utils.py | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'utils.py') diff --git a/utils.py b/utils.py index 1b16059..1638cd2 100644 --- a/utils.py +++ b/utils.py @@ -1,6 +1,6 @@ from django.conf import settings - import random, re, logging +from core.models import CaveDescription def weighted_choice(lst): n = random.uniform(0,1) @@ -76,6 +76,7 @@ def render_with_context(req, *args, **kwargs): re_body = re.compile(r"\]*\>(.*)\", re.DOTALL) re_title = re.compile(r"\]*\>(.*)\", re.DOTALL) + def get_html_body(text): return get_single_match(re_body, text) @@ -90,6 +91,21 @@ def get_single_match(regex, text): else: return None +def href_to_wikilinks(matchobj): + """ + Given an html link, checks for possible valid wikilinks. + + Returns the first valid wikilink. Valid means the target + object actually exists. + """ + res=CaveDescription.objects.filter(long_name__icontains=matchobj.groupdict()['text']) + if res: + return r'[[cavedescription:'+res[0].short_name+'|'+res[0].long_name+']]' + else: + return matchobj + #except: + #print 'fail' + re_subs = [(re.compile(r"\]*\>(.*?)\", re.DOTALL), r"'''\1'''"), (re.compile(r"\(.*?)\", re.DOTALL), r"''\1''"), @@ -99,10 +115,11 @@ re_subs = [(re.compile(r"\]*\>(.*?)\", re.DOTALL), r"'''\1'''"), (re.compile(r"\]*\>(.*?)\", re.DOTALL), r"====\1===="), (re.compile(r"\]*\>(.*?)\", re.DOTALL), r"=====\1====="), (re.compile(r"\]*\>(.*?)\", re.DOTALL), r"======\1======"), - (re.compile(r"\(.*?)\", re.DOTALL), r"[[subcave:\1|\2]]"), + (re.compile(r"\(.*?)\", re.DOTALL), r"[[subcave:\1|\2]]"), #assumes that all links with id attributes are subcaves. Not great. #interpage link needed - (re.compile(r"\(.*?)\", re.DOTALL), r"[[cavedescription:\1|\2]]"), + (re.compile(r"\(.*?)\", re.DOTALL), r"[[cavedescription:\1|\2]]"), #assumes that all links with target ids are subcaves. Not great. (re.compile(r"\[\([^\s]*).*?\\]", re.DOTALL), r"[[qm:\1]]"), + (re.compile(r'.*)"?>(?P.*)'),href_to_wikilinks) ] -- cgit v1.2.3