Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.40 KB | None | 0 0
  1. <cfcomponent>
  2. <cffunction name="init" access="public" output="false" returntype="PartnerHighlighter" hint="Pseudo-constructor">
  3. <cfargument name="stPartners" type="struct" required="true">
  4. <cfargument name="sLink" type="string" required="true" hint="the link to use when replacing.">
  5. <cfscript>
  6. variables.stPartners = arguments.stPartners;
  7. variables.sStopString = '<span class="nolink">';
  8. variables.sLink = arguments.sLink;
  9.  
  10. return this;
  11. </cfscript>
  12. </cffunction>
  13.  
  14. <cffunction name="highlightPartners" returntype="string" access="public" hint="this searches through the partner struct and highlights the partner">
  15. <cfargument name="sSearchText" type="string" required="true" hint="the text to search">
  16. <cfargument name="sHighlightType" type="string" required="false" default="link" hint="can add others if needed later">
  17. <cfscript>
  18. local.sHighlightedText = arguments.sSearchText;
  19. local.lstPunct = '';
  20. local.lstPunct = listAppend(local.lstPunct, ",", ',');
  21. local.lstPunct = listAppend(local.lstPunct, ".", ',');
  22. local.lstPunct = listAppend(local.lstPunct, " ", ',');
  23. local.lstPunct = listAppend(local.lstPunct, "'", ',');
  24. local.lstPunct = listAppend(local.lstPunct, '"', ',');
  25. local.lstPunct = listAppend(local.lstPunct, '’', ',');
  26. local.lstPunct = listAppend(local.lstPunct, '<', ',');
  27. local.lstPunct = listAppend(local.lstPunct, '>', ',');
  28. local.lstPunct = listAppend(local.lstPunct, '&', ',');
  29. local.lstPunct = listAppend(local.lstPunct, '?', ',');
  30. local.lstPunct = listAppend(local.lstPunct, '!', ',');
  31. local.lstReplaced = "";
  32.  
  33. for(local.stPartner in variables.stPartners){
  34. local.stArguments.stPartner = variables.stPartners[local.stPartner];
  35. local.stArguments.sHighlightType = arguments.sHighlightType;
  36. local.nNameLength = len(local.stArguments.stPartner.sFullName);
  37.  
  38. local.nExists = findnocase(local.stArguments.stPartner.sFullname, local.sHighlightedText);
  39. local.aBytes = local.sHighlightedText.GetBytes();
  40.  
  41. if(local.nExists gt 0){
  42. local.sSupposedName = mid(local.sHighlightedText, local.nExists, local.nNameLength);
  43.  
  44. local.nNextByte = (local.nExists-1)+(local.nNameLength+1);
  45.  
  46. if(listFindNoCase(local.lstPunct, Chr(local.aBytes[local.nNextByte]), ',') gt 0){
  47. local.nStop = findNoCase(variables.sStopString,local.sHighlightedText,local.nExists-22);
  48. if(listFindNoCase(local.lstReplaced, local.stArguments.stPartner.sFullName, "|") eq 0 AND local.nStop eq 0){
  49. local.sHighlighted = getHighlight(argumentCollection=local.stArguments);
  50. local.sHighlightedText = rereplacenocase(local.sHighlightedText, local.stArguments.stPartner.sFullname, local.sHighlighted, "all");
  51. local.lstReplaced = listAppend(local.lstReplaced, local.stArguments.stPartner.sFullName, "|");
  52. }
  53. }
  54. }
  55. }
  56. return local.sHighlightedText;
  57. </cfscript>
  58. </cffunction>
  59.  
  60. <cffunction name="getHighlight" returntype="string" access="private" hint="do the actual highlight">
  61. <cfargument name="stPartner" type="struct" required="true" hint="the partner">
  62. <cfargument name="sHighlightType" type="string" required="true" hint="can add others if needed later">
  63. <cfscript>
  64. var stLocalPartner = arguments.stPartner;
  65. var sLocalHighlightType = arguments.sHighlightType;
  66. var sHighlight = "";
  67. var sNewLink = "";
  68.  
  69. if(local.sLocalHighlightType eq "link"){
  70. local.sNewLink = rereplacenocase(variables.sLink, "[id]", local.stLocalPartner.nID, "all");
  71. local.sNewLink = rereplacenocase(local.sNewLink, "[lastname]", local.stLocalPartner.sLastName, "all");
  72. local.sNewLink = rereplacenocase(local.sNewLink, "[dupe]", local.stLocalPartner.sIsDupe, "all");
  73. local.sNewLink = rereplacenocase(local.sNewLink, "[seo]", local.stLocalPartner.sFullName, "all");
  74. local.sHighlight = '<a href="#local.sNewLink#" title="more information on this expert from example.com" target="_blank">#local.stLocalPartner.sFullName#</a>';
  75. local.sHighlight = "<span>#local.sHighlight#</span>";
  76. }
  77. return local.sHighlight;
  78. </cfscript>
  79. </cffunction>
  80. </cfcomponent>
  81.  
  82. <cfscript>
  83. variables.stPartners = {'123'={sFullName='Michael Fish',nID='133',sLastName='Fish',sIsDupe='No'},'333'={sFullName='Michael Fisher',nID='133',sLastName='Fisher',sIsDupe='No'}};
  84.  
  85. variables.stArgs = {};
  86. variables.stArgs.stPartners = variables.stPartners;
  87. variables.stArgs.sLink = '//example.com/profiles/[id]/[dupe]/[lastname]/[seo]/';
  88.  
  89. variables.oPartnerHighlight = createObject('component','PartnerHighlight').init(argumentCollection=variables.stArgs);
  90.  
  91. variables.stArgs = {};
  92. variables.stArgs.sSearchText = '<p>Michael Fish is a Weatheman</p>';
  93.  
  94. writeOutput(variables.oPartnerHighlight.highlightPartners(argumentCollection=variables.stArgs));
  95.  
  96. //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>
  97.  
  98. variables.stArgs = {};
  99. variables.stArgs.sSearchText = '<p>Michael Fi is a name</p>';
  100.  
  101. writeOutput(variables.oPartnerHighlight.highlightPartners(argumentCollection=variables.stArgs));
  102.  
  103. //expecting output <p>Michael Fi is a name</p>
  104.  
  105. variables.stArgs = {};
  106. variables.stArgs.sSearchText = '<p>John Travolta is an Actor</p>';
  107.  
  108. writeOutput(variables.oPartnerHighlight.highlightPartners(argumentCollection=variables.stArgs));
  109.  
  110. //expecting output <p>John Travolta is an Actor</p>
  111. </cfscript>
  112.  
  113. function linkifyNames(required struct names, required string template, required string text) {
  114.  
  115. for (var key in names){
  116. var name = names[key];
  117.  
  118. //this could be cleaned up more if you used the actual variable names from the struct with the hungarian notation
  119. //or if you dropped the hungarian notation
  120. name.expandedLink = '<a href="' & template & '" title="more information on this expert from example.com" target="_blank">' & name.sFullname & '</a>';
  121. name.expandedLink = replaceNoCase(name.expandedLink, "[id]", name.nID);
  122. name.expandedLink = replaceNoCase(name.expandedLink, "[dupe]", (name.sIsDupe ? 1 : 0));
  123. name.expandedLink = replaceNoCase(name.expandedLink, "[lastname]", name.sLastName);
  124. name.expandedLink = replaceNoCase(name.expandedLink, "[seo]", name.sFullName);
  125.  
  126. text = reReplaceNoCase(text, "b" & name.sFullname & "b", name.expandedLink, "all");//needs to be a regex
  127. }
  128.  
  129. return text;
  130. }
  131.  
  132.  
  133. variables.stPartners = {'123'={sFullName='Michael Fish',nID='133',sLastName='Fish',sIsDupe='No'},'333'={sFullName='Michael Fisher',nID='133',sLastName='Fisher',sIsDupe='No'}};
  134.  
  135. writedump(linkifyNames(variables.stPartners, '//example.com/profiles/[id]/[dupe]/[lastname]/[seo]/', '<p>Michael Fish is a name</p>'));
  136. 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