What is meta? ×
Meta Stack Exchange is where users like you discuss bugs, features, and support issues that affect the software powering all 131 Stack Exchange communities.

consider the following code:

var clark = { capeOn: false };
//Clark Kent's secret cape is red
clark.capeColor = 'red';

just a simple JavaScript with two assignments and a comment. The problem is that SO doesn't recognize the comment and confuses the apostrophe with a string opening. the result is that the syntax highlighter paints everything red until the next apostrophe is found, cause some very funky coloring in the rest of the code.

I know I'm nitpicking, but it irks me that because of an apostrophe in a comment the syntax highlighter gets all confused.

Is there any way to tell SO that it's a comment, or otherwise escape the apostrophe so as not to trip up the highlighter?

The original problem that prompts this question can be found here, in the second code block

share|improve this question

3 Answers 3

up vote 7 down vote accepted

The syntax highlighter handles this just fine.

var clark = { capeOn: false };
//Clark Kent's secret cape is red
clark.capeColor = 'red';

You just need to make sure the right syntax highlighting hint is provided. This usually happens automatically based on tags. If that doesn't kick in (for instance, multiple language tags that use different hints), you can manually add a hint as I did above.

<!-- language: lang-js -->

Now there are circumstances where this doesn't work (regex are iffy, heredocs usually cause issues), but then that's Google Prettify running - feel free to file bugs there (or propose patches!).

share|improve this answer
    
(OP's answer has already been edited to include this...) –  Wooble Oct 21 '13 at 18:23
1  
I edited the OP's answer to apply the proper formatting. The question is tagged both javascript and python, which confused the highlighter. –  JDB Oct 21 '13 at 18:25
    
I just selected bug out of ignorance. Thanks for the answer! –  Nicolás Straub Valdivieso Oct 21 '13 at 18:44

You should specify the language to achieve the appropriate syntax-highlighting. In your case, this would be lang-javascript.

Compare

var clark = { capeOn: false };
//Clark Kent's secret cape is red
clark.capeColor = 'red';

to

var clark = { capeOn: false };
//Clark Kent's secret cape is red
clark.capeColor = 'red';

As a main reference for syntax highlighting, see What is syntax highlighting and how does it work?.

share|improve this answer

It's not broken - you just aren't applying the proper javascript language formatting to your code. I went ahead and edited your original answer for you.

Use <!-- language: lang-js --> before each code block to apply javascript formatting. If you want to apply this formatting to every code block, use <!-- language-all: lang-js -->

When there's only one code tag on the question, the formatter is usually pretty good at picking the correct highlighter. But since the question contained both the javascript and python tags, the highlighter got confused and picked the wrong one.

share|improve this answer
2  
As you note, it was the presence of the python tag that threw things into confusion here - since Python syntax isn't auto-detected by prettify (at least, not with the reliability that JS or HTML are), that tag has to override the default. If the question had had an additional tag with such an override (for instance, vb.net), it would've ended up reverting back to the default highlighter (which wouldn't have worked well for anything but JS...) –  Shog9 Oct 21 '13 at 18:34
    
great, thanks for clearing that up –  Nicolás Straub Valdivieso Oct 21 '13 at 18:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .