New template tag: hilight which hilights a set of tokens with em tags

This commit is contained in:
Johannes Schriewer 2025-01-06 03:16:33 +01:00
parent 8e13f99b2f
commit b256d12fee

View file

@ -0,0 +1,24 @@
from django import template
from django.utils.safestring import mark_safe
from re import finditer
register = template.Library()
@register.filter(name='hilight')
def hilight(text, tokens):
text = str(text)
for token in tokens:
ltext = text.lower()
for token in tokens:
positions = []
for pos in finditer(token, ltext):
positions.append(pos.start())
if len(positions) > 0:
offset = 0
for pos in positions:
start = pos + offset
end = start + len(token)
text = text[:start] + f"<em>{text[start:end]}</em>" + text[end:]
offset += 9
return mark_safe(text)