Hi Herman, We have actually just implemented this on one of our other sites still in development. The problem with keyword highlighting is you have to be careful not to enter into infinite loops, by ensuring you only match keywords outside of existing HTML tags. Normally I'd just say "figure it out for yourself", but this particular task took me about 4-5 hours to figure out, and was eventually solved by someone elses regex on google, so it's only fair I share :)
Here is the code we have used (works 100% of the time so far) - this is quickly modified cut and paste, so you may need to tinker with it to make it work. #TODO: Apply keywords class Filtering: def apply_keywords(self, message, keywords): if message: _pattern = r'(%s)(?!([^<]+)?>)' % ( "|".join(map(lambda x: re.escape(x.get('word')), keywords)) ) _keyword_re = re.compile(_pattern, flags = re.IGNORECASE) message = _keyword_re.sub("<span class='KeywordMatch'>\\1</span>", message) return message Example usage: html_here = """ lazy dog <b>lazy</b> <b>dog</b> <div class="lazy">lazy</div> <div class="lazy">dog</div> """ >>> f = Filtering() >>> f.apply_keywords(html_here, [{'word':'lazy'}, {'word':'dog'}, {'word':'jumps'} ]); '\n<span class=\'KeywordMatch\'>lazy</span>\n<span class=\'KeywordMatch\'>dog</span>\n<b><span class=\'KeywordMatch\'>lazy</span></b>\n<b><span class=\'KeywordMatch\'>dog</span></b>\n\n<div class="lazy"><span class=\'KeywordMatch\'>lazy</span></div>\n<div class="lazy"><span class=\'KeywordMatch\'>dog</span></div>\n' Enjoy :) On Wed, Jun 22, 2011 at 1:28 PM, Herman Schistad <herman.schis...@gmail.com>wrote: > Hi there fellow developers. > > I'm wondering if anyone has seen an app which does the following: > * Finds keywords in the templates, which is defined by the user and > then creates internal links to these. > > Use-case: > 1. I create keyword: "Cupcakes" and give it the href/URL property: > /info/cupcakes/ in adminpanel > 2. I write a flatpage/template where I mention the word cupcakes > 3. Middleware notices this word and renders a <a > href="/info/cupcakes/>cupcakes</a> link automatically > > Kind of like Wikipedia, but the user does not need to define links > themselves in markup etc. > If this does not exist, I would guess it could be pretty popular as a > SEO tool and I would be glad to discuss development with interested > parties. > > -- > With regards, Herman Schistad > > -- > You received this message because you are subscribed to the Google Groups > "Django users" group. > To post to this group, send email to django-users@googlegroups.com. > To unsubscribe from this group, send email to > django-users+unsubscr...@googlegroups.com. > For more options, visit this group at > http://groups.google.com/group/django-users?hl=en. > > -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.