memopol_tags.py 951 Bytes
Newer Older
Nicolas Joyard's avatar
Nicolas Joyard committed
1
2
3
4
5
6
# coding: utf-8
from django import template
from django.utils.safestring import mark_safe
import re

register = template.Library()
Nicolas Joyard's avatar
Nicolas Joyard committed
7
link = '<a class="{network}-link" href="{url}" target="_blank">{label}</a>'
Nicolas Joyard's avatar
Nicolas Joyard committed
8
9
10
11
12


@register.filter
def twitter_link(url):
    return mark_safe(link.format(network='twitter', url=url,
Nicolas Joyard's avatar
Nicolas Joyard committed
13
                                 label=re.sub(r'.*/@?([^/]+)', '@\\1', url)))
Nicolas Joyard's avatar
Nicolas Joyard committed
14
15
16
17
18


@register.filter
def facebook_link(url):
    return mark_safe(link.format(network='facebook', url=url,
Nicolas Joyard's avatar
Nicolas Joyard committed
19
                                 label=re.sub(r'.*/([^/]+)', '\\1', url)))
Nicolas Joyard's avatar
Nicolas Joyard committed
20

Nicolas Joyard's avatar
Nicolas Joyard committed
21
22
23
24
25
26

@register.filter
def website_link(url):
    short_url = re.sub(r'^https?://([^/]+).*', '\\1', url)
    return mark_safe(link.format(network='website', url=url,
                                 label=short_url))
Nicolas Joyard's avatar
Nicolas Joyard committed
27
28
29
30
31
32


@register.filter
def email_link(address):
    return mark_safe(link.format(network='email', url='mailto:%s' % address,
                                 label=address))