Created
March 27, 2012 21:56
-
-
Save un33k/2220739 to your computer and use it in GitHub Desktop.
highlight needles in haystack -- search
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
| def highlight_word(needles, haystack, cls_name='highlighted'): | |
| regex = re.compile(r"(%s)" % "|".join(needles), re.I) | |
| i = 0; out = "" | |
| for m in regex.finditer(haystack): | |
| out += "".join([haystack[i:m.start()],'<span class="%s">'%cls_name,haystack[m.start():m.end()],"</span>"]) | |
| i = m.end() | |
| return mark_safe(out + haystack[i:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment