Created
April 6, 2015 14:24
-
-
Save victorono/768bbaa9b5e8e93f17a9 to your computer and use it in GitHub Desktop.
highlight.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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> " % 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