Skip to main content
September 9, 2006
Question

Code Colouring

  • September 9, 2006
  • 1 reply
  • 334 views
Does anyone know where I can find a UDF that colors code samples where for example...

<mycodeblock>

Would become...

<span style="color: brown;">&lt;mycodeblock&gt;</span>

When used such as...

<cfset text = "&lt;mycodeblock&gt;" />
<cfoutput>#colorCode(text)#</cfoutput>

This would be really handy to have so that if someone submits some code it can become coloured.

Many thanks.
    This topic has been closed for replies.

    1 reply

    Participating Frequently
    September 9, 2006
    Does this work for your intentions? It may need tweaking but is simple to follow should you want to alter it.
    September 10, 2006
    Thanks LeftCorner but I think my requirements would complicate that far more. I am looking for the kind of coloring that you normally see in code view in apps such as Dreamweaver or Homesite etc.

    The kind of code where it's clever enough to seperate tags, attributes and content so that maybe...

    <h1 title="my title">my title</h1>

    Would become...

    <span><h1 title=</span>
    <span>"my title"</span>
    <span>></span>
    <span>my title</span>
    <span></h1></span>

    Yes, that's alot of variables to go into there I'm sure...hmm...thanks for the help anyway!! I wonder if anyone out there has a UDF available for this specific purpose as I'm pretty sure it'd be very useful. Even colouring it manually with a WYSIWYG would be quite arduous!

    :)
    Participating Frequently
    September 10, 2006
    I liked the idea of this function so I started on it. Attached is what I have so far, which is to say it is incomplete, raw and hardly tested.

    It color codes a fair amount of HTML tags however it does not properly color inline CSS and JavaScript. I have to work on that. I'm sure it has some other bugs as well.

    The function takes two arguments, string to color code and the optional boolean argument of Preformatted (default to true). The preformatted argument simply wraps the whole string in a <pre> tag for display in the browser.

    Additionally one must escape the HTML entities if one is coloring code straight code. For example:

    <cfset str = "<b>This string needs to be HTML escaped</b>">

    <cfoutput>#ColorCode(HTMLEditFormat(str))#</cfoutput>

    Will preformat and color code to the browser. If your HTML is already escaped you can instead output by using this code:

    <cfset str = "&lt;b&gt;This string DOES NOT needs to be HTML escaped&lt;/b&gt;">

    <cfoutput>#ColorCode(str)#</cfoutput>

    I will continue to work on this function and will post it to my site, www.leftcorner.com, when it is complete.