Skip to content

Instantly share code, notes, and snippets.

@victorono
Created April 6, 2015 14:24
Show Gist options
  • Select an option

  • Save victorono/768bbaa9b5e8e93f17a9 to your computer and use it in GitHub Desktop.

Select an option

Save victorono/768bbaa9b5e8e93f17a9 to your computer and use it in GitHub Desktop.
highlight.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django import template
from django.utils.safestring import mark_safe
import re
register = template.Library()
@register.filter(name='highlight')
def highlight(text, word):
insensitive_hippo = re.compile(re.escape(word), re.IGNORECASE)
return mark_safe(insensitive_hippo.sub("<span class='bold'>%s</span>&nbsp;" % word, text))
# return mark_safe(text.replace(word, "<span class='bold'>%s</span>" % word))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment