middleware: external links open in new window

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class ExtlinksBlankMiddleware(object):
    '''
    Makes sure all external and only exernal links open in a new window.
    '''
    def __init__(self):
        self.targets = re.compile(r'''target=.\w*.''')
        self.extlinks = re.compile(r'''<a (?P<old>[^>]*http.?://)''')
    def process_response(self, request, response):
        if ("text" in response['Content-Type']):
            response.content = self.targets.sub('', response.content)
            response.content = self.extlinks.sub('<a target="_blank" \g<old>',response.content)
            return response
        else:
            return response

More like this

  1. Add rel=lightbox to all image-links by bartTC 6 years, 3 months ago
  2. Remove self links middleware by svetlyak 6 years ago
  3. True links in the admin list by svetlyak 6 years, 7 months ago
  4. CheckedField by oggy 5 years, 7 months ago
  5. Accessing URL variable from within a Form by blacktav 7 months, 3 weeks ago

Comments

marcob (on February 11, 2010):

Well done!

#

(Forgotten your password?)