Login

Remove self links middleware

Author:
svetlyak
Posted:
April 13, 2008
Language:
Python
Version:
.96
Tags:
middleware html output usability utils
Score:
3 (after 3 ratings)

This simple middleware replaces all 'a href' links to the current page to the 'span' elements. This very usefule from the usability point of view.

For example, user open in bowser page http://svetlyak.ru/blog/, and this middleware will replace all 'a' elements on this page, which refer to the '/blog/'. Because of this, link 'Blog' in the main menu, become a simple 'span'.

Next, when user goes to the next page, a post with full comments list ('/blog/123/'), for example, the item 'Blog' in the main menu become a link again!

To use this middleware, just add it to the list of middleware classes:

MIDDLEWARE_CLASSES = ('utils.middleware.RemoveSelfLinks',)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import re

class RemoveSelfLinks:
    def process_response(self, request, response):
        if response.status_code == 200:
            link = request.META['PATH_INFO']
            response.content = \
                re.sub( \
                    r'<a([^>]+)href="%s"([^>]*)>(.*?(?!</a>)[^<]*)</a>' % link, \
                    r'<span \1 \2>\3</span>', \
                    response.content)
        return response

More like this

  1. Login Required Middleware with Next Parameter by bernardoporto 2 years, 7 months ago
  2. @reply links in Twitter messages by ryanberg 7 years ago
  3. YUI Loader as Django middleware by akaihola 7 years, 2 months ago
  4. Find all links in a value and display them separatley by jcroft 8 years, 4 months ago
  5. Page numbers with ... like in Digg by Ciantic 6 years, 2 months ago

Comments

Please login first before commenting.