memopol_tags.py 905 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,
13
        label=re.sub(r'.*/@?([^/]+)', '@\\1', re.sub(r'/$', '', url.strip()))))
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,
19
        label=re.sub(r'.*/([^/]+)', '\\1', re.sub(r'/$', '', url.strip()))))
Nicolas Joyard's avatar
Nicolas Joyard committed
20

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

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


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