Instantly share code, notes, and snippets.
Created
January 5, 2011 22:17
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save aqlong/767133 to your computer and use it in GitHub Desktop.
First, the function. Then, the tests...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <cffunction | |
| name="addLinkTagsToURLStrings" | |
| access="public" | |
| returntype="string" | |
| output="false" | |
| hint="I parse some text, find URLs and apply an HTML HREF link to each."> | |
| <cfargument | |
| name="Text" | |
| type="string" | |
| required="true" | |
| hint="I am the status text to parse." | |
| /> | |
| <!--- OLD: (?i)(https?://[\w\.\-_\/\\##\?\+\=]+[-&\%\w\/\\##\?\+\=]+) ---> | |
| <!--- modified from Ben Nadel's work at http://www.bennadel.com/index.cfm?dax=blog:487.view ---> | |
| <!--- regex to find URLs, with or w/o http(s) ---> | |
| <cfsavecontent variable="local.strIsUrlRegEx" | |
| >(?ix) | |
| ## We are going to wrap the entire match in parenthesis so | |
| ## that we can refer to the entire group at match group | |
| ## one ($1). | |
| ( | |
| ## First, we want to come up with a the web protocol. | |
| ## This will probably be HTTP but, let's define some | |
| ## others just to make sure we cover the standards. | |
| ## protocol is optional | |
| ( | |
| ((ftp|http|https|mailto|news|svn)://)? | |
| ## If we do use a protocol, then we have the option | |
| ## to define a username and password for this url. | |
| ## Most urls will NOT have this, but sites that use | |
| ## Window's authentication (???) use this. Better | |
| ## safe than sorry. However, if login credentials | |
| ## are used, they must end with "@". | |
| ([^:]+\:[^@]*@)? | |
| ) | |
| ## Now, we have to define the sub-domain. This is generally | |
| ## the "www" before the domain, but this might include any | |
| ## number of values. This entire value is optional, | |
| ## however, if this value is used, it MUST end with a "." | |
| [[\d\w\-]+\.]+ | |
| ## Now, let's define the domain. This will be any | |
| ## combination of values that has a domain extension | |
| ## (ex. .com, .edu) and does not yet include a directory | |
| ## structure of any kind. This is NOT an optional value. | |
| [\w\d\-]+\.(com|net|org|info|biz|tv|tt|jp|us|co\.uk|ag|am|de|im|re|ro|nl|nr|me|ms|st|fr|gl|gy|it|li|ly|mp|bi|bz|cc|ch|co|gd|to|pr|re|aero|coop|museum|name|jobs|travel|mobi) | |
| ## Once the domain and extension are defined, everything | |
| ## else is optional. That means that everything after this | |
| ## point MIGHT be there, but is not required. Therefore, | |
| ## we have to group the rest of this and make it optional. | |
| ## After the domain name and extension comes the | |
| ## optional directory structure and file name. | |
| ( | |
| ( / [\w\d\.\-@%\\\/\#:]* )+ | |
| )? | |
| ## Now that we have the file name defined, we can | |
| ## define the optional query string. The query | |
| ## string, if it is used, MUST begin with the | |
| ## question mark literal "?". | |
| ( | |
| \? | |
| ## After the query string delimiter (?), pretty | |
| ## much anything is fair game. I don't actually | |
| ## know what is ## valid in a URL so I just go | |
| ## with this set of characters which is what I | |
| ## see a lot. | |
| [\w\d\?%,\.\/\#!@:=\+~_\-&]* | |
| ## Even though the query string can contain | |
| ## just about anything, I want to make sure that | |
| ## it does NOT end with certain characters. Of | |
| ## the characters above, I find that URLs using | |
| ## query strings often should not include the | |
| ## final "." as that is usually the period for | |
| ## the containing sentence. Use a negative | |
| ## look-behind to make sure that the previous | |
| ## character is not of this set. | |
| (?![\."]) | |
| )? | |
| ## ^ Ended the optional query string. | |
| ## ^ We just ended the optional post-domain-extension area | |
| ## of the url. | |
| ) | |
| ## ^ Just ended the convenience group one ($1) that allows | |
| ## us to refer the entire URL at the first group. | |
| </cfsavecontent> | |
| <cfset local.Text = ARGUMENTS.Text.ReplaceAll( | |
| JavaCast( "string", local.strIsUrlRegEx ), | |
| JavaCast( "string", '<a href="$1" target="_blank">$1</a>' ) | |
| ) /> | |
| <!--- add the http if it didn't already exist ---> | |
| <cfif NOT FindNoCase("http", local.Text)> | |
| <cfset local.Text = Replace(local.Text,'<a href="', '<a href="http://', 'all')> | |
| </cfif> | |
| <!--- Return the updated link. ---> | |
| <cfreturn local.Text /> | |
| </cffunction> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <cfcomponent extends="mxunit.framework.TestCase" output="false"> | |
| <cfprocessingdirective pageencoding="utf-8" /> | |
| <cffunction name="setUp" access="public" returntype="void"> | |
| <!--- Place additional setUp and initialization code here ---> | |
| <cfscript> | |
| this.obj = createObject("component","refynr.com.Utils").init(); | |
| </cfscript> | |
| </cffunction> | |
| <cffunction name="tearDown" access="public" returntype="void"> | |
| <!--- Place tearDown/clean up code here ---> | |
| </cffunction> | |
| <cffunction name="test_addLinkTagsToURLStrings" access="public"> | |
| <cfscript> | |
| value = this.obj.addLinkTagsToURLStrings( "test 1 http://refynr.com" ); | |
| assertEquals( | |
| 'test 1 <a href="http://refynr.com" target="_blank">http://refynr.com</a>', | |
| value, | |
| 'Should be equal.'); | |
| value = this.obj.addLinkTagsToURLStrings( "test 2 http://refynr.com/?test" ); | |
| assertEquals( | |
| 'test 2 <a href="http://refynr.com/?test" target="_blank">http://refynr.com/?test</a>', | |
| value, | |
| 'Should be equal.'); | |
| value = this.obj.addLinkTagsToURLStrings( "test 3 http://refynr.com/?test=333" ); | |
| assertEquals( | |
| 'test 3 <a href="http://refynr.com/?test=333" target="_blank">http://refynr.com/?test=333</a>', | |
| value, | |
| 'Should be equal.'); | |
| value = this.obj.addLinkTagsToURLStrings( " http://refynr.com/?test=444&fun=true test 4" ); | |
| assertEquals( | |
| ' <a href="http://refynr.com/?test=444&fun=true" target="_blank">http://refynr.com/?test=444&fun=true</a> test 4', | |
| value, | |
| 'Should be equal.'); | |
| value = this.obj.addLinkTagsToURLStrings( " https://ssl.refynr.com/?test=555&fun=true" ); | |
| assertEquals( | |
| ' <a href="https://ssl.refynr.com/?test=555&fun=true" target="_blank">https://ssl.refynr.com/?test=555&fun=true</a>', | |
| value, | |
| 'Should be equal.'); | |
| </cfscript> | |
| </cffunction> | |
| <cffunction name="test_addLinkTagsToURLStrings_domainOnly" access="public"> | |
| <cfscript> | |
| value = this.obj.addLinkTagsToURLStrings( "test 1 refynr.com" ); | |
| assertEquals( | |
| 'test 1 <a href="http://refynr.com" target="_blank">refynr.com</a>', | |
| value, | |
| 'Should be equal.'); | |
| value = this.obj.addLinkTagsToURLStrings( "test 1.1 refy.nr/index.cfm##link2" ); | |
| assertEquals( | |
| 'test 1.1 <a href="http://refy.nr/index.cfm##link2" target="_blank">refy.nr/index.cfm##link2</a>', | |
| value, | |
| 'Should be equal.'); | |
| value = this.obj.addLinkTagsToURLStrings( "test 1.2 refy.nr/##link2" ); | |
| assertEquals( | |
| 'test 1.2 <a href="http://refy.nr/##link2" target="_blank">refy.nr/##link2</a>', | |
| value, | |
| 'Should be equal.'); | |
| value = this.obj.addLinkTagsToURLStrings( "test 2 refynr.com/?test" ); | |
| assertEquals( | |
| 'test 2 <a href="http://refynr.com/?test" target="_blank">refynr.com/?test</a>', | |
| value, | |
| 'Should be equal.'); | |
| value = this.obj.addLinkTagsToURLStrings( "test 3 refynr.nl/?test 333 " ); | |
| assertEquals( | |
| 'test 3 <a href="http://refynr.nl/?test" target="_blank">refynr.nl/?test</a> 333 ', | |
| value, | |
| 'Should be equal.'); | |
| value = this.obj.addLinkTagsToURLStrings( "test 4 refynr.nl/?test=444 " ); | |
| assertEquals( | |
| 'test 4 <a href="http://refynr.nl/?test=444" target="_blank">refynr.nl/?test=444</a> ', | |
| value, | |
| 'Should be equal.'); | |
| </cfscript> | |
| </cffunction> | |
| <cffunction name="test_addLinkTagsToURLStrings_weird" access="public"> | |
| <cfscript> | |
| value = this.obj.addLinkTagsToURLStrings( "test 1 http://bit.ly" ); | |
| assertEquals( | |
| 'test 1 <a href="http://bit.ly" target="_blank">http://bit.ly</a>', | |
| value, | |
| 'Should be equal.'); | |
| value = this.obj.addLinkTagsToURLStrings( "test 2 http://refynr.com/?test##link1" ); | |
| assertEquals( | |
| 'test 2 <a href="http://refynr.com/?test##link1" target="_blank">http://refynr.com/?test##link1</a>', | |
| value, | |
| 'Should be equal.'); | |
| value = this.obj.addLinkTagsToURLStrings( "test 3 http://ad.ly/?test=333" ); | |
| assertEquals( | |
| 'test 3 <a href="http://ad.ly/?test=333" target="_blank">http://ad.ly/?test=333</a>', | |
| value, | |
| 'Should be equal.'); | |
| value = this.obj.addLinkTagsToURLStrings( "http://test.www1.refynr.com/?test=444&fun=true test 4" ); | |
| assertEquals( | |
| '<a href="http://test.www1.refynr.com/?test=444&fun=true" target="_blank">http://test.www1.refynr.com/?test=444&fun=true</a> test 4', | |
| value, | |
| 'Should be equal.'); | |
| value = this.obj.addLinkTagsToURLStrings( "https://ssl.refynr.com/?test=555&fun=true##link2" ); | |
| assertEquals( | |
| '<a href="https://ssl.refynr.com/?test=555&fun=true##link2" target="_blank">https://ssl.refynr.com/?test=555&fun=true##link2</a>', | |
| value, | |
| 'Should be equal.'); | |
| value = this.obj.addLinkTagsToURLStrings( "http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Java_6.html##1140554" ); | |
| assertEquals( | |
| '<a href="http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Java_6.html##1140554" target="_blank">http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Java_6.html##1140554</a>', | |
| value, | |
| 'Should be equal.'); | |
| value = this.obj.addLinkTagsToURLStrings( 'Your CF is showing in your Privacy Policy!: the "Account" section from Refynr.com or they can...' ); | |
| assertEquals( | |
| 'Your CF is showing in your Privacy Policy!: the "Account" section from <a href="http://Refynr.com" target="_blank">Refynr.com</a> or they can...', | |
| value, | |
| 'Should be equal.'); | |
| // Tea Retailers http://t.co/vjlbO9d Featuring | |
| /* TODO: figure out why this one doesn't work | |
| value = this.obj.addLinkTagsToURLStrings( 'Tea Retailers http://t.co/vjlbO9d Featuring' ); | |
| assertEquals( | |
| 'Tea Retailers <a href="http://t.co/vjlbO9d" target="_blank">http://t.co/vjlbO9d</a> Featuring', | |
| value, | |
| 'Should be equal.'); */ | |
| </cfscript> | |
| </cffunction> | |
| </cfcomponent> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment