from django import template from django.utils.safestring import mark_safe register = template.Library() @register.filter def highlight(text, word): """ Retorna texto com highlight. """ begin_mark = 0 end_mark = 0 if word.lower() in text.lower(): for i in word.lower(): for j in text.lower()[end_mark:]: if i == j: end_mark += 1 break else: begin_mark += 1 end_mark += 1 continue text_to_replace = text[begin_mark:end_mark] text = text.replace(text_to_replace, "%s" % text_to_replace) return mark_safe(text)