kpsul/shared/templatetags/search_utils.py
2020-07-18 16:07:12 +02:00

16 lines
384 B
Python

import re
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
@register.filter
def highlight(text, q):
q2 = "|".join(re.escape(word) for word in q.split())
pattern = re.compile(r"(?P<filter>%s)" % q2, re.IGNORECASE)
return mark_safe(
re.sub(pattern, r"<span class='highlight'>\g<filter></span>", text)
)