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

This question already has an answer here:

Do you think it would be worthwhile to provide hints as to what language to use for the syntax highlighting?

Sometimes I find the highlighting on SQL or VB.NET answers is more distracting than helpful; for example:

to pick the two I've been looking at recently.

share|improve this question

marked as duplicate by animuson Sep 16 '13 at 5:16

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Marking as [status-completed] per meta.stackexchange.com/questions/72082/…, but see also meta.stackexchange.com/questions/63800/…. –  mmyers Jan 12 '11 at 16:59
2  
Why is this status completed? Was something implemented to let us choose the language? Any SQL with a backslash in quotes ala '\' makes the rest of the code act as though it is inside a string. –  ErikE Jan 26 '11 at 19:25
1  
@Emtcuifor This is complete per meta.stackexchange.com/q/72082/130044 Explicit syntax is meta.stackexchange.com/q/63800/130044 –  Rowland Shaw Jan 26 '11 at 19:30

12 Answers 12

up vote 197 down vote accepted

Note that this question is a bit obsolete, because we now infer prettify language type based on the tags.

See more:
Changes to syntax highlighting

This is now implemented. In addition to tag inference (a recent change), you can manually specify the language as a hint to Google Code Prettify.

The spec is:

<!-- language: lang-or-tag-here -->

    code goes here

<!-- language: lang-or-tag-here -->

    code goes here

You may use either a tag or a prettify language code to specify, though prettify language codes are always guaranteed to work regardless of what language the tag happens to be set to.

Use <!-- language-all: lang-or-tag --> to use the given highlighting for all the following code blocks.

Available Language Hints Moved

Due to several lists of available hints existing throughout Meta, the entire list has been consolidated and moved to the following FAQ:

What is syntax highlighting and how does it work?

share|improve this answer
    
I think the current solution with the tag intelligence is a big improvement to what it was before. Especially VB code will look much better now. The only downside is non-language specific questions, that might get answers in VB.NET. –  awe Jan 26 '11 at 12:03
    
@awe indeed it will be formatted as a comment, unless you mix language tags and force default inference again.. looks fine to me, see stackoverflow.com/questions/4801650/… –  Jeff Atwood Jan 26 '11 at 12:19
    
Can we get this documented in the help pages? I've been looking around for this for a while and this is the first place I've found it, so if it is in help pages somewhere it's not visible enough. –  Old Pro May 31 '12 at 2:10
    
@old you somehow missed it, see meta.stackoverflow.com/editing-help#syntax-highlighting -- same on all Stack Exchange sites –  Jeff Atwood May 31 '12 at 5:17
    
@Jeff, my bad. I was looking for something related to "language" and I was looking on Ask Different where it's near the bottom. –  Old Pro May 31 '12 at 6:53
    
Is there one for assembly? –  Cole Johnson Dec 8 '12 at 6:45
1  
Great, thanks. But why do all the magic strings start lang-? –  Colonel Panic Apr 12 '13 at 21:07
1  
Thank you. FYI of all the solutions described here, only the <!-- language-all: lang-tag --> option worked for me. :/ –  user188035 May 25 '13 at 20:39
    
Is there any highlighter for DOS/Win shell scripts? –  Web Devie Jul 30 '13 at 6:57
2  
I wish we had this functionality built-in into post editor on SO. I always end up googling this and copy/pasting from this answer, which is counter-productive. –  Neolisk Sep 12 '13 at 18:35
2  
It's also far too sensitive - <!--language-all: ... --> doesn't work - no space before language. I end up at this post every single time I need this. Very frustrating that it's not in the editor and/or more forgiving –  Basic Nov 4 '13 at 9:47
1  
OMG I forget this thing every time... I've been back on this page for 145th time. They should bring some UI for language selection. –  user158239 May 19 at 19:16
    
I suggest using simpler markup like GitHub's triple backtick - language or similar. I also have to google this every time. –  Zoltán Oct 28 at 15:41

Another workaround for VB comments is to include two apostrophes and a pound symbol at the beginning of the line. That way, comments aren't highlighted as text and they stand out better.

''# You're commenting!
Dim c As New Comment()

see this Meta question.

share|improve this answer

Has anyone seen how terribly this general syntax works for Matlab? Look at the last piece of code in this question. Matlab uses apostrophe (') as a matrix transposition operator. As a result, everything in code following it is highlighted as a string in brown. Comments in Matlab start with a % and those never get highlighted either. Perhaps we could at least make separate HTML tags for certain languages like <asm></asm> or <matlab></matlab>. This would be very helpful, because posting a non-ugly Matlab question or answer takes ages of reformatting your original code. I personally volunteer to write the code to implement this, just tell me what to do.

share|improve this answer

Adding in an option for very light highlighting that would essentially do little more than colour brackets and isolated literals, treat '' and "" as text delimiters and nothing else would be useful for a vast number of languages currently unsupported.

Functional example code look like crap with the current highlighter as do punctuation-light languages like SQL. Perl is just hilarious.

Actually thinking about it more you wouldn't even want the text stuff. It is too costly a hit to readability when it fails (thinking generic type parameters in F# for example).

Essentially nothing that requires unbounded state and never goes across a new line should function very well. Something that is unbounded but the bounds are highly common like whitespace, new lines, any non-word character, etc. have a high probability of not messing up many languages.

share|improve this answer
1  
Was going to point out that in some languages ' is not a string delimiter, and -- isn't always "decrement". Can't say I've seen many Perl questions on SO (Guess everyone still uses perlmonks), but I'd fear how it would handle regexp delimiters... –  Rowland Shaw Jun 30 '09 at 12:38

A workaround for VB is to add an additional apostrophe ' at the end of every comment line, so that the daft syntax highlighter thinks you've terminated your string.

Dear MarkDown
  'Unterminated 
  Why Is Half My Code Red, It Is Not A String
End Moan

Becomes

Dear MarkDown
  'Terminated '
  This Is How It Should Look
End Moan

I sometimes edit other peoples' posts to add the apostrophes.


EDIT based on Joel's suggestion to use ''# which is even better:

Dear MarkDown
  ''# Terminated 
  This Is How It Should Look
End Moan
share|improve this answer
4  
A better workaround is to use ''# to indicate comments. This avoids need to worry about escaping quotes in the comment itself and actually colors the text like a comment rather than a string literal. –  Joel Coehoorn May 18 '10 at 16:15
    
@JoelCoehoorn — This is ugly. –  Nicolas Barbulesco Jun 21 at 14:28
    
@MarkJ — You degrade other people’s code to make the buggy syntax highlighter work ?? –  Nicolas Barbulesco Jun 21 at 14:30
    
@JoelCoehoorn — Altering the code to circumvent the bugs of the syntax highlighter. I had never seen that. –  Nicolas Barbulesco Jun 21 at 14:31

I think I know why this issue is now being ignored for at least eight months (and counting).

First note that the languages worst affected seem to be Lisp and Perl, since they get their code partially greyed out by false-positive comment character matching (# and //). The next most disturbing group of mistakes is false string interpretation, affecting Lisp and VB (and others). Then, SQL also seems to be affected by false-negative comment matching.

Look at the popular tag counts: http://stackoverflow.com/tags

As you see, C#, Java, PHP, Javascript, C++, and Python are dominating the programming language occurrences, before SQL, which is the first affected. The highest VB dialect is VB.NET, on the bottom of the second column. Perl is far behind, in the middle of the fourth column. The Lisp dialects do not even show up until page 7.

So, the affected languages do not show up very often. That in itself doesn't mean that no one in charge could notice. However, let's look at the tags of the people making StackOverflow (scroll down to the "Tags" section on their user pages):

  • Joel Spolsky seems to be mainly into VB, C++, C#, and SQL.
  • Jeff Atwood is a C# guy. None of the affected languages appear in his tags.
  • Jarrod Dixon does ASP.NET and C#. There is one occurrence of VB.NET.
  • Geoff Dalgas does C# and ASP.NET.
  • Brent Ozar seems to be mostly interested in databases with SQL.

So, not only do the most badly affected languages not turn up very often, the developers of StackOverflow also are not interested in them at all. They most likely see this as a non-issue. A bag of rice has fallen over in China, yawn. They are also perhaps not familiar with the syntax of those languages, so that they do not see the problem even if you show them examples.

In the end, the most we will get from them seems to be "the readme says it should mostly work". Perhaps StackOverflow is not a general programming oriented site after all.

Now, I don't want to sound so negative. There seems to be a simple solution: put <code class="prettyprint lang-whatever"> on the HTML whitelist.

share|improve this answer
1  
There's a simple workaround for the false string interpretation, for VB at least meta.stackexchange.com/questions/981/syntax-highlighting-hints/… –  MarkJ Mar 10 '10 at 13:10
9  
@MarkJ but it’s a workaround, it only fixes some things (i.e. comments, but no keywords etc.) and it’s just soo annoying, considering that implementing a proper fix is trivial. No wonder people like Svante (or indeed me) aren’t happy. Proper syntax highlighting should have been a priority on Stack Overflow from day one. It still isn’t. That sucks. –  Konrad Rudolph Jul 27 '10 at 11:41
    
@Konrad I agree! I agree! And although the affected tags are uncommon, that might be partly caused by the problem. –  MarkJ Jul 28 '10 at 11:33
    
At least there's some kind of support for <!-- language: lang-lisp -->. Try entering a Whitespace program; it renders only the comments. –  GargantuChet Feb 15 '13 at 14:50

Scavenger's answer is a good idea, but # might be something that is valid in the code like a pre-processor command in C++ that can be part of the actual code sample.

I suggest to have something that is specified before the code block in a special way. The code block is indented with 4 spaces, so the language specifier could be indented 3 spaces (and only mean something when appearing immediately before a code block). This follows the simplicity in the markup rules that already exists.

   VB.NET
     Dim myVariable As String 'Here is the first code line
     myVariable = "Hello world"

share|improve this answer

The cleanest syntax I think would be something like this:

# Python:

import this
this.eat()

That is, a language-specific comment mark, space, language identifier on the first line. Of course, "Py", "Python 2.6" and misspellings like "Pyton" should work as well.

share|improve this answer
    
Not sure how this would work with inline code samples? –  Rowland Shaw Mar 31 '10 at 7:40
3  
It wouldn't. Shouldn't inline code should be short enough to grasp without coloring anyway? –  Wander Nauta Apr 20 '10 at 8:04
2  
As it is, inline code samples are not highlighted anyway (just styled with monospace font and grey background...) –  awe Sep 21 '10 at 8:24
6  
I am of the firm belief that misspellings should not work. –  Svante Sep 29 '10 at 14:24

As I've already said in another answer, this could be helped if the language could be inferred from the tags. That way, this fix would work retroactively, and the work flow wouldn't change.

I know that prettify does have a way to specify the language, it would be only a matter of using that.

share|improve this answer
    
Yes, that is also a possibility. However, this would not help for posts containing snippets in several languages (e.g. SQL + PHP, or JavaScript + Perl, or C + Lisp). It also seems to be a much more involved code change than adding to the HTML whitelist. –  Svante Mar 10 '10 at 21:11
3  
@Svante - it would still help those if the list of possibles is filtered down to those tags. Then for example you might have a php and sql question, but at least it's not gonna try to color the sql like C. –  Joel Coehoorn May 18 '10 at 16:17

There are problems with code-prettify,

this [^\s>/] == "example" # is highlighted as a regex, not a comment, 1 / 2

..but it mostly does a really good job

There's no good solution to this, Markdown does not provide a way to specify what language a code-block is (see here), so StackOverflow would need to implement their own way.

In the past I've modified a markdown parser to look for a special first-line, something like..

{{language:python}}
import os
print os.listdir()

The usability problem then is, there's no way to know which languages are supported, or how I should specify say, Visual Basic .NET - should it be visualbasicnet, visual-basic.net, vb.net vbnet, etc..?

I suppose this could be overcome by making the code-block button ask for a language (or maybe add second specific-language-code button), but then the site isn't using Markdown anymore, it's using StackOverflows own non-standard version..

share|improve this answer
19  
If you were wanting to be really flash, you could see what languages the question was tagged as, and use this as a heuristic. People with sufficient rep could then do any association for new tags, (it may also encourage more consistent tagging) - still wouldn't always get it right for the "I'm trying to convert this perl to vb.net" type of question, mind you... –  Rowland Shaw Jul 2 '09 at 15:45
3  
I like the use the tags idea. it would cover most right –  ShuggyCoUk Jul 2 '09 at 18:18
2  
I agree that making SO stop being markdown would be bad. that said if you could embded it as something that is legit html...? Even then I prefer Rowland's idea –  ShuggyCoUk Jul 2 '09 at 18:19
    
@dbr - Not knowing what language could also be solved with documentation. –  MikeSchinkel Oct 22 '10 at 7:48
    
marked down for defeatist attitude. –  SamB Nov 20 '10 at 23:49

Last I know, the site used a whitelist approach to strip the unsupported tags or unsupported attributes on tags.
So I think it would be easy to at least let us provide the class attribute for the pre tag (right now, it doesn't). That way, we can manually set the language for the syntax highlighting like this :

<pre class="prettyprint lang-sql"><code>
--sql comment here
</code></pre>

To answer the question ; Yes, it would definitely be worthwhile to provide hints as to what language to use for the syntax highlighting. I mean take a look at ask.sqlservercentral.com or ask.sqlteam.com, both of them are stackexchange sites focused on SQL Server. But none of the code examples are highlighted correctly.

share|improve this answer
    
Shouldn't the class attribute go on the code tag? I'm assuming that's what you meant. –  Jon Seigel Mar 10 '10 at 16:31
    
@Jon Seigel, It works both on pre tag and code tag. –  çağdaş Mar 14 '10 at 0:05

One thing that works now, is to have no highlighting (none being better than wrong) by using <pre> code </pre>.

share|improve this answer
1  
agreed - this is worth pointing out –  ShuggyCoUk Jul 2 '09 at 18:19
4  
Has the added downside that you have to escape any </> in there. –  Joey Oct 13 '09 at 16:04
1  
And how does one escape an <? –  MPelletier Feb 21 '10 at 22:10
3  
@MPelletier: Use the entity "lt" (with the usual "&" prefix and ";" suffix). (Not just writing it, because I'm not sure how comments are escaped.) –  Richard Feb 22 '10 at 9:48
    
But wouldn't that just show up as "& lt ;" in your code, since pre stands for preformatted? –  MPelletier Mar 20 '10 at 20:11
    
OK, so yes, that shows up as <. Then how could one print &lt; for just that? –  MPelletier Mar 21 '10 at 5:08
    
@MPelletier: You use the entity amp to create a &. –  Richard Mar 21 '10 at 9:20
    
You have to use <PRE><code>code goes here</code></PRE> (yes, uppercase <PRE>) if you want syntax highlighting. You need to escape < to &lt;, > to &gt; and & to &amp;. –  nyuszika7h Mar 7 '11 at 11:28
3  
Note that it appears that <!-- language: none --> is now the preferred mechanism to disable syntax highlighting for a preformatted block of text. –  Phrogz Mar 31 '11 at 22:32

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