Color Coding My Code Samples - UGGG!

Posted August 29, 2006 at 8:26 AM by Ben Nadel

Tags: ColdFusion

I am finding it very hard to automate the coding of my code samples. I tried looking into Ray Camden's BlogCFC code, but unfortunately his does not work for my situation. My code is not stored a single string. Instead, my code is stored in an unordered list (<ol>) where each list item (<li>) is a single line of code (or partial line of code). I find this gives me much greater control over how the code is indented and how the lines wrap. I have to thank the XStandard people for the suggestion, which i LOVE.

However, because of this, I may or may not have a single ColdFusion tag that includes multiple list items. I am not sure how to add the color coded spans. I can't just add them at the beginning of one li and the end of another li as this is not XHTML compliant (nor do the color actually traverse the li's). I have to break each segment into it's own code block, therefore, remembering which the last code block was - am I in a new span tag? Am I continuing the span tag?

Then to make matters worse, what if one of the tags has a double quote in an attribute value, or a < or > in an attribute value. How do I know if I am at the end of a tag or in the middle of an attribute value? Both would be stored as &lt; and &gt; respectively. This is proving very frustrating.

But, I know I have to get this done as it makes the code soooo much more readable.




Reader Comments

Aug 29, 2006 at 8:53 AM // reply »
2 Comments

I put my code between <pre> tags so that they indent just like I want them to. I run it through Dain Anderson's coloredcode.cfm or if it is in my blog I use Ray Camden's which I have modified with the pre tags. It may work to run each individual line through coloredcode.cfm. I have the file if you need it.


Aug 29, 2006 at 8:59 AM // reply »
74 Comments

Trevor,

Hey man, thanks for dropping me a line. If I could get a look at Dain Anderson's code that would be sweeet. I am not sure I want to depart from the list code as I love the control it gives me over display, wrapping, scrolling, etc.... but how knows, I want to go with whatever works best.

Thanks for the help.


Aug 29, 2006 at 9:54 AM // reply »
153 Comments

I know this may be a silly question, but ...

Why not run it through the color-coder /before/ you split it into list items? (Or, for existing code, de-list, color-code, re-list.)

Admittedly, the re-listing part would be interesting. You'd have to have a tag stack for each line, and when you end a line, you close all of the open tags, then re-open them at the beginning of the next line. Aren't state machines fun?

Oh, and I don't know if you were aware, but your code-as-unordered-list trick mades viewing your posts in Google Reader that much more interesting:

http://rickosborne.org/images/screenshots/Ben-Nadel-Google-Reader.png

-R


Aug 29, 2006 at 10:00 AM // reply »
74 Comments

Rick,

Yeah, no matter what I do, its gonna be hard. I just don't want to do it manually as that will royally suck. I am still messing around with stuff... I am looking into using GetToken() to rip out each token, check the state (yeah state machines are fun) and rebuild. We will see... then I also have the problem of editing a blog entry... how will it know NOT to re-color code. I am thinking that this is something that I have to do PRE blog entry - like a special text field that I paste into that runs over the code, then I take that and put it into the blog.

I see the list in the PNG you sent. That is also the way it shows up in Feed-Squirrel. Are you saying you do or don't like it. Personally, I like it because it retains it's structure. Sure, you loose the indentation that you would have in my blog... but there is nothing I can do about that.

Of course, I am always open to other solutions as I always want what is best.


Aug 29, 2006 at 10:21 AM // reply »
153 Comments

Nah, I'm not saying I do or don't like it. It just takes a second to go "that's not a grocery list, that's code!". Maybe an ordered list would make for less cognitive dissonance?

Also, I've noticed that Yahoo! uses a JavaScript code-colorer, thus routing around many of the problems you mentioned. For example:

http://developer.yahoo.com/yui/connection/

Theirs also does spiffy things like adding in line numbers, making popups for windows with nothing but the code, etc.

But then, of course, you'd have the joys of writing a CF parser in JavaScript. At which point several dozen CF gurus would rip it from your site and give you absolutely no credit. So ... yeah.

Actually, I guess, you could probably take the one from Yahoo!, special-case it to color <cf* tags a little different, and then provide it with a list of CF-specific functions to also color a little differently. Shouldn't be *too* horrific.

...

It looks like Yahoo! uses the syntax highlighter from here:

http://www.dreamprojections.com/syntaxhighlighter/

It's got a concept of "brushes", with a brushes for SQL, JavaScript, HTML, and XML built-in. Adding a CF-brush probably wouldn't be too hard.


Aug 29, 2006 at 10:33 AM // reply »
74 Comments

Rick,

Awesome links man! Thanks for the good direction. I am not sure that I want to parse the code every time the page loads, but I might be able to go this route pre-form-submission and then save it as the highlighted code some how.

As far as the unordered list vs. ordered list, I went with the unordered list for copy-paste usage. If it was an ordered list and then you copied and pasted code into a text editor, each line begins with a different number (hence ordered list). However, with an unordered list, if you copy and paste into a text editor, only a bullet or a hash sign start each line. I just figured the hash symbol would be easier for people to replace out than unique numbers for each line.

But thanks again for the links, I will check them out asap.


Aug 29, 2006 at 6:07 PM // reply »
319 Comments

Rick, that is pretty dang slick. You know, I'm seriously considering trying that in BlogCFC.


Feb 2, 2007 at 12:21 AM // reply »
172 Comments

dp.SyntaxHighlighter, which Rick suggested, is indeed pretty cool, and it's what I use (not on my blog, which is shitty, but at work). SyntaxHighlighter has some very cool features like smart tabs, etc. Unfortunately, the author does not appear to be a regex expert (e.g., multi-line comments are very poorly supported in the XML/XTHML brush). I fixed the multi-line comments in my implementation, and started a basic CFML brush (one e.g. of a difference from the XML brush is not styling the variable name in cfset tags the same as attribute names). If you do end up using the library, I'd be very interested in seeing what you come up with for a CFML brush, or if you're interested I could show you the preliminary work I've done so far (which is good enough for basic CF code examples).


Feb 2, 2007 at 2:10 PM // reply »
11,246 Comments

Steve,

It does look like an awesome library. I am not quite sold on the client-side color coding. But, I am also not sold on color-coding ON OUTPUT. Ideally, what I would like is to color-code on input. I have not seen anything that I am fully in love with.

To date, I have not seen anything that works all that great. A lot of the libraries that color code use PRE tags. A PRE tag is great of a pop-up window or something, but as far as embedded within site content, PRE tags are horrible for layout control. One of the things that drives me up the wall is see poor line-wrapping in code. Code that wraps all the way to the gutter is not readable (at least to me). I use unordered lists which wrap text lines exactly how I want... problem is that LIs make SPAN tags hard to use. I am like 95% there in terms of parsing it and pushing-popping open/close span tags but I was doing it on output which was slow.

I will figure it out some day.


Feb 2, 2007 at 11:52 PM // reply »
172 Comments

Just FYI, dp.SyntaxHighlighter is implemented using <textarea> elements, which are converted to <ol>s at runtime. One big advantage of using textareas instead of pre elements for receiving input is that you can use "<" and ">" characters (vs. "<" and ">") inside it.


Feb 3, 2007 at 9:16 AM // reply »
11,246 Comments

Textarea... that is cool. Textarea can handle all the correct characters (as you point out). I think in order to use that, I would have to re-do the way I store my code snippets. Right now, my code goes right into my content on INPUT. I had an idea about storing my code snippets separately in my database and then in XStandard, I could put in templates like :

<codetemplate id="3453" />

or something, which would then get parsed on OUTPUT. But then again, I am doing too much on output. I don't like the idea of messing too much with my code on output. What to do, what to do.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 23, 2013 at 9:52 PM
Preventing Links In Standalone iPhone Applications From Opening In Mobile Safari
@Muhmmadibn Did you figure out a solution to launching PDFs? I am running into the same issues myself. There is no way to close the PDF or go back once you launch it. Thanks in advance! ... read »
May 23, 2013 at 6:06 PM
The Girl Who Broke My Heart, And Made Me A Better Person
Good day,ladies and gentle men, my name is Dr AMADI the great spell caster in Africa, i have help so many people for different kind of problems,who say there is no solution to problems on earth, that ... read »
May 23, 2013 at 4:26 PM
ColdFusion QueryAppend( qOne, qTwo )
@Heather, Glad people are still getting value out of this! ... read »
May 23, 2013 at 3:49 PM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@WebManWalking, I meant the code at the bottom (not the video). I did try to experiment with an intermediary variable, like: value = users.id[ i ]; arrayContains( userIDs, value ); ... but t ... read »
May 23, 2013 at 11:06 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, Are you talking about As Number: YES As String: YES As Java: YES? If so, that's with 3 different ways of referencing the constant 1, not users.id[1]. Query object references(*) are what seem ... read »
May 23, 2013 at 9:55 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Dan, According to the CF Admin, I'm running Java "1.6.0_45". As far as the DB column, in the database it's an INT. I'll see if I can dig into what CF sees it as. @WebManWalking, But h ... read »
May 23, 2013 at 9:49 AM
Strange Interaction Between DeserializeJson(), ArrayContains(), And Database Values In ColdFusion
@Ben, I think the problem is that we're used to loose typing in ColdFusion, like JavaScript. If a value is a number but it's needed in an expression to be a string, noooo problem. I've encountered ... read »
May 23, 2013 at 9:47 AM
ColdFusion QueryAppend( qOne, qTwo )
You rock! Thank you, thank you, thank you!!! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools