I have a feature request concerning syntax highlighting on Stack Overflow. I would like to be able to highlight code of nested syntax, e.g., for GWT's JSNI functions, where JavaScript code is embedded in Java code.

Currently, such code renders like this:

current highlighting

I would prefer to have that code rendered like this (like Eclipse does with the GWT plugin):

requested mixed highlighting

Such cases of nested syntax occur in other places as well, e.g., for JavaScript embedded in Ant scripts (XML) etc., so this is not just a GWT-specific request.

The means of choosing the syntax of the nested part should probably be manual, e.g., by using some kind of markup. Detecting the syntax of the nested part automatically would be very hard.

SO syntax highlighting is performed by Google Prettify, which cannot handle nested syntax just like that; so some pre-processing may be required on the SO side. (Such as, maybe, splitting the code into three parts: upper Java; JavaScript; lower Java.)

Update: I fiddled a bit with Prettify and got it to produce nested highlighting without changing anything on the Prettify side: http://jsbin.com/lupuvonapu/2/edit?html,output
I tested the result with Firefox 17esr, IE10, Opera, and Safari, getting the desired result in all cases.

The approach used in the example supports only 1 level of nesting, and requires Prettify to run twice (once for the outer code, and again for nested code). I marked the nested code by adding a nested <pre> tag to specify its language syntax. This way of doing it certainly leaves room for improvement, but I hope it helps the idea along.

  • 7
    Hopefully it is that easy. Trust me, it isn't. – Frédéric Hamidi Oct 27 '13 at 19:59
  • But it's a comment, so it shouldn't be highlighted. – Cole Johnson Oct 27 '13 at 20:13
  • 3
    @ColeJohnson That's the point: It's not a comment. It's code. – barfuin Oct 27 '13 at 20:16
  • 4
    @Thomas, you know it's code, but to a Java parser (especially to Prettify's parser), that's just a comment. The difficulty is 1) how to properly determine where embedded code begins and ends (assuming you don't want to run a parser for every language on every string and comment), and 2) how deep to go (e.g. should we prettify strings containing Javascript within VBScript fragments inside <msxsl:script> elements in an XSL stylesheet? Possibly embedded as CDATA inside another document?) – Frédéric Hamidi Oct 27 '13 at 20:22
  • @FrédéricHamidi Yes, those are (some of) the challenges. In my post, I suggest that some kind of markup be used to resolve this. I can't think of a way to detect such cases automatically. – barfuin Oct 27 '13 at 20:36
  • @FrédéricHamidi Might want to post that as an answer? – Cole Johnson Oct 27 '13 at 22:43
  • related / relevant : meta.stackexchange.com/questions/278141/… specially when it comes to some pre-processing may be required on the SO side which I expect would end up with a No from Oded... – rene Jun 17 '16 at 15:55
  • Possible duplicate of Highlight.js: For Real, Please – JonathanDavidArndt Aug 31 '18 at 16:50

You must log in to answer this question.

Browse other questions tagged .