New template tag: hilight which hilights a set of tokens with em tags
This commit is contained in:
parent
8e13f99b2f
commit
b256d12fee
1 changed files with 24 additions and 0 deletions
24
inventory/templatetags/hilight.py
Normal file
24
inventory/templatetags/hilight.py
Normal 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)
|
Loading…
Reference in a new issue