Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <cfcomponent>
- <cffunction name="init" access="public" output="false" returntype="PartnerHighlighter" hint="Pseudo-constructor">
- <cfargument name="stPartners" type="struct" required="true">
- <cfargument name="sLink" type="string" required="true" hint="the link to use when replacing.">
- <cfscript>
- variables.stPartners = arguments.stPartners;
- variables.sStopString = '<span class="nolink">';
- variables.sLink = arguments.sLink;
- return this;
- </cfscript>
- </cffunction>
- <cffunction name="highlightPartners" returntype="string" access="public" hint="this searches through the partner struct and highlights the partner">
- <cfargument name="sSearchText" type="string" required="true" hint="the text to search">
- <cfargument name="sHighlightType" type="string" required="false" default="link" hint="can add others if needed later">
- <cfscript>
- local.sHighlightedText = arguments.sSearchText;
- local.lstPunct = '';
- local.lstPunct = listAppend(local.lstPunct, ",", ',');
- local.lstPunct = listAppend(local.lstPunct, ".", ',');
- local.lstPunct = listAppend(local.lstPunct, " ", ',');
- local.lstPunct = listAppend(local.lstPunct, "'", ',');
- local.lstPunct = listAppend(local.lstPunct, '"', ',');
- local.lstPunct = listAppend(local.lstPunct, '’', ',');
- local.lstPunct = listAppend(local.lstPunct, '<', ',');
- local.lstPunct = listAppend(local.lstPunct, '>', ',');
- local.lstPunct = listAppend(local.lstPunct, '&', ',');
- local.lstPunct = listAppend(local.lstPunct, '?', ',');
- local.lstPunct = listAppend(local.lstPunct, '!', ',');
- local.lstReplaced = "";
- for(local.stPartner in variables.stPartners){
- local.stArguments.stPartner = variables.stPartners[local.stPartner];
- local.stArguments.sHighlightType = arguments.sHighlightType;
- local.nNameLength = len(local.stArguments.stPartner.sFullName);
- local.nExists = findnocase(local.stArguments.stPartner.sFullname, local.sHighlightedText);
- local.aBytes = local.sHighlightedText.GetBytes();
- if(local.nExists gt 0){
- local.sSupposedName = mid(local.sHighlightedText, local.nExists, local.nNameLength);
- local.nNextByte = (local.nExists-1)+(local.nNameLength+1);
- if(listFindNoCase(local.lstPunct, Chr(local.aBytes[local.nNextByte]), ',') gt 0){
- local.nStop = findNoCase(variables.sStopString,local.sHighlightedText,local.nExists-22);
- if(listFindNoCase(local.lstReplaced, local.stArguments.stPartner.sFullName, "|") eq 0 AND local.nStop eq 0){
- local.sHighlighted = getHighlight(argumentCollection=local.stArguments);
- local.sHighlightedText = rereplacenocase(local.sHighlightedText, local.stArguments.stPartner.sFullname, local.sHighlighted, "all");
- local.lstReplaced = listAppend(local.lstReplaced, local.stArguments.stPartner.sFullName, "|");
- }
- }
- }
- }
- return local.sHighlightedText;
- </cfscript>
- </cffunction>
- <cffunction name="getHighlight" returntype="string" access="private" hint="do the actual highlight">
- <cfargument name="stPartner" type="struct" required="true" hint="the partner">
- <cfargument name="sHighlightType" type="string" required="true" hint="can add others if needed later">
- <cfscript>
- var stLocalPartner = arguments.stPartner;
- var sLocalHighlightType = arguments.sHighlightType;
- var sHighlight = "";
- var sNewLink = "";
- if(local.sLocalHighlightType eq "link"){
- local.sNewLink = rereplacenocase(variables.sLink, "[id]", local.stLocalPartner.nID, "all");
- local.sNewLink = rereplacenocase(local.sNewLink, "[lastname]", local.stLocalPartner.sLastName, "all");
- local.sNewLink = rereplacenocase(local.sNewLink, "[dupe]", local.stLocalPartner.sIsDupe, "all");
- local.sNewLink = rereplacenocase(local.sNewLink, "[seo]", local.stLocalPartner.sFullName, "all");
- local.sHighlight = '<a href="#local.sNewLink#" title="more information on this expert from example.com" target="_blank">#local.stLocalPartner.sFullName#</a>';
- local.sHighlight = "<span>#local.sHighlight#</span>";
- }
- return local.sHighlight;
- </cfscript>
- </cffunction>
- </cfcomponent>
- <cfscript>
- variables.stPartners = {'123'={sFullName='Michael Fish',nID='133',sLastName='Fish',sIsDupe='No'},'333'={sFullName='Michael Fisher',nID='133',sLastName='Fisher',sIsDupe='No'}};
- variables.stArgs = {};
- variables.stArgs.stPartners = variables.stPartners;
- variables.stArgs.sLink = '//example.com/profiles/[id]/[dupe]/[lastname]/[seo]/';
- variables.oPartnerHighlight = createObject('component','PartnerHighlight').init(argumentCollection=variables.stArgs);
- variables.stArgs = {};
- variables.stArgs.sSearchText = '<p>Michael Fish is a Weatheman</p>';
- writeOutput(variables.oPartnerHighlight.highlightPartners(argumentCollection=variables.stArgs));
- //expecting output <p><a href="//example.com/profiles/133/0/Fish/Michael Fish/" title="more information on this expert from example.com" target="_blank">Michael Fish</a> is a Weatheman</p>
- variables.stArgs = {};
- variables.stArgs.sSearchText = '<p>Michael Fi is a name</p>';
- writeOutput(variables.oPartnerHighlight.highlightPartners(argumentCollection=variables.stArgs));
- //expecting output <p>Michael Fi is a name</p>
- variables.stArgs = {};
- variables.stArgs.sSearchText = '<p>John Travolta is an Actor</p>';
- writeOutput(variables.oPartnerHighlight.highlightPartners(argumentCollection=variables.stArgs));
- //expecting output <p>John Travolta is an Actor</p>
- </cfscript>
- function linkifyNames(required struct names, required string template, required string text) {
- for (var key in names){
- var name = names[key];
- //this could be cleaned up more if you used the actual variable names from the struct with the hungarian notation
- //or if you dropped the hungarian notation
- name.expandedLink = '<a href="' & template & '" title="more information on this expert from example.com" target="_blank">' & name.sFullname & '</a>';
- name.expandedLink = replaceNoCase(name.expandedLink, "[id]", name.nID);
- name.expandedLink = replaceNoCase(name.expandedLink, "[dupe]", (name.sIsDupe ? 1 : 0));
- name.expandedLink = replaceNoCase(name.expandedLink, "[lastname]", name.sLastName);
- name.expandedLink = replaceNoCase(name.expandedLink, "[seo]", name.sFullName);
- text = reReplaceNoCase(text, "b" & name.sFullname & "b", name.expandedLink, "all");//needs to be a regex
- }
- return text;
- }
- variables.stPartners = {'123'={sFullName='Michael Fish',nID='133',sLastName='Fish',sIsDupe='No'},'333'={sFullName='Michael Fisher',nID='133',sLastName='Fisher',sIsDupe='No'}};
- writedump(linkifyNames(variables.stPartners, '//example.com/profiles/[id]/[dupe]/[lastname]/[seo]/', '<p>Michael Fish is a name</p>'));
- writedump(linkifyNames(variables.stPartners, '//example.com/profiles/[id]/[dupe]/[lastname]/[seo]/', '<p>Michael Fi is a name</p>'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement