Colored Code with Coldfusion

On February 14, 2008, in ColdFusion, by Anuj Gakhar

Just found this little thingy which according to me is quite impressive.

<cfset obj = createObject("java","coldfusion.util.FormatSource")>
<cfoutput>
#obj.formatFile(Expandpath('cfdirectory.cfm'))#
</cfoutput>

Now what that does is takes a file from your local folder and applies colored coding to it. Not the best colored code but a lot better than the heavy Regular expression.

Try it out !

Tagged with:  

8 Responses to Colored Code with Coldfusion

  1. Just so you know, there is also a formatString() option. Don’t forget you can dump Java objects to see their methods.

  2. Anuj Gakhar says:

    @Ray, yes thats right. Only after I dumped these , I found out about these 2 public methods. Very useful I think. Looks like there are lots of little hidden utilities out there.

  3. […] correct dimensions, as Rupesh points out. Anuj Gakhar has found another hidden gem using Java – the ability to display color-coded source code using a method in one of the built-in libraries.  Ben Nadel has created a user-defined […]

  4. Tracy Logan says:

    Thanks very much for posting this — it is exactly what I needed, a simple way to display tag-based source code.

  5. akos says:

    Thanks for this it really helped, how can I do the same without color coding it? Also to display just a code snippet rather than an entire page?
    Do I still need to write it to a file?
    Just to display maybe a line of code like you have on this page:

    Any help would be appreciated.

    • Anuj Gakhar says:

      You are probably better off using one of the code coloring JavaScript libraries e.g. Google prettify etc there are loads of them around. This ColdFusion code won’t work on railo and I wouldn’t use it for production use.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 445 other subscribers

© 2011 Anuj Gakhar
%d bloggers like this: