Saturday, May 27, 2006

Coloring problems

So, last time I started on a little javascript, which would colorize automatically the sample codes I embed here. Well, I’ve regretted it when I had to fight with RegExp and IE and FF differences (IE - Internet Explorer, FF - Mozilla Firefox).

The first major annoyance (I consider something major annoyance, when you can’t find a solution in Google in less than two minutes, or when there is no general solution, which is the case now, because IE and FF not willing to do things the same) is when I can access html blocks marked with <div id=”code”> in IE as an array (if there is more repeated blocks) with
document.all(“code”)
, but I cannot wit FF. So I can use the standard getElementById, but then it will give back only the first block.
To make a not so long, but mind-bogging story short, finally I came up with what you see in the code in the previous post, using getElementsByName, and marking the div part with both id and name.

And while veteran coders all praise RegExp, and I cannot say different, than to learn it and use it, it has its own ways… and it’s really hard to test the RegExp machine inside javascript, considering it may have differences in IE and FF.

The extra parsing that this blogger site puts into the blog posts doesn’t help also. I had to put and upload my code changes into the template of this site, and check the blog post containing sample code in IE and in FF.
After a lot of trial and error, I begun to think that the replacing approach maybe not the best one, because I still have problems with string literals inside comments, comment characters inside string literals, and keywords in comments and string literals. But to parse the code as a real parser would do just to colorize it, and do it in javascript… well, that’s a task I’m not willing to take.

And I still cannot escape some manual replacement and formatting when posting code, I have to break the long lines, and have to replace lesser than and greater than signs, and this saddens me, because this way I’d even replace all that is needed in one pass, offline, before posting…
But typing in really simple code right into the blogger web ui is much simpler.

If you have ideas to improve the colorizer, or have a completely different solution, let me know.

0 Comments:

Post a Comment

<< Home