Login

Add rel=lightbox to all image-links

Author:
bartTC
Posted:
January 16, 2008
Language:
Python
Version:
.96
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. A wrapper around cache_page making it optional by rixx 4 days, 12 hours ago
  2. Treat empty value on DateRangeFilter as a filter for non filled dates by kmmbvnr 1 month, 1 week ago
  3. Django Settings Assignment Expressions aka Walrus Operator example by webology 2 months, 2 weeks ago
  4. codigo alto nivel by MrRocklion 4 months, 1 week ago
  5. Load template from specific app by Krzysiek555 5 months ago

Comments

Please login first before commenting.