Woo hoo! Thanks Ninja Ben. -d ----- Original Message ----- From: "Ben Doom" <[EMAIL PROTECTED]> To: "CF-Community" <[EMAIL PROTECTED]> Sent: Monday, September 08, 2003 4:19 PM Subject: RE: highlight UDF
> Okay, folks, here's the link to see it work: > > http://cf.moonbow.com/doom/hilite.cfm > > Please note that this still has a few caveats: > > **it can break tags already in the text run thru it; that is, if you have > <span> tags already there, highlighting "pan" will break them > **it won't highlight one of the words if two in the wordlist share letters; > that is, if you have "roomate" (note misspelling) and search "room,mate" it > won't highlight the whole word, just "room" > **if you put spaces in the list, it does search for those spaces, so be sure > you meant for them to be there; that was true of previous versions, too > > The short version is that I replaced recursion with a single regex call. > Since regex is supposed to be my thing (I am the ninja, after all) I thought > I'd let the engine do the heavy lifting. > > If anyone wants to submit this to CFLib, feel free. Just make sure I get > credit (ie leave my comments). > > On a geeky note, to Ray: if you can do it with tail recursion (which you > did) you can do it iteratively, which generally has less overhead. I only > point it out because my CS profs beat it into me, as I kept doing the same > thing. :-) Here, I avoid both recursion and iteration and let the regex > engine handle it. > > Anyway, here's the code : > > <cfscript> > /** > * Applies a simple highlight to a word in a string. > * > * @param string The string to format. > * @param word The word to highlight. > * @param front This is the HTML that will be placed in front of the > highlighted match. It defaults to <span style= > * @param back This is the HTML that will be placed at the end of the > highlighted match. Defaults to </span> > * @param matchCase If true, the highlight will only match when the case > is the same. Defaults to false. > * @author Raymond Camden ([EMAIL PROTECTED]) > * @version 1, July 30, 2001 > */ > function highLight(str, wordList) { > > > /** > * Modification by Kevin Graeme > * Added a class descriptor called "hlt" to the global css styles, > * and changed the span style from a direct style to a called class. > * This resolves a Netscape 4.x CSS implementation problem. > */ > /* var front = "<span style=""background-color: yellow;"">"; */ > var front = "<span class=""hlt"">"; > var back = "</span>"; > var matchCase = false; > var newwordlist = listchangedelims(wordlist, '|'); // added by Ben -- see > comment below > > //check for empty word list, this is the base condition for the recursion > if(ListLen(wordList) Lt 1) > return str; > else > word = ListFirst(wordList); > > if(ArrayLen(arguments) GTE 3) front = arguments[3]; > if(ArrayLen(arguments) GTE 4) back = arguments[4]; > if(ArrayLen(arguments) GTE 5) matchCase = arguments[5]; > > > /** > * Modification by Benjamin Doom > * Moonbow Software, Inc. > * This will run faster and prevent overwrites of span calls. > */ > > /* > if(Not matchCase) > CurTxt = REReplaceNoCase(str,"(#word#)","#front#\1#back#","ALL"); > else > CurTxt = REReplace(str,"(#word#)","#front#\1#back#","ALL"); > > return Highlight(CurTxt, ListRest(wordList), front, back, matchCase); > */ > > if (matchCase) { > return rereplace(str, "(#newwordlist#)", "#front#\1#back#", "all"); } > else { > return rereplacenocase(str, "(#newwordlist#)", "#front#\1#back#", "all"); } > > > } > > </cfscript> > > > -- Ben Doom > Programmer & General Lackey > Moonbow Software, Inc > > : -----Original Message----- > : From: Deanna Schneider [mailto:[EMAIL PROTECTED] > : Sent: Monday, September 08, 2003 1:43 PM > : To: CF-Community > : Subject: highlight UDF > : > : > : Hey folks, > : I suck at regex, which is why I helped sponsor that little tool that Mike > : found, but I'm still having trouble figuring this out. And, since it was > : originally written by Raymond, I thought I'd throw this out here. > : > : There's this UDF, which we already modified once to use a span > : class instead > : of just a simple background color, that highlights a search term. But, if > : the search term happens to contain the letters inside the span, like say, > : for instance, if someone searchs for "pan", the UDF overwrites the span > : text. I'm sure there's a way to write the regex to look for the > : search term > : except when it's in this "<span class="hlt">anythinghere</span>" > : but I don't > : know how to do it. Here's the UDF: > > <snip /> > > > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| Archives: http://www.houseoffusion.com/lists.cfm?link=t:5 Subscription: http://www.houseoffusion.com/lists.cfm?link=s:5 Unsubscribe: http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=89.70.5 This list and all House of Fusion resources hosted by CFHosting.com. The place for dependable ColdFusion Hosting. http://www.cfhosting.com
