kpsul/shared/templatetags/search_utils.py

16 lines
384 B
Python
Raw Normal View History

import re
from django import template
from django.utils.safestring import mark_safe
register = template.Library()
2020-07-01 22:29:07 +02:00
@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)
)