Dreaming in color

Once I had a beautiful dream: I could display a JavaScript script in a web page, by simply setting the CSS style to

display: block; font-family: monospace; white-space: pre;

That way, I didn’t have to keep the real script synched up with the HTML preformatted text! The script was the displayed text. And my text editor (vi) recognized and colorized the script while I was editing it.

This worked fabulously in the warm friendly worlds of FireFox and Opera.

But then I woke up to the cold dull world of software to which most computer users are subjected, a world in which many neat and reasonable things fail. And the W3C agrees: script tags do not take class or style attributes.

So I had to go back to the pillow. And what I came out with was: PHP. Not quite as neat, but much more powerful. I would be surprised is someone hasn’t already done it: you just need a JavaScript parser written in PHP, and voilà, you have a JavaScript colorizer!

PHP and XSLT

Of course, for the simple copying job, one could also use XSLT. (Oh no. Now I’m going to have to go off and try that, too.) But this isn’t as flexible as PHP. (AW heck, somebody with a sufficiently miniscule appreciation for their time on the planet might write a JavaScript parser in XSLT, and then we could have a spiff XSLT colorizer).

One advantage of PHP over XSLT is that it depends only on mature software support on the server. On the other hand, I can't open the raw files with a browser…they have to pass through a server to be readable. With the XSLT approach, the transformation could be done on the server side, or a sufficiently-equipped browser could also handle it.

XSLT is intended to be a kind of styling language, so it makes better sense.

JavaScript colorizes itself

On third thought, these pages are all about JavaScript, which is quite capable of colorizing itself, thank you. So that is the current solution.