Posted by Dan | Posted in ColdFusion | Posted on 02-19-2010
0
I put this ColdFusion UDF together the other day to turn relative URLs to Absolute. Code is pretty straightforward.
<cffunction name="URLRelativeToAbsolute" returntype="string"
hint="Converts relative URLs in an element and converts to absolute. It includes the http:// protocol prefix.">
<cfargument name="content" type="string" required="true" hint="HTML content that will be scanned and replaced." />
<cfargument name="domain" type="string" required="true" hint="Add domain name to relative links." />
<cfset var local = StructNew() />
<!--- The following regexp handles the following elements: link, a, img, script, form, frame. --->
<cfset local.contentFixed = REReplaceNoCase( Arguments.content, "(href|src|action)=""/?((\./)|(\.\./)+|)(?=[^http])", "\1=""http://" & domain & "/", "all" ) />
<!--- The following regexp handles the url() attribute of the background CSS property. --->
<cfset local.contentFixed = REReplaceNoCase( local.contentFixed, "url\((\s)?(')?/?((\./)|(\.\./)+|)(?=[^http])", "url(\2http://" & domain & "/", "all" ) />
<cfreturn local.contentFixed />
</cffunction>
Usage:
<cfsavecontent variable="htmlContent">
<textarea name="data" rows="20" cols="60">
<style>
body {
background-image:url('stars.png');
background-image:url('../stars.png');
background-image:url('/stars.png');
background-image:url('/../../../stars.png');
}
</style>
<a href="../../../images/shiny.jpg">Shiny</a>
<a href="http://www.google.com">This should not be touched</a>
<img border="0" src="/images/cool.png" />
<link rel="index" href="../../index.asp">
<form method="POST" action="cgi/processing.cgi"></form>
</textarea>
</cfsavecontent>
<cfoutput>
#htmlContent#
#URLRelativeToAbsolute( htmlContent, "www.shinylight.com" )#
</cfoutput>
Result:

Posted by Dan | Posted in ColdFusion, XML | Posted on 01-03-2010
0
Whipped out this little script to read an OPML file from Google Reader. Thought it may be handy.
<cfset GoogleOPMLFile = "C:/google-reader-subscriptions.xml" />
<cffile action="READ" variable="xml" file="#GoogleOPMLFile#" />
<cfset xmlDoc = XMLParse(xml) />
<cfset StartingDataNode = 2 />
<cfset Categories = ArrayLen( xmlDoc.opml.xmlChildren[2].XmlChildren ) />
<cfoutput>
<cfloop index="i" from="2" to="#Categories#">
<strong>#xmlDoc.opml.xmlChildren[StartingDataNode].XmlChildren[i].XmlAttributes.Title#</strong>
<ul>
<cfloop index="j" from="1" to="#ArrayLen( xmlDoc.opml.xmlChildren[StartingDataNode].XmlChildren[i].XmlChildren )#">
<li>
<a href="#xmlDoc.opml.xmlChildren[StartingDataNode].XmlChildren[i].XmlChildren[j].XmlAttributes.htmlURL#">
#xmlDoc.opml.xmlChildren[StartingDataNode].XmlChildren[i].XmlChildren[j].XmlAttributes.title#</a>
</li>
</cfloop>
</ul>
</cfloop>
</cfoutput>
The code will display as follows:

Posted by Dan | Posted in ColdFusion, Python | Posted on 12-29-2009
0
I just tried to write a quick script in Python that scans CFCs and generates a yUML URL to diagram. I pointed my script to my root CFC path and I got a 13K strlen URL. I pasted it in the address bar to see what happened and I got the following:
Request-URI Too Large
The requested URL's length exceeds the capacity limit for this server.
Apache/2.2.3 (Debian) Phusion_Passenger/2.0.2 Server at Ess000235.gtcust.grouptelecom.net Port 80
I wonder what the limitation is. I suppose I’ll have to do a CFC per diagram and then bind them together somehow. I’m choosing Python so this script can be part of my build script.
Here’s the code so far, which of course, could be optimized:
import re
import os
# UML Syntax
# http://yuml.me/diagram/class/[User|Property1;Property2|Method1();Method2()]
# http://yuml.me/diagram/class/
# [
# User
# |
# Property1;
# Property2
# |
# Method1();
# Method2()
# ]
# Master Path
ROOT_PATH = 'C:\\temp\\cf-yuml'
def SearchForFile( rootpath, searchfor, includepath = 0 ):
# Search for a file recursively from a root directory.
# rootpath = root directory to start searching from.
# searchfor = regexp to search for, e.g.:
# search for *.jpg : \.exe$
# includepath = appends the full path to the file
# this attribute is optional
# Returns a list of filenames that can be used to loop
# through.
#
# TODO: Use the glob module instead. Could be faster.
names = []
append = ""
for root, dirs, files in os.walk( rootpath ):
for name in files:
if re.search( searchfor, name ):
if includepath == 0:
root = ""
else:
append = "\\"
names.append( root + append + name )
return names
def getCFCInfo ( FILE, path ):
FILE.seek( 0, 0 )
CFCLines = FILE.readlines()
CFCFunctions = []
CFCProperties = []
CFC = {}
for i in CFCLines:
# Get names of methods
if re.search( "^<cffunction", i , re.IGNORECASE | re.MULTILINE ):
CFCFunctions.append( re.search( r'name\s*=\s*"([\w$-]+)"', i, re.DOTALL | re.IGNORECASE).group(1) )
# Get names of properties
if re.search( "^<cfproperty", i , re.IGNORECASE | re.MULTILINE ):
CFCProperties.append( re.search( r'name\s*=\s*"([\w$-]+)"', i, re.DOTALL | re.IGNORECASE).group(1) )
CFC = { "properties":CFCProperties, "methods":CFCFunctions }
# Generate URL
strFunctions = ""
strProperties = ""
for i in CFCFunctions:
strFunctions += i + "();"
for i in CFCProperties:
strProperties += i + ";"
CFCFileName = re.search(r"\\([\w-]+)\.cfc$", path, re.DOTALL | re.IGNORECASE).group(1)
return "[" + CFCFileName + "|" + ( strProperties.strip()[:-1] + "|" if strProperties.strip()[:-1] else "" ) + strFunctions.strip()[:-1] + "]"
URL = ""
for i in SearchForFile( ROOT_PATH, "\.cfc$", 1 ):
CFCFile = open( i, "r" )
URL += getCFCInfo( CFCFile, i ) + ","
CFCFile.close()
URL = URL[:-1]
print "http://yuml.me/diagram/class/" + URL
I’ll keep working on this as time goes on. So far it just goes through all the CFC’s from the path you point to. It will crawl through all sub directories. There’s no relationship between classes, however. Not yet at least.