2016-07-15 00:02:56 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
2016-05-26 22:44:10 +02:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
2012-07-11 17:39:20 +02:00
|
|
|
from django import template
|
2013-09-05 22:20:52 +02:00
|
|
|
from django.utils.safestring import mark_safe
|
|
|
|
|
|
|
|
import re
|
2012-07-11 17:39:20 +02:00
|
|
|
|
|
|
|
register = template.Library()
|
|
|
|
|
2016-07-16 20:54:52 +02:00
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
@register.filter
|
2012-07-11 17:39:20 +02:00
|
|
|
def key(d, key_name):
|
|
|
|
try:
|
|
|
|
value = d[key_name]
|
|
|
|
except KeyError:
|
|
|
|
from django.conf import settings
|
|
|
|
value = settings.TEMPLATE_STRING_IF_INVALID
|
|
|
|
return value
|
2013-09-05 22:20:52 +02:00
|
|
|
|
2016-07-16 20:54:52 +02:00
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
def highlight_text(text, q):
|
|
|
|
q2 = "|".join(q.split())
|
|
|
|
pattern = re.compile(r"(?P<filter>%s)" % q2, re.IGNORECASE)
|
2016-07-16 20:54:52 +02:00
|
|
|
return mark_safe(re.sub(pattern,
|
|
|
|
r"<span class='highlight'>\g<filter></span>",
|
|
|
|
text))
|
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
|
|
|
|
@register.filter
|
|
|
|
def highlight_user(user, q):
|
|
|
|
if user.first_name and user.last_name:
|
2016-07-16 20:54:52 +02:00
|
|
|
text = "%s %s (<tt>%s</tt>)" % (user.first_name, user.last_name,
|
|
|
|
user.username)
|
2013-09-05 22:20:52 +02:00
|
|
|
else:
|
|
|
|
text = user.username
|
|
|
|
return highlight_text(text, q)
|
|
|
|
|
2016-07-16 20:54:52 +02:00
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
@register.filter
|
|
|
|
def highlight_clipper(clipper, q):
|
|
|
|
if clipper.fullname:
|
2016-05-26 22:44:10 +02:00
|
|
|
text = "%s (<tt>%s</tt>)" % (clipper.fullname, clipper.username)
|
2013-09-05 22:20:52 +02:00
|
|
|
else:
|
|
|
|
text = clipper.username
|
|
|
|
return highlight_text(text, q)
|