Skip to content

Instantly share code, notes, and snippets.

@notanumber
Created July 28, 2009 15:33
Show Gist options
  • Save notanumber/157458 to your computer and use it in GitHub Desktop.
Save notanumber/157458 to your computer and use it in GitHub Desktop.
def _do_highlight(self, content, text, tag='em'):
"""
Highlight `text` in `content` with html `tag`.
This method assumes that the input text (`content`) does not contain
any special formatting. That is, it does not contain any html tags
or similar markup that could be screwed up by the highlighting.
Required arguments:
`content` -- Content to search for instances of `text`
`text` -- The text to be highlighted
"""
for term in [term.replace('*', '') for term in text.split()]:
term_re = re.compile(re.escape(term), re.IGNORECASE)
content = term_re.sub('<%s>%s</%s>' % (tag, term, tag), content)
return content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment