New feature: java syntax highlighting (finally!)

I just fixed the ‘last’ bugs in the java syntax highlighter. The combination of SMF (which alters code) and JsSyntaxHighlighter (which alters code) was causing a lot of problems. In the end I solved the problem with a lot of string-replace operations in javascript, and fed the result to JsSyntaxHighlighter.

I disabled the syntax highlighter in MSIE, because it is still unreliable there.

Please test this new functionality, as it’s likely to still have a bug or too.

Here goes nothing:

/*
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Oracle or the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */ 

class ArithmeticDemo {

     public static void main (String[] args){
          
          int result = 1 + 2; // result is now 3
          System.out.println(result);

          result = result - 1; // result is now 2
          System.out.println(result);

          result = result * 2; // result is now 4
          System.out.println(result);

          result = result / 2; // result is now 2
          System.out.println(result);

          result = result + 8; // result is now 10
          result = result % 7; // result is now 3
          System.out.println(result);

     }
}

yay for double scroll bars :slight_smile:

Browser? Version?

Google Chrome 8.0.552.224.

Looking good, but same here (FF). There’s a slight delay then the colouring & second scroll bar appear.


code = code.replace(/
/g,    "\r\n");
code = code.replace(/
/g,    "\r\n");
code = code.replace(/<br \/>/g, "\r\n");
code = code.replace(/<BR \/>/g, "\r\n");

Yikes! o_O

Better:


code = code.replace(/<br\s*\/?>/gi, "\r\n");

I hate any of IE but I tested with 7 at work for testing purposes: Single scroll bar but as you mentionned, no color syntax, which is normal for IE.

Double scrollbar in Chrome 8 is solved now.

Javascript cleaned up too.

Works in MSIE 9 now too.

Could anybody please test MSIE 7 and 8 ?

Looking good. Thanks for hashing these features out so quickly.

Some things in the code are kinda weird:


if(count > 0)
{
	foundAnyPre=true;
}
else if(foundAnyPre)
{
	SyntaxHighlighter.all();
	return;
}

I don’t really see where foundAnyPre is useful … couldn’t you just:


if(count > 0)
{
	SyntaxHighlighter.all();
	return;
}

Also, what is the purpose of the delay? Are you waiting for the page to have content?

If so… just place that script directly before the tag. That way, when the script runs, the entire DOM will be ready for you to instantly query/fix. No need to delay.

works fine in Chrome now, gj :slight_smile:

Broken in IE8 for me

and

The only thing I did was:


   <style  type="text/css">
      div.code > div > div.syntaxhighlighter { overflow: hidden !important; }
   </style> 

:persecutioncomplex:

Looks great in Chrome / OSX here. :slight_smile:

Disabled it in MSIE again. I’m not going to spend more time on that.

It seems to work fine in Firefox, after a delay.

Did you consider doing it server side? There is a PEAR package called Text_Highlighter that looks like it could do it.

I did consider it, but I really like not to have to mess to much with the SMF internals… for the moment, I think the client-side syntax highlighter is acceptable.

That IE crash happens because you are trying to manipulate the DOM before it has been completely loaded. See Woogley’s post for a quick fix.

I’m not a huge fan of jQuery, but at least it takes care of these things for you.

I already put it after the – it’s another problem in MSIE, and I will look into it later.

The syntax highlighting doesn’t scroll horizontally (on FireFox). Eg:
http://www.java-gaming.org/index.php/topic,22899.msg189422.html#msg189422

[quote]The syntax highlighting doesn’t scroll horizontally (on FireFox). Eg:
http://www.java-gaming.org/index.php/topic,22899.msg189422.html#msg189422
[/quote]
Meaning to report this too. Same problem on Chrome.

To fix the problem in chrome
change:

div.code > div > div.syntaxhighlighter {
overflow: hidden !important;
}

to:

div.code > div > div.syntaxhighlighter {
overflow: visible !important;
}

No idea if this breaks anything.