Code highlighter: expand on mouse over

Written on 30 December 2011, 11:03am

Tagged with: , ,

Inspired by the article A Design Is Only As Deep As It Is Usable featured in the e-book Modern Web Design & Development – I decided to make the blocks of code on this WordPress-powered blog expandable on mouse over. As you will see soon, unfortunately this is not an easy job.

1. Syntax Highlighting

Let’s get back a little. When it comes to syntax highlighting, you have 2 main options:

  1. Alex Gorbatchev’s SyntaxHighlighter
  2. GeSHi – Generic Syntax Highlighter

They both support ColdFusion syntax, and they are both featured in WordPress plugins. Because of the better functionalities, ease of use and because it simply looks better, when I started this blog I decided to use the SyntaxHighlighter – by installing the SyntaxHighlighter Evolved WordPress plugin. This allowed me to simply add ColdFusion code by simply wrapping the code in [ cf ] ...[ /cf ] markers. I solved the long lines problem by using the ‘wrap long lines’ option, as shown below:

2. Expand the code


ColdFusion – get admin mappings

Written on 19 December 2011, 03:43pm

Tagged with:

Here is a very useful recipe from the Adobe Cookbooks: how to retrieve the mappings from the admin area.
It’s really simple, it involves using the coldfusion.server.ServiceFactory java class, but I am posting it here because I am sure that I will come back to this post at some point in the future 🙂

<cffunction name="getMappings" access="public" returntype="struct" output="false">
    <cfset var mappings = StructNew()>
    <cfset ServiceFactory = createObject("java","coldfusion.server.ServiceFactory")>
    <cfset mappings = ServiceFactory.runtimeService.getMappings()>
    <cfreturn mappings>

Et voila:

This is useful when you don’t have access to the CF Admin interface.

Remember that the CF Admin Mappings are for ColdFusion, while the virtual directories are for the web server!

Update, 02 March 2015: In case you have access to ColdFusion configuration files, the mappings can be found in plain text in the file neo-runtime.xml (link)

How many lines of code do you have?

Written on 2 December 2011, 05:41pm

Tagged with:

Do you want to know how many lines of code you have in your application? Challenge accepted 🙂
Here’s a 10-minutes, 50-lines ColdFusion script that counts the number of lines in all the files from a given folder.

<cfsetting requesttimeout="300" showdebugoutput="no" />
<cfset initialDir = ExpandPath( '../' )>
<cfset files = 0> <!-- we will store the number of files that we traversed --->
<cfset lines = 0> <!--- this is what we need  --->
<cfset localRoot = "E:\your\input\folder" />
<cfset separator = '\'>
<cfset extensions = 'cfm,cfc,htm,html,css,js'> <!-- add here the list of extensions --->

<cfset tick = GetTickCount()> <!--- start the clock --->

<!--- get list of ALL files --->
<cfdirectory action="list" directory="#initialDir#" name="localQuery" recurse="true"/>

<cfoutput query="localQuery">
	<cfif type EQ 'File'>
		<cfset extension = LCase(listLast(name,".")) /> <!--- get the extension --->
		<cfset temp = ListFindNoCase(extensions, extension)> 
		<cfif temp IS NOT 0> <!--- if the file extension is one in our initial list --->
			<cfset path = directory & separator &  name>
			<cfset files = files + 1>
			<cfif FileExists(path)>
					<cffile action="read" file="#path#" variable="content" charset="utf-8">
				<cfcatch type="all">Error reading file</cfcatch>
				<cfset content = replace(content,Chr(10),Chr(13),"ALL")/> <!--- replace all LFs with CRs --->
				<cfset content = replace(content,Chr(13)&Chr(13),Chr(13),"ALL")/> <!--- replace all double CRs with single CRs --->
				...and here is most important part: 
				we remove ALL the other characters that are NOT CR (\r)
				we end up with a string containing ONLY the CRs
				we simply count the length of this string and then add 1
				<cfset thisFileLines = Len( REReplace( content, "[^\r]+", "", "ALL" ) ) />
				<cfset thisFileLines = thisFileLines + 1> 

				<cfset lines = lines + thisFileLines>
				File #path# has <strong>#thisFileLines#</strong> lines<br /> 
		</cfif> <!--- end if extension --->
	</cfif><!--- end if type file --->

<cfset tock = GetTickCount()> <!--- stop the clock --->
<cfset time = round((tock-tick)/1000)>

<cfoutput><hr />There are <strong>#lines#</strong> lines in #files# files. Search took #time# seconds.</cfoutput>

I got:

There are 101637 lines in 527 files. Search took 3 seconds.

See also