Login

Add rel=lightbox to all image-links

Author:
bartTC
Posted:
January 16, 2008
Language:
Python
Version:
.96
Tags:
filter re lightbox regular-expression
Score:
4 (after 4 ratings)

Add the attribute "rel='lightbox'" to all Links, if the target is an image.

<a href="/path/to/image.jpg">Image</a>

becomes

<a rel="lightbox" href="/path/to/image.jpg">Image</a>

Works for JPG, GIF and PNG Files.

1
2
3
4
5
6
7
8
9
import re
from django.utils.encoding import smart_unicode

r_lightbox = re.compile('<a (?=[^>]*\.(jpg|gif|png))(?![^>]*lightbox)')
s_lightbox = '<a rel="lightbox" '

@register.filter
def lightbox(content):
    return r_lightbox.sub(s_lightbox, smart_unicode(content))

More like this

  1. YUI Loader as Django middleware by akaihola 7 years, 4 months ago
  2. Get active page's url by another language (templatetag) by muratcorlu 2 years, 7 months ago
  3. Template tags to integrate with modconcat by matthanger 6 years, 1 month ago
  4. load m2m fields objects by dirol 5 years, 2 months ago
  5. Git media cache busting tag by adamlofts 5 years, 9 months ago

Comments

Please login first before commenting.